CSS学习Day02(狂神说Java)

3.7 背景

背景颜色

背景图片

<style>
        div{
            width: 1000px;
            height: 700px;
            border: 1px solid red;
            background-image:url("images/1.png") ;
            /*默认全部是平铺的*/
        }
        .div1{
            background-repeat: repeat-x;
        }
        .div2{
            background-repeat: repeat-y;
        }
        .div3{
            background-repeat:no-repeat;
        }
    </style>

练习:

在这里插入图片描述

3.8 渐变

background-color: #85FFBD;
background-image: linear-gradient(225deg, #85FFBD 0%, #FFFB7D 100%);

4.盒子模型

4.1 什么是盒子

在这里插入图片描述

margin:外边距

padding:内边距

border:边框

4.2 边框

  1. 边框的粗细
  2. 边框的样式
  3. 边框的颜色
<style>
/*body总有一个默认的外边距margin:0*/
/*         hl,ul,a,body{*/
/*             margin: 0;*/
/*             padding: 0;*/
/*             text-decoration: none;*/
/*         }*/
        /*border:粗细,样式,颜色*/
        #box{
            width: 300px;
            border:1px solid red;
        }
        h2{
            font-size: 16px;
            background-color: #000;
            line-height: 30px;
            color: white;
        }
        form{
            background:#3cbda6;
        }
        div:nth-of-type(1) input{
            border:3px solid black;
        }
        div:nth-of-type(2) input{
            border:3px dashed yellow;
        }
        div:nth-of-type(1) >iinput{
            border:2px dashed black;
        }
    </style>

4.3 内外边距

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    外边距的妙用,居中元素
margin: 0,auto;-->
    <style>
        /*body总有一个默认的外边距margin:0*/
        /*         hl,ul,a,body{*/
        /*             margin: 0;*/
        /*             padding: 0;*/
        /*             text-decoration: none;*/
        /*         }*/
        /*border:粗细,样式,颜色*/
        #box{
            width: 300px;
            border:1px solid white;
            margin: 0,auto;
        }
        /*
        顺时针旋转
        margin:0
        margin:0 1px
        margin:0 1px 2px 3px
        */
        h2{
            font-size: 16px;
            background-color: white;
            line-height: 30px;
            color:#000;
            margin: 0 1px;
        }
        form{
            background:yellow;
        }
        input{
            border:1px solid black;
        }
        div:nth-of-type(1){
            border: 10x solid black;
        }
    </style>
</head>
<body>
<div id="box">
    <h2>会员登录</h2>
    <form action="#">
        <div>
            <span>用户名:</span>
            <input type="text">
        </div>
        <div>
            <span>密码:</span>
            <input type="text">
        </div>
        <div>
            <span>邮箱:</span>
            <input type="text">
        </div>

    </form>

</div>
</body>
</html>

盒子的计算方式:

在这里插入图片描述

margin+border+padding+内容宽度

4.4 圆角边框

四个角

<style>
        div{
            width:100px;
            height:100px;
            border: 10px solid red;
            border-radius: 50px 20px;
        }
    </style>

4.5 盒子阴影

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    margin:0 auto;居中
要求:块元素,块元素有固定的宽度
-->
    <style>
        img{


            border-radius: 1000px;
            box-shadow:10px 10px 100px #333397;
        }
    </style>
</head>
<body>
<div style="width: 500px;display:block;text-align:center">
  
        <img src="images/1.png" alt="">


</div>
</body>
</html>

5.浮动

5.1标准文档流

在这里插入图片描述

块级元素:独占一行

h1-h6 p div 列表……

行内元素:不独占一行

span a img strong……

行内元素可以被包含在块级元素中,反之,则不可以

5.2 display

<style>
      /*block块元素
      inline 行内元素
      inline-block  是块元素,但是可以内联,在一行
      */
        div{
            width:100px;
            height:100px;
            border:1px solid red;
            display: inline;
        }
        span{
            width:100px;
            height:100px;
            border:1px solid red;
            display:inline-block;
        }
    </style>
  1. 这个也是一种实现行内元素排列的方式,但是我们很多情况都是用float

5.3 float

  1. 左右浮动float
div{
    margin:10px;
    padding:5px;
}
#father{
    border:1px #000 solid;
}
.layer01{
    border:1px #F00 dashed;
    display:inline-block;
    float:right;
}
.layer02{
    border:1px #00F dashed;
    display:inline-block;
    float:right;
}
.layer03{
    border:1px #060 dashed;
    display:inline-block;
    float:right;
}
.layer04{
    border:1px #666 dashed;
    font-size:12px;
    line-height:23px;
    display:inline-block;
    float:right;
}

5.4 父级边框塌陷的问题

clear

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

解决方案:

  1. 增加父级元素的高度
#father{
    border:1px #000 solid;
    height:800px;
}
  1. 增肌一个空的div标签,清除浮动
<div class="clear"></div>

.clear{
    clear:both;
    margin:0;
    padding:0;
}
  1. overflow
在父级元素中增加一个 overflow:hidden;
  1. 父类添加一个伪类:after
#father:after{
    content:'';
    display:block;
    clear:both;
}#father:after{
    content:'';
    display:block;
    clear:both;
}

小结:

  1. 浮动元素后增加空div

    简单,代码中尽量避免空div

  2. 设置父元素的高度

    简单,元素假设有固定的高度,就会被限制

  3. overflow

    简单,下拉的一些场景避免使用

  4. 父类添加一个伪类:after(推荐)

    写法稍微复杂一点,但是没有副作用,推荐使用!

5.5 对比

  • display

    方向不可以控制

  • float

    浮动起来会脱离标准文档流,所以要解决父级边框塌陷的问题

6.定位

6.1 相对定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    相对定位 相对于自己原来的位置进行偏移-->
    <style>
        body{
            padding:20px;
        }
        div{
            margin:10px;
            padding:5px;
            font-size:12px;
            line-height:25px;
        }
        #father{
            border:1px solid #666;
            padding: 0;
        }
        #first{
            background-color: #3cbda6;
            border:1px dashed #937e32;
            position: relative;
            top:-50px;
            left:50px;
        }
        #second{
            background-color: #e8bf6a;
            border:1px solid #511a57;
            position: relative;
            bottom: 50px;

        }
        #third{
            background-color: red;
            border:1px solid #1a3b1e;
            right:20px;
        }
    </style>
</head>
<body>
<div id="father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>

</div>
</body>
</html>

相对定位:

position:relative;

相对于原来的位置,进行指定的偏移,相对定位的话,他仍然在标准文档流中,原来的位置会被保留

top:-50px;
left:50px;
bottom:-10px;
right:20px;

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #box{
            width:300px;
            height:300px;
            padding:10px;
            border:2px solid red;
        }
        a{
            width:100px;
            height:100px;
            text-decoration:none;
            background:pink;
            link-height:100px;
            text-align: center;
            line-height: 100px;
            color:white;
            display:block;
        }
        a:hover{
            background-color:blue;
        }
        .a2,.a4{
            position:relative;
            left:200px;
            top:-100px;
        }
        .a5{
            position:relative;
            left:100px;
            top:-300px;
        }
    </style>


<body>
<div id="box">
    <a class="a1" href="#">链接1</a>
    <a class="a2" href="#">链接2</a>
    <a class="a3" href="#">链接3</a>
    <a class="a4" href="#">链接4</a>
    <a class="a5" href="#">链接5</a>
</div>

</body>
</html>

6.2 绝对定位

定位:基于XXX定位,上下左右

  1. 没有父级元素定位的前提下,相当于浏览器定位

  2. 假设父级元素存在定位,我们通常会相对于父级元素进行偏移

  3. 在父级元素范围内移动

    相对于父级或者浏览器的位置,进行指定的偏移,相对定位的话,他不·不·在标准文档流中,原来的位置会被保留

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--    相对定位 相对于自己原来的位置进行偏移-->
    <style>

        div{
            margin:10px;
            padding:5px;
            font-size:12px;
            line-height:25px;
        }
        #father{
            border:1px solid #666;
            padding: 0;
            position: relative;
        }
        #first{
            background-color: #3cbda6;
            border:1px dashed #937e32;

        }
        #second{
            background-color: #e8bf6a;
            border:1px solid #511a57;
            position: relative;
            left:100px;

        }
        #third{
            background-color: red;
            border:1px solid #1a3b1e;

        }
    </style>
</head>
<body>
<div id="father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>

</div>
</body>
</html>

6.3 固定定位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: red;
            position: absolute;
            right:0;
            bpttom:0;
        }
        div:nth-of-type(2){/*fixed 固定定位*/
            width:50px;
            height:50px;
            background: yellow;
            position:fixed;
            right: 0;
            bottom: 0;
        }
    </style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>
</html>

6.4 Z-index

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

</head>
<body>
<div id="content">
    <ul>
        <li><img src="images/1.png" alt=""></li>
        <li class="tipText">时光洪流</li>
        <li class="tipBg"></li>
        <li>时间:2022-03-29</li>
        <li>地点:火星</li>
    </ul>
</div>
</body>
</html>
#content{
    width: 400px;

    padding:0px;
    margin:0px;
    overflow: hidden;
    font-size: 12px;
    line-height: 25px;
    border:1px #000 solid;
}
ul,li{
    padding:0px;
    margin: 0px;
    list-style: none;

}
/*父级元素相对定位*/
#content{
    position:relative;
}
.tipText,.tipBg{
    position:absolute;
    width:380px;
    height:25px;
    top:512px;
    text-align: center;
}
.tipText{
    color: white;
    z-index:999;
}
.tipBg{
    background: black;
    width: 10000px;
    opacity: 0.5;/*背景透明度*/
}

7.网页动画(特效效果)

8.总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值