Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What CSS selector can be used to select the first div within another div

I have something like:

<div id="content>     <h1>Welcome to Motor City Deli!</h1>    <div style=" font-size: 1.2em; font-weight: bolder;">Sep 19, 2010</div>    <div > ... </div> 

What is the css selector for the second div (1st div within the "content" div) such that I can set the font color of the date within that div?

like image 832
GregH Avatar asked Sep 19 '10 21:09

GregH


People also ask

How do I select a div inside a div in CSS?

The element element selector is used to select elements inside elements.

Which selector is used to select only Div 1?

Definition and Usage. The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

How do you select the first element in CSS?

The :first-child selector allows you to target the first element immediately inside another element. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.


1 Answers

The MOST CORRECT answer to your question is...

#content > div:first-of-type { /* css */ } 

This will apply the CSS to the first div that is a direct child of #content (which may or may not be the first child element of #content)

Another option:

#content > div:nth-of-type(1) { /* css */ } 
like image 174
Jeremy Moritz Avatar answered Oct 20 '22 09:10

Jeremy Moritz