前端入门05 -- 定位,元素的显示与隐藏,CSS高级技巧

2021-12-20  本文已影响0人  YanZi_33

定位

image.png image.png
静态定位
相对定位
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位布局Position</title>

    <style>
        div {
            width: 200px;
            height: 100px;
        }

        .one {
            background-color: blueviolet;
        }

        .two {
            position: relative;
            top: 50px;
            left: 50px;
            background-color: pink;
        }

        .three {
            background-color: red;
        }
    </style>

</head>

<body>
    <div class="one">one</div>
    <div class="two">two</div>
    <div class="three">three</div>
</body>

</html>
image.png
绝对定位
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定位布局Position</title>

    <style>
        div {
            width: 200px;
            height: 100px;
        }

        .one {
            background-color: blueviolet;
        }

        .two {
            position: absolute;
            top: 50px;
            left: 50px;
            background-color: pink;
        }

        .three {
            background-color: red;
        }
    </style>

</head>

<body>
    <div class="one">one</div>
    <div class="two">two</div>
    <div class="three">three</div>
</body>

</html>
image.png
定位技巧之子绝父相
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        li {
            list-style: none;
        }
        
        .pic {
            height: 150px;
            background-color: red;
        }
        
        .icon {
            position: absolute;
            right: -10px;
            top: 10px;
            width: 20px;
            height: 20px;
            background-color: slateblue;
        }
        
        ul li {
            position: relative;
            width: 228px;
            height: 270px;
            background-color: pink;
        }
    </style>

</head>

<body>

    <ul class="clearfix">
        <li>
            <div class="icon"></div>
            <div class="pic"></div>

            <h4> ThinkPad 地方喝酒房贷首付款打发时computer game</h4>
            <div class="info"> <span>高级</span> 人才 1000左右</div>
        </li>
    </ul>
</body>
</html>
image.png
固定定位
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .w {
            width: 800px;
            height: 1400px;
            background-color: pink;
            margin: 0 auto;
        }
        
        .fixed {
            position: fixed;
            /* 走浏览器宽度的一半 */
            left: 50%;
            margin-left: 405px;
            width: 50px;
            height: 150px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div class="fixed"></div>
    <div class="w"></div>
</body>
</html>
image.png
粘性定位
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            height: 3000px;
        }
        
        .nav {
            position: sticky;
            top: 0;
            width: 800px;
            height: 50px;
            background-color: pink;
            margin: 100px auto;
        }
    </style>
</head>

<body>
    <div class="nav">我的导航栏</div>
</body>
</html>
image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .box {
            position: absolute;
            top: 0;
            width: 200px;
            height: 200px;
        }
        
        .one {
            background-color: pink;
            z-index: 1;
        }
        
        .two {
            background-color: green;
            z-index: 2;
        }
        
        .three {
            background-color: hotpink;
            z-index: 3;
        }
    </style>

</head>

<body>
    <div class="box one">熊大</div>
    <div class="box two">熊二</div>
    <div class="box three">光头强</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .box {
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -100px;
            margin-top: -100px;
            width: 200px;
            height: 200px;
            background-color: pink;
        }
    </style>

</head>

<body>
    <div class="box"></div>
</body>
</html>
淘宝焦点图
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        li {
            list-style: none;
        }
        
        .box {
            position: relative;
            width: 400px;
            height: 300px;
            background-color: pink;
            margin: 100px auto;
        }
        
        .prev {
            position: absolute;
            left: 0;
            top: 50%;
            margin-top: -15px;
            width: 30px;
            height: 30px;
            background-color: rgba(0, 0, 0, 0.3);
        }
        
        .next {
            position: absolute;
            right: 0;
            top: 50%;
            margin-top: -15px;
            width: 30px;
            height: 30px;
            background-color: rgba(0, 0, 0, 0.3);
        }
        
        .bottom-nav {
            position: absolute;
            left: 50%;
            margin-left: -45px;
            bottom: 15px;
            background-color: red;
            width: 90px;
            height: 20px;
        }
        
        .bottom-nav li {
            float: left;
            width: 8px;
            height: 8px;
            background-color: #fff;
            border-radius: 50%;
            margin: 7px;
        }
        
        .bottom-nav .selected {
            background-color: chocolate;
        }
    </style>
</head>

<body>

    <div class="box">
        <div class="pic"></div>
        <div class="prev"></div>
        <div class="next"></div>
        <ul class="bottom-nav">
            <li class="selected"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>

</body>

</html>
image.png

元素的显示与隐藏

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            position: relative;
            width: 444px;
            height: 320px;
            background-color: pink;
            margin: 50px auto;
        }
        
        .pic {
            width: 100%;
            height: 100%;
            background-color: green;
        }
        
        .mask {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
        }
        
        .box:hover .mask {
            display: block;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="" alt="" class="pic">
        <div class="mask"></div>
    </div>
</body>

</html>

CSS高级技巧

精灵图技术
字体图标iconfont
image.png image.png image.png image.png Snip20211221_15.png
CSS三角
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .box1 {
            width: 0;
            height: 0;
            border-top: 10px solid pink;
            border-left: 10px solid red;
            border-right: 10px solid blue;
            border-bottom: 10px solid green;
        }
        
        .box2 {
            width: 0;
            height: 0;
            border: 10px solid transparent;
            border-left-color: pink;
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
</html>
image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .box1 {
            width: 0;
            height: 0;
            border-top: 10px solid pink;
            border-left: 10px solid red;
            border-right: 10px solid blue;
            border-bottom: 10px solid green;
        }
        
        .box2 {
            width: 0;
            height: 0;
            border: 10px solid transparent;
            border-left-color: pink;
        }
        
        .jd {
            position: relative;
            width: 120px;
            height: 240px;
            margin-top: 100px;
            background-color: pink;
        }
        
        .jd span {
            position: absolute;
            right: 15px;
            top: -10px;
            width: 0;
            height: 0;
            line-height: 0;
            font-size: 0;
            border: 5px solid transparent;
            border-bottom-color: pink;
        }
    </style>
</head>

<body>

    <div class="box1"></div>
    <div class="box2"></div>
    <div class="jd">
        <span></span>
    </div>

</body>

</html>
image.png
CSS用户界面样式
image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <ul>
        <li style="cursor: default;">的说三道四颠三倒四</li>
        <li style="cursor: pointer;">的说三道四颠三倒四</li>
        <li style="cursor: move;">的说三道四颠三倒四</li>
        <li style="cursor: text;">的说三道四颠三倒四</li>
        <li style="cursor: not-allowed;">的说三道四颠三倒四</li>
    </ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        input,
        textarea {
            outline: none;
        }
        
        textarea {
            resize: none;
        }
    </style>

</head>

<body>
    <input type="text">
    <textarea name="text" id="" cols="30" rows="10"></textarea>
</body>
</html>
vertical-align属性的应用
image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        img {
            vertical-align: bottom;
        }
    </style>

</head>

<body>
    <img src="120.png" alt="">都是艰苦奋斗死了
</body>
</html>
image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        div {
            width: 150px;
            height: 80px;
            background-color: pink;
            margin: 100px auto;
            /* 首先强制一行 */
            white-space: nowrap;
            /* 溢出的部分进行隐藏 */
            overflow: hidden;
            /* 文字溢出时用省略号显示 */
            text-overflow: ellipsis;
        }
    </style>

</head>
<body>
    <div>似懂非懂是否颠三倒四舒服的沙发</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        div {
            width: 150px;
            height: 80px;
            background-color: pink;
            margin: 100px auto;
            /* 实现多行 有省略号 */
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }
    </style>

</head>

<body>
    <div>似懂非懂是否颠三倒四舒服的懂非懂是否颠三倒四舒服的沙发</div>
</body>
</html>
常见的布局技巧
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        li {
            list-style: none;
        }
        
        ul li {
            position: relative;
            float: left;
            width: 150px;
            height: 100px;
            border: 1px solid red;
            /* 设置负值 */
            margin-left: -1px;
        }
        
        ul li:hover {
            /* 相对定位 会压住标准流和浮动 */
            /* position: relative; */
            border: 1px solid blue;
            z-index: 1;
        }
    </style>

</head>

<body>

    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
    </ul>

</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .box {
            width: 300px;
            height: 120px;
            background-color: pink;
            margin: 50px auto;
            padding: 5px;
        }
        
        .pic_box {
            float: left;
            width: 120px;
            height: 120px;
            margin-right: 5px;
        }
        
        img {
            width: 120px;
            height: 120px;
        }
    </style>

</head>

<body>
    <div class="box">
        <div class="pic_box">
            <img src="120.png" alt="">
        </div>
        <p>的舒服的是割发代首股份大股东发送给对方发多少发多少</p>
    </div>
</body>
</html>
image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .box {
            margin-top: 100px;
            text-align: center;
        }
        
        .box a {
            display: inline-block;
            width: 36px;
            height: 36px;
            background-color: #f7f7f7;
            border: 1px solid #ccc;
            text-align: center;
            line-height: 36px;
            text-decoration: none;
            color: #333;
        }
        
        .box .prev {
            width: 85px;
        }
        
        .box .next {
            width: 85px;
        }
        
        .box .current,
        .box .elp {
            background-color: #fff;
            border: none;
        }
        
        .box input {
            width: 45px;
            height: 36px;
            border: 1px solid #ccc;
            outline: none;
        }
        
        .box button {
            width: 60px;
            height: 36px;
        }
    </style>

</head>

<body>

    <div class="box">
        <a href="#" class="prev">&lt;&lt;上一页</a>
        <a href="#" class="current">2</a>
        <a href="#">3</a>
        <a href="#">4</a>
        <a href="#">5</a>
        <a href="#">6</a>
        <a href="#" class="elp">...</a>
        <a href="#" class="next">&gt;&gt;下一页</a> 到第
        <input type="text">页
        <button>确定</button>
    </div>

</body>

</html>
image.png
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .box1 {
            width: 0;
            height: 0;
            border-top: 100px solid transparent;
            border-right: 50px solid red;
            border-bottom: 0 solid blue;
            border-left: 0 solid green;
        }
        
        .price {
            width: 160px;
            height: 24px;
            margin: 0 auto;
            border: 1px solid red;
        }
        
        .one {
            position: relative;
            float: left;
            width: 90px;
            height: 100%;
            background-color: red;
            text-align: center;
            color: white;
            margin-right: 3px;
        }
        
        .one i {
            position: absolute;
            top: 0;
            right: 0;
            width: 0;
            height: 0;
            border-top: 24px solid transparent;
            border-right: 10px solid white;
            border-bottom: 0 solid red;
            border-left: 0 solid green;
        }
    </style>

</head>

<body>

    <div class="box1"></div>
    <div class="price">
        <span class="one">$1650
            <i></i>
        </span>
        <span class="two">$4563</span>
    </div>

</body>

</html>
image.png
CSS初始化
/* 所有标签的内外边距清零 */
* {
    margin: 0;
    padding: 0;
    /* CSS3盒子模型 */
    box-sizing: border-box;
}

/* em 和 i标签的文字不倾斜 */
em,i {
    font-style: normal;
}

/* 去掉li的小圆点 */
li {
    list-style: none;
}

img {
    /* border: 0 照顾低版本浏览器 若图片外面包含了链接 会有边框的问题 */
    border: 0;
    /* 取消图片底部有空白缝隙的问题 */
    vertical-align: middle;
}

/* 当鼠标经过按钮时,鼠标样式变成小手 */
button {
    cursor: pointer;
}

/* 设置链接颜色 去除下划线*/
a {
    color: #666;
    text-decoration: none;
}

a:hover {
    color: #c81623;
}

/* 设置 背景颜色 字体 字体颜色 */
body {
    background-color: #fff;
    font: 12px/1.5 Microsoft YaHei, Heiti SC, tahoma, arial, Hiragino Sans GB, "\5B8B\4F53", sans-serif;
    color: #666;
}

/* 隐藏 */
.hide,
.none {
    display: none;
}

/* 清除浮动 */
.clearfix:after {
    visibility: hidden;
    clear: both;
    content: ".";
    display: block;
    height: 0;
}

.clearfix:after {
    clear: both;
}

.clearfix {
    *zoom: 1;
}
上一篇 下一篇

猜你喜欢

热点阅读