You’ve implicitly encountered inheritance and default values while working with CSS.
Contents
- Inheritance
- Default Values
- Table of Inheritance and Default Values
- The ‘inherit’ Value
- The ‘default’ Value
- Final Words
- References
Inheritance
When working with CSS, you probably noticed that styling like this:
When applied to HTML like this:
Results in output that looks like this:
This is important!
This is because the p element inherits the font-size property applied to its parent div.
Not all properties are inherited by child elements.
Consider the following CSS:
When applied to the following HTML:
Results in the border only applied to the div element and not the p element:
This is important!
If the p tag inherited the border property from the parent div tag, it would have looked like this:
This is important!
Which is, probably, not what you want.
Default Values
When you write some HTML like:
<!DOCTYPE HTML>
<html>
<head>
<title>Hello World</title>
<meta charset="utf-8" />
</head>
<body>
<p>Hello World!</p>
</body>
</html>
You see Hello World! displayed in the browser.
It is very likely that the font will be serif or sans serif, it will be 16px in size, the text will be black on a white background, and there is no border – and you didn’t have to style anything.
This is because every CSS property has a default value. The default value may be changed through inheritance or when you explicitly set the value.
Table of Inheritance and Default Values
The following table shows which CSS properties (that we’ve seen) are inherited by child elements from their parents.
It also shows the default values for those CSS properties.
Browser implementations sometimes set values other than what is defined in the CSS specification. Footnotes are given for many of the values and any alternate values given should be regarded as informative rather than absolute.
CSS Property | Inherited | Default Value |
---|---|---|
color | Yes | Depends on the browser (usually black)1 |
background-color | No | transparent2 |
padding | No | See the individual properties |
padding-left | No | 03 |
padding-right | No | 04 |
padding-top | No | 05 |
padding-bottom | No | 06 |
margin | No | See the individual properties |
margin-left | No | 07 |
margin-right | No | 08 |
margin-top | No | 09 |
margin-bottom | No | 010 |
border | No | The border is transparent and has a style of none |
display | No | Depends on the HTML element, but the most common are inline and block11. |
width | No | auto |
height | No | auto |
box-sizing | No | content-box |
font-size | Yes | medium (usually 16px)12 |
font-family | Yes | Depends on the browser |
font-weight | Yes | normal13 |
font-style | Yes | normal14 |
line-height | No | normal |
text-align | No | A nameless value that acts as ‘left’ if the text direction is left-to-right and ‘right’ if ‘direction’ is right-to-left. |
float | No | none |
clear | No | none |
content | No | normal |
The ‘inherit’ Value
You can force a child element to inherit its parent’s property by using the CSS value inherit:
When applied to the following HTML:
Will cause the child element to inherit the parent’s border property:
This is important!
The ‘default’ Value
If you want to reset an element’s property to its default value, you can use the CSS value initial15.
Consider the following styling:
When applied to the following code:
Results in the following:16
This is some styled text!
This is some default styled text
Final Words
Child elements may inherit CSS values from their parents – it depends on the specific CSS property.
All HTML elements have default styles applied to them. Some of these styles are determined by browser settings, others from the CSS specification.
References
- CSS Default Properties
- CSS Inheritance
- Firefox’s Default Style Settings
- Chrome’s Default Style Settings
- The <mark> tag has a default color of black. The <a> tag default color is defined by the browser – it is usually #0000EE for an unvisited link, #551A8B for a visited link, and #EE0000 for a link you are hovering over.↩
- The <mark> tag has a default background-color of yellow.↩
- This is true for all the HTML elements we have seen so far. Some HTML elements we haven’t seen may have a non-zero default value.↩
- This is true for all the HTML elements we have seen so far. Some HTML elements we haven’t seen may have a non-zero default value.↩
- This is true for all the HTML elements we have seen so far. Some HTML elements we haven’t seen may have a non-zero default value.↩
- This is true for all the HTML elements we have seen so far. Some HTML elements we haven’t seen may have a non-zero default value.↩
- This is true for all the HTML elements we have seen so far. Some HTML elements we haven’t seen may have a non-zero default value.↩
- This is true for all the HTML elements we have seen so far. Some HTML elements we haven’t seen may have a non-zero default value.↩
- This is true for most HTML elements we have seen so far. For <p> tags it is equal to the font-size. The <h1> <through <h6> also have default values for top and bottom margin that are non-zero – the specific value depends on the tag.↩
- This is true for most HTML elements we have seen so far. For <p> tags it is equal to the font-size. The <h1> <through <h6> also have default values for top and bottom margin that are non-zero – the specific value depends on the tag.↩
- The following tags have a default display of none: <head>, <title>, and <meta>. The following tags have a a default display of block: <html>, <body>, <h1> through <h6>, <img>, <ol>, <ul>, <li>, and <p>. The following tags have a a default display of inline: <strong>, <em>, <i>, <mark>, and <a>.↩
- The <h1> through <h6> tags have varying default font sizes. Assuming 16px as the default font size: H1 = 32px, H2 = 24px, H3 = 19px, H4 = 16px, H5 = 13px, and H6 = 11px.↩
- The <h1> through <h6> and the <b> and <strong> tags have a default font-weight of bold.↩
- The <i> and <em> tags have a default font-style of italic.↩
- Yes, it would make more sense if it was called default, but the CSS specification calls it initial.↩
- If your default color (set in the browser) is red, then both outputs will be red.↩