美文网首页
前端05day

前端05day

作者: Cyj___ | 来源:发表于2018-07-11 08:30 被阅读0次

相对定位

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>相对定位</title>
<style type="text/css">
.box1{
    height: 200px;
    background-color: red;
    position: relative;
}
.box2{
    width: 200px;
    height: 200px;
    background-color: yellow;
    position: relative;
    left: 100px;
    top: 200px;
}
.box3{
    width: 200px;
    height: 200px;
    background-color: yellowgreen;
}
.s1{
    position: relative;
    width: 200px;
    height: 200px;
    background-color: yellow;
}
</style>
</head>
<body>
 <div class="box1"></div>
 <div class="box2"></div>
 <div class="box3"></div>
<span class="s1">我是一个span</span>
 </body>
 </html>

绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>绝对定位</title>
<style type="text/css">
.box1{
    width: 200px;
    height: 200px;
    background-color: red;
}
.box2{
    width: 200px;
    height: 200px;
    background-color: yellow;
    position: absolute;
}
.box3{
    width: 300px;
    height: 300px;
    background-color: yellowgreen;
}
.box4{
    width: 300px;
    height: 300px;
    background-color: orange;
}
.s1{
    width: 100px;
    height: 100px;
    background-color: yellow;
    position: absolute;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box5">
<div class="box4">
<div class="box2"></div>
</div>
</div>
<div class="box3"></div>

<span class="s1">我是一个span</span>
</body>
</html>

固定定位

   <!DOCTYPE html>
<html lang="en">
<head>
           <meta charset="UTF-8">
           <title>固定定位</title>
            <style type="text/css">
.box1{
    width: 200px;
    height: 200px;
    background-color: red;
}
.box2{
    width: 200px;
    height: 200px;
    background-color: yellow;
    position: fixed;
    left: 0px;
    top: 0px;
}
.box3{
    width: 200px;
    height: 200px;
    background-color: yellowgreen;
}
    </style>
</head>
<body style="height: 5000px;">
<div class="box1"></div>

<div class="box4" style="width: 300px; height: 300px; background-color: orange; position: relative;">
<div class="box2"></div>
</div>

<div class="box3"></div>
</body>

CSS高度塌陷

高度塌陷问题

在文档流中,父元素的高度默认是被子元素撑开的,也就是子元素多高,父元素就多高。但是当子元素设置浮动之后,子元素会完全脱离文档流,此时将会导致子元素无法撑起父元素的高度,导致父元素的高度塌陷。

<!DOCTYPE html>
 <html lang="en">
 <head>
    <meta charset="UTF-8">
    <title>高度塌陷</title>
    <style type="text/css">     
   .box1{
        border: 10px red solid;
    }

    .box2{
        background-color: yellow;
        width: 100px;
        height: 100px;
        float: left;
    }
</style>
</head>
<body>
<div class="box1">
    <div class="box2"></div>
</div>
</body>
</html>

解决高度塌陷的方法

解决高度塌陷 <一>

根据W3C的标准,在页面中元素都一个隐含的属性叫做Block Formatting Context(块的格式化环境)简称BFC,该属性可以设置打开或者关闭,默认是关闭的

当开启元素的BFC以后,元素将会具有如下的特性:

1.父元素的垂直外边距不会和子元素重叠
2.开启BFC的元素不会被浮动元素所覆盖
3.开启BFC的元素可以包含浮动的子元素

如何开启元素的BFC

1.设置元素浮动

  • 使用这种方式开启,虽然可以撑开父元素,但是会导致父元素的宽度丢失,而且使用这种方式也会导致下边的元素上移,不能解决问题
    2.设置元素绝对定位
    3.设置元素为inline-block
  • 可以解决问题,但是会导致宽度丢失,不推荐使用这种方式
    4.将元素的overflow设置为一个非visible的值
    推荐方式:将overflow设置为hidden是副作用最小的开启BFC的方式
  <!DOCTYPE html>
  <html lang="en">
     <head>
     <meta charset="UTF-8">
     <title>高度塌陷</title>
     <style type="text/css">
    .box1{
        border: 10px red solid;

        overflow: hidden;
        zoom: 1;
    }
    .box2{
        width: 100px;
        height: 100px;
        background-color: blue;
        float: left;
    }
    .box3{
        height: 100px;
        background-color: yellow;
    }
</style>
</head>
<body>
<div class="box1">
 <div class="box2"></div>
</div>
<div class="box3"></div>
</body>
</html>

解决高度塌陷<二>

可以直接在高度塌陷的父元素的最后,添加一个空白的div,由于这个div并没有浮动,所以他是可以撑开父元素的高度的
然后再对其进行清除浮动,这样可以通过这个空白的div来撑开父元素的高度,基本没有副作用
使用这种方式虽然可以解决问题,但是会在页面中添加多余的结构

  <!DOCTYPE html>
  <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>解决高度塌陷方法2</title>
    <style type="text/css">
    .box1{
        border: 1px solid red;
    }
    .box2{
        width: 100px;
        height: 100px;
        background-color: blue;
        float: left;
    }
    .clear{
        clear: both;
    }
</style>
</head>
<body>
  <div class="box1">
    <div class="box2"></div>
    <div class="clear"></div>
  </div>
</body>
</html>

解决高度塌陷<三>

可以通过after伪类向元素的最后添加一个空白的块元素,然后对其清除浮动,
这样做和添加一个div的原理一样,可以达到一个相同的效果,
而且不会在页面中添加多余的div,这是我们最推荐使用的方式,几乎没有副作用

  <!DOCTYPE html>
  <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>解决高度塌陷方法3</title>
      <style type="text/css">
    .box1{
        border: 1px solid red;
    }
    .box2{
        width: 100px;
        height: 100px;
        background-color: blue;
        float: left;
    }
    .clearfix:after{
        content: "";
        display: block;
        clear: both;
    }
    .clearfix{
        zoom: 1;
    }
</style>
</head>
<body>
  <div class="box1 clearfix">
    <div class="box2"></div>
  </div>
</body>

作业

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>开班计划</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        body{
            font: 12px/1 宋体;
        }
        .box1{
            width: 300px;
        
            margin: 50px auto;
        }
        .box2{
            border-top: 2px #52bbae solid;
            height: 36px;
            background-color: #f5f5f5;
            line-height: 36px;
            padding:0px 20px 0px 16px;
        }
        .box2 a{
            float: right;
            color: red;
        }
        .box2 h3:hover{
            float:left;
            font: 10px '宋体';
            list-style: none;
        }

        .box3 a{
            color: black;
            text-decoration: none;
            font-size: 12px;
        }
        .box3 a:hover{
            color: red; 
            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 .no-border{
            border: none;
        }
    </style>
</head>
<body> 
    <div class="box1">
        <div class="box2">
            <a href="#">18年面授开班计划</a>
            <h3>近期开班</h3>
        </div>
        <div class="box3">
            <h3><a href="#">人工智能+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>
            </ul>
            <ul>
                <li>
                    <a href="#">开班时间:<span class="red-font">2018-03-23</span></a>
                    <a class="right" href="#"><span class="red-font">无座,名额爆满</span></a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="#">开班时间:<span>2018-01-23</span></a>
                    <a class="right" href="# "><span>开班盛况</span></a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="#">开班时间:<span>2017-12-10</span></a>
                    <a class="right" href="# "><span>开班盛况</span></a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="#">开班时间:<span>2017-11-18</span></a>
                    <a class="right" href="# "><span>开班盛况</span></a>
                </li>
            </ul>
        </div>
        <div class="box3">
            <h3><a href="#">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>
            </ul>
            <ul>
                <li>
                    <a href="#">开班时间:<span>2018-03-23</span></a>
                    <a class="right" href="#"><span>开班盛况</span></a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="#">开班时间:<span>2018-01-23</span></a>
                    <a class="right" href="# "><span>开班盛况</span></a>
                </li>
            </ul>
            <ul>
                <li>
                    <a href="#">开班时间:<span>2017-12-10</span></a>
                    <a class="right" href="# "><span>开班盛况</span></a>
                </li>
            </ul>
            
        </div>
        <div class="box3">
            <h3><a href="#">大数据软件开发-青芒工作室</a></h3>
            <ul class="no-border">
                <li>
                    <a href="#">开班时间:<span class="red-font">2018-04-26</span></a>
                    <a class="right" href="# "><span class="red-font">预约报名</span></a>
                </li>
            </ul>
            
            <ul>
                <li>
                    <a href="#">开班时间:<span>2018-01-23</span></a>
                    <a class="right" href="# "><span>开班盛况</span></a>
                </li>
            </ul>
            
        </div>
    </div>
    
        
    
    
    
    
</body>
</html>

相关文章

  • 前端05day

    **em标签用于表示一段内容中的着重点。**strong标签用于表示一个内容的重要性。...

  • 前端05day

    相对定位 绝对定位 固定定位 CSS高度塌陷 高度塌陷问题 在文档流中,父元素的高度默认是被子元素撑开的,也就是子...

  • 高度塌陷 3月 前端 05Day

    高度塌陷 块的格式化环境 BFC BFC (隐含的属性) 1.父元素的垂直外边距不会和子元素重叠; 2.开启BF...

  • 05day

    BFC (隐含的属性)1 父元素的垂直外边距不会和元素重叠2 开启BFC的元素不会被浮动元素所覆盖的3 开启BFC...

  • 05day

    文档流 将窗口自上而下分成一行行,在每行中从左向右的顺序排放元素,即为文档流。文档流中元素默认会紧贴到上一个元素右...

  • mysql 05day

    mysql的基本命令

  • mysql(05day)

    1.排序ASC: 升序,默认的情况下就是升序DESC: 如果要降序,指明为DESC 2.单列排序SELECT * ...

  • 网页05day

    定位:position属性可以控制Web浏览器如何以 及在何处显示特定的元素。可以使用position属性把一个元...

  • 《让未来现在就来》打卡05Day

    读的什么书:《让未来现在就来》 阅读有效时间:一个小时 阅读中遇到的困难:整本书读完,感觉效果不大。 阅读有什么收...

  • 前端文章系列

    【前端】从0.1开始,创建第一个项目 【前端】初识HTML 【前端】HTML标签 【前端】HTML属性 【前端】C...

网友评论

      本文标题:前端05day

      本文链接:https://www.haomeiwen.com/subject/ybifpftx.html