1. Margin can be negative
In the box model, the width/height, padding, and border of the content area cannot be negative, but margin is an exception, it can be negative.
I don’t know much about the skills of using negative margin values. I will add more when I have the opportunity in the future. Here is a classic application. The negative value of margin-left is combined with floating to achieve a fluid layout that does not change the DOM structure.
nbsp;html> <meta> <title>不改变DOM结构的流体布局</title> <style> .container { width:600px; margin-left: auto; margin-right: auto; background-color: orange; font-size: 16px; line-height: 1.5; } .box1 { width:100%; float:left; } .box2 { margin-right: 220px; padding-left: 20px; } img { width:200px; float:left; margin-left:-200px; } .clearfix:after { content: ""; display: table; clear: both; } .clearfix { *zoom: 1; } </style> <div> <div> <div> <h3 id="不改变DOM位置的流体布局">不改变DOM位置的流体布局</h3> <p>假如有一段文本和一幅图像,在DOM节点中,文本在前,图像在后,怎么能把图像定位到右边呢?</p> <p>通常的做法是,调换DOM节点中图像与文本的位置,让图像在前,文本在后,然后将图像浮动到右边即可。</p> <p>但这样改变DOM节点顺序始终不妥,还有什么更好的方法呢?</p> <p>下面就介绍一种新的思路来完成布局。</p> <ul> <li>将文本用div包起来,定义为box1;现在的结构是一个box1和一个img。</li> <li>将box1宽度设为100%,左浮动;将img设置一个宽度,也左浮动,然后margin-left设为负的宽度值;此时图像就定位到文本的右边啦。</li> <li>但是有一个问题,图像盖住了文本内容,这可怎么办?</li> <li>重点来了,在box1中增加一个box2,box2把文本全部包起来,然后margin-right设为图像的宽度(+额外的间距),这样就解决问题啦!</li> </ul> </div> <!--关闭box2--> </div> <!--关闭box1--> <img src="/static/imghwm/default1.png" data-src="http://imgsrc.baidu.com/forum/w%3D580/sign=0c101fe665380cd7e61ea2e59145ad14/f9a3492762d0f7032de1758a08fa513d2797c542.jpg" class="lazy" alt="Comprehensive understanding of CSS margin" > </div><!--关闭container-->
2. The percentage value of margin
When the value of the margin attribute is a percentage, it is always calculated based on the width of the parent element.
Please take a look at the demo below, which tortured me for a long time. . . It's just that I found out too late, and it would make me cry if I talked about it too much. . .
nbsp;html> <meta> <title>margin的百分数值</title> <style> .container { width: 500px; height: 300px; margin: 50px auto; background-color: orange; border: 1px solid black; } .box { width: 250px; height: 150px; margin-left: auto; margin-right: auto; background-color: cyan; } .box1 { margin-top: 75px; margin-bottom: 75px; padding: 5px; } .box2 { margin-top: 25%; margin-bottom: 25%; padding: 5px; } </style> <div> <div> <p>父元素的高度为300px,子元素的高度为150px,只要margin-top和margin-bottom都为75px,这个盒子就能垂直居中。</p> <p>OK,居中啦!!!</p> </div> </div> <div> <div> <p>既然子元素的高度是父元素高度的50%,那么只要margin-top和margin-bottom都为25%,应该也能垂直居中。</p> <p>额,这什么鬼?说好的居中呢?</p> </div> </div>
3. Merging of margins in the vertical direction
This problem often causes some confusion, but you only need to remember one sentence. Margins in the vertical direction will merge as long as they are in close contact. Only close contact will merge.
The merging of margins in the vertical direction is actually easy to understand if it occurs on adjacent elements; but if it occurs between a parent element and a child element, it is a little weird.
Let’s look at an example:
nbsp;html> <meta> <title>垂直方向上的margin合并</title> <style> .container { width: 500px; height: 300px; margin: 50px auto; background-color: orange; } .box { width: 300px; height: 200px; margin-left: auto; margin-right: auto; background-color: cyan; margin-top: 25px; padding: 5px; } .border { border: 1px solid black; /*padding: 1px;*/ } </style> <div> <div> <p>父元素的margin-top为50px,子元素的margin-top为25px;</p> <p>咦,子元素的margin-top呢?为什么都顶到父元素上边界了?</p> <p>额,因为父元素与子元素的margin-top亲密接触了呀,所以它们合并在一起了啊。</p> </div> </div> <div> <div> <p>可是我就是想让子元素距离父元素的上边界25px啊,我不想让它们合并呀。</p> <p>很简单,给父元素加个边框,它们就无法亲密接触了,就不会合并了啊。</p> <p>或者给父元素设置padding也是可以的喔。</p> </div> </div>
Methods to eliminate margin merging in the vertical direction: add a border or padding to the parent element to break the close contact between the margin of the parent element and the child element.
Margin merging rules:
nbsp;html> <meta> <title>margin合并规则</title> <style> .container { width: 300px; height: 500px; margin: 50px; background-color: orange; float: left; border: 1px solid black; } .box1,.box2,.box3, .box4,.box5,.box6 { width: 200px; height: 150px; margin: 30px auto; background-color: cyan; text-align: center; line-height: 150px; } .box1 { margin-bottom: 30px; } .box2 { margin-top: 20px; } .box3 { margin-bottom: 30px; } .box4 { margin-top: -30px; } .box5 { margin-bottom: -30px; } .box6 { margin-top: -50px; background-color: green; } p { width: 220px; margin:10px auto; font-size: 16px; line-height: 1.5; } </style> <div> <div>box1</div> <div>box2</div> <p>box1的margin-bottom为30px,box2的margin-top为20px,两个margin都是正数,取绝对值大的。</p> </div> <div> <div>box3</div> <div>box4</div> <p>box3的margin-bottom为30px,box4的margin-top为-30px,两个margin一正一负,相加。</p> </div> <div> <div>box5</div> <div>box6</div> <p>box5的margin-bottom为-30px,box6的margin-top为-50px,两个margin都是负数,取绝对值大的。</p> </div>
1. Both margins are positive numbers, take the larger absolute value;
2. One margin is a positive number, and the other margin is Negative numbers, add;
3. Both margins are negative numbers, take the larger absolute value.
The above comprehensive understanding of CSS margin is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.
For more CSS margin comprehensive information, please pay attention to the PHP Chinese website!

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
