CSS深度操作(1)

2018-06-06  本文已影响0人  梦亦殇灬

一、浮动

<style type="text/css">
    .box1{
            width: 600px;
            height: 200px;
            background-color: red;
            float: left;
}
</style>

二、文字绕图

浮动的元素不会盖住文字,文字会自动环绕在浮动元素的周围,所以我们可以通过浮动来设置文字环绕图片的效果

<style type="text/css">
    .box1{
            width: 600px;
            height: 200px;
            background-color: red;
            float: left;
}
    .p1{
            /*height: 200px;*/
            background-color: yellow;
}
</style>

三、内联元素的浮动

<style type="text/css">
p{
           float: left;
           width: 100px;
           height: 100px;
           background-color: yellow;
</style>

四、高度塌陷及解决办法

导读塌陷:在文档流中,父元素的高度默认是被子元素撑开的,也就是子元素多高,父元素就多高但是当为子元素设置浮动以后,子元素会完全脱离文档流,此时将会导致子元素无法撑起父元素的高度,导致父元素的高度塌陷由于父元素的高度塌陷了,则父元素下的所有元素都会向上移动,这样将会导致页面布局混乱所以在开发中一定要避免出现高度塌陷的问题,

<style type="text/css">
.box1{
         /*为box1设置一个边框*/
            border: 10px red solid;
            height: 100px;}
.box2{
            width: 100px;
            height: 100px;
            background-color: blue;
            float: left;}
</style>
<style type="text/css">
.box1{
        overflow: hidden;}
</style>
<style type="text/css">
.box1{
        clear: both;} 这里还可以写left和right单清除一边 bonth是两边同时清除
</style>
<div class="box1"><div>
<style type="text/css">
.box1:after{
      /*添加一个内容*/
            content: "";
            /*转换为一个块元素*/
            display: block;
            /*清除两侧的浮动*/
            clear: both;
}
</style>
<div class="box1"><div>

五、定位

定位指的就是将指定的元素摆放到页面的任意位置通过定位可以任意的摆放元素通过position属性来设置元素的定位
可选值:
static:默认值,元素没有开启定位
relative:开启元素的相对定位
absolute:开启元素的绝对定位
fixed:开启元素的固定定位(也是绝对定位的一种)

<style type="text/css">
当开启了元素的定位(position属性值是一个非static的值)时,可以通过left right top bottom四个属性来设置元素的偏移量
一般选择水平方向的一个偏移量和垂直方向的偏移量来为一个元素进行定位
.box1{
         position: relative;
        left: 100px;
        top: 200px;
}
</style>
<div class="box1"><div>
<style type="text/css">
.box{width: 200px;
    height: 200px;
    background-color: yellow;
    position: absolute;
}
.box1{
      position: absolute;
      left: 100px;
      top: 100px;
}
</style>
<div class ="box"></div>
<div class="box1"><div>
<style type="text/css">
.box{width: 200px;
    height: 200px;
    background-color: yellow;
    position: fixed;
    left: 0px;
    top: 0px;
}
</style>
<div class ="box"></div>
<div class="box1"><div
上一篇 下一篇

猜你喜欢

热点阅读