负边距在布局中的使用

负边距在布局中的使用

负边距即margin属性的值设为负值,在CSS布局中时一个很有用的技巧。值为正的场景很常见,大家都很熟悉其表现

  • margin-topmargin-left为负值的时候,会把元素上移、左移,同时文档流中的位置也发生相应变化,这点与position:relative的元素设置top、left后元素还占据原来位置不同
  • margin-bottommargin-right设为负值的时候,元素本身没有位置变化,后面的元素会下移、右移

看几个应用场景

绝对定位元素

当元素被设置为绝对定位的时候其top、right、bottom、left值是指离最近的非static元素的距离,经典的垂直居中的一种方式正是利用的绝对定位元素的负边距实现的

<style>
.wrap4{
    position:relative;
    margin:10px;
    width:200px;
    height:200px;
    border:dashed 1px orange;
}

.wrap4 .content{
    position:absolute;
    width:100px;
    height:100px;
    top:50%;
    left:50%;
    margin-top:-50px;
    margin-left:-50px;
    background:orange;
}
</style>

<div class="wrap4">
    <div class="content"></div>
</div>

把元素设置为绝对定位,然后设置top和left为50%,这时候元素的上边、左边就到了父元素的50%处,再对元素设置其自身高度、长度一般的负边距,使元素中心移动到父元素中心,实现居中对齐

.wrap4 { position: relative; margin: 10px; width: 200px; height: 200px; border: dashed 1px orange }
.wrap4 .content { position: absolute; width: 100px; height: 100px; top: 50%; left: 50%; margin-top: -50px; margin-left: -50px; background: orange }

float元素

负边距对float元素的影响也是按照上面说的,不过有其特殊性,我们看个例子就很清楚了

浮动元素负边距

<style>
.float{
    overflow:hidden;
    width:280px;
    border:dashed 1px orange;
}

.float .item{
    width:100px;
    height:100px;
    float:left;
}

.float .item:nth-child(1){
    background:red;
}
.float .item:nth-child(2){
    background:grey;
}
.float .item:nth-child(3){
    background:blue;
}
</style>

<div class="float">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

在一个宽度为280px的div中右3个float:left的子元素,宽度为100px,由于一排放不下,最后一个陪移动到了下一行

.float { overflow: hidden; width: 280px; border: dashed 1px orange }
.float .item { width: 100px; height: 100px; float: left }
.float .item:nth-child(0n+1) { background: red }
.float .item:nth-child(0n+2) { background: grey }
.float .item:nth-child(0n+3) { background: blue }

我们对代码稍作修改

<style>
.float{
    overflow:hidden;
    width:280px;
    border:dashed 1px orange;
}

.float .item{
    width:100px;
    height:100px;
    float:left;
}

.float .item:nth-child(1){
    background:red;
}
.float .item:nth-child(2){
    background:grey;
}
.float .item:nth-child(3){
    background:blue;
    margin-left:-20px;
}
</style>

<div class="float">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

第三个元素添加-20px的负边距

.float .n { margin-left: -20px }

这时候发现第三个元素移上去了,并且覆盖了第二个元素20px,经典的多列布局正是利用此原理

多列布局

<style>
.body{
    width:500px;
    margin:10px;
    border:dashed 1px orange;
    overflow:hidden;
}

.wrap3{
    float:left;
    width:100%;
}

.wrap3 .content{
    height:200px;
    margin-right:100px;
    background:rgba(255,0,0,0.5);
}

.body .right{
    width:100px;
    height:200px;
    float:left;
    margin-left:-100px;
    background:rgba(0,255,0,0.5)
}
</style>

<div class="body">
    <div class="wrap3">
        <div class="content">
            Content Content Content Content Content Content Content
            Content Content Content Content Content Content Content Content
        </div>
    </div>
    <div class="right">Right</div>
</div>

代码很简单

  1. 为content元素添加父元素,设置左浮动,宽度100%
  2. content元素设置右边距,值等于right的宽度
  3. right左浮动,然后设置其宽度的负边距

本来right应该在第二行显示了,但是其宽度的左浮动使它到了第一行的最右边,覆盖了wrap的一部分,但是content有right宽度的右边距,覆盖区域没有内容,这样就实现了两列布局

.body { width: 500px; margin: 10px; border: dashed 1px orange; overflow: hidden }
.wrap3 { float: left; width: 100% }
.wrap3 .content { height: 200px; margin-right: 100px; background: rgba(255,0,0,0.5) }
.body .right { width: 100px; height: 200px; float: left; margin-left: -100px; background: rgba(0,255,0,0.5) }

Content Content Content Content Content Content Content Content Content Content Content Content Content Content Content

Right

PS. 其它此类更复杂的布局原理类似,感兴趣的同学可以看这里学40种。。。

普通元素

负边距对不同块元素的影响很有意思,我们通过几个例子来看一下

多列列表

<style>
li{
    line-height:2em;
}

.col2{
    margin-left:150px;
}

.col3{
    margin-left:300px;
}

li.top{
    margin-top:-9em;
}
</style>

<ul>
    <li class="col1">aaa</li>
    <li class="col1">bbb</li>
    <li class="col1">ccc</li>
    <li class="col2 top">ddd</li>
    <li class="col2">eee</li>
    <li class="col2">fff</li>
    <li class="col3 top">ggg</li>
    <li class="col3">hhh</li>
    <li class="col3">iii</li>
</ul>

定义一个列表,三列显示

li { line-height: 2em }
.col2 { margin-left: 150px }
.col3 { margin-left: 300px }
li.top { margin-top: -9em }

  • aaa
  • bbb
  • ccc
  • ddd
  • eee
  • fff
  • ggg
  • hhh
  • iii

普通的做法我们肯定是通过浮动实现,通过刚才介绍的知识应该不难理解为什么这样也行。看起来在普通元素上没什么稀奇的

放大元素

什么?负边距还可以放大元素!!!

<style>
.wrap{
    width:300px;
    border:dashed 5px orange;
}

.wrap .inner{
    height:50px;
    margin:0 -50px;
    background:blue;
    opacity:0.5;
}
</style>

<div class="wrap0">
  <div class="inner0">
    inner inner inner inner inner inner inner inner inner inner inner inner
  </div>
</div>

这个例子看起来平淡无奇,效果却很惊人,内层的div设置了水平的负边距后竟然变大了

.wrap0 { width: 300px; margin: 50px; border: dashed 5px orange }
.wrap0 .inner0 { height: 50px; margin: 0 -50px; background: rgba(0,0,255,0.5) }

inner inner inner inner inner inner inner inner inner inner inner inner inner

PS. 效果能实现的前提是元素的宽度不能设置为auto以外的值

带有右边距的浮动子元素列表

.wrap2 { width: 320px; border: dashed 1px orange }
.wrap2 .inner2 { overflow: hidden; margin-right: -10px }
.wrap2 .item { float: left; width: 100px; height: 100px; margin: 10px 10px 10px 0; background: blue }

看到这种效果你第一想法是什么?会不会是子元素设置margin-right,在遍历的时候nth-child(3n)还要设置为0,看看利用上面知识我们可以怎样处理

<style>
.wrap2{
    width:320px;
    border:dashed 1px orange;
}

.wrap2 .inner{
  overflow:hidden;
  margin-right:-10px;
}

.wrap2 .item{
    float:left;
    width:100px;
    height:100px;
    margin:10px 10px 10px 0;
    background:blue;
}
</style>

<div class="wrap2">
    <div class="inner">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
  </div>
</div>

我们没有设置nth-child(3n)的边距为0,而是通过负边距使父元素“变大”。

负边距是不是很有意思,不很了解的少年们学起来吧!

参考

CSS布局奇淫巧计之-强大的负边距

转自Samaritans

时间: 2024-08-27 11:47:29

负边距在布局中的使用的相关文章

负边距在布局中的应用

根据前几天对css负边距的研究(<浅析css负边距>),在理解负边距的原理之后,可以用在以下情况当中(持续更新). 一.两列布局 1.右侧定宽,左侧宽度自适应 思路:要实现效果,首先,左侧的宽度要能够自适应,那么宽度可选100%或auto,我们知道设置成100%会使左侧div的宽度为父元素的100%,设置成auto会根据左侧div的实际大小自适应宽度,那如果div里面有块级元素,右侧就会被挤到下一行,所以我们选择100%:然后我们需要在左侧div的右边空出位置给右侧div,那么只要把左侧div

负margin在页面布局中的应用

负数给人总是一种消极.否定.拒绝之感,不过有时利用负margin可以达到奇妙的效果,今天就表一表负值在页面布局中的应用.这里说的负值主要指的是负margin. 关于负margin的原理建议大家看看这篇文章:http://www.cnblogs.com/2050/archive/2012/08/13/2636467.html#2457812 本文不讲原理,主要展示几个应用. 一.左右列固定,中间列自适应布局 此例适用于左右栏宽度固定,中间栏宽度自适应的布局.由于网页的主体部分一般在中间,很多网页都

CSS之固定定位,绝对定位+负边距,双飞翼布局,属性选择器,伪类选择器补,状态伪类选择器,相邻全部兄弟选择器,取非选择器,em与rem,变形效果

固定定位: 绝对定位+负边距 !!!!!!!!!!!!!!!!!!!超重要 负边距 双飞翼布局 属性选择器 伪类选择器 补 状态伪类选择器 相邻全部兄弟选择器 取非选择器 em与rem 变形效果 固定定位: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <style> #div1 { height: 1200px; backgr

css负边距之详解(子绝父相)

来源 | http://segmentfault.com 原文 |  The Definitive Guide to Using Negative Margins   自从1998年CSS2作为推荐以来,表格的使用渐渐退去,成为历史.正因为此,从那以后CSS布局成为了优雅代码的代名词. 对于所有设计师使用过的CSS概念,负边距作为最少讨论到的定位方式要记上一功.这就像是在线纹身-每个人都会做,但是没有人会谈论它.(It’s like an online taboo—everyone’s doin

负边距实现圣杯布局以及列等高

圣杯布局如下图所示, 图一 两边的内容宽度固定,中间栏宽度自适应.html代码如下, <div class="container"> <div class='main'> <p>main</p> <p>主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干内容主干

(转)CSS布局-负边距-margin

css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果.很多特殊的css布局方法都依赖于负边距,所以掌握它的用法对于前端的同学来说,那是必须的.本文非常基础,老鸟可以略过. 负边距在普通文档流中的作用和效果 那些没有脱离文档流的元素(指不是浮动元素也不是绝对定位.固定定位的元素等),其在页面中的位置是跟随者文档流的变化而变化的.看下面这幅图: 负边距对这些由文档流控制的元素的作用是,会使它们在文档流中的位置发生偏移,但这种偏移不同于相对定位

负边距、三栏布局(圣杯布局、双飞翼布局)

首先来了解下 一.负边距在让元素产生偏移时和position: relative有什么区别? position:relative产生偏移时,他原来的位置不会脱离文档流的,即还占用原来的空间,但负边距产生偏移时它原来的位置并不占用空间: 例如下面的代码 Document *{ margin: 0px; padding: 0px; } .up,.down,.middle{ background-color: red; height: 100px; width: 100px; } .negative-

负margin在布局中的运用(*****************************************************************)

一.左右栏宽度固定,中间栏宽度自适应 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>左右栏宽度固定,中间栏宽度自适应</title> <style> body{ margin: 0; padding: 0; min-width:600px; color: #fff; font-weight:

【CSS】 布局之剖析负边距

我们都知道,一个元素框的大小是由元素内容+内边距+边框+外边距来决定的. 关于内边距padding,内边距呈现了元素的背景,其设置值是不可以为负的. 而对于外边距margin,默认为透明,设置值可以为负,其实这也就是所谓的负边距. 下面我们来分析一下margin负边距的原理. 首先我们看看下面的布局: 1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4