Several ways to solve CSS style conflicts (summary)

Several ways to solve CSS style conflicts (summary)

1. Refine the selector

By using combinators, the description of the selector can be written more precisely (refer to CSS selectors - MDN). For example, for the following code snippet, if you want to add styles to .apple in .cellphones, using only .apple will inevitably affect .apple in .fruit as well.

<div class="cellphones">
  <div class="apple"></div>
</div>
<div class="fruit">
  <div class="apple"></div>
</div>

A more precise description could be Descendant Combinator or Child Combinator. The more precise the description, the higher the priority. A higher priority description will override a lower priority description.

/* Descendant combiner: all descendant nodes*/
.cellphones .apple {
 border: 1px solid black;
}

/* More precise descendant combinator */
body .cellphones .apple {
  border: 1px solid blue;
}

/* Child combiner: direct child nodes */
.cellphones > .apple {
  border: 1px solid red;
}

If you add all the above styles to .apple in order, the border will eventually appear blue.

For detailed priority rules, see CSS Priority

2. Write the selector name again

Essentially a special case of the previous case. For example, for .apple, add the following style:

.cellphones > .apple.apple {
  border: 1px solid purple;
}
.cellphones > .apple {
  border: 1px solid red;
}

Eventually, the border will appear purple.

3. Change the order in the CSS stylesheet

For styles specified by the same type selector, the styles that come later in the CSS file will override the earlier styles.

For example, for the div element in the following code, the browser rendering result will be red:

<div class="redBorder" class="blackBorder"></div>
.blackBorder {
  border: 1px solid black;
}
.redBorder {
  border: 1px solid red;
}

It is important to note that although .blackBorder appears after .redBorder in the HTML file, the priority is determined based on their order in the CSS file. That is to say, only the .redBorder that is later in the CSS file will be used.

4. Actively increase priority (not recommended)

There is also a simple and crude method, but it is not recommended, which is to add the keyword !important after the style you want to use to raise the style priority to a very high level. For example:

<div class="redBorder" class="greenBorder"></div>
.greenBorder {
  border: 1px solid green !important;
}
.redBorder {
  border: 1px solid red;
}

For the above code, the border will be displayed in green.

Using !important is a very bad habit that breaks the cascading rules inherent in style sheets, making debugging very difficult!

References:
Priority - MDN
How to solve CSS style conflicts - Magic Time Machine

This concludes this article on several ways to resolve CSS style conflicts (summary). For more relevant CSS style conflicts, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Some issues we should pay attention to when designing a web page

>>:  MySQL table return causes index invalidation case explanation

Recommend

How to simulate enumeration with JS

Preface In current JavaScript, there is no concep...

Sharing some details about MySQL indexes

A few days ago, a colleague asked me a question a...

Detailed explanation of the core concepts and basic usage of Vuex

Table of contents introduce start Install ① Direc...

Detailed usage of js array forEach instance

1. forEach() is similar to map(). It also applies...

Detailed explanation of how to use Tomcat Native to improve Tomcat IO efficiency

Table of contents Introduction How to connect to ...

How to Check Memory Usage in Linux

When troubleshooting system problems, application...

How to use glog log library in Linux environment

Generate Linux library The Linux version uses cen...

Vue-Element-Admin integrates its own interface to realize login jump

1. First look at the request configuration file, ...

VMware Workstation installation Linux system

From getting started to becoming a novice, the Li...

Building the User Experience

<br />Maybe you've just come into a comp...

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

Vue implements card flip carousel display

Vue card flip carousel display, while switching d...