任务10——浮动定位

2017-07-04  本文已影响0人  upup_dayday

1.浮动元素有什么特征?对父容器、其他浮动元素、普通元素、文字分别有什么影响?

float:right/left;

2.清除浮动指什么? 如何清除浮动? 两种以上方法

当为元素设定浮动后,元素脱离普通流,其他元素感受不到它的存在,会造成父元素高度塌陷、元素覆盖等一系列副作用;而这些并不是我们在布局时使用浮动的本意;清除这些浮动所带来的副作用,就是清除浮动;

清除浮动方法:

<div style="clear:both;"></div>

在浮动的父级元素中,插入一个无意义的标签,这样便可以解决父元素的塌陷问题;

overflow:auto/hidden

3.有几种定位方式?参考点是什么?使用场景是什么?

位置参考点 使用场景
inherit 规定应该从父元素继承 position 属性的值 需要继承父元素的定位
static 默认值。没有定位 元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)
relative 内生成相对定位的元素,相对于其正常位置进行定位
因此,"left:20" 会向元素的 LEFT 位置添加 20 像素
微调元素在正常流中的位置,用于对齐或调整效果
absolute 相对于 距离最近的非static的祖先元素 定位以外的第一个父元素进行定位。
元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。
使元素脱离普通流的同时,可以精确指定位置
可以覆盖页面其他元素,可以通过z-index调整叠放顺序
相当于可以指定位置的浮动效果
fixed 相对于浏览器窗口进行定位。
元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。
使元素固定在窗口的某个位置,当页面滚动时,元素始终保持在窗口固定位置

4.z-index 有什么作用? 如何使用?

z-index 属性指定了一个元素及其子元素的 z-order。 当元素之间重叠的时候,z-order 决定哪一个元素覆盖在其余元素的上方显示。 通常来说 z-index 较大的元素会覆盖较小的一个。
这里需要注意的是,z-index使用对象是定位的元素,即position属性是非static的元素;
对于一个已经定位的元素(即position属性值是非static的元素),z-index 属性指定:

z-index 的值有两种:

img
  {
  position:absolute;
  left:0px;
  top:0px;
  z-index:-1;
  }

例如上面对img设置z-index以后,便可是img向下移动一层,衬在默认z-index的元素下方

5.position:relative和负margin都可以使元素位置发生偏移,二者有什么区别?

虽然两者都可以使元素位置发生偏移,但是意义是不同的。

用两个例子来说明
原始样式及代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="box1">
    
    <div class="box2">
      <p id="p1"> 饥人谷饥人谷饥人谷饥人谷饥人谷饥人谷饥人谷饥人谷饥人谷饥人谷</p>
      <p id="p2">天天向上天天向上天天向上天天向上天天向上天天向上天天向上天天向上</p>
      <p id="p3">123456781234567812345678 12345678123456781234567812345678</p>
     
    </div>
    <div class="box3">
      <p>hellow jirengu hellow jirengu hellow jirengu hellow jirengu</p>
    </div>

  </div>
</body>
</html>

*{
  margin:0;
  padding:0;
}
.box1{
  width:600px;
  height: 500px;
  border: 1px solid red;
}

.box2{
  width: 300px;
  height: 300px;
  border: 1px solid black;
  margin:20px;
}

 #p1{
   background:green;
  /*position: relative;
  top:-10px;*/
   /*margin-top:-10px;*/
   
 }
 #p2{
   width: 100%;
    height: 100px;
   background:blue;
   /*position: relative;
  top:-10px;*/
   /*margin-top:-10px;*/
 }
 #p3{
    width: 100%;
    height: 100px;
    background:yellow;
 }

.box3{
  width:100px;
  height:100px;
  background:yellow;
  /*position: relative;
  top:-30px;*/
 /*margin-top:-30px;*/
  
}
Paste_Image.png
.box2{
  width: 300px;
  height: 300px;
  border: 1px solid black;
  margin:20px;
  position: relative;
  top:-30px;
}
Paste_Image.png

当对box2设置负margin时,可以看到box3也随着一起向上移动了,同时box2原有设置的margin在左右下方向仍生效,只是top方向,发生了改变,如下图所示

.box2{
  width: 300px;
  height: 300px;
  border: 1px solid black;
  margin:20px;
  /*position: relative;
  top:-30px;*/
  margin-top:-30px;
}
Paste_Image.png
 #p2{
   width: 100%;
    height: 100px;
   background:blue;
   position: relative;
  top:-10px;
   /*margin-top:-10px;*/
 }
Paste_Image.png

当设置p2为负margin时,p2向上移动,p3也跟着向上移动,但是p2并不是完全覆盖住p1的内容,文字部分有重叠

#p2{
   width: 100%;
    height: 100px;
   background:blue;
   /*position: relative;
  top:-10px;*/
   margin-top:-10px;
 }
Paste_Image.png

6.BFC 是什么?如何生成 BFC?BFC 有什么作用?举例说明

BFC是块级格式上下文,Block Format Content
在块格式化上下文中,从一个块容器的顶部开始,盒模型被一个接一个地垂直放置。两个兄弟盒之间的垂直距离是由“margin”决定的。在同一个块级格式上下文中,相邻块级框之间的垂直边距会塌陷,这个包括相邻元素,嵌套元素,只要他们之间没有阻挡(例如边框,非空内容,padding等)就会发生margin重叠。
在块格式化上下文中,每个框的左外边缘与容器的左边缘接触(右向左格式化,右边缘接触)。即使在浮动的情况下也是如此(尽管框中的框可能由于浮动而收缩),除非框建立了一个新的块格式化上下文(在这种情况下,由于浮动,盒本身可能变得更窄)。

生成BFC的方法有:

BFC的用处主要有两个:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="box1">
    <div class="box3">
    </div>
    <div class="box4">
      <h1>h1</h1>
    </div>
  </div>
</body>
</html>

.box1{
  width: 400px;
  height:500px;
  border: 1px solid red;
  overflow:auto;
}
.box3{
  width: 100px;
  height: 100px;
  background: yellow;
  margin:20px;
}

.box4{
  width: 100px;
  height: 100px;
  background: green;
  margin:20px;
}
h1{
  margin:20px;
}
Paste_Image.png

这里可以看到h1并没有像设想的那样与box4之间有20px的margin,通过设置overflow,让box4生成BFC,可以解决父子元素见的边距合并

.box4{
  width: 100px;
  height: 100px;
  background: green;
  margin:20px;
  overflow:auto;
}
Paste_Image.png
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="box1">
    <div class="box3">
    </div>
    <div class="box4">
    </div>
  </div>
</body>
</html>

.box1{
  border: 1px solid red;
}
.box3{
  width: 100px;
  height: 100px;
  background: yellow;
  float: left;

}

.box4{
  width: 100px;
  height: 100px;
  background: green;  
  float: left;
}
Paste_Image.png

通过对box1设置overflow:auto,可以解决父容器高度塌陷的问题

.box1{
  border: 1px solid red;
  overflow:auto;
}
Paste_Image.png

7.在什么场景下会出现外边距合并?如何合并?如何不让相邻元素外边距合并?给个父子外边距合并的范例

当两个块级元素同属于一个BFC时,会发生外边距合并,水平的边距不会合并,只是vertical方向,如下举例:

.box1{
  border: 1px solid red;
}
.box3{
  width: 100px;
  height: 100px;
  background: blue;
  margin:20px;
}

.box4{
  width: 100px;
  height: 100px;
  background: green;  
  margin:20px;
}

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="box1">
    <div class="box3">
    </div>
    <div class="box4">
    </div>
  </div>
</body>
</html>
Paste_Image.png

例子中,对box3,4都设置了margin20px,但实际上两个box之间的margin只有20,这就是外边距合并;

解决相邻元素间的外边距合并,可以通过以下三种方法:

  1. postion:absolute;
  2. float:left;
  3. display:inline-block;

这里用inline-block举例

.box3{
  width: 100px;
  height: 100px;
  background: blue;
  margin:20px;
  display:inline-block;
}
Paste_Image.png

可以看到,通过设置inline-block,两个box之间的margin达到了40px;

根据两个元素的边距间的数值取值不同,有以下规律:

上例的取值,正好是两边的margin值相等,以下分别列举了其他几种情况的例子

.box1{
  border: 1px solid red;
}
.box3{
  width: 100px;
  height: 100px;
  background: blue;
  margin:30px;
}

.box4{
  width: 100px;
  height: 100px;
  background: green;  
  margin:20px;
}

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="box1">
    <div class="box3">
    </div>
    <div class="box4">
    </div>
  </div>
</body>
</html>
Paste_Image.png

通过审查元素可以看到,合并后,取的是较大值的margin作为合并后的margin;

.box1{  
  width: 600px;
  margin:0 auto;
  border:1px solid red;
}

.box3{
  width: 100px;
  height: 100px;
  background: blue;
  margin:30px;
}

.box4{
  width: 100px;
  height: 100px;
  background: green;  
  /*margin:-30px;*/
}

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="box1">
    <div class="box3">
    </div>
    <div class="box4">
    </div>
  </div>
</body>
</html>
Paste_Image.png

当box不设置margin时,我们可以看到正常的box4位置
当box设置margin为-30时,可以看到box4的位置是在box3设置margin的基础上再负向移动的,正负值抵消,两者垂直方向没有margin;
一正一负的情况下,两者的值计算后正常偏移;

Paste_Image.png
.box1{  
  width: 600px;
  margin:0 auto;
  border:1px solid red;
}

.box3{
  width: 100px;
  height: 100px;
  background: blue;
  margin:30px;
}

.box4{
  width: 100px;
  height: 100px;
  background: green;  
  margin:-10px;
}

.box5{
  width: 100px;
  height: 100px;
  background: yellow;  
  /*margin:-10px;*/
}

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div class="box1">
    <div class="box3">
    </div>
    <div class="box4">
    </div>
    <div class="box5">
    </div>
  </div>
</body>
</html>

首先看box4设置负边距,box5未设置边距的效果


Paste_Image.png

当box也设置-10的边距时,可以看到两个边距合并,box4和box5之间边距为-10

.box5{
  width: 100px;
  height: 100px;
  background: yellow;  
  margin:-10px;
}
Paste_Image.png

当box5边距设置为-20px时,可以看到box4和box5垂直方向上边距为-20
所以,当两个边距均为负值时,取小的那个值作为合并后的边距

父子外边距的说明见“6”

8.代码

代码1
代码2
代码3
代码4

上一篇 下一篇

猜你喜欢

热点阅读