Web Front-end
CSS Tutorial
Detailed explanation of offset of margin attribute in CSS (code example)This article will introduce to you the offset of the margin attribute in CSS. Interested friends can take a look.
Without further ado, let’s get straight to the point~
Let’s look at a specific example first (related recommendations: CSS Learning Manual)
Enter the code below: HTML file and CSS file.
MarginCollapsing01.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="MarginCollapsing01.css" />
</head>
<body>
<div class="BaseCanvas">
<div class="MarginFrame1">php中文网margin属性的偏移量</div>
<div class="MarginFrame2">php中文网</div>
</div>
</body>
</html>MarginCollapsing01.css
.BaseCanvas {
margin-left: 32px;
margin-right: 32px;
margin-top: 32px;
width: calc(100%-64px);
height: 320px;
background-color: #E0E0E0;
border: solid 1px #202020;
}
.MarginFrame1 {
width: 80%;
background-color: #fff2a9;
border: solid 1px #ff6a00;
margin-top: 40px;
margin-bottom: 40px;
margin-left: 12px;
margin-right: 8px;
}
.MarginFrame2 {
width: 80%;
background-color: #9effb5;
margin-top: 40px;
margin-bottom: 40px;
margin-left: 12px;
margin-right: 8px;
}The effect is as follows:
MarginFrame1 sets a margin of 40 pixels at the bottom, MarginFrame2 Set a top margin of 40 pixels. The distance between the upper and lower frames is equal to the distance between the outer and upper frames so that you don't get an edge with 80 pixels per edge and you can confirm that there is only a distance of 40 pixels.

Change margin size
Edit the CSS file and change the margin below MarginFrame 1 to 60 pixels.
MarginCollapsing01.css
.BaseCanvas {
margin-left: 32px;
margin-right: 32px;
margin-top: 32px;
width: calc(100%-64px);
height: 320px;
background-color: #E0E0E0;
border: solid 1px #202020;
}
.MarginFrame1 {
width: 80%;
background-color: #fff2a9;
border: solid 1px #ff6a00;
margin-top: 40px;
margin-bottom: 60px;
margin-left: 12px;
margin-right: 8px;
}
.MarginFrame2 {
width: 80%;
background-color: #9effb5;
margin-top: 40px;
margin-bottom: 40px;
margin-left: 12px;
margin-right: 8px;
}The effect is as follows: You can see that the distance between the upper and lower frames has become wider.

Set the value of float
The code is as follows:
MarginCollapsing02.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="MarginCollapsing02.css" />
</head>
<body>
<div class="BaseCanvas">
<div class="MarginFrame1">框架1</div>
<div class="MarginFrame2">框架2</div>
<div class="MarginFrame3">框架3</div>
<div class="MarginFrame4">框架4</div>
</div>
</body>
</html> MarginCollapsing02.css
.BaseCanvas {
margin-left: 32px;
margin-right: 32px;
margin-top: 32px;
width: calc(100%-64px);
height: 320px;
background-color: #E0E0E0;
border: solid 1px #202020;
}
.MarginFrame1 {
width: 160px;
float: left;
background-color: #fff2a9;
border: solid 1px #ff6a00;
margin-top: 32px;
margin-bottom: 48px;
margin-left: 24px;
margin-right: 24px;
}
.MarginFrame2 {
width: 160px;
float: left;
background-color: #fff2a9;
border: solid 1px #ff6a00;
margin-top: 32px;
margin-bottom: 48px;
margin-left: 24px;
margin-right: 24px;
}
.MarginFrame3 {
width: 160px;
float: left;
background-color: #fff2a9;
border: solid 1px #ff6a00;
margin-top: 32px;
margin-bottom: 48px;
margin-left: 24px;
margin-right: 24px;
}
.MarginFrame4 {
width: 160px;
float: left;
background-color: #fff2a9;
border: solid 1px #ff6a00;
margin-top: 32px;
margin-bottom: 48px;
margin-left: 24px;
margin-right: 24px;
}The effect is as follows:

This article ends here. For more related content, please pay attention to php Chinese website The CSS video tutorial column! ! !
The above is the detailed content of Detailed explanation of offset of margin attribute in CSS (code example). For more information, please follow other related articles on the PHP Chinese website!
Explain the concept of BEM (Block Element Modifier) naming convention in CSSJul 24, 2025 am 04:06 AMBEM is a CSS naming specification that improves code readability, reduces conflicts and facilitates maintenance. 1.Block is a separate component, such as .btn; 2.Element is an integral part of Block, such as .nav\_\_link; 3.Modifier represents a state or variant, such as .btn--large. The naming formats are .block, .block\_\_element, .block--modifier or .block\_\_element--modifier respectively. Using BEM can avoid class name conflicts, improve maintainability, and facilitate team collaboration. Notes include: avoiding too deep levels, using Modifier reasonably,
Explain CSS specificity and how it is calculatedJul 24, 2025 am 04:03 AMCSS refers to the weighting mechanism used by the browser to determine which style rules are preferred. When multiple rules act on the same element, a high-weight rule takes effect. For example, #mainp is more specific than p, and will cover the color blue. The calculation method is based on the selector type and is divided into four levels: a is the number of inline styles, b is the number of ID selectors, c is the number of classes, attributes, and pseudo-classes, and d is the number of elements and pseudo-elements. Compare bit by bit when comparing, and compare a→b→c→d in sequence like a phone number. Common misunderstandings include abuse!important, over-necking selectors, dependency ID selectors, etc. It is recommended to use clear structure and unified class name naming to reduce conflicts, and you can check the style coverage through the developer tool and adjust the selector rights.
How to create a custom cursor with CSS?Jul 24, 2025 am 04:03 AMThere are four ways to customize mouse pointers using CSS: 1. Use the cursor attribute to set built-in styles, such as pointer, text, etc.; 2. Specify the custom picture as the cursor through url() and set alternate styles; 3. Add coordinate values after url() to adjust the hot spot position of the cursor; 4. Use JavaScript to hide the native cursor and use elements to simulate dynamic effects. Each method is suitable for different scenarios, and needs to pay attention to compatibility, performance and user experience details.
What is the difference between `id` and `class` selectors?Jul 24, 2025 am 03:56 AMThe role of id and class in CSS is different, and the usage scenarios are also different. 1.id is a unique identifier, used for style design of a single unique element, such as #main-content; 2.class can be reused and is suitable for multiple elements to apply the same style, such as .button; 3.id's selector priority is higher than class, which will affect style overwrite; 4. id is used in HTML, class is used in class, 5. id is used in CSS, and class is used in class. 6. It is recommended to avoid excessive use of id for style design, because it is not easy to cover; 7.
How do you center an element horizontally and vertically with CSS?Jul 24, 2025 am 03:55 AMTo center elements horizontally and vertically, the most common methods include using Flexbox, Grid and absolute positioning to match transform. 1. Use Flexbox: Set the container to flex layout, and set it to center through justify-content and align-items, which is suitable for modern browsers and is simple and efficient; 2. Use Grid: Use display:grid and place-items:center to achieve centering, which is concise and clear; 3. Absolute positioning and transform: suitable for old browsers, set it to 50% by left and top and use transform to move half of its width and height back to achieve centering. It is commonly used
How to change the font color in CSS?Jul 24, 2025 am 03:51 AMTo change the text color in a web page, use the color attribute of CSS. This property controls the foreground color of the text and supports color name, hexadecimal value, RGB or HSL format, such as: p{color:red;} or h1{color:#00ff00;}, which can be applied to label selector, class or ID; commonly used methods include: 1. Color name (such as red); 2. Hexadecimal (such as #ff0000); 3. RGB (such as rgb(255,0,0)); 4. HSL (such as hsl(0,100%,50%)); If the color does not take effect during debugging, it may be overwritten by other rules, insufficient contrast or wrong format. It is recommended to use browser developer tools to check and debug in real time to ensure the effect
Explain techniques for image replacement using CSSJul 24, 2025 am 03:49 AMImagereplacementinCSSinvolveshidingrealtextanddisplayinganimageorstyledversioninstead,ensuringaccessibilityandSEObenefitswhileallowingvisualcustomization.1.Theclassicmethodusestext-indenttomovetextoff-screenwhileshowingabackgroundimage,requiringsetel
What are CSS Logical Properties and Values?Jul 24, 2025 am 03:48 AMCSSLogicalProperties and Values provides a layout based on writing mode, using inline, block, start, end and other logical directions to replace the traditional left, right, top, bottom. 1. It enables the style to automatically adapt to LTR, RTL and vertical layout without additional adjustments; 2. Common attributes include margin-inline-start, padding-block-end, inline-size and block-size; 3. Applicable to international websites, component library development and responsive design; 4. Be careful about compatibility when using it, and some browsers may need to fall


Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.






