Home Web Front-end CSS Tutorial Summary of css margin properties and usage

Summary of css margin properties and usage

Jun 01, 2017 am 11:33 AM

The white space surrounding the element's border is the margin. Setting margins creates additional "white space" outside the element. The simplest way to set margins is to use the margin attribute. The margin attribute accepts any length unit, which can be pixels, inches, millimeters or ems, percentage values ​​or even negative values. Next, this article will talk about the properties and use of margins in detail, the overlap and superposition of margins, and the role of negative margins.

1. Introduction to the margin attribute of CSS

1.Detailed explanation of CSS The margin attribute uses

Summary of css margin properties and usage

## The default value of margin is 0, So if a value is not declared for margin, no margin will appear. However, in practice, browsers already provide predetermined styles for many elements, and margins are no exception. For example, in browsers that support CSS, margins create "empty lines" above and below each paragraph element. Therefore, if no margin is declared for the p element, the browser may apply one on its own. Of course, as long as you specifically declare it, the default style will be overridden.

2. Detailed explanation of the use of css outer margin

Summary of css margin properties and usage

#Retrieve or set the extended margins on the four sides of the object.

If all four parameter values ​​are provided, the four sides will be applied in the order of upper, right, lower, and left. If only one is provided, it will be used on all four sides. If two are provided, the first is used for up and down, the second for left and right. If three are provided, the first is used for top, the second for left and right, and the third for bottom. Non-Replaced inline elements can use this attribute to set outer patches on the left and right sides; to set outer patches on the top and bottom sides, the object must first be rendered as block level or inline block level. Extended margins are always transparent.

3. Margin Property Knowledge Collection Introduction to Important CSS Properties

##1) Margin Property Brief introduction

2) Elements for which margin cannot be applied

3) Collapsing margins

4) Collapsing margins solution


## 2. The margins overlap or Overlay problem1.

Introduction to the solution to the margin boundary overlay problem in CSS (picture and text)

Summary of css margin properties and usage#Boundary overlay is a fairly simple concept. However, it can cause a lot of confusion when laying out web pages in practice. Simply put, when two vertical boundaries meet, they form a boundary. The height of this boundary is equal to the greater of the heights of the two superimposed boundaries.

2.

CSS margin overlap and prevention methods

Border overlap refers to two or more The adjacent borders of two boxes (which may be adjacent or nested) (without any non-empty content, padding, or borders between them) overlap to form a single border.

The vertically adjacent boundaries of two or more block-level boxes will overlap. The resulting border width is the largest of adjacent border widths. If a negative boundary occurs, the negative boundary with the largest absolute value is subtracted from the largest positive boundary. If there are no positive boundaries, the negative boundary with the largest absolute value is subtracted from zero. Note: Adjacent boxes may not be generated by elements with parent-child or sibling relationships.


3. The effect of negative margin

1. Briefly talk about the role of negative margin values

We all use margin in CSS, but if margin is set to a negative number, then It may not be easy to handle. Negative margin values ​​are not a hack. Negative margin values ​​follow the document flow. If you use negative margin values ​​to move an element upward, related elements will also be displaced accordingly. Negative margin values ​​are well compatible with various browsers. .

2. Use negative margin values ​​in CSS to adjust the center position

Summary of css margin properties and usage

This is perhaps the most commonly used centering method. If you know the size of each element, set a negative margin value equal to half the width and height (if you do not use the box-sizing: border-box style, you also need to add a padding value), and then match top: 50%; left: 50% ; style will center the block element.


##Related questions and answers about CSSmargin

1.

Front-end-Margin problem, that old man can help me explain it

2.

Front-end-CSS three-column equal-height layout question Margin negative compensation

3.

css - Why should negative margin values ​​be designed like this?


##[Related recommendations]

1.

CSS Centering: The Most Comprehensive Collection of CSS Centering Methods2.

DIV

Centering: The Most Comprehensive Collection p Centering method summary3.

css image centering: css image centering up, down, left, right (horizontally and vertically centered)

The above is the detailed content of Summary of css margin properties and usage. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dynamic web page elements XPath and Class names change frequently. How to stably crawl the target a tag? Dynamic web page elements XPath and Class names change frequently. How to stably crawl the target a tag? Apr 01, 2025 pm 04:12 PM

Dynamic web element crawling problem: dealing with XPath and Class name changes, many crawler developers will encounter a difficult problem when crawling dynamic web pages: the goal...

Is Debian Strings compatible with multiple browsers Is Debian Strings compatible with multiple browsers Apr 02, 2025 am 08:30 AM

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness

How to convert XML to PDF on Android phone? How to convert XML to PDF on Android phone? Apr 02, 2025 pm 09:51 PM

Converting XML to PDF directly on Android phones cannot be achieved through the built-in features. You need to save the country through the following steps: convert XML data to formats recognized by the PDF generator (such as text or HTML); convert HTML to PDF using HTML generation libraries such as Flying Saucer.

How to solve the problem of style loss after Django project is deployed to Pagoda panel? How to solve the problem of style loss after Django project is deployed to Pagoda panel? Apr 01, 2025 pm 09:09 PM

Detailed explanation of style loss after Django project is deployed to pagoda panel. After deploying Django project to pagoda panel, you may encounter style loss problem. This...

How to display hidden lines in xml How to display hidden lines in xml Apr 02, 2025 pm 11:45 PM

There are two common ways to hide rows in XML: Use the display property in CSS to set to none Use XSLT to skip hidden rows via conditional copying

How to display the content of the interface with xml How to display the content of the interface with xml Apr 02, 2025 pm 11:48 PM

XML is widely used to build and manage user interfaces. It defines and displays the interface content through the following steps: Define interface elements: XML uses tags to define interface elements and their properties. Building a hierarchy: XML organizes interface elements according to hierarchical relationships to form a tree structure. Using Stylesheets: Developers use stylesheet languages ​​such as CSS or XSL to specify the visual appearance and behavior of elements. Rendering process: A browser or application uses an XML parser and stylesheet to parse an XML file and render interface elements to make it visible on the screen.

If you convert XML to PDF on your mobile phone, will the format be messy after conversion? If you convert XML to PDF on your mobile phone, will the format be messy after conversion? Apr 02, 2025 pm 10:21 PM

When converting XML to PDF on mobile phone, whether the format is chaotic depends on: 1. The quality of the conversion tool; 2. XML structure and content; 3. Style sheet writing. Specifically, poor conversion tools, messy XML structures, or wrong XSLT code can lead to malformation.

How to implement a tight transition animation in React using react-transition-group? How to implement a tight transition animation in React using react-transition-group? Apr 04, 2025 pm 11:27 PM

Using react-transition-group in React to achieve confusion about closely following transition animations. In React projects, many developers will choose to use react-transition-group library to...

See all articles