【CSS-定位和浮动】

1. display和浮动

1.1 display

块级元素:独占一行

h1~h6 p div 列表....

行内元素:不独占一行

span a img strong......

使用dispaly写一个界面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>QQ会员</title>
    <link rel="stylesheet" href="style.css">

</head>
<body>
    <div class="wrap">
    <!-- 头部 -->
        <header class="nav-header">
            <div class="head-contain">
                <a href="" class="top-logo"><img src="../img/1.jpeg" alt="" width="90" height="90"></a>
                <nav class="top-nav">
                    <ul>
                        <li><a href="">功能特权</a></li>
                        <li><a href="">游戏特权</a></li>
                        <li><a href="">生活特权</a></li>
                        <li><a href="">会员活动</a></li>
                        <li><a href="">成长体系</a></li>
                        <li><a href="">年费专区</a></li>
                        <li><a href="">超级会员</a></li>
                    </ul>
                </nav>
                <div class="top-right">
                    <a href="">登录</a>
                    <a href="">开通超级会员</a>
                </div>
            </div>
        </header>
    </div>
</body>
</html>

/*display:
    block 块元素
    inline 行内元素
    inline-block 是块元素,但可以内联,在一行
    none
*/

*{
    margin: 0;
    padding: 0;
}
a{
    text-decoration: none;
    list-style: none;
}
/*.nav-header{*/
/*    height: 90px;*/
/*    width: 100px;*/
/*    background: rgba(0,0,0,6);*/
/*}*/
.head-contain{
    width: 1180px;
    height: 90px;
    margin: 0 auto;
    text-align: center;
    background: gray;
}
.top-nav,.top-nav li ,.top-right {
    height: 90px;
    display: inline-block;
    vertical-align: top;
}

.top-nav{
    margin: 0 48px;
}
.top-nav li {
    line-height: 90px;
    width: 90px;
}
.top-nav li a{
    display: block;
    text-align: center;
    font-size: 16px;

}
.top-nav li a:hover{
    color: #C850C0;
}

.top-right a{
    display: inline-block;
    text-align: center;
    font-size: 16px;
    margin-top: 25px;
    border-radius: 35px;
    border: 1px solid #fad65c;
}
.top-right a:first-of-type{
    width: 93px;
    height: 38px;
    line-height: 38px;
    color: #fad65c;
}
.top-right a:first-of-type:hover{
    color: blueviolet;
    background: coral;
}
.top-right a:last-of-type{
    width: 140px;
    height: 40px;
    line-height: 40px;
    color: #fad65c;
}
.top-right a:last-of-type:hover{
    color: blueviolet;
    background: coral;
}

效果:
在这里插入图片描述


1.2 float

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>float</title>
    <link rel="stylesheet" href="style1.css">
</head>
<body>
<div id="div1">
    <div class="layer1"><img src="../img/1.jpeg" alt="" width="90" height="80"></div>
    <div class="layer2"><img src="../img/d.jpeg" alt=""></div>
    <div class="layer3"><img src="../img/r%20(1).jpeg" alt=""></div>
    <div class="layer4">
        浮动的盒子
    </div>
</div>
</body>
</html>
div{
    margin: 10px;
    padding: 5px;
}
#div1{
    border: 1px dashed #4158D0;
}
.layer1{
    border: 1px dashed coral;
    display: inline-block;
    float: left;
}
.layer2{
    border: 1px dashed coral;
    display: inline-block;
    float: right;
}
.layer3{
    border: 1px dashed coral;
    display: inline-block;
}
.layer4{
    border: 1px dashed coral;
    display: inline-block;
}

效果:
在这里插入图片描述


1.3. 0verflow及腹肌父级边框塌陷

clesr:

/*
    clear: right; 右侧不允许有浮动元素
    clear: left;  左侧不允许有浮动元素
    clear: both;  两侧不允许有浮动元素
    clear: none;
*/

腹肌边框塌陷问题的解决方案:

  1. 增加腹肌元素的高度:简单,但有了固定高度会被限制
#div1{
    border: 1px solid darkcyan;
    height: 800px;
}
  1. 增加一个空的div标签,清除浮动。代码中尽量避免空的div标签
<div class="clear"> </div>

.clear{
    clear: both;
    margin: 0;
    padding: 0;
}
  1. overflow:简单,但下拉的一些场景避免使用
在父级元素中添加一个 overflow: hidden;
  1. 在父类中添加一个伪类:after . 推荐
#div1:after{
    content: '';
    display: block;
    clear: both;
}

1.4 小节

  • display:方向不可控,但没有边框塌陷问题
  • float:浮动起来会脱离标准文档流,要解决父级边框问题

2. 定位

2.1 相对定位

相对定位:position:relative
相对于原来的位置进行指定偏移,相对定位的话,它依然在标准文档流中,原来的位置会被保留。
方块定位练习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div id="div1">
    <a href="" class="block1">链接1</a>
    <a href="" class="block2">链接2</a>
    <a href="" class="block3">链接3</a>
    <a href="" class="block4">链接4</a>
    <a href="" class="block5">链接5</a>
</div>

</body>
</html>
#div1{
    margin: 10px;
    padding: 5px;
    width: 300px;
    height: 300px;
    border: 1px solid dodgerblue;
}
a{
    width: 100px;
    height: 100px;
    font-size: 16px;
    background:salmon ;
    text-decoration: none;
    color: white;
    border: 1px solid salmon;
    display: block;
}
a:hover{
    background: #4158D0;
}

.block2{

    position: relative;
    left: 200PX;
    top: -100px;
}
.block3{
    position: relative;
    top: -10px;
}
.block4{
    position: relative;
    top: -110px;
    left: 200PX;
}
.block5{
    position: relative;
    top: -300px;
    left: 100px;
}

2.2 绝对定位

  1. 没有父级元素定位的前提下,相对于浏览器定位
  2. 假设父级元素存在定位,我们通常会相对于父级元素进行定位

相对于浏览器进行指定偏移,相对定位的话,它不在标准文档流中,原来的位置不会被保留。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #box{
            width: 300px;
            height: 300px;
            padding: 10px;
            border:  1px solid blue;
        }
        a{
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-decoration: none;
            text-align: center;
            color: wheat;
            background: coral;
            font-size: 16px;
            display: block;
        }
        a:hover{
            background: #C850C0;
        }
        .a1{
            position: absolute;
            left: 200px;
        }
        .a2{
            position: absolute;
            top: 180px;
        }
    </style>
</head>
<body>
<div id="father">
    <div id="box">
        <a href="" class="a1">第一个盒子</a>
        <a href="" class="a2">第二个盒子</a>
        <a href="" class="a3">第三个盒子</a>
    </div>
</div>
</body>
</html>

2.3 固定定位fixed

使用fixed,不管怎么滑动页面,位置永远不变,用于固定位置

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            height: 1000px;
        }
        div:nth-of-type(1){
            width: 100px;
            height: 100px;
            background: #C850C0;
            position: absolute;
            right: 0;
            bottom: 0;
        }
        div:nth-of-type(2){
            width: 50px;
            height: 50px;
            background: #fad65c;
            position: fixed;
            right: 0;
            bottom: 0;
        }
    </style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>

2.4 z-index

z-index:默认是0.最高无线

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="z-index.css">
</head>
<body>
<div id="content">
    <ul>
        <li><img src="../img/1.jpeg" alt="" width="300px" height="300px"></li>
        <li class="text">贫穷的本质</li>
    </ul>
</div>

</body>
</html>
#content{
    width: 400px;
    height: 400px;
    margin: 0;
    overflow: hidden;
    font-size: 12px;
    line-height: 25px;
    border: 1px solid salmon;

}

ul,li{
    padding: 0px;
    margin: 0px;
    list-style: none;

}

#content ul{
    position: relative;
}
.text{
    position: absolute;
    width: 200px;
    height: 25px;
    color: white;
    top: 250px;
    z-index: 1;
    background: #4158D0;
    /*背景透明度*/
    opacity: 0.5;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS定位浮动属性可以用来控制HTML元素的位置和布局。下面是它们的详细介绍: 1. 定位属性 定位属性包括:static、relative、absolute、fixed和sticky。其中,static是默认值,表示元素出现在正常的流中,不受top、bottom、left和right属性的影响;relative表示相对于元素本身的位置进行定位;absolute表示相对于最近的已定位祖先元素进行定位;fixed表示相对于浏览器窗口进行定位;sticky表示根据用户的滚动位置进行定位。 2. 浮动属性 浮动属性包括:left、right和none。浮动元素会脱离文档流,向左或向右移动,直到它的外边缘碰到包含它的元素或者另一个浮动元素的边缘为止。如果没有足够的空间,浮动元素会向下移动,直到它可以放置在页面上。 下面是一个例子,演示如何使用定位浮动属性来布局HTML元素: ```html <!DOCTYPE html> <html> <head> <title>定位浮动属性的例子</title> <style> .container { border: 1px solid black; padding: 10px; overflow: auto; } .box { width: 100px; height: 100px; margin: 10px; background-color: red; float: left; } .relative { position: relative; left: 50px; top: 50px; } .absolute { position: absolute; right: 50px; top: 50px; } .fixed { position: fixed; right: 50px; bottom: 50px; } .sticky { position: sticky; top: 0; } </style> </head> <body> <div class="container"> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box relative"></div> <div class="box absolute"></div> <div class="box fixed"></div> <div class="box sticky"></div> </div> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值