What is user agent stylesheet

2019-01-01 02:58发布

问题:

I\'m working on a web page in Google Chrome. It displays correctly with the following styles.

table {
    display: table;
    border-collapse: separate;
    border-spacing: 2px;
    border-color: gray;
}

It is important to note that I didn\'t define these styles. On Chrome dev tools, it says user agent stylesheet in place of the CSS file name.

Now if I submit a form and some validation error occurs, I get the following stylesheet:

table {
    white-space: normal;
    line-height: normal;
    font-weight: normal;
    font-size: medium;
    font-variant: normal;
    font-style: normal;
    color: -webkit-text;
    text-align: -webkit-auto;
}
table {
    display: table;
    border-collapse: separate;
    border-spacing: 2px;
    border-color: gray;
}

The font-size from these new styles is disturbing my design. Is there any way to force my stylesheets and if possible, completely overwrite Chrome\'s default stylesheet?

回答1:

What is the target browser? Different browsers set different default CSS rules. Try including a reset.css or a normalise.css (Google for either one or for \"reset vs normalise\" to see the differences) to remove those defaults.



回答2:

If <!DOCTYPE> is missing in your html you may experience that the browser gives preference to the \"user agent stylesheet\" over your custom stylesheet. Adding the doctype fixes this.



回答3:

Regarding the concept “user agent style sheet”, consult section Cascade in the CSS 2.1 spec.

User agent style sheets are overridden by anything that you set in your own style sheet. They are just the rock bottom: in the absence of any style sheets provided by the page or by the user, the browser still has to render the content somehow, and the user agent style sheet just describes this.

So if you think you have a problem with a user agent style sheet, then you really have a problem with your markup, or your style sheet, or both (about which you wrote nothing).



回答4:

Marking the document as HTML5 by the proper doctype on the first line, solved my issue.

<!DOCTYPE html>
<html>...


回答5:

Basically, a default stylesheet provided by the browser. From the spec...

A user agent\'s default style sheet should present the elements of the document language in ways that satisfy general presentation expectations for the document language. ~ The Cascade.

Also, see for more details on user agent in general...

https://www.w3.org/TR/UAAG20/#def-user-agent



回答6:

Define the values that you don\'t want to be used from Chrome\'s user agent style in your own CSS.



回答7:

Some browsers use their own way to read .css files. So the right way to beat this: If you type the command line directly in the .html source code, this beats the .css file, in that way, you told the browser directly what to do and the browser is at position not to read the commands from the .css file. Remember that the commands writen in the .html file is stronger than the command in the .css.



回答8:

I had the same problem as one of my was have margin set by the browser, it was quite annoying but then i figured out as most of the people said, its a mark up error.

I went back and checked my section and my css link was like below:

<link rel=\"stylesheet\" href=\"ex.css\">

i included type in it and made it like below:

<link rel=\"stylesheet\" type=\"text/css\" href=\"ex.css\">

my problem was solved.

Hope it helps for some of u.



回答9:

Each browser provides a default stylesheet, called the user agent stylesheet, in case an HTML file does not specify one. Styles that you specify override the defaults. Because you have not specified values for the table element’s box, the default styles have been applied



回答10:

put the following code in your CSS file

table {
    font-size: inherit;
}