The Wayback Machine - https://web.archive.org/web/20241119220205/https://www.geeksforgeeks.org/how-to-apply-important-in-css/
Open In App

How to apply !important in CSS?

Last Updated : 13 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The !important rule in CSS is used to add more importance to a property/value than normal. It forces a style to override any other declarations, ensuring the specified property value is applied, regardless of specificity. It helps resolve conflicts but should be used sparingly to avoid complicating stylesheet management.

Syntax:

element {
color: blue !important;
font-size: 14px !important;
...
}

Example 1: In this example, we use CSS to set a <h1> color to white with !important, overriding blue. The body has a yellow background, despite a green !important due to later definition.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Document</title>
    <style>
        h1 {
            color: blue;
        }

        h1 {
            color: white !important;
        }

        body {
            background-color: green !important;
            text-align: center;
            background-color: yellow;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>!important property</h2>
</body>

</html>

Output: important property

Example 2: In this example demonstrates the !important CSS property, which forces styles like color, size, and background. Despite conflicting rules, !important ensures elements .geeks and #gfg follow specified styles.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>!important property</title>
    <style>
        .geeks {
            color: green !important;
            size: 10ex !important;
            background-color: lightgray !important;
        }

        .geeks {
            color: red;
            size: 100ex;
            text-align: justify;
            background-color: purple;
        }

        h1,
        h2 {
            text-align: center;
        }

        h1 {
            color: green;
        }

        body {
            width: 65%;
            margin-left: 15%;
        }

        #gfg {
            color: lightgreen !important;
            size: 10ex !important;
            text-align: justify !important;
            background-color: darkgreen !important;
        }

        #gfg {
            color: orange;
            size: 1000ex;
            background-color: magenta;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>!important property</h2>
    <div class=geeks>
        A Computer Science portal for geeks.
        It contains well written, well thought and well explained
        computer science and programming articles and quizzes.
    </div>
    <div id=gfg>
        <p>
            Computer programming is the process of writing
            instructions that get executed by computers. The
            instructions, also known as code, are written in
            a programming language which the computer can
            understand and use to perform a task or solve a
            problem.
        </p>
    </div>
</body>

</html>

Output:

CSS is the foundation of webpages, is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.



Previous Article
Next Article

Similar Reads

How to apply multiple transform property to an element using CSS ?
There is a variety of procedures to apply multiple transform property in an element. In this article we will discuss the easiest and popular ones. In the 1st method we will be combining the transform property itself. In the 2nd method, we will use the nested classes to apply other transforms. Method 1: In this method, we will combine the transform
3 min read
How to apply box-shadow to display elements like single element to user in CSS ?
In CSS, the box-shadow property allows you to add a shadow effect to an element. However, when it comes to applying box shadow to a combination of elements such as a <span> and <input>, it can be a bit tricky to get the desired effect. In this article, we will discuss how to apply box-shadow to a <span> <input> combination s
2 min read
How to apply CSS page-break to print a table with lots of rows?
When printing a table with lots of rows the problem can arise in keeping the data together when the page ends. Since the data presented continuously makes more sense. Here we will track down ways by which one can print contents of a table with lots of rows when the situation of page-break is encountered. The most logical property that can be used f
3 min read
How to apply css property to a child element using JQuery?
The task is to apply CSS property to a child element using jQuery. To do that, first we select the child element with the help of children() method in jQuery and then apply CSS property to it with the help of css() method in jQuery. Syntax: // Note : children method selects the direct child to parent $(selector).children("selector").css("property-n
2 min read
How to apply Amazing Hover Effect over Button using HTML and CSS ?
You have visited many websites and you have seen many hover buttons present in those websites. So, such Hover Button animation effect can be easily generated by using HTML and CSS. By using HTML we will design the basic structure of the button and then by using the properties of CSS, we can create the hover animation effect. A sample video is provi
3 min read
How to apply CSS Padding Shorthand Property ?
Padding is the space between the element and its border. The shorthand property of padding sets the padding area in all four sides of the element. The padding value can be in px, em, rem or %. If we want to apply padding on a specific side we can use the following properties: padding-left: Sets padding on the left side of the element.padding-right:
2 min read
How to apply inline CSS ?
In this article we will learn how to apply inline CSS, Inline CSS contains the CSS property in the body section attached to the element is known as inline CSS. This kind of style is specified within an HTML tag using the style attribute. It is used to apply a unique style to a single HTML element. Syntax: <tag style = " "></tag> Example
1 min read
How to apply no shadow in an element using CSS ?
In this article, we are going to learn How to apply no shadow in an element using CSS, To remove the box shadow from an element in CSS, you can use the box-shadow property with a value of none, like this: box-shadow: none, This will effectively remove any existing shadow or prevent a shadow from being applied to the element. In this article, we wil
1 min read
How to Apply Shadow Effect on Text Using CSS ?
In this article, we will learn how to Apply the Shadow Effect on Text Using CSS. The text-shadow property of CSS is used to apply the shadow effect on text. Approach: The text-shadow property of CSS is used to apply the shadow effect on text. It takes four values vertical_shadow, horizontal_shadow, blur, and color. All the details of the properties
2 min read
What is @apply in CSS ?
In this article, we will see the @apply rule property in CSS. This rule is used to implement a bunch of CSS properties which is already been declared in a variable like a method in JAVA. In simple words, we will make a set of CSS properties and store them into a variable for later use and we can use these properties as much time as we want. Syntax:
2 min read
How to apply background image with linear gradient using Tailwind CSS ?
In this article, we will see how to apply background images with a linear gradient using Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. To apply background images with linear gradients we use the background image utility of Tailwind CSS. It is the alternati
2 min read
How to apply CSS style using jQuery ?
In this article, we will explore how we can apply CSS styles to HTML elements. It is recommended that you have some basic knowledge of HTML, CSS, JavaScript, and jQuery before beginning this article. It is possible to change the CSS property of an element by using a simple JavaScript API, but we try to complete this challenge using jQuery css() met
2 min read
How to apply CSS in last child of parent using jQuery ?
In this article, we will see how to set styles in the last child of the parent element using jQuery. To set the style on the last child of parent, we use jQuery :last-child selector. The :last-child selector is used to select every element that is the last child of its parent element. Syntax: $("Selector:last-child") Approach: First, we create an H
1 min read
How to apply CSS in even childs of parent node using jQuery ?
In this article, we will see how to set styles in even the child of the parent element using jQuery. To set the style on the even child of the parent, we use jQuery :nth-child(even) selector. The :nth-child(even) selector is used to select every element that is the even child of its parent element. Syntax: $("Selector:nth-child(even)") Approach: Fi
1 min read
How to apply CSS in odd childs of parent node using jQuery ?
In this article, we will see how to set styles in the odd child of the parent element using jQuery. To set the style on the odd child of the parent, we use jQuery :nth-child(odd) selector. The :nth-child(odd) selector is used to select every element that is the odd child of its parent element. Syntax: $("Selector:nth-child(odd)") Approach: First, w
1 min read
How to apply CSS style to the different elements having same class name in HTML ?
In this article, We will learn to apply CSS style to the different elements having the same class name in the HTML. The class attribute is used to create a class for the HTML element and multiple elements can have the same class. The class name is often used to apply CSS style and also used in javascript to manipulate elements. The CSS class select
2 min read
How to use @apply directive in Tailwind-CSS ?
Tailwind CSS is a popular utility-first CSS framework. It is a set of pre-defined CSS classes that can be used to quickly and easily style your HTML elements without having to write custom CSS code. The classes are organized into a set of utility classes that can be used to control the layout, spacing, typography, and other aspects of your design.
3 min read
How to dynamically create and apply CSS class in JavaScript ?
To dynamically create a CSS class and apply it to the element dynamically using JavaScript, first, we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList properties in JavaScript. ApproachThe className property is used to add a class in JavaScript. It overwrites existing classes o
2 min read
How to apply gradient as the mask using CSS ?
In this article, we will learn how to include the gradient as the mask layer in CSS, along with understanding their basic implementation with the help of suitable examples. The Mask Property with a Gradient is used to create a mask layer, that can be utilized for rendering or hiding the area of the content of an element. There are mainly 2 types of
3 min read
Apply Glowing Effect to the Image using HTML and CSS
While surfing most of the websites you have seen some special effects which can be seen on various images while putting your cursor over them. So, in this article, we are going to implement the same. We can use such images as a card for our website.  In this article, you’re going to learn how to apply the glowing effect to a particular image, when
2 min read
How to apply the Clearfix Hack in CSS ?
The "Clearfix" hack is a technique used in CSS to clear or contain floated elements within a container. It addresses issues where a container does not fully encompass its floated child elements, causing layout problems, especially with collapsing margins. There are various ways to implement the clearfix hack, but a common method involves adding a p
1 min read
How to apply the @media rule in CSS ?
The @media rule in CSS allows you to apply different styles for different media types or devices. It's particularly useful for creating responsive designs that adapt to various screen sizes, resolutions, or other media characteristics. The @media rule is used in combination with media queries to conditionally apply styles. Here are some things you
1 min read
How to apply Hover Effect in CSS ?
Applying a hover effect in CSS allows you to change the styling of an element when a user hovers over it. This provides a dynamic and interactive experience for the user. Hover effects are typically defined using the :hover pseudo-class along with the desired styling. Syntax:/* Changing text color on hover */a:hover { color: red;}/* Adding a backgr
1 min read
How to apply styles only to the first child of an element using CSS ?
The :first-child selector is a pseudo-class in CSS that enables you to target the initial child element within a parent container. This selector handpicks elements that are the first immediate child of their parent. It serves as a powerful selector that makes it easier to apply unique styles or modifications to that specific element. <div class=
1 min read
How to apply a Filter to an Image in CSS ?
Applying filters to an image in CSS allows you to manipulate its visual appearance, adjusting properties like brightness, contrast, and color saturation. This is achieved using the filter property. Note: The filter property can take various filter functions, each controlling a specific aspect of the image. Syntax/* Applying a grayscale filter to an
1 min read
What is the purpose of @apply Directive in Tailwind CSS ?
The @apply directive in Tailwind CSS allows you to create and apply custom utility classes by defining styles in your own CSS or Sass files. This is useful when you want to reuse a set of Tailwind utility classes. SyntaxCreate a custom class in your CSS or Sass file.custom-button { @apply bg-blue-500 text-white p-2 rounded-md;}Use the custom class
1 min read
How to Apply Opacity to a Color Variable in CSS ?
To adjust opacity, we can use RGBA (Red, Green, Blue, Alpha) or HSLA (Hue, Saturation, Lightness, Alpha) values. By defining a color variable with an alpha value, we can dynamically control the transparency of elements in our stylesheets. Opacity is the attribute that controls the transparency of an element. Table of Content Using RGBA ValuesUsing
3 min read
How to Apply Transitions to the Display Property in CSS ?
In CSS, transitions allow you to change property values smoothly over a specified duration. However, the display property is not animatable, which means you cannot directly apply transitions to it like you can with properties such as opacity or transform. This limitation is because the display property does not support values between its states (e.
3 min read
How to apply concept of inheritance in CSS ?
Inheritance, a fundamental concept in object-oriented programming (OOP), mirrors real-life inheritance where children inherit traits from parents. In OOP, a child class inherits properties from a parent class, establishing a hierarchical relationship between them. CSS Inheritance: In CSS inheritance, the child element will naturally inherit propert
2 min read
How to Apply an Ellipsis to Multiline Text in CSS ?
Ellipsis refers to three dots (...) and is a user-friendly UI modification that indicates more information is available but has been truncated due to lack of space. In this article, we will explore two approaches to applying ellipses to multi-line text in CSS. Understanding Ellipsis in CSSEllipsis helps in providing a visual cue to users that there
3 min read
Article Tags :
three90RightbarBannerImg