h1~h6,p,div,ul,li等。width区域(即默认width = 100%;);如果设置不高度,会被内容自动撑开。span,a,label ,strong 等。padding和margin可以设置,垂直方向上的无效。input与img等,它们虽然被归类为行内元素,但可以单独设置宽高和内外边距,在表现上更倾向于inline-block,我们也把它们叫做行内替换元素。(参考:html中的行内替换元素)display属性来转换元素的加载级别或者隐藏元素:
display:block; - 定义元素为块级元素。display: inline; - 定义元素为行内元素。display: inline-block; - 定义元素为行内块级元素。display: none; - 隐藏元素。DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
div {
width: 200px;
height: 200px;
background-color: deepskyblue;
}
span {
width: 200px;
height: 200px;
background-color: lightpink;
}
style>
head>
<body>
<div>div块级元素div>
<span>span行内元素span>
body>
html>

div块级元素独占一行;span行内元素设置的宽度和高度无效,它的实际大小仅由内容撑开。display将它们的元素类型都转换为行内块元素(inline-block):DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
div {
width: 200px;
height: 200px;
background-color: deepskyblue;
display: inline-block;
}
span {
width: 200px;
height: 200px;
background-color: lightpink;
display: inline-block;
}
style>
head>
<body>
<div>div块级元素div>
<span>span行内元素span>
body>
html>

display将div块级元素隐藏:DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
div {
width: 200px;
height: 200px;
background-color: deepskyblue;
display: none;
}
span {
width: 200px;
height: 200px;
background-color: lightpink;
display: inline-block;
}
style>
head>
<body>
<div>div块级元素div>
<span>span行内元素span>
body>
html>

float(浮动)- 指定一个元素应沿其容器的左侧或右侧放置,允许文本和内联元素环绕它。该元素会脱离正常的文档流,但是仍然保持部分的流动性(与绝对定位相反)。【脱离文档流就是将元素从普通的布局排版中移除】div(father)四个子级div(layer01~layer04)组成,我们给每个div内都添加一些内容(图片或文字):index.html
DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>浮动title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
head>
<body>
<div id="father">
<div class="layer01"><img src="images/1.jpg" alt=""/>div>
<div class="layer02"><img src="images/2.jpg" alt=""/>div>
<div class="layer03"><img src="images/3.jpg" alt=""/>div>
<div class="layer04">
浮动的盒子可以向左浮动,也可以向右浮动,直到它的外边缘碰到包含框或另一个浮动盒子为止。
div>
div>
body>
html>
div都设置不同颜色的边框,并将它们都设置为行内块元素(inline-block):style.css
div {
margin: 10px;
padding: 5px;
}
#father {
border: 2px black solid;
}
.layer01 {
border: 2px red dashed;
display: inline-block;
}
.layer02 {
border: 2px blue dashed;
display: inline-block;
}
.layer03 {
border: 2px green dashed;
display: inline-block;
}
.layer04 {
border: 2px grey dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
}

float让layer01向左浮动:style.css
div {
margin: 10px;
padding: 5px;
}
#father {
border: 2px black solid;
}
.layer01 {
border: 2px red dashed;
display: inline-block;
float: left; /* 向左浮动 */
}
.layer02 {
border: 2px blue dashed;
display: inline-block;
}
.layer03 {
border: 2px green dashed;
display: inline-block;
}
.layer04 {
border: 2px grey dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
}

layer01的表现就像是漂浮在father的上层,同时father的边框高度变小了(父级边框塌陷问题)。float让layer02向右浮动:style.css
div {
margin: 10px;
padding: 5px;
}
#father {
border: 2px black solid;
}
.layer01 {
border: 2px red dashed;
display: inline-block;
float: left; /* 向左浮动 */
}
.layer02 {
border: 2px blue dashed;
display: inline-block;
float: right; /* 向右浮动 */
}
.layer03 {
border: 2px green dashed;
display: inline-block;
}
.layer04 {
border: 2px grey dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
}

layer02也漂浮了起来,并且向右浮动直到碰到了father右侧的边框,同时father的边框进一步塌陷了。float)进行网页布局时,当我们将父级元素中的子元素浮动了时,就可能会发生父级边框塌陷问题。因此我们在使用float进行网页布局时,要采取一些措施来防止父级元素的边框发生塌陷。layer01~layer04都设置浮动:style.css
div {
margin: 10px;
padding: 5px;
}
#father {
border: 2px black solid;
}
.layer01 {
border: 2px red dashed;
display: inline-block;
float: left; /* 向左浮动 */
}
.layer02 {
border: 2px blue dashed;
display: inline-block;
float: left; /* 向左浮动 */
}
.layer03 {
border: 2px green dashed;
display: inline-block;
float: right; /* 向右浮动 */
}
.layer04 {
border: 2px grey dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
float: right; /* 向右浮动 */
}

father的边框发生了塌陷。设置父级元素的高度:(不推荐)
father设置一个合适的固定高度:style.css
div {
margin: 10px;
padding: 5px;
}
#father {
height: 350px; /* 设置父级元素的高度 */
border: 2px black solid;
}
.layer01 {
border: 2px red dashed;
display: inline-block;
float: left;
}
.layer02 {
border: 2px blue dashed;
display: inline-block;
float: left;
}
.layer03 {
border: 2px green dashed;
display: inline-block;
float: right;
}
.layer04 {
border: 2px grey dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
float: right;
}

在最后一个浮动子元素后加一个空的div,使用clear清除它两侧的浮动元素:
clear - 指定该元素的左侧或右侧不允许有浮动的元素。它有四个常见的属性值:
clear: left; - 在左侧不允许有浮动元素。clear: right; - 在右侧不允许有浮动元素。clear: both; - 在左右两侧都不允许有浮动元素。clear: none; - 默认值。允许浮动元素出现在左右两侧。layer04的后面添加一个空的div:index.html
DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>浮动title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
head>
<body>
<div id="father">
<div class="layer01"><img src="images/1.jpg" alt=""/>div>
<div class="layer02"><img src="images/2.jpg" alt=""/>div>
<div class="layer03"><img src="images/3.jpg" alt=""/>div>
<div class="layer04">
浮动的盒子可以向左浮动,也可以向右浮动,直到它的外边缘碰到包含框或另一个浮动盒子为止。
div>
<div class="clear">div>
div>
body>
html>
clear: both; - 在左右两侧都不允许有浮动元素。(规范的写法:为了避免这个空div有默认的内外边距,影响我们的页面效果,通常我们都会手动将它的内外边距设置为0)style.css
div {
margin: 10px;
padding: 5px;
}
#father {
border: 2px black solid;
}
.layer01 {
border: 2px red dashed;
display: inline-block;
float: left;
}
.layer02 {
border: 2px blue dashed;
display: inline-block;
float: left;
}
.layer03 {
border: 2px green dashed;
display: inline-block;
float: right;
}
.layer04 {
border: 2px grey dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
float: right;
}
/* 设置不允许两侧出现浮动元素 */
.clear {
clear: both;
margin: 0;
padding: 0;
}

div,并使用clear清除它两侧的浮动元素后,父级元素边框塌陷的问题也得到了解决。(优点:简单;缺点:空div会使我们 HTML 代码变得冗余,我们在编写代码时应该尽量避免定义空的div)给父级元素的样式中添加overflow属性(通常使用overflow: hidden; - 没有滚动条,美观):
overflow - 它指定当元素的内容太大溢出某个元素的框时,会发生什么(是否对内容进行裁剪、是否显示滚动条等行为)。 它有四个常见的属性值:overflow: visible; - 默认值。内容不会被修剪,会呈现在元素框之外。overflow: hidden; - 内容会被修剪,并且其余内容是不可见的。overflow: scroll; - 内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。overflow: auto; - 如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。scroll和auto的区别:
scroll:在内容没有溢出的时候,也会展示滚动条的样式。auto:只有在内容溢出的时候,才会出现滚动条。auto。但在使用auto的时候,写容器内容的样式时,记得将滚动条的宽度预留出来,不然内容溢出的时候,展示效果可能会有些变化。overflow 属性的演示:DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)title>
<style>
div.ex1 {
background-color: lightblue;
width: 110px;
height: 110px;
overflow: scroll;
}
div.ex2 {
background-color: lightblue;
width: 110px;
height: 110px;
overflow: hidden;
}
div.ex3 {
background-color: lightblue;
width: 110px;
height: 110px;
overflow: auto;
}
div.ex4 {
background-color: lightblue;
width: 110px;
height: 110px;
overflow: visible;
}
style>
head>
<body>
<h1>overflow 属性h1>
<p>如果元素中的内容超出了给定的宽度和高度属性,overflow 属性可以确定是否显示滚动条等行为。p>
<h2>overflow: scroll:h2>
<div class="ex1">菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!div>
<h2>overflow: hidden:h2>
<div class="ex2">菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!div>
<h2>overflow: auto:h2>
<div class="ex3">菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!div>
<h2>overflow: visible (默认):h2>
<div class="ex4">菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!菜鸟教程 -- 学的不仅是技术,更是梦想!!!div>
body>
html>

overflow,现在我们继续回到之前的例子。给父级元素father的样式中添加overflow: hidden;:style.css
div {
margin: 10px;
padding: 5px;
}
#father {
border: 2px black solid;
overflow: hidden;
}
.layer01 {
border: 2px red dashed;
display: inline-block;
float: left;
}
.layer02 {
border: 2px blue dashed;
display: inline-block;
float: left;
}
.layer03 {
border: 2px green dashed;
display: inline-block;
float: right;
}
.layer04 {
border: 2px grey dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
float: right;
}

father的样式中添加overflow: hidden;后,父级元素边框塌陷的问题也得到了解决。(优点:简单;缺点:在有下拉列表框等内容不能被裁剪的场景中应避免使用此方法,若使用overflow: hidden;有可能导致部分溢出的内容被裁剪掉而无法展示完全)使用::after给浮动元素的父级元素添加一个内容为空的伪元素,设置它的样式为块级元素并清除它两侧的浮动元素:(推荐使用)
::after - 它用来创建一个伪元素,作为已选中元素的最后一个子元素。通常会配合content属性来为该元素添加装饰内容。这个虚拟元素默认是行内元素。补充:伪元素::after给浮动元素的父级元素添加一个伪元素这个方法本质上和方法二中在 HTML 中添加一个空div的原理是一样的,只不过方法四是在 CSS 中添加虚拟的块元素,而方法二是在 HTML 中添加一个真实的块元素。::after给浮动元素的父级元素添加一个内容为空的伪元素,设置它的样式为块级元素并清除它两侧的浮动元素style.css
div {
margin: 10px;
padding: 5px;
}
#father {
border: 2px black solid;
overflow: hidden;
}
/* ::after - 创建一个伪元素,作为此已选中的元素(father)的最后一个子元素 */
#father::after {
content: ''; /* 设置伪元素的内容为空 */
display: block; /* 设置伪元素为块级元素 */
clear: both; /* 设置伪元素的左右两侧都不允许有浮动元素 */
}
.layer01 {
border: 2px red dashed;
display: inline-block;
float: left;
}
.layer02 {
border: 2px blue dashed;
display: inline-block;
float: left;
}
.layer03 {
border: 2px green dashed;
display: inline-block;
float: right;
}
.layer04 {
border: 2px grey dashed;
font-size: 12px;
line-height: 23px;
display: inline-block;
float: right;
}

::after给浮动元素的父级元素添加一个内容为空的伪元素,设置它的样式为块级元素并清除它两侧的浮动元素后,父级元素边框塌陷的问题也得到了解决。(除了稍微复杂一点之外,此方法没有任何弊端,推荐使用此方法)display:不可以选择元素放置的方向,标准文档流。float:可以选择元素放置的方向(靠左或者靠右),浮动元素会脱离标准文档流,需要解决父级边框塌陷问题