Box model and CSS inheritance
Ranked #2,679 in Internet, #155,420 overall
What is the box model?
Contents at a Glance
Preamble
Most of HTML elements can be considered as boxes. In CSS, the term "box model" is used when talking about design and layout.The CSS box model is essentially a box that wraps around HTML elements, and it consists of: margins, borders, padding, and the actual content. The box model allows us to place a border around elements and space elements in relation to other elements.
The graphics settings are assigned to this element through CSS. Is unclear how the presence of nested elements often make it difficult to assign the correct settings graphics. Before explaining the box model, then illustrate some basic concepts of using CSS.
Inheritance and specificity
The main characteristic of Cascading Style Sheets is the order in which properties are assigned to the rules, that is in the cascade way.
"Cascading" means that if two equal properties but with different value are both assigned to an element, only the last one will be considerated.
Examples
1) p { color: green; } p { color: red; }
2) p { color: black; color: red;}
Both in the the first and second case, the second rule will overwrite the first one so text will be in red colour.
Another important CSSs characteristic is inheritance. We know that the structure of an HTML page is in a tree way, so, an appropriate property assigned to a tree node extends to all the other nodes children.
Example
XHTML code: <div id="content"><h1>Lorem ipsum dolor sit amet</h1></div>
CSS rules: div#content { font-size: 10px; } h1 { font-size: 120%; }
The final size of the text contained in h1 will be 12 pixels. This happens because the h1 tag is contained in the div#content in which an initial 10px value has been assigned. So, the rule calculates the final size from the property assigned to his container.
Having clarified the basic concept of inheritance, the problem arises precisely when we want to assign a graphic specificity to a single element, to a specific tag in a page, even overwriting previous graphic instructions. How to 'fight' the settings inherited from the element? Calculating, in fact, the specificity and obtaining a numerical value to assign to the rule. If several rules are involved on the same item, the higher specificity will be charged.
Calculate the specificity
There is a precise calculation to estimate the specificity of a CSS rule.- Assign 1000 if the rule is contained within the style attribute
- Add 100 to each ID in the rule
- Add 10 to each class, pseudo-class and attribute in the rule
- Add 1 to each element and pseudo-element contained in the rule.
Adding these CSS rule values up, you will have the specificity value. It is evident that, assigning rules through inline styles ( tag style attribute ), there will be greater specificity and rule will overwrite any other one.
Example: Consider the #container ul#menu li.active ul li:hover {} rule. Proceeding from left to right, the sum of specificity and thus the value would be: 100 (#container) + 1 (ul) + 100 (# menu) + 1 (li) + 10 (. active) + 1 (ul) + 1 (li) + 10 (:hover) = 224. This way, the specificity of this rule is 224. At equal specificity, cascade is applied again so, every next rule will overwrite the previous one.
In complex style sheets it can be difficult to calculate some specificity values. In these cases, you can use the!important rule. This statement assigns a precedence value to the rule. It will prevail over all the others, regardless of specificity and position.
Example
p { color: red !important; }
p { color: green; }
First rule will not be overwritten, text colour will be red.
IDs and classes
Assigning IDs and class to the HTML elements of your pages you can accurately manage the graphics of your site
IDs allow you to identify uniquely any element in the page. By its very definition, a single ID can be assigned to each element. In CSS it is identified by a # character, written before the id name (#idname).
Example
XHTML code: <div id="container">content</div>
CSS rule: #container {width:960px;}
Classes allow you to assign a same style to multiple elements, even to different ones. Unlike Ids, they can then be assigned to several elements in the same page. In CSS they are identified with a dot before the class name. (.classname).
Example
XHTML code: <p class="red">Lorem ipsum dolor sit amet.</p>
CSS rule: .red {color:red;}
Another important peculiarity of the classes is the ability to assign more than one to the same element:
Example
XHTML code: <p class="red underline">Lorem ipsum dolor sit amet.</p>
CSS rule: .red {color:red;} .underline { text-decoration: underline; }
When you use combined classes within an element, if a property is used by more than one class, the classical 'cascade' method will be applied in order to establish which one is used. So, in essence, the property contained in the last included class will be assigned.
Example
XHTML code: <p class="red blue ">Lorem ipsum dolor sit amet.</p>
CSS rule: .red {color:red;} .blue { color: blue; }
The blue colour will be assigned to this section.
IDs and Classes in the same element
In a website structure, IDs and classes are often used simultaneously within the same element. It becomes possible to set generically elements of a determined area through classes. At the same time we can set specifically a particular element through IDs. Of course, we must carefully consider cascade and specificity.Example
XHTML code: <div class="Display" id="Display_articles">
CSS rule: .Display{...} #Display_articles{...}
In this way you can assign general graphic attributes to all class=display divs. You can also define some special settings for divs with an id=Display_articles, even if they already have a class=display.
Usually, IDs have a higher specificity than classes so, properties assigned to an ID will overwrte those contained in a class.Remember that, the way in which a rule is written sometimes changes the specificity substantially.
The box model
For CSSs there are two types of elements: the inline and the block ones. The block elements create a new line in the document's flow, occupying the entire width.
An example of block elements are divs, paragraphs, headings (h1, h2, etc.).. Instead, inline elements are those who are on a line within a text. For example, span or strong tags are inline elements. The management of the box model concerns the block elements. Please note that through the CSS display attribute it is possible to transform a block element into an inline one, and vice versa.
Then, the block element creates a frame in the document. The components of that frame are margins, padding and borders. as well as width and height The layout of a generic block element would be as follows:
Let's examine these attributes one at a time:
width: it is the width of the area in which is the content element.
height: it is the height of the area in which is the content element.
padding: sometimes called as 'inner margin', it is the distance of the content from the inner edge.
border: it is the element border.
margin: it is a blank around the block, preventing overlap with other elements.
If we imagine the block as a case, we see that width is represented by the size of the content, padding by the case inner package. Border is the edge of the case, while margin is the space that separates it from other cases. The width element is obtained by the sum of width plus the value of margins, padding and borders.
If in the style sheet, we set width and height to 100 pixels each (width:100px;height:100px), left and right margins to 10 px each (width:0 10px 0 10px) and padding to 5 pixels for all edges (padding:5px), the element will be a total of 130 pixels wide and 110px high.
The total width is obtained by 100px width, 20px margin (10 left and 10 right) and 10 padding (5 left, 5 right). The height will be 110 because only the 10px padding (5px top and bottom) are counted, while the top and bottom margin was set to 0.
On this count we should add a possible border too. If we assign a two pixel border to our div (border:2px), the overall height will be 114 pixels, while the width will be 134 pixels. In summary, the width of a box is calculated as follows:
left margin + left border + left padding + width + right padding + right border + right margin.
The box model is interpreted a little differently in older versions of Internet Explorer (up to version 6).
“Design is not primarily supposed to be nice or aesthetically please. It's supposed to perform”
My web pages
- Zenukers CMS, accessible and semantic
- The offical websiite of Zenukers, a Content Management System totally Opne Source, created to be accessibile and semantic.
- Zenukers fan page
- To follow the news and the apdating of Zenukers' project
- SEO e web graphic | webinprogress
- Semantic and accessible webisites, web graphic and search engine optimization
I'm ready, be honest

Frankly, after reading this lens you understand more about the box model?
I'm sure you have something to say ...
-
-
goo2eyes
Apr 6, 2012 @ 1:39 pm | delete
- i applaud you for writing about what you have 100% knowledge of.
-
-
-
adashim
Mar 29, 2012 @ 11:50 pm | delete
- You shouldn't assume everyone has heard of box model; I never have. I've seen html and css commands (and have managed to master some in html) - you explained this so logically that I caught on. Makes sense! And you took out some of the mystery so I will probably seek to learn more. Thanks.
-
-
-
karmicchristian
Mar 26, 2012 @ 6:24 am | delete
- Super cool lens. Just what the doctor ordered for me!
-
-
-
JDEEZY
Mar 10, 2012 @ 7:27 pm | delete
- nice lens you got here
-
-
-
flycatcher
Feb 25, 2012 @ 1:12 pm | delete
- I really appreciate this lens! I've been trying to explain all this to a friend and making a hopeless mishmash of it - now I can just point him here. Thanks! :)
-
- Load More
Great Stuff on Amazon
RSS: from webinprogress by studiomennella
by mennella
I'm a journalist and media agent. I focus my communication ability on the World Wide Web industry, working on positioning strategies and search engine... more »
- 11 featured lenses
- Winner of 22 trophies!
- Top lens » SEO: The On Page Optimization
Explore related pages
- SEO: The On Page Optimization SEO: The On Page Optimization
- Lens' Keyword Optimization Lens' Keyword Optimization
- Zenukers CMS Open Source Zenukers CMS Open Source
- How to Start a Web Design Business How to Start a Web Design Business
- How To Create An Impressive Writing Website How To Create An Impressive Writing Website
- Create Stunning CSS Backgrounds with Images Create Stunning CSS Backgrounds with Images






