Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop user agent stylesheets from overriding my css

I'm trying to set cursor: pointer on a dom element, but the input isn't taking it (I am not seeing a pointer cursor when I hover over the checkbox). In chrome, I see that the "user agent stylesheet" is overriding my css. Wtf?

<!doctype html> <body>     <div class='a'>         <input type='checkbox'>     </div>      <style>         .a {             cursor: pointer;         }     </style> </body> 

I have seen this question: Why does user agent stylesheet override my styles? But my problem does not seem to be the doctype, which is the recommended doctype.

Using !important isn't acceptable here, and neither is styling the input node directly - I shouldn't have to worry about weird browser useragent styles. What's going on?

To clarify, my question is about why the user agent stylesheet is overriding the css and how to make that stop. My question is not how I can hack around this behavior. If I'm not mistaken, the correct behavior of css is that the cursor style should be inherited by child nodes.

Is this the expected behavior of css?

like image 998
B T Avatar asked Jun 25 '14 17:06

B T


People also ask

Where does the user agent stylesheet come from?

A user agent style sheet is a ”default style sheet” provided by the browser (e.g., Chrome, Firefox, Edge, etc.)

How do I get rid of user agent stylesheet in Safari?

Question: Q: Mac - safari -User Agent StylesheetClear the history and browser cache, after you have set the Safari Preferences : Advanced : StyleSheet to None. You will have to quit and relaunch Safari afterwards to be rid of the “User agent Stylesheet” properties.


2 Answers

The "problem" here is that there is actually no input style in the author stylesheet (see the spec for more info), and therefore the style that is defined in the user agent stylesheet is used.

The (relevant) user agent rule (on chrome) is:

input, input[type="password"], input[type="search"] {    cursor: auto; } 

You can achieve what you want in a couple ways:

  1. Create a css class that selects the input directly, for example
    • using another css class, or
    • selecting the input within the already-defined class,
    • etc
  2. Explicitly setting inheritance behavior for the cursor style on all inputs

For (1):

<style>   .a, .a input {       cursor: pointer;   } </style> 

For (2):

<style>   input {       cursor: inherit;   }   .a {       cursor: pointer;   } </style> 
like image 176
Beterraba Avatar answered Sep 23 '22 16:09

Beterraba


It seems like this might just be css being css, which is unfortunate. The most general workaround I can come up with is to defined this css:

<style>   input {     cursor: inherit;       } </style> 

This allows the behavior I originally expected to happen in all cases where the user agent would otherwise cause the style not to inherit. This is better than styling the input with "cursor: pointer" directly because you only have to set this style up once, and any domNodes with a "cursor: pointer" style that contain an input will automatically have the input have a pointer cursor.

like image 36
B T Avatar answered Sep 21 '22 16:09

B T