Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div#name vs #name

I know that in a stylesheet div#name and #name do the same thing. Personally I've taken to using div#name for most styling I do, with the reasoning that it's slightly faster, and means that I can identify HTML elements more easily by looking at the CSS.

However all of the big websites I seem to look at use #name over div#name (stack overflow included)

In fact I'm finding it very difficult to find many websites at all that use div#name over #name

Is there some advantage to doing #name that I'm missing? Are there any reasons to use it over div#name that I don't yet know about?

like image 749
Sean Avatar asked Apr 03 '12 22:04

Sean


People also ask

What div means?

div (plural divs) (mathematics, computing) A function, implemented in many programming languages, that returns the result of a division of two integers. (web design) A section of a web page, or the div element that represents it in HTML code. (UK, Eton College, school slang) A division; a lesson.

What is div HTML?

The HTML division tag, called "div" for short, is a special element that lets you group similar sets of content together on a web page. You can use it as a generic container for associating similar content.

Why do we use div?

The Div is the most usable tag in web development because it helps us to separate out data in the web page and we can create a particular section for particular data or function in the web pages. It is used to the group of various tags of HTML so that sections can be created and style can be applied to them.

What's in a div?

Disseminated intravascular coagulation (DIC) is a rare but serious condition that causes abnormal blood clotting throughout the body's blood vessels. You may develop DIC if you have an infection or injury that affects the body's normal blood clotting process.


5 Answers

Since the div part of div#name is not required (because ID are unique per page), it makes for smaller CSS files to remove it. Smaller CSS files means faster HTTP requests and page load times.

And as NickC pointed out, lack of div allows one to change the HTML tag of the element without breaking the style rule.

like image 123
calebds Avatar answered Nov 09 '22 10:11

calebds


Since ID's have to be unique on the page, most ID's you'd run into would only ever appear once in your style sheet, so it makes sense not to bother including what element it would appear on. Excluding it also saves a few characters in your style sheet, which for large sites which get visited millions and millions of times a day, saves quite a bit of bandwidth.

There is an advantage to including the element name in the case where a division with ID "name" might appear differently than a span with ID "name" (where it would show a division on one type of page and a span on another type of page). This is pretty rare though, and I've never personally run across a site that has done this. Usually they just use different ID's for them.

It's true that including the element name is faster, but the speed difference between including it and excluding it on an ID selector is very, very small. Much smaller than the bandwidth that the site is saving by excluding it.

like image 45
animuson Avatar answered Nov 09 '22 10:11

animuson


a matter of code maintainability and readability.

when declaring element#foo the code-style becomes rigid - if one desires to change the document's structure, or replace element types, one would have to change the stylesheets as well.

if declaring #foo we'll better conform to the 'separation of concerns' and 'KISS' principals.

another important issue is the CSS files get minified by a couple of characters, that may build up to many of characters on large stylesheets.

like image 3
Eliran Malka Avatar answered Nov 09 '22 08:11

Eliran Malka


Since an id like #name should be unique to the page, there is no reason per se to put the element with it. However, div#name will have a higher precedence, which may (or may not) be desired. See this fiddle where the following #name does not override the css of div#name.

like image 3
ScottS Avatar answered Nov 09 '22 08:11

ScottS


I would guess that including the element name in your id selector would actually be slower – browsers typically hash elements with id attributes for quicker element look up. Adding in the element name would add an extra step that could potentially slow it down.

One reason you might want to use element name with id is if you need to create a stronger selector. For example you have a base stylesheet with:

#titlebar {
     background-color: #fafafa;
}

But, on a few pages, you include another stylesheet with some styles that are unique to those pages. If you wanted to override the style in the base stylesheet, you could beef up your selector:

div#titlebar {
    background-color: #ffff00;
}

This selector is more specific (has a higher specificity), so it will overwrite the base style.

Another reason you would want to use element name with id would be if different pages use a different element for the same id. Eg, using a span instead of a link when there is no appropriate link:

a#productId {
     color: #0000ff;
}
span#productId {
    color: #cccccc;
}
like image 3
gilly3 Avatar answered Nov 09 '22 08:11

gilly3