第五章 CSS盒模型

5.1盒模型的定义
        Web页面上大部分的元素(特别是块状元素)都可以看作是一个盒子,W3C组织建议把所有网页上的对象都放在一个盒子(box)中,设计者可以通过创建定义来控制这个盒子的各种属性,这些对象包括段落、列表、标题、图片及层。
盒子的结构可以看作一个矩形框,包括边框(border)、外边距(margin)、内边距(padding),以及元素内容(content),元素会在指定的高度和宽度范围内在页面上显示出来。它们之间的关系,如图所示。

一个页面由许多这样的盒子组成,这些盒子层层嵌套,互相影响,设计者在布局网页和定位Web元素时要充分考虑到这些要素,才能更自如地摆弄这些盒子。
        而网页元素的定位实际上就是这些大大小小的盒子在页面中的定位,这些盒子在页面中是“浮动”的,当某个块状元素被CSS设置了浮动属性,这个盒子就会移动到上一块级元素所处的行中。要关注的是这些盒子在页面中如何摆放、如何嵌套的问题,而这么多盒子摆在一起,其中盒子尺寸计算,是否浮动等要素是最需要关注的。

        默认情况下盒子的边框是“无”,背景色是透明的一所以在默认情况下看不到盒子。内边距、边框和外边距这些属性都是可选的,默认值都是0。边框的作用就是在内边距和外边距之间创建一个隔离带,以避免视觉上的混淆。


5.2 CSS元素的高度和宽度
        当设计者布局一个网页时,网页最终的宽度和高度会超出预计的数值,这是因为盒模型的宽度或高度计算误差造成的。指定一个CSS元素的宽度和高度属性时,只是设置内容区域的宽度和高度。而浏览器实际大小的元素,还必须添加内边距、外边距和边框。而增加或不△影响内家区域的尺寸一只会增加盒模型的总尺寸。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style>
			*{
				margin: 0px;
				padding: 0px;
			}
			div{
				width:300px;
				height: 240px;
				border:10px #ff0000 solid;
				margin: 30px;
				display: block;
				padding:30px;
				float: left;
			}
			img{
				width:300px;
				height: 240px;
			}
		</style>
	</head>
	<body>
		<h1>盒模型示例</h1>
		<p>css盒模型本质上是一个盒子,外边距为30px;内边距为30px;外边框为10px的红色实线;盒子左浮动在同一行处。</p>
		<div><img src="img/1662389752960.png"></div>
		<div><img src="img/1662389752960.png"></div>
	</body>
</html>

5.2.1 盒模型的宽度
        盒模型的宽度=左外边距(margin-left)+左边框(border-left)+左内边距(padding-left)+内容宽度(width)+右内边距(padding-right)+右边框(border-right)+右外边距(margin-right)。


5.1.2 盒模型的高度
        盒模型的高度=上外边距(margin-top)+上边框(border-top)+上内边距(padding-top)+内容高度(height)+下内边距(padding-bottom)+下边框(border-bottom)+下外边距(margin-bottom)。

        所以,图片内容是100像素,而浏览器显示的页面实际大小是200像

        默认情况下,块级元素可以设置宽度和高度,但行级元素是不能设置的

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>块,行级元素的宽,高度设置</title>
        <style type="text/css">
            .ap{
                width: 200px;
                height: 100px;
                border: 2px solid red;
                margin: 10px;
                background-color: lightblue;
            }
        </style>
    </head>
    <body>
        <div class="ap">这是div块级元素,可以设置宽度和高度</div>
        <span class="ap">这是span块级元素,不能设置宽度和高度</span>
    </body>
</html>

5.3 边距设置和边框设置
        margin-border-padding模型是最常用的盒子布局形式。对于任何一个盒子,都可以分别通过设置四条边各自的外边距(margin)、边框(border)和内边距(padding),实现各种各样的排版效果,而且它们各自的四条边在多参数同时设置时,均按照上→右→下→左的顺序(顺时针)。


5.3.1 外边距设置
        外边距是指元素与元素之间的距离,外边距设置属性,可分别设置margin-top、margin-right、margin-bottom、margin-left,也可以用margin属性一次性设置所有外边距。


1 上外边距

 语法:margin-top:length I percent I auto

        参数:Iengh包括长度值和长度单位,包括绝对单位和相对单位。percent是基于父对象的高度。auto值为自动提取边距值,是默认值。
        说明:设置对象上外边距,外边距始终透明。内联元素要使用该属性,必须先设定元素的 height属性或 width属性,或者设定 position属性为absolute。


2 右外边距

语法:margin-right: length I pereent I auto
参数:同margin-top。
说明:同margin-top。


3 下外边距

语法:margin-bottom:length 1 percent I auto
参数:同 margin-topal
说明:同margin-tope


4 左外边距

语法:mangin-left: length I percent I auto
参数:同margin-1op。
说明:同margin-top。

例5.3四外边距设置

<! DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>示例5.3</title>
        <style type="text/css">
        #big{
            width:450px;
            height: 200px;
            margin: 0;
            background-color:#ff0;
        }
        #small1,#small2,#small3,#small4{
            width: 200px;
            height: 50px;
            text-align: center;
            background-color: #0ff;
        }
        #small1{
            margin-left: 20px;
            margin-bottom: 30px;
        }
        #small2{
            margin-right: 20px;
            margin-top: 10px;
            float: right;
        }
        #small3{
            margin-bottom: 5px;
        }
        #small4{
        margin-left: 10px;
        margin-top: 15px;
        }
        </style>
    </head>
    <body>
        <div id="big">
        <div id="small1">A:左外边距20像素,下外边距30像素</div>
        <div id="small2">B:右外边距20像素,上外边距10像素,右浮动</div>
        <div id="small3">C:下外边距5像素</div>
        <div id="small4">D:左外边距10像素,上外边距15像素</div>
        </div>
    </body>
</html>

5 外边距

语法:margin:length I percent / auto
        参数:length包括长度值和长度单位,包括绝对单位和相对单位。percent是基于父对象的高度,左右外边距允许使用负数。auto值为自动提取边距值,是默认值。
        说明:设置对象四边的外边距,包括margin-top(上外边距)、margin-right(右外边距margin-bottom下外边距)、margin-left(左外边距),外边距始终是透明的。
        如果只提供1个参数,将应用于全部的外边距。
        如果提供2个参数,第1个参数应用于上、下外边距,第2个参数应用于左、右外边距。
        如果提供3个参数,第1个参数应用于上外边距,第2个参数应用于左、右外边距,第3个参数应用于下外边距

例5.4外边距设置
<! DOCTYPE html>
<html>
<head>
    <meta charset=" utf-8"/>
    <title>示例 5.4</title>
    <style type="text/css"> 
    div{
        border:solid #0000FF 1px; 
        width:auto; 
        margin :5px; 
        float :left;
    }
    .margin1{
            background-color:#9F6; 
            border:none;/*无边框*/ 
            width:200px; 
            height:80px;
            margin:0px 15px 5px 30px;
    }
    .margin2{
        background-color:yellow ; 
        border:none; width:200px; 
        height:80px;
        margin:5px 30px 15px;
    }
        .margin3{
            background-color:lightgreen ;
            border:none;
            width:200px; 
            height:80px;
            margin:10px 30px;
    }
    .margin4{
                background-color:#FC0; 
                border:none; width:200px; 
                height:80px; 
                margin:15px;
    }
        </style>
    </head>
    <body>
        <div>
            <div class =" margin1">上、右、下、左外边距分别为:0px、15px、5px、30px</div>
            </div>
            <div>
            <div class=" margin2">上外边距为:5px,下外边距为:15px,左右外边距为:30px</div>
            </div>
            <div>
            <div class=" margin3">上、下外边距为10px,左、右外边距为:30px</div>
            </div>
            <div>
            <div class=" margin4">上、下、左、右外边距均为:15px</div>
            </div>
        </body>
    </html>

5.3.2 外边距的合并
        通常,盒子与盒子之间的外边距相遇会互相影响,必须对margin属性深入了解,才能精确地控制盒子的位置。


1 行级元素外边距合并

        行级元素的盒子相遇,盒子与盒子之间的距离等于两个盒子外边距的总和。

 例5.5行级元素之间的外边距设置。

<! DOCTYPE html>
<html>
    <head>
        <meta charset =" utf-8"/>
        <title>示例5.5</title>
        <style type="text/css">
        *{
            margin:50px;    
        }
        .hb1{
            background-color:yellow; 
            margin-right:20px; 
            padding:30px;
        }
        .hb2{
            background-color:lightpink; 
            margin-left:30px; 
            padding:30px;
        }
        </style>
        </head>
        <body>
            <span class=" hb1" >黄色 span</span><span class="hb2">粉红色span</span>
    </body>
</html>

2 块级元素外边距合并

        块级元素的盒子相遇,盒子与盒子之间的距离等于两盒子中外边距的较大者。

例5.6垂直相遇块级元素之间的外边距设置 

<! DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>示例5.6</title>
        <style type="text/css">
            *{
                margin :50px;
            }
            .div1{
                background-color:yellow; 
                margin-bottom:30px; 
                padding:30px;
            }
            .div2{
                background-color:lightpink; 
                margin-top:30px;
                padding:30px;
            }
            </style>
        </head>
        <body>
            <div class="div1">黄色div</div>
            <div class="div2">粉红色div</div>
    </body>
</html>

5.3.3 内边距设置
元素的内边距用来控制边框和内容区之间的空白距离,并非实体,用padding属性表示,类似于HTML中表格单元格的填充属性。内边距(padding)和外边距(margin)很相似,都是透明不可见的,只是内边距和外边距之间还有边框。从语法和用法上来说,内边距的属性与外边距的属性也是类似的,既可以使用复合属性,也可以使用单边属性,padding属性接受 length 值或 percent值,区别于外边距,内边距不可以使用负值。 

例5.7内边距的设置。

<!DOCTYPE html>
<html>
    <head>
        <meta chanset=" utf-8" />
        <title>示例5.7</title>
        <style type="text/css"> 
        span{
            background-color:#FFFF99;
        }        
        div{
            border:solid #000000 1px;
            width:auto; 
            height:auto;
            margin:15px; 
            float:left;
        }
        .padding1{
            padding-top:30px; 
            padding-left:15px;
        }
        .padding2{
            padding-bottom:30px; 
            padding-right:30px;
        }
        .padding3{
            padding:5px 30px;
        }
        .padding4{
            padding:20px;
        }
        </style>
    </head>
    <body>
        <div class =" padding1">
        <span>文字元素的上内边距为30px,左内边距为15px</span>
        </div>
        <div class =" padding2">
        <span>文字元素的下内边距为30px,右内边距为30px</span>
        </div>
        <div class=" padding3" >
        <span>文字元素的上、下内边距为5px,左、右内边距为:30px</span>
        </div>
        <div class=" padding4" >
        <span>文字元素的上、右、下、左内边距均为:20px</span>
        </div>
    </body>
</html>

5.3.4 边框设置
        元素外边距内就是元素的边框(border),它是围绕内边距和元素内容的一条或多条线在内边距和外边距之间。边框的四条边分别用border-top、border-right、border-bottomborder-let 表示,它们的属性与内外边距的属性也是类似的,既可以使用复合属性,也可以使用单边属性。
        边框作为盒模型的某个组成部分,边框的CSS样式设置将直接影响到盒子的尺寸和外观。而通过使用 border属性,可以创建出更佳的边框效果,还可以应用于任何元素。boner属性设置通常有3种:样式(border-style)、宽度(border-width)和颜色(border-color)。


1上边框

语法:border-top : border-style l border-width l border-color
参数:该属性是复合属性。需要通过参数设置来实现。


2右边框

语法:border-right : border-style l border-width l border-color
参数:该属性是复合属性。需要通过参数设置来实现。


3下边框

语法:border-bottom : border-style l border-width l border-color
参数:该属性是复合属性。需要通过参数设置来实现。


4 左边框

语法:border-left : border-style l border-width l border-color
参数:该属性是复合属性。需要通过参数设置来实现。


5 边框样式

        在CSS中,样式是边框最重要的一个设置,因为如果没有样式,在Web页面中边框就不会显示。
        border-style 是一个复合属性,可以同时取1~4个值,取值方法与外边距相似。边框属性有 12个值可选,包括默认(imnital)和无边框(none)。属性值说明见表 

6 边框宽度

在CSS中,宽度是通过border-width属性来设置边框粗细的。
        与border-slyle属性相同,border-width也是一个复合属性。设置边框宽度时,可以直接输入leng 确定长度值,如5px或2cm,但不可以为负值;或者选择系统预设属性值。属性值说明见表。


7 边框颜色

         在CSS中,边框颜色是通过border-color 属性来设置的,该属性可以使用任何类型的颜色值,包括用颜色命名的值、十六进制参数或RGB值。但是如果对象的border-style 设置为none或者border-width设置为0,本属性将失去作用。

例5.8边框的设置。

<! DOCTYPE html>
<html>
    <head>
        <meta charset=" utf-8" />
        <title>示例5.8</title>
        <style type="text/css"> 
        h2{
            text-align:center;
            barder-bottom:dashed 5px red ;
        }
        p{
            font-family:"楷体";
            border-top:solid thin purple;
            border-right:dashed 5px #99FF66; /*水绿色*/ 
            border-bottom:double thick purple;
            border-left :dashed 5px #FF9999:/*粉红色*/
        }
        div{
            border-style:solid dashed double; 
            border-width:10px;
            border-color:deepskyblue ;
        }
        </style>
    </head>
    <body>
        <h2>边框的实现</h2>
        <p>这个页面主要显示边框的不同样式、宽度和颜色,以实现对象的美观化。</p >
        <div>1.h2处所用边框 CSS是5像素红色虚线的下边框;<br>
        2.p处所用边框CSS是细的紫色实线上边框、5像素水绿色虚线右边框、粗的紫色双实线下边框、5像素粉红色虚线左边框;<br> 
        3.div处所用边枢 CSS是10像素蓝色实线、虚线、双实线。
        <div>    
    </body>
</html>

5.3.5 新增边框属性
        CSS3中对边框新增了几个属性,设计者可以通过这些属性创建圆角边框、添加边框阴影、使用图片来绘制边框等。


1圆角边框

        border-radius:设置边框四个角有弧度成为圆角,需要设置相关参数。

 例5.9圆角边框

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>示例5.9</title>
        <style type="text/css"> 
        div{
        border:2px solid red; 
        border-radius:25px;
        }
        </style>
    </head>
    <body>
        <center>
        <div>利用border-radius属性设置四个圆角边框。</div>
        </center>
    </body>
</html>

2阴影边框

        box-shadow:向四个边框添加一到多个阴影,需要设置相关参数。

例5.10阴影边框的设置

<! DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>示例 5.10</title>
        <style type="text/css"> 
        div{
            margin:100px;
            border:2px solid blue;
            box-shadow:-15px -10px 50px red;
        }
        </style>
    </head>
    <body>
        <div>利用box-shadow属性设置边框的红色阴影。其中阴影向左偏移15像素,向上偏移20像素,模糊距离为50像素。
        </div>
    </body>
</html>

3图片绘制边框

        border-image:设置所有边框用图片显示,需要嵌人相关图片,但是部分浏览器不支持此属性。

例5.11图片边框的设置。

<! DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>示例5.11</title>
        <style type="text/css"> 
        div{
            margin:100px;
            border:50px solid blue;
            border-image:url(img/border.jpg)5 10 round;
        }
        </style>
        </head>
        <body>
            <div>利用border-image属性设置图片边框铺满效果。上下向内偏移5像素,左右向内偏移10像素。</div>
    </body>
</html>

5.4 CSS元素的定位
         前面所讲的盒模型都是标准流情况下可用的,但是盒模型仅有的几种排版对布局有很大的限制,导致元素无法在页面中随意地摆放,因此,我们需要使用盒子的定位(position)来增加排版的灵活性和适应性,
        定位 (position)的思想是,它允许你定义元素框相对于其正常位置应该出现的位置或者相对于父元素、另一个元素甚至浏览器窗口本身的位置。
        position 属性值有4个。
        语法:position:staticlrelative l absolute l fixed
        参数:static是默认值,默认没有定位,或者用于取消特殊定位的继承,恢复默认,又称静态定位。relatives是相对定位,生成相对定位的元素,相对于其正常位置进行定位。ab-solute 是绝对定位,相对于父元素或者浏览器窗口进行定位,需要 top、right、bottom 和 left属性辅助完成。fixed是固定定位,相对于浏览器窗口进行定位,需要top、right、bottom 和left 属性辅助完成。

 例5.12 静态定位

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>示例5.12</title>
        <style type="text/css">
            .father{
                border: 2px solid red;
                width: 300px;
                height: 250px;
            }
            .son1{
                border: 2px double red;
                background-color: yellow;
                width: 200px;
                height: 80px;
            }
            .son2{
                border: 2px double red;
                width: 200px;
                height: 25px;
                margin-top: 50px;
            }
        </style>
    </head>
    <body>
        <div class="father">父盒子:无定位
        <div class="son1">子盒子1:无定位的盒子
        <h2>静态定位的盒子</h2>
        </div>
        <div class="son2">子盒子2:无定位的盒子</div>
        </div>
    </body>
</html>

5.4.1 static 定位
        static 是 HTML 元素的默认值,不受 top、right、bottom 和left 属性影响,元素出现在正常的文档流中。


5.4.2 relative定位
        relative 不脱离文档流的布局,需要参照父元素的四条边(不是浏览器),oP、right、bottom 和let属性的参数,从盒子中独立出来浮在上面。相对定位只改变自身的位置,在文档流原先的位置留出空白区域。定位的起始位置为此元素原先在文档流的位置。


5.4.3 absolute定位
        absolute 脱离原来文档流的布局,浮在其他盒子上面,独立出来。子盒子原来位置的空间由后面的盒子填充。绝对定位的起始位置为最近已定位的父盒子,如果父盒子没有定位,那么子盒子的起始位置为浏览器,并随着滚动条的移动而改变位置。


1 相对浏览器绝对定位

        利用position、top和left的参数相对浏览器绝对定位,独立开了原来位置

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CSS元素的定位</title>
        <style type="text/css">
            .father{
                border: 2px solid red;
                width: 300px;
                height: 200px;
            }
            .son1{
                border: 2px double red;
                background-color: yellow;
                width: 200px;
                height: 80px;
                position: absolute;        /*相对浏览器绝对定位*/
                top: 10px;
                left: 30px;
            }
            .son2{
                border: 2px double red;
                width: 200px;
                height: 25px;
                margin-top: 50px;
            }
        </style>
    </head>
    <body>
        <div class="father">父盒子:无定位
        <div class="son1">子盒子1:无定位的盒子
            <h2>静态定位的盒子</h2>
        </div>
        <div class="son2">子盒子2:无定位的盒子
        </div>
        </div>
    </body>
</html>

 2 相对父盒子绝对定位

        父盒子设置了相对定位,所以子盒子以最近的已定位的父盒子为基准,进行偏移,独立开了原来位置,其原来位置被填充 。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CSS元素的定位</title>
        <style type="text/css">
            .father{
                border: 2px solid red;
                width: 300px;
                height: 200px;
                position: relative;
            }
            .son1{
                border: 2px double red;
                background-color: yellow;
                width: 200px;
                height: 80px;
                position: absolute;
                top: 10px;
                left: 30px;
            }
            .son2{
                border: 2px double red;
                width: 200px;
                height: 25px;
                margin-top: 50px;
            }
        </style>
    </head>
    <body>
        <div class="father">父盒子:相对定位
        <div class="son1">子盒子1:独立上浮,相对父盒子右边水平偏离10像素,相对父盒子上边垂直偏离30像素
            
        </div>
        <div class="son2">子盒子2:无定位的盒子
        </div>
        </div>
    </body>
</html>

 5.4.4 fixed定位
         hxed 类似于 absolute,但在固定定位中,盒子的位置不随着滚动条的移动而改变位置。相对于浏览器窗口是固定不变的。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CSS元素的定位</title>
        <style type="text/css">
            .father{
                border: 2px solid red;
                width: 300px;
                height: 200px;
                /* position: relative; */
            }
            .son1{
                border: 2px double red;
                background-color: yellow;
                width: 200px;
                height: 80px;
                position: fixed;
                top: 10px;
                right: 30px;
            }
            .son2{
                border: 2px double red;
                width: 200px;
                height: 25px;
                margin-top: 50px;
            }
        </style>
    </head>
    <body>
        <div class="father">父盒子:无定位
        <div class="son1">子盒子1:固定定位,相对浏览器右边水平偏离10像素,相对浏览器上边垂直偏离30像素
        </div>
        <div class="son2">子盒子2:无定位的盒子
        </div>
    </body>
</html>

5.5 CSS元素的浮动

5.5.1 盒子的浮动添加
float 属性: 用于控制元素的浮动方向。可以取值:
left: 元素向左浮动,允许文本和内联元素在其右侧环绕。
right: 元素向右浮动,允许文本和内联元素在其左侧环绕。
none: 默认值,元素不浮动。
inherit: 继承父元素的 float 属性。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CSS元素的浮动</title>
        <style type="text/css">
            .father{
                border: 2px solid red;
                background-color: #ffCCff;
                padding: 5px;
            }
            .father div{
                border: 2px dashed blue;
                background-color: #CCFFFF;
                width: 100px;
                height: 20px;
                margin: 10px;
                padding: 10px;
                
            }
            .father p{
                border: 2px dotted green;
                background-color: #FFFF99;
            }
        </style>
    </head>
    <body>
        <div class="father">
        <h2>父盒子</h2>
        <div style="float: right;">右浮动盒子1</div>
        <div>标准流盒子2</div>
        <div>标准流盒子3</div>
        <p>css 中,有一个 foat 属性,默认为 none,也就是标准流通常的情况。若果将 foat 属性的值设
        置为let 或 nght,元素就会向其父级元素的左侧或右侧靠近,同时默认的情况下,盒子的宽度不再伸展,
        而是根据盒子里面的内容的宽度确定。</p>
        </div>
    </body>
</html>

5.5.2 盒子的浮动清除
浮动会导致父元素的高度塌陷,因为浮动的元素不占据空间。为了解决这个问题,可以使用以下几种方式清除浮动:

设置父容器的 overflow 属性为 auto 或 hidden:css.container { overflow: auto; /* 或者 overflow: hidden; */ }

语法: clear:left | right | both | none

参数:left清除左边浮动,right清除右边浮动,both清除左右边浮动。

 1.未清除浮动

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CSS元素的浮动</title>
        <style type="text/css">
            .father{
                border: 2px solid red;
                background-color: #ffCCff;
                padding: 5px;
            }
            .father div{
                border: 2px dashed blue;
                background-color: #CCFFFF;
                width: 100px;
                height: 20px;
                margin: 10px;
                padding: 10px;
                
            }
            .father p{
                border: 2px dotted green;
                background-color: #FFFF99;
            }
        </style>
    </head>
    <body>
        <div class="father">
        <h2>父盒子</h2>
        <div style="float: right;">右浮动盒子1</div>
        <div style="float: right;">右浮动盒子2</div>
        <div style="float: right;">右浮动盒子3</div>
        <p>css 中,有一个 foat 属性,默认为 none,也就是标准流通常的情况。若果将 foat 属性的值设
        置为let 或 nght,元素就会向其父级元素的左侧或右侧靠近,同时默认的情况下,盒子的宽度不再伸展,
        而是根据盒子里面的内容的宽度确定。</p>
        </div>
    </body>
</html>

2.清除浮动后 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CSS元素的浮动</title>
        <style type="text/css">
            .father{
                border: 2px solid red;
                background-color: #ffCCff;
                padding: 5px;
            }
            .father div{
                border: 2px dashed blue;
                background-color: #CCFFFF;
                width: 100px;
                height: 20px;
                margin: 10px;
                padding: 10px;
                
            }
            .father p{
                border: 2px dotted green;
                background-color: #FFFF99;
            }
        </style>
    </head>
    <body>
        <div class="father">
        <h2>父盒子</h2>
        <div style="float: right;">右浮动盒子1</div>
        <div style="float: right;">右浮动盒子2</div>
        <div style="float: right;">右浮动盒子3</div>
        <p style="clear: both;">css 中,有一个 foat 属性,默认为 none,也就是标准流通常的情况。若果将 foat 属性的值设
        置为let 或 nght,元素就会向其父级元素的左侧或右侧靠近,同时默认的情况下,盒子的宽度不再伸展,
        而是根据盒子里面的内容的宽度确定。</p>
        </div>
    </body>
</html>

5.6 综合案例——昵心美食空间
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>昵心美食空间</title>
    <style type="text/css">
        * {
            background-color: #ffff99;
        }
        a {
            color: red;
        }
        .all {
            width: 700px;
            height: 650px;
            margin: 10px auto;
            padding: 5px;
            background-image: url(img/bg1.JPG);
        }
        .banner {
            width: 700px;
            height: 70px;
        }
        .menu {
            width: 690px;
            height: 40px;
            padding: 5px;
        }
        .main {
            width: 700px;
            height: 450px;
            margin: 5px 0px;
            position: relative;
        }
        .left, .right {
            width: 150px;
            height: 440px;
            border: 1px solid #999; 
            float: left;
        }
        .middle {
            width: 384px;
            height: 450px;
            margin: 0px 5px;
            float: left;
            font-size: 20px;
            font-family: "楷体";
            font-weight: 700;
            color: #0000ff;
        }
        .one {
            width: 380px;
            height: 155px;
            border: 1px solid #999;
        }
        
        .two {
            width: 255px;
            height: 100px;
            border: 5px double red;
            margin-top: 20px;
            margin-bottom: 20px;
            border-radius: 25px;
        }
        .three{
            width: 380px;
            height: 155px;
            border: 1px solid #999;
        }
        .bottom {
            width: 700px;
            height: 70px;
        }
    </style>
</head>
<body>
    <div class="all">
        <div class="banner">
            <img src="img/banner.jpg" width="700px" height="70px"  />
        </div>
        <div class="menu">
            <img src="img/menu.jpg" width="690px" height="40px"  />
        </div>
        <div class="main">
            <div class="left">
                <marquee direction="up">
                    <img src="img/mm_1.jpg" width="150px" height="140px"  />
                    <img src="img/mm_2.jpg" width="150px" height="140px"  />
                    <img src="img/mm_3.jpg" width="150px" height="140px"  />
                </marquee>
            </div>
            <div class="middle">
                <div class="one">
                    <img src="img/font.jpg" width="25px" height="25px"  />为您推荐
                    <br><br>
                    <img src="img/x_1.jpg" width="80px" height="40px"  />
                    <img src="img/x_2.jpg" width="80px" height="40px"  />
                    <img src="img/x_3.jpg" width="80px" height="40px"  />
                    <img src="img/x_4.jpg" width="80px" height="40px"  />
                    <img src="img/x_5.jpg" width="80px" height="40px"  />
                    <img src="img/x_6.jpg" width="80px" height="40px"  />
                </div>
                <center>
                <div class="two">
                    <h1>昵心美食空间</h1>
                </div>
                </center>
                <div class="three">
                    <img src="img/font.jpg" width="25px" height="25px"  />团购信息
                    <br>
                    <a href="#">1.火锅团购</a><br>
                    <a href="#">2.烧烤团购</a><br>
                    <a href="#">3.自助餐团购</a><br>
                    <a href="#">4.新春特惠</a><br>
                </div>
            </div>
            <div class="right">
                <marquee direction="up">
                    <img src="img/good_1.jpg" width="150px" height="140px"  />
                    <img src="img/good_2.jpg" width="148px" height="140px"  />
                    <img src="img/good_3.jpg" width="148px" height="140px"  />
                </marquee>
            </div>
        </div>
        <div class="bottom">
            <hr color="#0000FF">
            <center style="font-family:'楷体'";>版权所有 &copy; 昵心美食空间<br />
            地址:江门市大学路XXX号,邮编:500000,电话:0750-9999999</center>
        </div>
        </div>
    </body>
</html>

出现这个错误的原因是在导入seaborn包时,无法从typing模块中导入名为'Protocol'的对象。 解决这个问题的方法有以下几种: 1. 检查你的Python版本是否符合seaborn包的要求,如果不符合,尝试更新Python版本。 2. 检查你的环境中是否安装了typing_extensions包,如果没有安装,可以使用以下命令安装:pip install typing_extensions。 3. 如果你使用的是Python 3.8版本以下的版本,你可以尝试使用typing_extensions包来代替typing模块来解决该问题。 4. 检查你的代码是否正确导入了seaborn包,并且没有其他导入错误。 5. 如果以上方法都无法解决问题,可以尝试在你的代码中使用其他的可替代包或者更新seaborn包的版本来解决该问题。 总结: 出现ImportError: cannot import name 'Protocol' from 'typing'错误的原因可能是由于Python版本不兼容、缺少typing_extensions包或者导入错误等原因造成的。可以根据具体情况尝试上述方法来解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ImportError: cannot import name ‘Literal‘ from ‘typing‘ (D:\Anaconda\envs\tensorflow\lib\typing....](https://blog.youkuaiyun.com/yuhaix/article/details/124528628)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值