views:

149

answers:

5

Looking through style sheets from popular & unpopular websites I have found the div selector included in them. The bottom four examples were taken from the style sheets of the popular sites Stack Overflow, Github, Youtube & Twitter:

div.form-item-info{padding:4px 0 4px 4px;width:80%;color:#777;}


.searchFooterBox div span.smallLabel{font-size:14px}


#readme.rst div.align-right{text-align:left;}


.hentry .actions>div.follow-actions{visibility:visible;text-align:left;}

I find that I can design fully functioning CSS style sheets with out using the div selector so the question is:

What is the div selector's function?
&
Why do so many developers use it?

EDIT:

To be clear, when using the div selector, what does it mean when div appears before an id or class? For example:

div.foo { color:black; }
div#bar { color:gray; }

And what does it mean when div appears after an id or class? For example:

.foo div { color:black; }
#bar div { color:gray; }

When the div selector appears after an id or class does it have to have another selector appear after it? For example:

#foo div span { color:black; }
#foo div p { color:black; }
+9  A: 
  1. Being more explicit in your selector makes it easier to remember what the HTML structure is like. Months down the line I can read the CSS and based on my explicit rules I can automatically map the structure in my head without going back to the HTML.
  2. By specifying the node name before the class or ID, the rule becomes more specific. If you want to override .foo { color:black; } for a div that has a class of foo, just do div.foo { color:red; }. Paragraphs that have a class of foo would be black, while divs would be red.
  3. It can be useful if you want to serve different css rules based on HTML structure. In the rules below, Any span inside a div is red. Any direct span under #foo is blue.

CSS:

#foo div span { color:red; }
#foo span { color:blue; }

html for that:

<div id="foo"><span>blah</span> <div><span>blah</span></div> </div> 

Live demo that you can play around with: http://jsfiddle.net/6dw2r/

EDIT:

  1. div#foo matches a div with an id of foo.
  2. div#foo div means any div descendants of #foo.
  3. No. It doesn't.

Please read http://www.w3.org/TR/CSS2/selector.html#pattern-matching for further questions.

meder
While what you said is valid, you didn't actually answer his question.
helixed
He's asking about `div` being used in selectors. How is my answer not relevant?
meder
@meder So the first 'blah' would be colored 'blue' and the second 'blah' would be colored 'red'. Correct?
J3M 7OR3
@meder Also, In the 4 examples I included the 'div' selector appears before the classes like 'div.form-item-info'. Can you write your example like 'div#foo span'? And can you write my included example like '.form-item-info div'?
J3M 7OR3
Pretty much.. Here's a demo... you can play around: http://jsfiddle.net/6dw2r/
meder
Yes, you can specify the node name before the id or class.
meder
Nice one meder - good explanation. @helixed - not sure what you're on about :)
Marko
@meder thank you very much. you have enlightened me. And thanks for the jsfiddle link. Nice and handy tool. Wish the updates happened in real time though.
J3M 7OR3
@Marko I believe @helixed was referring to @meder's first answer which was not as complete in answering my question as it is now.
J3M 7OR3
I misunderstood the question the first time I saw it. The edits helped too. My bad. Vote reversed.
helixed
+2  A: 

The tag defines a division or a section in an HTML document.

The tag is often used to group block-elements to format them with styles.

http://www.w3schools.com/tags/tag_div.asp

They're just being explicit about their selectors, which tends to be a good thing, as you're being more specific when addressing the elements to be styled. (Smaller chance of conflicts and unintended styling.)

George Marian
+4  A: 
div.form-item-info{...} // all div elements that have class~=form-item-info

.form-item-info{...}  // all elements that have class~=form-item-info
Andy Lin
Exactly right!!
Marko
@Andy thanks, made things clearer for me :)
J3M 7OR3
+3  A: 

In HTML and XHTML, <div> and <span> are generic container elements. <div> is a block-level element. <span> is an inline element.

Most other elements (<h1>, <p>, <strong>, etc.) have specific semantic meanings. It's bad practice to use, say, a <p> element for something that's not a paragraph. But <div> is just a container.

If you need something to be purely decorative, or to group related elements, <div> is a good choice.

Paul Schreiber
I think the question is why people declare div#header vs just #header. Not what a DIV is. I am truly amazed this received 5 upvotes :/
Marko
The <header> tag is new in HTML5. It doesn't exist in XHTML 1.0/1.1 or XHTML4.01.
Paul Schreiber
Who mentioned anything about <header> ? lol
Marko
@Paul Schreiber I was refering to the 'div' selector that is often included in a CSS style sheet, not the 'div' tag that is included in html.
J3M 7OR3
Ooops. Misread. But: div#header has a higher CSS specificity score than just #header. And: you might have a div#header and a ul#header (or whatnot).
Paul Schreiber
Technically you shouldn't, since ID's should be unique. And I'm sorry I feel like I'm bullying you :/
Marko
IDs are unique *per page*. But not per site.
Paul Schreiber
That is correct :)
Marko
J3M 7OR3
In the absence of any other CSS rules, the two examples you've given will behave identically.
Paul Schreiber
+2  A: 
div.foo { rule }
div#bar { rule }

This means the rule only applies to div elements with class foo or id bar, and the rule would not apply to non-div elements with class foo or id bar.

.foo div { rule }
#bar div { rule }

This means the rule applies to all div elements inside any element with class foo or id bar. This is called a descendant selector.

#foo div span { rule }
#foo div p { rule }

When a div selector appears after an id or class, it is not required to have another selector after it. If there is such a selector, the rule will apply to the selected elements only if they are inside a div element which is inside an element having id foo.

You may want to read up on your selectors here:

http://www.w3.org/TR/CSS2/selector.html

babtek
@babtek great job at explaining the div selector and descendant selectors. thanks
J3M 7OR3