What should you pay attention to in the order of writing css?
1. CSS order
# First of all, it is stated that the way the browser reads css is from top to bottom. We generally write CSS and as long as the elements have these attributes, we will achieve the desired effect. However, this will have a certain impact on future maintenance and the rendering efficiency of the browser. So how should we write the order of CSS? Are there any certain norms?
First of all, we know that CSS properties are divided into several categories according to characteristics;
1. Specify element characteristics, such as display, position, float, These attributes will determine its layout method
2. Specify the space occupied by the element, such as line-height, margin, padding, width, height, etc. These attributes will determine the size and Position
3. Specify the element’s own effect, such as font-size, color, background, etc. These attributes will determine the effect of the element
In fact, when we finish classifying the properties of CSS, we will get an obvious answer. We can imagine ourselves using CSS to draw the element in the order of writing. For example
p.detail { font-size: 10px; line-height: 12px; width: 30px; height: 30px; display: inline; }
Description: This is an element with a font size of 10px, a line height of 12px, a width of 30px, and a height of 30px. The display mode is inline style
When we start reading When it comes to attributes, it will be difficult for us to locate the element because we don't know the characteristics and display method of the element. When I read the last line, I discovered that it turned out to be an inline element, and the definition of width and height would be invalid, so this CSS writing order is not recommended
p.detail { display: inline-block; margin-top: 20px; width: 100%; height: 20px; color: #fff; font-size: 10px; }
Description: This is an element, the display mode is inline-block mode, the distance is 20px, the width is the same as the parent element, the height is 20px, the color is white, the font size is 10px
This way of writing can follow a We render in a way that is easy to understand
Summary: The recommended specification when we write CSS is to first write the attributes that affect the display characteristics of the elements, then write the attributes that affect the position of the elements, and finally Writing internal attributes of elements
2. Writing css that triggers highlighting when clicking or hovering
When the mouse hovers, we often add elements that need to be changed An active class name, and then write the attributes we need to change in active
For example:
.content { background: black; } .active { background: white; }
When we click on an element, we need the background color of .content From black to white, adding the active class name to .content will achieve our expected effect. However, sometimes we have to change more than one element's attributes when clicking. We may have this situation
We need to know what to do if the icon font of the element and the span element become larger and the other becomes red when we click on an element with a class name of .click. We can do this
<div class="parent"> <div class="icon-font"></div> <span class="text"></span> </div> <div class="click"></div> <style> .icon-active{ font-size: 40px; } .text-active{ color: red; } </style> <script src="jquery.js?1.1.11"></script> <script> $(‘.click’).click(function() { $('.icon-font').addClass('icon-active'); $('.text').addClass('text-active') }) </script>
This can achieve our expected effect, but this is the associated reaction of two elements. If there are three elements or more, we will need more code,
In fact, careful friends may have discovered that I have A .parent tag, we can add the active class name code to the .parent tag as follows:
<div class="parent"> <div class="icon-font"></div> <span class="text"></span> </div> <div class="click"></div> <style> .active .icon-font{ font-size: 40px; } .active .text{ color: red; } </style> <script src="jquery.js?1.1.11"></script> <script> $(‘.click’).click(function() { $('.parent').addClass('active'); }) </script>
In this case, we only need to change the last part of the css element Add the active class name to the nearest outermost layer, and then set the style of the descendant elements under this class name, so that we only need to add an active class name to achieve the desired effect
The above is the detailed content of What should you pay attention to in the order of writing css?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
