display:inline、block、inline-block的区别
display:block就是将元素显示为块级元素.
block元素的特点是:
总是在新行上开始;
高度,行高以及顶和底边距都可控制;
宽度缺省是它的容器的100%,除非设定一个宽度
<div>, <p>, <h1>, <form>, <ul> 和 <li>是块元素的例子。
display:inline就是将元素显示为行内元素.
inline元素的特点是:
和其他元素都在一行上;
高,行高及顶和底边距不可改变;
宽度就是它的文字或图片的宽度,不可改变。
<span>, <a>, <label>, <input>, <img>, <strong> 和<em>是inline元素的例子。
inline和block可以控制一个元素的行宽高等特性,需要切换的情况如下:
让一个inline元素从新行开始;
让块元素和其他元素保持在一行上;
控制inline元素的宽度(对导航条特别有用);
控制inline元素的高度;
无须设定宽度即可为一个块元素设定与文字同宽的背景色。
display:inline-block将对象呈递为内联对象,但是对象的内容作为块对象呈递。旁边的内联对象会被呈递在同一行内,允许空格。
inline-block的元素特点:
将对象呈递为内联对象,但是对象的内容作为块对象呈递。旁边的内联对象会被呈递在同一行内,允许空格。(准确地说,应用此特性的元素呈现为内联对象,周围元素保持在同一行,但可以设置宽度和高度地块元素的属性)
并不是所有浏览器都支持此属性,目前支持的浏览器有:Opera、Safari在IE中对内联元素使用display:inline-
block,IE是不识别的,但使用display:inline-block在IE下会触发layout,从而使内联元素拥有了
display:inline-block属性的表症。从上面的这个分析,也不难理解为什么IE下,对块元素设置display:inline-
block属性无法实现inline-block的效果。这时块元素仅仅是被display:inline-block触发了layout,而它本就是行
布局,所以触发后,块元素依然还是行布局,而不会如Opera中块元素呈递为内联对象。
IE下块元素如何实现display:inline-block的效果?
有两种方法:
1、先使用display:inline-block属性触发块元素,然后再定义display:inline,让块
元素呈递为内联对象(两个display要先后放在两个CSS声明中才有效果,这是IE的一个经典bug,如果先定义了display:inline-
block,然后再将display设回inline或block,layout不会消失)。代码如下(...为省略的其他属性内容):
div {display:inline-block;...}
div {display:inline;}
2、直接让块元素设置为内联对象呈递(设置属性display:inline),然后触发块元素的layout(如:zoom:1等)。代码如下:
div {display:inline; zoom:1;...}
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>display:inline、block、inline-block的区别</title>
</head>
<style>
div, span {
background-color: palevioletred;
margin: 0px;
border: 1px solid #333;
padding: 0px;
height: 52px;
color: #fff;
} .b {
display: block;
} .i {
display: inline;
} .ib {
display: inline-block;
} </style>
<body>
<div class="i">div display:inline</div>
<div class="ib">div display:inline-block</div>
<div class="b">div display:block</div> <span class="i">span display:inline</span>
<span class="b">span display:block</span>
<span class="ib">span display:inline-block</span> </body> 结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnEAAAEUCAIAAADLPvcbAAAN/klEQVR4nO3dXXLjOrIA4dlTb+zuZZbnxeg+OFqhIQiwABQhCf1l5IMkU0WQdDOP3D/nPw8AAJDBf969AAAANkFTAQDIQVM35w8wzbu/i4Gv4aSp7/71ixFqF/jPnz8///dfctjGdxeAA+dNffsvY3apqbxPTQXiaOoOairvU1OBOJq6g5rK+9RUIE6oqb+v157Oe8f85+Hcd6/pmt9Yz/w6u5rqan741RxYYcr5qU3QVCBO9HPq48678H3z09cZvMd1rWf+eHs/p7qaKVfhpqs5fAbm93s6QVOBOCNNXX+z+LSxn7aemaZ+0WH+I1dzeI/z6zmdoKlAHE2NzkkZddNhamrvnE++msN7nF/P6QRNBeJcNPX19cMr5U2qvU3j7hZ872GdrzfHxlsGdlqu4fKQD5vVjuL0ld455fqDTb08cFfzE65m+wzPn5/Gak9PmqYCcVpNfZz9YmvcTS5/eTfeMjz/d83Bd7Xn9K6/Nr/ruHrnnA6MNNXV/Jar+dz+QOL5qT0+Ha6pQJxqUx/N28fj7G7yqP9qv7zvXM6vzRxY5+GeNbb+gbtY/CgabyyvV/uu52p+3dW87zxfnpPH33K/vqipQJyvaerP1a1zYJ21x5H1997FIvey4bvh1zXV1exd29h6evf7vByvG2gqECe/qadfmr871N7eO6exQdf6B+a372Uzd8P7mupqrr+aiee5d7+nG2gqECf0+6m/r4/dTSI3iMj82gaP4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