Home > Web Front-end > HTML Tutorial > display attribute in CSS: the difference between block, inline and inline-block_html/css_WEB-ITnose

display attribute in CSS: the difference between block, inline and inline-block_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:48:58
Original
1150 people have browsed it

I recently encountered a problem when writing HTML: I want to set the width and height of span, but it always doesn't work under IE9 and chrome. The code and renderings are as follows:

<head>	 <style>		 span{			background-color:#43be60;			width:100px;			height:50px;			margin-top:20px;			margin-left:20px;		 }	 </style></head><body>	<div style="background-color:#ededed;width:400px;height:400px;">		 <span>1</span>		 <span>10</span>		 <span>100</span>		 <span>1000</span>	</div></body>
Copy after login



You can see that the width and height set by css have no effect, and the actual span Display width and height are determined by its content. Why is this happening? This involves the difference between block-level elements (block) and row-level elements (inline) in HTML. For example, div is a common block-level element, and span is a common row-level element. We can use the display attribute of CSS to set whether an element is a block-level or row-level element. display:block sets the element to block level, and display:inline sets the element to line level.


display:block features:

1. The block element will occupy its own line, and multiple block elements will each start a new line. By default, the width of a block element automatically fills the width of its parent element.
2. The block element can set width and height attributes. Even if the width of a block-level element is set, it still occupies an exclusive line.
3. The block element can set margin and padding attributes.


display:inline features:

1. Inline elements will not occupy one line, multiple adjacent inline elements will Arranged in the same row, until one row cannot fit, a new row will be added, and its width will change with the content of the element.
2. The width and height attributes set for inline elements are invalid.
3. The margin and padding attributes of inline elements, the horizontal padding-left, padding-right, margin-left, and margin-right all produce margin effects; but the vertical padding-top, padding-bottom, margin-top, margin-bottom will not produce margin effects.


Since span is a row-level element, its width and height cannot be set; if span is set to block, it will automatically wrap. How to display multiple spans on the same line and have a fixed width? This requires the use of display:inline-block. The code and renderings are as follows:

<head>	 <style>		 span{			background-color:#43be60;			width:100px;			height:50px;			margin-top:20px;			margin-left:20px;			display:inline-block;		 }	 </style></head><body>	<div style="background-color:#ededed;width:400px;height:400px;">		 <span>1</span>		 <span>10</span>		 <span>100</span>		 <span>1000</span>	</div></body>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template