Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would a universal CSS selector (*) override an inline style?

I am working with an internal administration tool that runs on Javascript that has the following in its core CSS file:

* {
    font-family: Helvetica, Verdana, Arial, sans-serif;
}

Based on my research, this would be the lowest level of specificity. Anything would override that setting.

My goal is to change the font on the entire page to improve legibility. I am using Python / Selenium webdriver with Firefox to modify the tag's style setting with this Javascript, which results in the following inline HTML:

document.getElementsByTagName("body")[0].style = "font-family:Lucida Fax;";

<body style="font-family:Lucida Fax;" >

The change is propagating to the sheet. However, the font doesn't change. Under the "Computed" view, I see the following:

font-family: Helvetica,Verdana,Arial,sans-serif;
------------------------------------------------
*             > Helvetica,Verdana,Arial,sans-serif   core.css;
BODY[1].style > Lucida Fax                           element;

When I disable the * CSS property in the Firefox Inspector after making the change, the font change will occur. So something is overriding my inline style change.

I am in a blackbox environment as an end user, so I can't account for everything happening.Could this be caused by an actively-running Javascript that is forcing the stylesheet to take precedent over inline styles?

like image 542
MavenACTG Avatar asked Jan 19 '17 17:01

MavenACTG


People also ask

Can you override inline style with CSS?

The only way to override inline style is by using ! important keyword beside the CSS rule. The following is an example of it.

Can universal selector override?

Universal selector (*) and combinators (+, >, ~, space) have no effect on specificity. We can not override the inline style from internal and external styles as inline style has high priority of overriding all external and internal styles. We can override inline styles from external and internal styles by using "!

What does the universal selector (*) Select?

The asterisk (*) is known as the CSS universal selectors. It can be used to select any and all types of elements in an HTML page. The asterisk can also be followed by a selector while using to select a child object. This selector is useful when we want to select all the elements on the page.

Can you override inline style with important?

Approach: To override the inline CSS, ! important keyword is used. This makes the CSS property precede all the other CSS properties for that element.


1 Answers

The "style" property on the <body> tag only affects content that's in the body directly. All the various <div> and <span> and etc. tags in your HTML are matched by the CSS rule. (Without that * rule then the natural behavior is for font information to be inherited; inheritance doesn't happen for all CSS properties however.)

What I've seen recommended instead is to set everything to "inherit" and then apply the setting to the <body>:

body { font-family: Whatever; }
*, *::before, *::after { font-family: inherit; }

That allows you to have overrides for some elements (like various sorts of form widgets or whatever).

like image 117
Pointy Avatar answered Oct 18 '22 06:10

Pointy