深入理解CSS中的margin负值_html/css_WEB-ITnose
× 目录 [1]表现 [2]重叠 [3]浮动 [4]定位 [5]应用
前面的话
margin属性在实际中非常常用,也是平时踩坑较多的地方。margin折叠部分相信不少人都因为这样那样的原因中过招。margin负值也是很常用的功能,很多特殊的布局方法都依赖于它。它看似简单,实际上却蛮复杂,本文就margin负值作详细介绍和梳理
[注意]关于margin部分的基础知识移步至此
表现
虽然margin可以应用到所有元素,但display属性不同时,表现也不同
【1】block元素可以使用四个方向的margin值
【2】inline元素使用上下方向的margin值无效
【3】inline-block使用上下方向的margin负值看上去无效
[注意]inline-block使用上下方向的margin负值只是看上去无效,这与其默认的vertical-align:baseline有关系,当垂直对齐的属性值为其他值时,则会显示不同的视觉效果
重叠
margin负值并不总是后面元素覆盖前面元素,它与元素display属性有关系
【1】两个block元素重叠时,后面元素可以覆盖前面元素的背景,但无法覆盖其内容
【2】当两个inline元素,或两个line-block元素,或inline与inline-block元素重叠时,后面元素可以覆盖前面元素的背景和内容
【3】当inline元素与block元素重叠时,inline的元素覆盖block元素的背景和内容
【4】当inline-block元素与block元素重叠时,inline-block元素覆盖block元素的背景,但无法覆盖其内容
浮动
【1】block元素与浮动元素重叠时,其边框和背景在该浮动元素之下显示,而内容在浮动元素之上显示
【2】inline或inline-block元素与浮动元素重叠时,其边框、背景和内容都在该浮动元素之上显示
定位
【1】定位元素(position不为static)覆盖其他元素的背景和内容
【2】将relative属性值应用于inline或inline-block元素后,由于无法改变其行内元素的本质,所以其上下margin依然存在问题
应用
【1】水平垂直居中
由于margin的百分比相对于包含块的宽度,所以在需要居中的元素外面套一个空的
1 |
|
1 |
|
【2】列表项两端对齐
在列表项外面包一层元素,使用margin负值来将最后一个列表项拉回来
1 |
|
1 |
|
【3】三栏自适应布局
中间的主体使用双层标签,外层
1 |
|
1 |
|
【4】三栏等高布局
给每栏设置大的底部内边距,然后用数值相同的负外边距消除这个高度,然后在外层容器中设置overflow:hidden
1 |
|
1 |
|

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











HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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

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.

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.
