In most cases, it is possible to get rid of the need for using !important rules by simply changing the HTML tags on your page or assigning new CSS selectors to them.Custom user style sheets are the main use case for the !important rule as they let the user to modify the layout and style of the websites they are viewing to fit their needs. In the example below, the font-size of the paragraphs is overridden by !important rule in the first declaration, even though the second declaration should have been in effect considering the natural flow of CSS.Initially, the !important rule was introduced for use in user style sheets as a means for providing the ability to the user to modify the style of a web page to fit their needs (e.g. After source order is taken into consideration it’s clear that priority goes from inherited styles to element selectors to class selectors.It can get much more specific than that though. As such, you’ll notice most stylesheets utilizing classes, pseudo-classes, and attributes more often than the other selector types.The cheat sheet above can help you find a good starting point with CSS specificity. This is where incremental specificity comes in handy.For example, let’s imagine we have the following HTMLThe “text” class has already declared in your stylesheet.You want to keep the underline and the italic styles, but you want to change the font color of the text inside the “module” div to red without affecting the navy font color inside the “box” div.Because of the source order rule, we already know that doing something like this will change the text to red for all elements with the “text” class, which is not what we want.However, since our “text” class is applied to a element AND a

element, we can achieve incremental specificity by adding the p selector.By targeting both an element and class, we’ve incrementally changed our specificity value from 0.0.0.1.0 to 0.0.0.1.1, which results in this:However, if for some reason we decided to replace our element with a

element, then we’re stuck with the same problem as before.So how can we increase the specificity even more? In this case, styles that are declared specifically for child elements will override the inherited styles of their parent elements.With CSS we have the ability to “select” our HTML in a variety of ways.

If you try this practice and look at a site's cascading style sheets, one thing you may see in that code is a line that says You have other options. The weight is represented by a calculated specificity score between 0.0.0.0.0 and 1.0.0.0.0.In the chart above, we can see the least specific (0.0.0.0.0) selector to most specific (1.0.0.0.0) selector. 100% guaranteed.Let's review your current tech training programs and we'll help you baseline your success against some of our big industry partners. As you might know, IE …

We can use the following selectors, or a combination of these selectors, to tell the browser specifically what we’re trying to do:Each selector carries a different “weight,” which helps browsers understand which styles should take precedence. Since classes are all "equal important" when added to an element it doesn't matter in which order you assign them to your paragraph. important tag seems like a secret weapon that you can pull out when styles aren’t working as expected. If you lean too heavily on the !important declaration to achieve your desired styles, you will eventually have a style sheet littered with !important styles. You can also check the Getting used to the !important declaration in CSS is very easy, but it is also a bad habit for a web developer since it leads to missing or ignoring the real underlying issues in the HTML structure and the corresponding CSS styles that should better be fixed in other ways. We can target the “text” class This increases our specificity from 0.0.0.1.0 to 0.0.0.2.0 because we are now using two classes in our selector. Chat with one of our experts to create a custom training proposal.

This feature comes in handy in making websites work smoothly at lower internet speeds. Need to stay ahead of technology shifts and upskill your current workforce on the latest technologies?Is your engineering new hire experience encouraging retention or attrition?Looking for in-the-trenches experiences to level-up your internal learning and development offerings?https://s3-us-east-2.amazonaws.com/ditrainingco/wp-content/uploads/2020/05/26142129/pexels-photo-248515.pnghttps://www.developintelligence.com/wp-content/uploads/2019/11/Develop-Intelligence-logo-h.png CSS rules marked !important take precedence over later rules. This hierarchy is helpful for users who need to set styles in a certain way.

Our styles are now back where we want them to be.To drive the point home, let’s imagine that we have to create another

element with the “module” class. Browsers download the CSS rules once and cache them for loading all the pages of a website. The !important directive overrides the normal rules of the cascade and it gives that style very high specificity. It is a great option to have for sure, but are we really using it in the correct way and only in cases where we have no other option? Now all the text will appear in red, even though the "black" value is listed second. For example, a visually impaired reader may need to increase default font sizes on all web pages they use. If you’re stuck trying to understand why you’re styles aren’t being applied, check to see if the same styles are applied later in the CSS file first. However, in this post, you’ll learn why this is rarely a good idea, and more importantly, how to simply avoid using !important in your CSSThere’s quite a bit to cover here. Basically, if two styles are saying the same thing (which means they have the same level of specificity), the last one listed will be used. Inline-styles are more specific than IDs.And that’s when we get to the !important tag.