How to override inline styles with external in CSS ?
Last Updated :
26 Jun, 2022
In this article, we are going to learn how we can override inline styles with external CSS. Generally, we use inline CSS to override all the other styles. In some circumstances, we have to do the opposite. We have to override the inline CSS which has come from foreign sources and cannot be removed.
Approach: To override the inline CSS, !important keyword is used. This makes the CSS property precede all the other CSS properties for that element.
Used Keyword:
- !important: This keyword can be used with a CSS property in inline, internal, or external CSS. This specifies that the property with which it is used will be given the highest priority for that element.
The below example demonstrates the above approach.
Example 1: The below code demonstrates how the color of the heading is changed by the external CSS using the !important keyword.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>
How to override inline styles with external CSS
</title>
</head>
<body>
<h1 style="color:green; margin:2rem;">
GeeksforGeeks
</h1>
<h3 style="margin:2.2rem;margin-top:-2rem;">
How can I override inline styles with external CSS
</h3>
<div style="border: 10px solid red;
width: 10rem; height: 10rem;
margin: 2rem; align-content: center;
display: flex;justify-content: center;">
GeeksforGeeks
</div>
</body>
</html>
style.css: This is the external CSS used in the above HTML code.
CSS
h3 {
color: brown !important;
}
div {
border-color: green !important;
width: 20rem !important;
}
Output:
Â
Example 2: The code below demonstrates how we can override the display, width, and height of the div elements given in the inline CSS with the external CSS.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style1.css">
<title>
How to override inline styles with external CSS
</title>
</head>
<body>
<h1 style="color:green;margin:2rem;">
GeeksforGeeks
</h1>
<h3 style="margin:2.2rem;margin-top:-2rem;">
How can I override inline styles with external CSS
</h3>
<div class="container" style="display:block">
<div style="border:10px solid red; width:10rem;
height:10rem; margin:2rem;
align-content:center; display:flex;
justify-content:center;">
GeeksforGeeks
</div>
<div style="border:10px solid red;
width:10rem; height:10rem;
margin:2rem; align-content:center;
display:flex; justify-content:center;">
GeeksforGeeks
</div>
</div>
</body>
</html>
style1.css: This is the external CSS used in the above HTML code.
CSS
/* Write CSS Here */
h3 {
color: brown !important;
}
.container {
display: flex !important;
}
div {
font-family: 'Lucida Sans', 'Lucida Sans Regular',
'Lucida Grande', 'Lucida Sans Unicode',
Geneva, Verdana, sans-serif;
border-color: green !important;
width: 18rem !important;
height: 7rem !important;
}
Output:
Â
Similar Reads
How to use Inline Styles in React ? ReactJS is a JavaScript library that is used to build a user interface. React makes it easy to create an interactive user interface by using a component-based approach. It offers various ways to apply styles to components. One of the methods is using inline styles, where styles are defined directly
3 min read
How To Use Inline Styles in ReactJS? Inline styles in React allow you to directly apply CSS styles to elements using the style attribute. This is often used for dynamic styling, where styles need to be applied based on the component's state or props. Inline styles in React are written as JavaScript objects. Each CSS property is camelCa
3 min read
How to Write a:hover in Inline CSS? The :hover pseudo-selector in CSS enables you to modify the style of elements when a user hovers their mouse over them. This selector is widely used to improve user experience by adding visual feedback on elements like buttons, links, images, or any interactive items on a webpage. For the :hover eff
2 min read
How to Link External CSS to HTML? External CSS is a method used to style multiple HTML pages with a single stylesheet. This approach involves creating a separate CSS file with a .css extension that contains style properties applied to various selectors (such as classes, IDs, headings, etc.). By using external CSS, you can maintain a
3 min read
How to Style Links in CSS ? Styling links in CSS is one of the fundamentals in web design that helps in creating an interactive and pleasant user experience through navigation. Links are core components for web pages, and using CSS, you can easily come up with the kind of look you want in terms of the overall appearance of the
7 min read