高度塌陷与元素定位

2018-10-22  本文已影响0人  岁月悄然飞逝徒留回忆_54a5

在文档流中,父元素的高度默认是被子元素撑开的,也就是子元素多高,父元素就多高
但是当为子元素设置浮动以后,子元素会完全脱离文档流,此时将会导致子元素无法撑起父元素的高度,导致父元素的高度塌陷
由于父元素的高度塌陷了,则父元素下的所有元素都会向上移动,这样将会导致页面布局混乱
解决方案
将元素overflow设置为一个非visibility的值 scroll或auto

最好用的是hidden

EI6 以及以下的浏览器不支持BFC :

hasLayout

解决方法:

zoom:1;

list-style:none; 去除项目符号
清除浮动:

clear:both;清楚影响最大的浮动

配合使用解决高度塌陷

.clearfix(类名,约定俗称):after{content:内容;display:block; clear:both;}

zoom:1 兼容IE6

定位:

position属性可以控制Web浏览器如何以 及在何处显示特定的元素。
可以使用position属性把一个元素放置到网 页中的任何位置。
可选值:
static
relative
absolute
fixed
相对定位:
每个元素在页面的文档流中都有一个自然位置。相 对于这个位置对元素进行移动就称为相对定位。周 围的元素完全不受此影响。
当将position属性设置为relative时,则开启了元素 的相对定位。
当开启了相对定位以后,可以使用top、right、 bottom、left四个属性对元素进行定位。
相对定位的特点:
如果不设置元素的偏移量,元素位置不会发生改变。
相对定位不会使元素脱离文本流。元素在文本流中 的位置不会改变。
相对定位不会改变元素原来的特性。
相对定位会使元素的层级提升,使元素可以覆盖文 本流中的元素。
绝对定位:
绝对定位指使元素相对于html元素或离他最近 的祖先定位元素进行定位。
当将position属性设置为absolute时,则开启 了元素的绝对定位。
当开启了绝对定位以后,可以使用top、right、 bottom、left四个属性对元素进行定位。
绝对定位的特点:
绝对定位会使元素完全脱离文本流。
绝对定位的块元素的宽度会被其内容撑开。
绝对定位会使行内元素变成块元素。
一般使用绝对定位时会同时为其父元素指定一 个相对定位,以确保元素可以相对于父元素进 行定位。
固定定位:
固定定位的元素会被锁定在屏幕的某个位置上,当 访问者滚动网页时,固定元素会在屏幕上保持不动。
当将position属性设置为fixed时,则开启了元素的 固定定位。
当开启了固定定位以后,可以使用top、right、bottom、left四个属性对元素进行定位。
固定定位的其他特性和绝对定位类似。
z-index:
当元素开启定位以后就可以设置z-index这 个属性。
这个属性可以提升定位元素所在的层级。
z-index可以指定一个整数作为参数,值越 大元素显示的优先级越高,也就是z-index 值较大的元素会显示在网页的最上层。

作业一开班信息表

  <!DOCTYPE html>
  <html lang="en">
  <head>
            <meta charset="UTF-8">
            <title>作业1</title>
            <style type="text/css">
            *{
                margin: 0;
                padding: 0;
              }
            .box{
                font: 12px/1 宋体;

            }
            .box1{
                  width: 300px;
                  /*height: 471px;
                  background-color: #bfa;*/
                  /*text-align: center;*/
                  margin: 50px auto;
            }
            .box2{
                    border-top: 2px #019e8b solid;
                    height: 36px;
                    background-color: #f5f5f5;
                    line-height: 36px;
                    padding: 0px 22px 0px 16px;
                  }
          .box2 a{
                      float: right;
                      color: red;
                      font-size: 12px;
                }
          .box2 h3{
                          font:16px/36px "微软雅黑";
                }
           .box3{
                       border: 1px solid #deddd9;
                       padding: 0px 28px 0px 20px;
                 }
            .box3 a{
                          color: black;
                          text-decoration: none;
                          font-size: 12px;
                    }
           .box3 a:hover{
                                    color: green;
                                     text-decoration: underline;
                     }
            .box3 h3{
                        margin-top: 15px;
                        margin-bottom: 15px;
                   }
            .box3 ul{
                         list-style: none;
                          border-bottom: 1px dashed #deddd9;
               }
            .box3 li{
                             margin-bottom: 15px;
              }
           .box3 .red-font{
                            color: red;
                             font-weight: bold;
                }
            .box3 .right{
                          float: right;
              }
            .box3 .box4{
                         border: none;
             }
                   .right{
                           margin-top: 9px;
               }
</style>
 </head>
 <body>
<div class="box1">
    <div class="box2">
        <a href="#">18年面授开班计划</a>
        <h3>近期开班</h3>
    </div>
    <div class="box3">
        <h3><a herf="#">人工智能+Python-高新就业班</a></h3>
        <ul>
            <li>
                <a href="#">开班时间:<span class="red-font">2018-04-26</span></a>
                <a class="right" href="#"><span class="red-font">预约报名</span></a>
            </li>
            <li>
                <a href="#">开班时间:<span class="red-font">2018-04-26</span></a>
                <a class="right" href="#"><span class="red-font">无座,名额爆满</span></a>
            </li>
            <li>
                <a href="#">开班时间:<span>2018-04-26</span></a>
                <a class="right" href="#"><span>开班盛况</span></a>
            </li>
            <li>
                <a href="#">开班时间:<span>2018-04-26</span></a>
                <a class="right" href="#"><span>开班盛况</span></a>
            </li>
            <li>
                <a href="#">开班时间:<span>2018-04-26</span></a>
                <a class="right" href="#"><span>开班盛况</span></a>
            </li>
        </ul>
        <h3><a herf="#">Android开发+测试-高薪就业班</a></h3>
        <ul>
            <li>
                <a href="#">开班时间:<span class="red-font">2018-04-26</span></a>
                <a class="right" href="#"><span class="red-font">预约报名</span></a>
            </li>
            <li>
                <a href="#">开班时间:<span>2018-03-23</span></a>
                <a class="right" href="#"><span>开班盛况</span></a>
            </li>
            <li>
                <a href="#">开班时间:<span>2018-01-23</span></a>
                <a class="right" href="#"><span>开班盛况</span></a>
            </li>
            <li>
                <a href="#">开班时间:<span>2018-12-20</span></a>
                <a class="right" href="#"><span>开班盛况</span></a>
            </li>
        </ul>
        <h3><a herf="#">大数据软件开发-青芒工作室</a></h3>
        <ul class="box4">
            <li>
                <a href="#">开班时间:<span class="red-font">2018-04-26</span></a>
                <a class="right" href="#"><span class="red-font">预约报名</span></a>
            </li>
            <li>
                <a href="#">开班时间:<span>2018-01-23</span></a>
                <a class="right" href="#"><span>开班盛况</span></a>
            </li>
        </ul>
      </div>
    </div>
</body>
</html>
image.png

作业二 导航条

  <!DOCTYPE html>
  <html lang="en">
  <head>
         <meta charset="UTF-8">
          <title>Document</title>
          <style type="text/css">
              *{ 
                  margin: 0;
                   padding:0;
                }   
              .p1{
                   width: 1000px;
                   margin:  50px auto;
                    overflow: hidden;
                   background-color: #55a8ea;
                }

             .p1 li{
                   list-style: none;
                   float: left;
                  width: 25%;
                  text-align: center;
                }


            .p1 a{
                  display: block;
    
                   width: 100%;
                  text-decoration: none;
                  font-size: 14px;
                  line-height: 40px;
                 color: white;


               }
            .p1 li:hover{
                        background-color:  #00619f;
                       color: white;
             }
</style>

</head>
<body>
          <ul class="p1">
                    <li><a href="#">首页</a></li>
                   <li><a href="#">新闻</a></li>
                  <li><a href="#">联系</a></li>
                  <li><a href="#">关于</a></li>
          </ul>


</body>
</html>
image.png
上一篇下一篇

猜你喜欢

热点阅读