任务10

  1. 文档流的概念指什么?有哪种方式可以让元素脱离文档流?
  • 文档流:将窗体自上而下分成一行一行,并在每行中按从左至右的挨次排放元素,即为文档流。
  • 浮动绝对定位可以让元素脱离文档流,区别如下:
    元素浮动之后,会跳出文档流,也就是说当它后面还有元素时,其他元素会无视它所占据了的区域,直接在它身下布局。但是文字却会认同浮动元素所占据的区域,围绕它布局,相当于没有脱离文本流(文档流是相对于盒子模型讲的,文本流是相对于文章段落讲的)。more
    但是绝对定位后,元素不仅会脱离文档流,文字也会出文本流,后面元素的文本就不会在认同它的区域位置,会直接在它后面布局,不会再环绕。
<style type="text/css">
    .big{
        width: 500px;
        height: 500px;
        margin: 0 auto;
        position: relative;
    }
    .small{
        width: 50px;
        height: 50px;   
    }
    .one{
        float: right;
        border: 5px solid red;
    }
    .two{
        position: absolute;
        border: 5px solid blue;
    }
    </style>
</head>
<body>
    <div class="big">
        <div class="small one"></div>
        <div class="small two"></div>
        <p class=" ">
            充满鲜花.....最耀眼的瞬间。
        </p>
    </div>
</body>
效果对比图
  1. 有几种定位方式,分别是如何实现定位的,使用场景如何?偏移的参考点分别是什么?
    • relative不脱离文档流的布局,只改变自身的位置,在文档流原先的位置遗留空白区域,移动后它覆盖其它框。参考点为本身原来所处的位置进行偏移。
    • absolute脱离文档流的布局,遗留下来的空间由后面的元素填充,参考点为距离最近的祖先元素,如果都没有那么相对于body
<style type="text/css">
    .grandfather{
        width: 300px;
        height: 300px;
        border: 1px solid;
        position: relative;
        top: 50px;
        left: 100px;
}
    .father{
        width: 200px;
        height: 200px;
        border: 1px solid;
        /*position: relative;*/
        top: 50px;
        left: 100px;
    }
    .son1{
        width: 50px;
        height: 50px;
        position: relative;
        top: 30px;
        left: 50px;
        background-color: green;
    }
    .son2{
        width: 50px;
        height: 50px;
        top: 50%;
        left: 50%;
        margin-left: -25px;
        margin-top: -25px;
        position: absolute;
        background-color: red;
    }
    </style>
</head>
<body>
    <div class="grandfather">
        <div class="father">
            <div class="son1"></div>
            <div class="son2"></div>
        </div>
    </div>
</body>```
![红色框现在参考点为grandfather](http://upload-images.jianshu.io/upload_images/2150964-7cc866a7ecd53ba3.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
   * `fixed`可通过 "left"、"top"、"right" 以及"bottom" 属性来规定元素的位置。参考点为浏览器窗口,不论窗口滚动与否,元素都会留在那个位置。
应用场景:登陆框;页面弹窗广告,底部导航,回到顶部等。

3. z-index 有什么作用? 如何使用?
![z-index](http://upload-images.jianshu.io/upload_images/2150964-d0ac07ddbaf554ef.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

<style type="text/css">
.fa{
width: 300px;
height: 300px;
border: 1px solid;
margin: 0 auto;
position: relative;
}
.one{
width: 300px;
height: 50px;
background-color: #f00;
position: absolute;
top: 0;
}
.two{
width: 200px;
height: 150px;
background-color: #0f0;
top: 0;
position: absolute;
}
.three{
width: 150px;
height: 100px;
background-color: #00f;
position: absolute;
top: 0;
}```


效果
.one{z-index: 2;}
. two{z-index: 0;}
.three{z-index: 1;}```
![加入z-index](http://upload-images.jianshu.io/upload_images/2150964-6ae04274c7fe2bbf.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

4. `position:relative`和负margin都可以使元素位置发生偏移,二者有什么区别?
  * `position:relative`只改变了位置,所占空间位置和大小没有改变,因此不会影响它结构下的元素。好比灵魂出窍,肉身还在,且还活着。
  * 而负margin通过改变本身占据空间的大小来改变位置,会影响位于它结构下的元素。这是一种自杀式的,偏移后真身直接被高效处理了,看到的不是灵魂是鬼魂。

<style type="text/css">
.box{
width: 300px;
height: 200px;
border: 1px solid;
margin: 30px auto;
}
.one{
width: 50px;
height: 50px;
background-color: #f00;
position: relative;
}
.two{
width: 50px;
height: 50px;
background-color: #f00;
}
.con{
width: 50px;
height: 50px;
background-color: #0f0;
}
</style>
</head>
<body>
<div class="fa">
<div class="box">
<div class="one"></div>
<div class="con"></div>
</div>
<div class="box">
<div class="two"></div>
<div class="con"></div>
</div>
</body>```


偏移前

偏移代码

.one{top: -20px;}
.two{margin-top: -20px; }```
![偏移后](http://upload-images.jianshu.io/upload_images/2150964-0325829e265590b1.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

5. 如何让一个固定宽高的元素在页面上垂直水平居中?
父元素设置`position: relative`,目标元素为`position: absolute`。设置`top:50%;left: 50%;`、再设置`margin-top、margin-left `为负值,绝对值得大小是本身高度和宽度值的一半。

.outer{
width: 500px;
height: 300px;
margin: 0 auto;
border: 1px solid;
position: relative;
}
.one{
width: 50px;
height: 50px;
top: 50%;
left: 50%;
margin-left: -25px;
margin-top: -25px;
position: absolute;
background-color: red;
}```

垂直水平居中

  1. 浮动元素有什么特征?对其他浮动元素、普通元素、文字分别有什么影响?
    元素浮动后,会脱离文档流,不再占据空间。浮动的元素会覆盖普通元素,但是文字会环绕它存在。
.big{
    width: 500px;
    height: 500px;
    margin: 0 auto;
}   
.one{
    width: 100px;
    height: 130px;
    background-color: red;
    float: left;
}
.two{
    width: 200px;
    height: 120px;
    background-color: blue;
}
.three{
    width: 300px;
    height: 110px;
    background-color: green;
}
浮动元素覆盖普通元素(只有红色浮动)

如果之前有浮动,它会浮动到上一个左或右,空间不足会向下浮动。
不足时向下浮动(三个都浮动空间)
  1. 清除浮动指什么? 如何清除浮动?
    清除浮动是指清除浮动元素带来的影响。比如,
    .outer{
        width: 500px;
        margin: 0 auto;
        border: 1px solid;
    }   
    .one{
        width: 50px;
        height: 50px;
        background-color: red;
        float: left;
    }
    .two{
        width: 50px;
        height: 50px;
        background-color: blue;
        float: left;
    }
    .three{
        width: 50px;
        height: 50px;
        background-color: green;
        float: left;
    }
    </style>
</head>
<body>
    <div class="outer">
        <div class="one"></div>
        <div class="two"></div>
        <div class="three"></div>
    </div>
</body>```
![父元素没有被撑开](http://upload-images.jianshu.io/upload_images/2150964-34f430a66722750e.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
   - 方法1:在父元素中加入一个新元素清除浮动。
```<body>
    <div class="outer">
        <div class="one"></div>
        <div class="two"></div>
        <div class="three"></div>
        <br class="c"/>    /*设置.c{clear: both}*/
    </div>
</body>```
   - 方法2:给父元素设置`overflow: hidden`或者`overflow: auto`
```.outer{
    width: 500px;
    margin: 0 auto;
    border: 1px solid;
    overflow: auto; }/*或者设为hidden*/```
>  使用overflow属性来清除浮动有一点需要注意,overflow属性共有三个属性值:hidden,auto,visible。我们可以使用hiddent和auto值来清除浮动,但切记不能使用visible值,如果使用这个值将无法达到清除浮动效果,其他两个值都可以,其区据说在于一个对seo比较友好,而hidden对seo不是太友好。
   - 方法3:把父元素本身也浮动。
```.outer{
    width: 500px;
    margin: 0 auto;
    border: 1px solid;
        float: left;}```
##代码
[两栏布局](https://github.com/jirengu-inc/jrg-renwu6/blob/master/homework/%E9%83%AD%E5%BF%97%E6%98%8E/%E4%BB%BB%E5%8A%A110/%E4%B8%A4%E6%A0%8F%E5%B8%83%E5%B1%80.html)——[效果](http://book.jirengu.com/jirengu-inc/jrg-renwu6/homework/%E9%83%AD%E5%BF%97%E6%98%8E/%E4%BB%BB%E5%8A%A110/%E4%B8%A4%E6%A0%8F%E5%B8%83%E5%B1%80.html)
[三角](https://github.com/jirengu-inc/jrg-renwu6/blob/master/homework/%E9%83%AD%E5%BF%97%E6%98%8E/%E4%BB%BB%E5%8A%A110/two.html)  ——[效果](http://book.jirengu.com/jirengu-inc/jrg-renwu6/homework/%E9%83%AD%E5%BF%97%E6%98%8E/%E4%BB%BB%E5%8A%A110/two.html)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容