Table of Contents
前面的话
表现
重叠
浮动
定位
应用
Home Web Front-end HTML Tutorial 深入理解CSS中的margin负值_html/css_WEB-ITnose

深入理解CSS中的margin负值_html/css_WEB-ITnose

Jun 24, 2016 am 11:22 AM

× 目录 [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的百分比相对于包含块的宽度,所以在需要居中的元素外面套一个空的

元素,并且利用absolute的包裹性,使该元素的宽高等于需要定位的元素的宽高

1

.box{    position:relative;    width: 200px;    height: 200px;    background-color: lightgreen;    border: 2px solid black;}.out{    position: absolute;    left: 50%;    top: 50%;}    .in{    height: 100px;    width: 100px;    background-color: pink;    margin-left: -50%;    margin-top: -50%;}

Copy after login

1

<div class="box">    <div class="out">        <div class="in">测试内容</div>        </div>    </div>

Copy after login

【2】列表项两端对齐

  在列表项外面包一层元素,使用margin负值来将最后一个列表项拉回来

1

ul{    margin: 0;    padding: 0;    list-style:none;}.box{    width: 200px;    background-color: pink;    }.list{    overflow: hidden;    margin-right: -10px;}.in{    float: left;    width: 60px;    height: 100px;    background-color: lightgreen;    margin-right: 10px;}

Copy after login

1

<div class="box">    <ul class="list">        <li class="in">1</li>        <li class="in">2</li>        <li class="in">3</li>    </ul>    </div>

Copy after login

【3】三栏自适应布局

  中间的主体使用双层标签,外层

宽度100%显示,并且浮动,内层
为真正的主体内容,含有左右210px的margin值。左栏和右栏都采用margin负值。左栏左浮动,margin-left为-100%,正好使左栏位于页面左侧。右栏左浮动,大小为其本身的宽度200px

1

html,body{    height: 100%;}body{    margin: 0;}.main{    width: 100%;    height: 100%;    float: left;}.main .in{    margin: 0 210px;    background-color: pink;    height: 100%;}.left,.right{    height: 100%;    width: 200px;    float: left;    background-color: lightgreen;}.left{    margin-left: -100%;}.right{    margin-left: -200px;}

Copy after login

1

<body><div class="main">    <div class="in"></div></div><div class="left"></div><div class="right"></div></body>

Copy after login

【4】三栏等高布局

  给每栏设置大的底部内边距,然后用数值相同的负外边距消除这个高度,然后在外层容器中设置overflow:hidden

1

body{    margin: 0;    overflow: hidden;}ul{    margin: 0;    padding: 0;    list-style: none;}.list{    overflow: hidden;    width: 100%;    height: 100%;}.main{    margin: 0 210px;    background-color: lightgreen;}.left{    width: 200px;    float: left;    background-color: pink;}.right{    width: 200px;    float: right;    background-color: pink;}.main,.left,.right{    margin-bottom: -9999px;    padding-bottom: 9999px;}

Copy after login

1

<ul class="list">    <li class="left">左侧文字比较少</li>    <li class="right">右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多右侧文字比较多</li>    <li class="main">中间文字比较少</li>    </ul></body>

Copy after login

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

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.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

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

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

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.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

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: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

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 of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

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.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

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.

See all articles