饥人谷技术博客

CSS浮动、定位

2016-07-24  本文已影响67人  Nicklzy

文档流的概念指什么?有哪种方式可以让元素脱离文档流?

有几种定位方式,分别是如何实现定位的,使用场景如何?

描述 使用场景
absolute 生成绝对定位的元素,相对于** static 定位以外第一个父元素**进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。
fixed 生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。 广告窗口
relative 生成相对定位的元素,相对于其正常位置进行定位。占用空间,相当于原元素的位置移动,不会脱离文档流。left:20会向元素的 LEFT 位置添加 20 像素。
static 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。 没用
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
      .container{
        width:400px;
        height:400px;
        border:1px solid ;
        position:fixed;
        margin:0px;
        left:8px;/*浏览器默认值,不设置也为8px*/
        top:8px;/*浏览器默认值,不设置也为8px*/
      }
      .box{
        width:100px;
        height:100px;
        border:1px solid black;
      }
      .box:nth-child(1){
        background:red;
        position:fixed;
        left:0px;
        top:0px;
      }
      .box:nth-child(2){
        background:blue;
        position:absolute;
        left:20px;
        top:20px;
      }
      .box:nth-child(3){
        background:green;
        position:relative;
        left:50px;
        top:50px
      }
      .box:nth-child(4){
        background:yellow;
        position:relative;
        left:50px;
        top:50px
      }
  </style>
</head>
<body>
    <div class="container">
      <div class="box">1</div>
      <div class="box">2</div>
      <div class="box">3</div>
      <div class="box">4</div>
    </div>
</body>
</html>

代码

效果

absolute, relative, fixed 偏移的参考点分别是什么

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

未加入z-index


未加入z-index.png

box1加入z-index

box1加入z-index

详细资料http://kejian.jirengu.com/web-fe3

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

效果

如何让一个固定宽高的元素在页面上垂直水平居中?

效果

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

01.png 02.png 03.png 04.png 05.png

元素设为浮动元素之后,会导致其丢失高度,父元素检测不到,导致父元素高度无法撑开。使得有了后面的浮动清除。


06

清除浮动指什么? 如何清除浮动?

  1. 父元素的高度无法被撑开,影响与父元素同级的元素
1.png

对box2设置清除右浮动。元素3不受影响。


2.png

对box2设置清除左浮动。由于box2左边并没有浮动元素,所以无效。


3

clear属性用于清除浮动,浮动元素在左用left,在右用right,both属性皆生效;
清除浮动是针对元素本身的,只能对自己有效果,不能够影响到别的元素。


本文版权归本人和饥人谷所有,转载请注明来源。

上一篇 下一篇

猜你喜欢

热点阅读