search
HomeWeb Front-endCSS TutorialCSS attribute margin
CSS attribute marginFeb 13, 2017 pm 02:57 PM

0. Impact on its own visual width

 CSS属性之margin

1>Change the visual width of the block element that is in the standard document flow and has no width value set

In the standard document flow, for a block element that does not have a width set, when it has content or a height is set, its own width is 100% of the width of the parent element. At this time, set the horizontal margin value for the block element. Will change the visual width of the element. But the height cannot be changed, because the height of the block element is fixed or equal to the height of its content and will not stretch.

This feature is useful in bootstrap layout.

<p>
  父元素padding: 0 10px;  </p><p>此block元素没有设置margin值</p>
  <p>此block元素设置margin-left:-10px;margin-right:-10px; 拉伸了元素宽度</p>

* {margin:0; padding:0;}.wrap {
  width: 400px;
  height: 400px;
  margin: 50px auto;
  padding: 0 10px;
  border: 1px solid #ccc;
}.red {
  height: 100px;
  background-color: red;
  margin: 0 -10px;
}.yellow {
  height: 100px;
  background-color: yellow;  
}

 CSS属性之margin

##2>Change width is not set, but is set The visible width and height of the absolutely positioned element that has been positioned (positioning set in pairs, such as top/bottom, left/right)

When the absolutely positioned element does not have width set, but top/bottom, left is set /right value, the element will be stretched. For example, if left:0; right:0; is set, the width of the element is 100% of the width of the first relatively positioned parent element. At this time, the visible width of the element can also be changed through margin. Similarly, for absolutely positioned elements, we can also change its visual height.

<p>
  父元素position: relative;  </p><p>绝对定位,并且设置top:0; bottom:0; 垂直拉伸元素,通过设置margin来改变可视高度</p>

* {margin:0; padding:0;}.wrap {
  position: relative;
  width: 400px;
  height: 400px;
  margin: 50px auto;
  border: 1px solid #ccc;
}.yellow {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100px;
  margin: 50px 0;
  background-color: yellow;  
}

 CSS属性之margin ##1. Percentage The margin of the value is the same as padding. For elements that are normally in the standard document flow, when the margin value of the element is a percentage value, its actual value is equal to the width of the parent element * percentage;

When an element is set to absolute positioning, its actual value is equal to the width * percentage of the first parent element positioned relative to it

<p>
  父元素宽度400px;  </p><p>margin-top:10%; 实际的margin-top=400px*10%=40px</p>

* {margin:0; padding:0;}.wrap {
  width: 400px;
  height: 400px;
  margin: 50px auto;
  border: 1px solid #ccc;
}.yellow {
  width: 200px;
  height: 200px;
  margin-top: 10%;
  background-color: yellow;  
}

2. Margin overlap

 CSS属性之margin Margin overlap usually occurs between sibling elements and between parent and child elements. If you don’t pay attention to it, margin Overlap may often cause us some minor troubles. Here is a summary of the situations where margin overlap occurs.

1>Between sibling elements

When two elements have margin overlap, their actual values ​​are:

When two margin values When both are positive, the actual value = the larger of the two values

  1. When the two margin values ​​are one positive and one negative, the actual value = the addition of the two values

  2. When both margin values ​​are negative, the actual value = the larger absolute value of the two

  3. When will margin overlap occur?

  4. For two adjacent sibling elements, as long as they are block elements in the standard document flow, margin overlap will occur in the vertical direction.

2>Between the parent element and the first/last child element

Set border-top/bottom

  1. Set padding-top/bottom

  2. Set attributes such as overflow:hidden/auto to implement the auto value of BFC

  3. 3.margin

  4. Set margin: 0 auto; for a fixed-width block element to center the element horizontally. This should be commonly used by many people, but few people may know the specific role of the auto value.

To put it simply, for elements that do not have a fixed width or height value set, if they can be automatically stretched, then after a fixed value is set, auto can be used to allocate the remaining space.

It feels a bit confusing. For example, there is a block element with a fixed width. We want it to be displayed on the right side. The most commonly used method is to use the float attribute, but it can also be achieved with margin-left: auto;:

<p>
  </p><p>margin-left: auto;</p>

* {margin:0; padding:0;}.wrap {
  position: relative;
  width: 400px;
  height: 400px;
  margin: 50px auto;
  border: 1px solid #ccc;
}.red {
  width: 100px;
  height: 100px;
  background-color: red;
  margin-left: auto;
}

Valid scenarios for setting the margin value to auto include :

 CSS属性之margin

Ordinary fixed-width block elements, the horizontal direction setting is valid;

  1. Absolutely positioned elements, when paired left/right, top are set When /bottom or both are set, fixed width or fixed height is valid;

  2. #When the parent element is display: flex;, the child element margin value is auto

  3. For more CSS attribute margin related articles, please pay attention to the PHP Chinese website!

Statement
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
Demystifying Screen Readers: Accessible Forms & Best PracticesDemystifying Screen Readers: Accessible Forms & Best PracticesMar 08, 2025 am 09:45 AM

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

Adding Box Shadows to WordPress Blocks and ElementsAdding Box Shadows to WordPress Blocks and ElementsMar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Working With GraphQL CachingWorking With GraphQL CachingMar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Making Your First Custom Svelte TransitionMaking Your First Custom Svelte TransitionMar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Classy and Cool Custom CSS Scrollbars: A ShowcaseClassy and Cool Custom CSS Scrollbars: A ShowcaseMar 10, 2025 am 11:37 AM

In this article we will be diving into the world of scrollbars. I know, it doesn’t sound too glamorous, but trust me, a well-designed page goes hand-in-hand

Show, Don't TellShow, Don't TellMar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

Building an Ethereum app using Redwood.js and FaunaBuilding an Ethereum app using Redwood.js and FaunaMar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

What the Heck Are npm Commands?What the Heck Are npm Commands?Mar 15, 2025 am 11:36 AM

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.