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

Advantages and disadvantages of conditional comments in IE

IE's conditional comments are a proprietary (...

Detailed explanation of Linux host name modification command

Linux change hostname command 1. If you only need...

A brief discussion on VUE uni-app template syntax

1.v-bind (abbreviation:) To use data variables de...

Detailed explanation of the basic use of centos7 firewall in linux

1. Basic use of firewalld start up: systemctl sta...

JS implements the snake game

Table of contents 1. Initialization structure 2. ...

XHTML Getting Started Tutorial: XHTML Hyperlinks

It is no exaggeration to say that hyperlinks conne...

How to solve the timeout during pip operation in Linux

How to solve the timeout problem when pip is used...

Native JS to implement the aircraft war game

This article example shares the specific code of ...

Detailed explanation of angular content projection

Table of contents Single content projection Multi...

Detailed explanation of nginx upstream configuration and function

Configuration Example upstream backend { server b...

MySQL series of experience summary and analysis tutorials on NUll values

Table of contents 1. Test Data 2. The inconvenien...

Detailed tutorial on installing mysql-8.0.20 under Linux

** Install mysql-8.0.20 under Linux ** Environmen...

Instructions for using JSON operation functions in Mysql5.7

Preface JSON is a lightweight data exchange forma...

Historical Linux image processing and repair solutions

The ECS cloud server created by the historical Li...