盒子模型布局(div+css)
本次记录html里元素布局的实现,即使用css实现元素的水平居中、垂直居中、水平垂直居中
。
在此之前先记录一下,块级元素、行级(内联)元素、内联块元素
这三个概念。接下来就依次作简单介绍。
1.块级元素(display:block)
其特性有:
- 元素独占一行,其自己另起一行开始,并且其后元素也另一起一行开始。
- 元素高度(height)、宽度(width)、行高(line-height)、内边距(padding)、外边距(margin)均可设置。
- 元素占据其父元素的整个容器,也就是说块级元素的宽度和父元素的宽度是一样的,其占满了父元素,所以其后元素只能另起一行。
- 可以容纳内联元素以及其他块级元素。
块级元素主要有:
<address></address> , <blockquote></blockquote> ,<center></center> , <dir></dir> , <div></div> , <dl></dl> , <fieldset></fieldset> , <form></form> , <h1></h1> , <h2></h2> , <h3></h3> , <h4></h4> , <h5></h5> , <h6></h6> , <hr> , <isindex></isindex> , <menu></menu> , <noframes></noframes> , <noscript></noscript> , <ol></ol> , <p></p> , <pre></pre> , <table></table> , <ul></ul> , <li></li>
demo演示:
<!--
div为块级元素,默认display:block,
设置父级元素宽高,块级元素子元素会继承其父元素的宽度,所以两个子元素都是新起一行开始。
-->
<div style="width: 100px;height: 100px;background-color: #fff;">
<div style="height:50px;background-color: blue;"></div>
<address style="height:60px;background-color: red;"></address>
</div>
2.行级(内联)元素(display:inline)
其特性有:
- 和其他同级行级元素排列在同一行。
- 元素高度(height)、宽度(width)、垂直方向的内边距(padding-top/padding-bottom)、外边距(margin-top/margin-bottom)不可设置。但可以设置行高(line-height)、水平方向外边距(margin-left/margin-right)以及内边距(padding)
- 行级元素只能容纳文本或者其他行级元素,行级元素的宽度取决于其内容的宽度,其内容一般是文本或者图像。
行级元素主要有:
<a></a> , <abbr></abbr> , <acronym></acronym> , <b></b> , <bdo></bdo> , <big></big> , <br> , <cite></cite> , <code></code> , <dfn></dfn> , <em></em> , <font></font> , <i></i> , <img> , <input> , <kbd></kbd> , <label></label> , <q></q> , <s></s> , <samp></samp> , <select></select> , <small></small> , <span></span> , <strike></strike> , <strong></strong> , <sub></sub> , <sup></sup> ,<textarea></textarea> , <tt></tt> , <u></u> , <var></var>
demo演示:
<!--
块级元素div可以容纳行级元素,该demo容纳了a、q、sup三个行级元素
a标签测试结果发现行级元素里padding可以改变
q标签测试发现行级元素里宽度取决于内容的宽度,设置无法改变
sup标签测试发现行级元素水平方向的margin是可以改变的
-->
<div style="width: 800px;height: 100px;margin: 20px 0;">
<a style="padding: 10px 10px;">内联元素a标签</a>
<q style="width:100px">内联元素q标签测试宽度能否改变</q>
<sup style="margin-left:10px;margin-right:10px ;">内联元素sup标签测试能否改变水平方向的内外边距</sup>
</div>
3.行内块级元素(display:inline-block)
其特性有:
-
和其他元素排列在一行上,无论其为块级元素还是行级元素,设置其展示方式为inline-block都会被排列在一行内。
-
元素高度(height)、宽度(width)、行高(line-height)、内边距(padding)、外边距(margin)均可设置。
行内块级元素主要有:
<img> , <inpput>
demo演示:
<!--
img标签行测试行内块级元素可以设置其内外边距
input标签测试行内块级元素可以设置其宽高
-->
<img src="./img/logo.png" alt="JingDong icon" style="margin: 10px auto;padding: auto 10px;">
<input type="text" style="width: 200px; height: 20px;">
4.水平居中
水平居中也分为两种,行内元素水平居中和块级元素水平居中
-
行内元素居中:在父元素css中添加text-align:center即可
demo演示:
<!-- 父元素标签为div,设置其样式为text-align:center子元素水平居中 子元素为行级元素、也可以是行内块级元素 --> <div style="text-align: center;"> <a>行内元素1</a> <q>行内元素2</q> <sup>行内元素3</sup> <img src="./img/logo.png" alt="JingDong icon"> </div>
-
块级元素居中
-
定宽
-
正常流布局–未设置定位(position):子元素宽度已知,设置其外边距margin:0 auto即可
demo演示:
<!-- 子元素宽度确定,且为正常流布局,直接设置其margin:0 auto即可实现水平居中 p:first-child 代表选择所有父元素底下第一个子元素标签为p的元素 p:first-child~p 代表选择除第一个子元素为p标签的元素外的其他所有同级p标签元素 --> <style type="text/css"> #parent p { width: 100px; margin: 0 auto; } p:first-child { background-color: red; } p:first-child~p { background-color: blue; } </style> <div id="parent"> <p id="child-first">子元素正常流定宽1</p> <p id="child-second">子元素正常流定宽2</p> <p id="child-second">子元素正常流定宽3</p> </div>
-
非正常流布局–子元素设置绝对定位脱离文档:需要确定其四方定位
demo演示:
<!-- 父元素设置相对定位,子元素设置决定定位. 子元素在设置magrin:0 auto的同时还需要设置元素的定位,确保实现水平居中 --> <style type="text/css"> #parent { width: 400px; height: 400px; position: relative; } #parent p { width: 100px; height: 100px; margin: 0 auto; top: 0; bottom: 0; left: 0; right: 0; position: absolute; background-color: red; } </style> <div id="parent"> <p>子元素非正常流定宽1</p> </div>
-
-
不定宽
-
使用transform
demo演示:
<!-- 子元素绝对定位距离父元素左端50%处,然后再以x轴向左平移子元素大小的50% 即实现水平居中 --> <style type="text/css"> #parent { position: relative; } #parent p { left: 50%; position: absolute; transform: translateX(-50%); background-color: red; } </style> <div id="parent"> <p>子元素正常流定宽1</p> </div>
-
借助table
demo演示:
<!-- --> <style type="text/css"> #parent { width: 400px; height: 400px; } #parent table { margin: 0 auto; } </style> <div id="parent"> <table> <tbody> <tr> <td> <p>子元素非正常流定宽1</p> </td> </tr> </tbody> </table> </div>
-
-
参考css之元素居中
5.垂直居中
定宽就不详述了,讲讲不定宽的情况。
-
使用绝对定位和transform
demo演示:
<!-- 子元素绝对定位距离父元素顶端50%处,然后再以y轴向上平移子元素大小的50% 即实现垂直居中 --> <style type="text/css"> #parent { width: 400px; height: 400px; position: relative; } #parent p { top: 50%; position: absolute; transform: translateY(-50%); background-color: red; } </style> <div id="parent"> <p>子元素非正常流定宽1</p> </div>
-
使用绝对定位和负外边距
demo演示:
<!-- 子元素绝对定位距离父元素顶端50%处,然后再设置其上外边距为子元素高度的一半,手动实现平移 最后实现垂直居中 --> <style type="text/css"> #parent { width: 400px; height: 400px; position: relative; } #parent p { width: 50%; height: 30%; top: 50%; position: absolute; margin: -15% 0 0 0; background-color: red; } </style> <div id="parent"> <p>子元素非正常流定宽1</p> </div>
-
使用flex布局
demo演示:
<!-- 元素通过设置 display:flex; 将其指定为 flex 布局的容器,指定好了容器之后再为其添加 align-items 属性, 该属性定义项目在交叉轴(这里是纵向轴)上的对齐方式。 --> <style type="text/css"> #parent { width: 400px; height: 400px; display: flex; align-items: center; } #parent p { background-color: red; } </style> <div id="parent"> <p>子元素非正常流定宽1</p> </div>
-
使用flex布局(第二种)
demo演示:
<!-- 首先给父元素设置 display:flex; 设置好之后改变主轴的方向 flex-direction: column;意思是布局主轴方向为垂直方向,起点在顶端 随后设置justify-content:center,代表着项目在主轴上的对齐方式为居中对齐 --> <style type="text/css"> #parent { width: 400px; height: 400px; display: flex; flex-direction: column; justify-content: center; } #parent p { background-color: red; } </style> <div id="parent"> <p>子元素非正常流定宽1</p> </div>
-
一些特殊简便的垂直居中方式
demo演示:
<!-- 设置line-height高度与父元素高度相同可实现单行文本元素垂直居中 --> <style type="text/css"> #parent { width: 400px; height: 400px; } #parent p { line-height: 400px; background-color: red; } </style> <div id="parent"> <p>子元素非正常流定宽1</p> </div>
-
使用 line-height 和 vertical-align 对图片进行垂直居中
demo演示:
<!-- 父元素设置line-height与其高度相同,子元素再设置vertical-align: middle实现图片垂直居中 --> <style type="text/css"> #parent { width: 400px; height: 400px; line-height: 400px; } #parent img { vertical-align: middle; } </style> <div id="parent"> <img src="./img/logo.png" alt="JingDong icon"> </div>
参考css垂直居中
6.水平垂直居中
-
使用flex
demo演示:
<!-- 父元素使用flex布局,然后设定align-items: center;使其子元素水平居中,再定义justify-content: center;使其垂直居中,最后实现水平垂直居中。 --> <style type="text/css"> #parent { width: 400px; height: 400px; background-color: yellow; display: flex; align-items: center; justify-content: center; } #child { background-color: red; } </style> <div id="parent"> <div id="child">水平垂直居中测试</div> </div> <!-- 父元素使用flex布局,然后设定子元素nargin:auto,最后实现水平垂直居中。 --> <style type="text/css"> #parent { width: 400px; height: 400px; background-color: yellow; display: flex; } #child { background-color: red; margin: auto; } </style> <div id="parent"> <div id="child">水平垂直居中测试</div> </div>
-
使用grid
demo演示:
<!-- 与flex布局类似 --> <style type="text/css"> #parent { width: 400px; height: 400px; background-color: yellow; display: grid; } #child { background-color: red; align-self: center; justify-self: center; } </style> <div id="parent"> <div id="child">水平垂直居中测试</div> </div> <!-- 与flex布局类似 --> <style type="text/css"> #parent { width: 400px; height: 400px; background-color: yellow; display: grid; } #child { background-color: red; margin:auto; } </style> <div id="parent"> <div id="child">水平垂直居中测试</div> </div>
-
使用absolute+transform
demo演示:
<!-- 于上述方法里结合水平居中与垂直居中,实现水平垂直居中 --> <style type="text/css"> #parent { width: 400px; height: 400px; background-color: yellow; position: relative; } #child { background-color: red; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style> <div id="parent"> <div id="child">水平垂直居中测试</div> </div>
7.遇到问题
出了点意外情况,用Typro做笔记,结果没有保存却退出了。解决办法见typro文件未保存找回。
简单记录一下,div+css居中问题,面试经常问。