In CSS, what is the difference between default, user and author style sheets?
Nice question! Here is the detailed explanation of various types of style sheets:
Default (aka User-agent/Browser) style sheet: First thing, Default style sheet is also known as Browser style sheet Or User-agent style sheet. This is the style sheet which browser applies by Default for every web page which it renders. So if as the author of a web page you don't apply any styling, even then it is not styleless. It still applies the styling details present in the default style sheet installed within the browser. We can assume that it must be containing some styles for all standard HTML tags e.g. <span>
, <p>
, <h1>
etc. This SO post provides great details about how the default style sheets of various browsers look like.
Look at the following HTML page. I've created a very basic HTML table with no styling at all:
<html>
<head>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
</body>
</html>
But still you see that headers of the table are shown in bold. That is coming from browser's default style sheet.
User style sheet: Now the second level is user style sheet. Browsers give you the option of extending the browser's default style sheet. For e.g. in internet explorer you can go to Tool
> Internet Options
> General Tab
> Accessibility button
> Accessibility Window
> User style sheet section
> "Format documents using my style sheet" check-box.
So for the same html tag if I provide a different style in my own style sheet ("D:\myuserstylesheet.css" in this case) then same it will start overriding it.
myuserstylesheet.css
looks like this:
td
{
color: green;
}
Now, If I load the same web page containing a simple table after making these changes in internet explorer browser settings then the styles present in user style sheet start overriding the browser style sheet as shown below:
Author style sheets:Then comes author style sheet which is what you have defined in your web site as a creator/author of the website. This comes in three flavors:
<div style="width:20px;height:20px;background-color:#ffcc00;"></div>
<style>
tag inside <head>
tag of an html page.<head>
tag : <link rel="stylesheet" type="text/css" href="abc.css">
There is one fundamental difference in the priority of application of style-sheets for any HTML element as detailed below:
For !important
styles when there is collision of equal specificity for an element (Highest to lowest priority)
Then, for normal styles when there is collision of equal specificity for an element (Highest to lowest priority) - It is just the reverse
When there is collision of equal specificity among different types of Author style sheets then their proximity to the HTML element (Textual order) matters while deciding the precedence/priority. It is as below (Highest to lowest priority):
Note: !important
styles always have higher precedence as compared to normal styles.
You can read more about specificity calculation in CSS here.
The 2.1 Spec gives a good explanation of each:
Default style sheets are supplied by the browser vendor.
User style sheets are supplied by the user of the browser.
Author style sheets are supplied by the author of a webpage.