HTML基础第四课(冲浪笔记4)

一、CSS继承(常用于:模块区域样式一样的时候,可以写个父级全部继承)

1、文本样式会继承

①代码例子
<!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>
        .father{
            color: aqua;
            font-size: 24px;
        }
    </style>
</head>
<body>
    <div class="father">
        父级
        <div class="son">
            子级
        </div>
    </div>
</body>
</html>
②效果图:父级的样式继承给了子级

 2、布局样式默认不会继承

①代码例子

<!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>
        .father{
            color: aqua;
            font-size: 24px;
            height: 100px;
            width: 100px;
            background-color: rgb(79, 79, 238);
        }
        .son{
            background-color: rgb(202, 72, 72);
        }
    </style>
</head>
<body>
    <div class="father">
        父级
        <div class="son">
            子级
        </div>
    </div>
</body>
</html>
②效果图:父级的大小没有继承给子级

3、height: inherit;强制继承

①代码例子

<!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>
        .father{
            color: aqua;
            font-size: 24px;
            height: 100px;
            width: 100px;
            background-color: rgb(79, 79, 238);
        }
        .son{
            background-color: rgb(202, 72, 72);
            height: inherit;
        }
    </style>
</head>
<body>
    <div class="father">
        父级
        <div class="son">
            子级
        </div>
    </div>
</body>
</html>
②效果图:父级所有样式都继承给了子级

二、盒子模型

概念:CSS假定每⼀个标签都是⼀个矩阵,围绕着这个矩阵,从内到外展开⼀系列的属性来描述它,这⼀系列就被称为【盒⼦模型】
组成:content、padding、border、margin
举例:篮球场有⼀个箱⼦📦,箱⼦⾥有⼀颗篮球🏀
          篮球场⼜有⼀个箱⼦📦,箱⼦⾥也有⼀颗篮球🏀

1、content:内容,由width、height组成
2、padding:内边距,内容到边框的距离
①上下左右四个方向padding-left/right/top/bottom
        a.一个值
        b.两个值:1上下,2左右
        c.三个值:1上,2左右,3下
        d.四个值:顺时针,上右下左
②注意:系统在设置 padding时 ,默认撑⼤宽⾼,拿出对应的间距作为内间距
如果希望 padding 从宽⾼本身去减⼩的话,设置 box-sizing:border-box 属性

3、border:边框border-top/bottom/left/right
①三部分
        a.边框样式:border-style
        b.边框颜***order-color
        c.边框粗细:border-width
②可单独设置
        border-⽅向-style/color/width
③注意:系统在设置border-width的时候,默认先撑⼤宽⾼,再拿出对应的间距作为边框粗细如果我们希望边框粗细从宽⾼本身去减⼩的话,设置box-sizing:border-box属性
④圆角:border-radius
        a.正方形圆角=边长一半时,是圆形
        b.可以单独设置某角:border-top/bottom-left/right-radius

4、margin:外间距,边框到边框的距离
同padding一样,四个方向:margin-left/right/top/bottom

补充1:
①背景颜色会自动填充到margin以内的区域
②内容一直在content区域
③属性box-sizing:content-box(默认,宽高只控制content区域,使用boder,padding时会改变整体大小)、border-box(宽高控制整个盒子,在设定的宽高中改变)
④盒子居中 margin: auto (使标签处于所占据⽂档流的正中央)
⑤内容居中text-align: center (使得内容处于他所占据位置的正中央)
⑥height:容器高度、line-height:行高
<!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{
            width: 500px;
            height: 500px;
            background-color: rgb(25, 144, 223);
            padding: 3px;/* 内间距 */
            box-sizing: border-box;
            border: 5px solid red;/* 边框 */
            border-radius: 50px;
            margin: auto;/* 居中 */
            text-align: center;/* 文本内容居中 */
            line-height: 500px;/* 文本高度居中 */
        }
        
    </style>
</head>
<body>
    <div class="box">内容</div>
</body>
</html>

 补充2:盒子模型存在的问题

1、两两标签为父子关系:

(1)子级设置的margin-top等等会传递给父级

<!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;
        }
        .father{
            width: 200px;
            height: 200px;
            background-color: aqua;
        }
        .son{
            width: 100px;
            height: 100px;
            background-color: black;
            margin-top: 100px;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

(2)解决措施:在父级写属性padding-top: 100px;box-sizing: boder-box;

2、两两标签为兄弟关系:同样的属性会被叠加,取其中的最大值
<!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;
        }
        .father{
            width: 200px;
            height: 200px;
            background-color: aqua;
        }
        .son1{
            width: 100px;
            height: 100px;
            background-color: black;
            margin-bottom: 50px;
        }
        .son2{
            width: 200px;
            height: 100px;
            background-color: rgb(215, 37, 37);
            margin-top: 100px;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son1"></div>
        <div class="son2"></div>
    </div>
</body>
</html>

四、转换元素特性:display

1、属性:
(1)none:隐藏(不会显示出来,检查时可找到)
(2)block:转成块级元素(会自动换行h1,div,p)
(3)inline:转成内联元素(不会自动换行span,a)
(4)inline-block:行内块(转换成具有自己大小的且横向排列的元素,块与块之间会有间隙,会占位置)

2、与float的区别:
(1)float浮动:会脱离文档流,不占位置,针对块级元素(left/right)
(2)style="display: none;"占位置

3、float浮动代码举例
<!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,.box2,.box3{
            width: 100px;
            height: 100px;
        }
        .box1{
             background-color: rgb(25, 144, 223);
             float: left;
        }
        .box2{
            background-color: rgb(223, 25, 187);
            float: left;
        }
        .box3{
            background-color: rgb(84, 223, 25);
            float: right;
        }
        .box4{
            width: 300px;
            height: 300px;
            background-color: black;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
    <div class="box4"></div>
</body>
</html>

五、定位position

1、相对定位:相对于当前的正常位置
position: relative(通常占位置)
2、绝对定位:
①position: absolute(通常不占位置)
父子关系(子元素进行定位,相对于其父级【设置了定位/没有定位而是找到浏览器边缘】的绝对定位)
②fiexd:窗口定位(任何浏览器放大缩小任意窗口时,数值不变)
3、定位的区别
(1)margin-top的效果
<!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;
        }
 
        .father {
            width: 200px;
            height: 200px;
            background-color: aqua;
            margin-top: 50px;
 
        }
 
        .son1 {
            width: 100px;
            height: 100px;
            background-color: black;
        }
        .son2 {
            width: 100px;
            height: 100px;
            background-color: rgb(212, 134, 24);
        }
    </style>
</head>
 
<body>
    <div class="father">
        <div class="son1"></div>
    </div>
    <div class="son2"></div>
</body>
 
</html>

 (2)相对定位position: relative的效果
<!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;
        }
 
        .father {
            width: 200px;
            height: 200px;
            background-color: aqua;
            /* margin-top: 50px; */
            position: relative;
            top: 50px;
 
        }
 
        .son1 {
            width: 100px;
            height: 100px;
            background-color: black;
        }
        .son2 {
            width: 100px;
            height: 100px;
            background-color: rgb(212, 134, 24);
        }
    </style>
</head>
 
<body>
    <div class="father">
        <div class="son1"></div>
    </div>
    <div class="son2"></div>
</body>
 
</html>

(3)绝对定位position: absolute的效果
<!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;
        }
 
        .father {
            width: 200px;
            height: 200px;
            background-color: aqua;
            /* margin-top: 50px; */
            position: absolute;
            top: 50px;
 
        }
 
        .son1 {
            width: 100px;
            height: 100px;
            background-color: black;
        }
        .son2 {
            width: 100px;
            height: 100px;
            background-color: rgb(212, 134, 24);
        }
    </style>
</head>
 
<body>
    <div class="father">
        <div class="son1"></div>
    </div>
    <div class="son2"></div>
</body>
 
</html>

六、溢出隐藏overflow-x/overflow-y

1、overflow: hidden; 溢出隐藏
代码例子
<!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: 200px;
            height: 200px;
            overflow: hidden;
        }
 
    </style>
</head>
<body>
    <div class="box">
        <img src="https://img0.baidu.com/it/u=4002835238,1224729013&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500" alt="">
    </div>
</body>
</html>
代码效果图

实际全图

2、overflow: scroll; 滚动条(不管有无溢出)

3、auto:自动识别需不需要滚动条
前端HTML、CSS基础知识 文章被收录于专栏

带小伙伴们一起从零认识HTML,学习常用标签,表单,样式,属性,选择器,基本定位,通过学习方法巧记盒子模型,学习基础简单的动画效果

全部评论
这笔试做的太好了
点赞 回复 分享
发布于 2022-08-27 22:55 陕西

相关推荐

会员标识
02-20 16:28
已编辑
牛客运营
从03年的“北大毕业生卖猪肉”到前段时间上热搜的“北大博士入职城管”,这些年“下沉式就业”现象频繁牵动着大家的视野和目光吧,很吸睛?我觉得并不是,如果你说985大学生XXX,那可能成不了焦点,如果说是北大清华毕业生去当城管,卖猪肉,大家都会讨论一番,无论是谁都知道北大清华的过人之处。但是呢近些年的确有很多985、211名校毕业生选择到基层就业或回老家创业,会不会觉得大财小用?老家的哥哥,因为当时学的专业不是很好,但好在学校不错,一路本硕连读,毕业之后在上海打拼了2年,也攒了一些小钱,随后回村选择科学养鸡,买了很大一块地开始科学方法的养鸡、卖鸡蛋,村里的老人都会议论纷纷,白瞎了家里供你读书,又回...
下午吃泡馍:不是每一个脱下长衫的人在下沉市场重获新生,并不是每一个养猪养鸡的高学历人才都会成功。现实是很多人的“长衫”就是自己为数不多甚至唯一的底牌了,拼尽全力拿到一个不错的学历,这时候主流媒体告诉对方脱下长衫也可以活的精彩,其实真的挺难过的。强者恒强,但是弱者是人群的底色。 本质上是整个市场的问题,没有足够多的增长点,没有足够多的岗位,自上而下没有积极向上的氛围。外企撤出,供应链缺失...在发展的过程中总有阵痛,现阶段可能就是我们承受阵痛的过程。之前在牛客看到一个小伙伴说:时代的一粒灰尘,落在谁的身上,都将是无法承受之重!深有感触。
点赞 评论 收藏
分享
MingoTree:看不出你你的技术栈,想找什么工作,然后课设项目别写上去了,自我评价删了,前后端你想好你要干啥,这种简历投上去秒挂的
点赞 评论 收藏
分享
02-11 11:02
已编辑
门头沟学院 硬件开发
Just路人甲:个人建议,把“课程设计”删掉,面试官又不知道你到底是课设还是竞赛,他问你你就说竞赛整的。面试考察的是是你能否🐯得住面试官,即使你插葱装象只要他看不出来,那你也赢了。当然倘若你本身就很硬那自然无懈可击。身边有哥们就是这么拿下心中的 offer 的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务