|
|
|
|
|
|
|
|
|
|
|
|
CSS Display |
|
The display property is the most important CSS property for
controlling layout. |
|
|
Block-level Elements |
<div> |
|
The display property specifies if/how an element is displayed. |
|
|
<h1> - <h6> |
|
Every HTML element has a default display value depending on
what type of element it is. The default display value for most elements
is block or inline. |
|
|
<p> |
|
A block-level element always starts on a new line and takes
up the full width available (stretches out to the left and right as far as it
can). |
|
|
<form> |
|
|
<header> |
|
|
<footer> |
|
|
<section> |
|
|
|
|
|
|
|
|
|
|
|
Inline Elements |
<span> |
|
An inline element does not start on a new line and only takes
up as much width as necessary. |
|
|
<a> |
|
This is an inline <span> element inside a
paragraph. |
|
|
<img> |
|
|
<video> |
|
Every element has a default display value. However, you can
override this. |
|
|
|
|
Changing an inline element to
a block element, or vice versa, can be useful for making the page look a
specific way, and still follow the web standards. |
|
|
|
|
A common example is making
inline <li> elements
for horizontal menus: |
|
|
|
|
<head> |
|
|
|
|
<style> |
|
|
li { |
|
|
display: inline; |
|
|
} |
|
|
display: |
inline; |
default |
Default value. Displays an element as an inline element (like
<span>) |
</style> |
|
|
block; |
|
Displays an element as a block element (like <p>) |
</head> |
|
|
none; |
|
The element will not be displayed at all (has no effect on
layout) |
<body> |
|
|
inline-block; |
|
Displays an element as an inline-level block container. |
<p>Display a list of links as a horizontal
menu:</p> |
|
|
The inside of this block is formatted as block-level box, and
the element itself is formatted as an inline-level box |
<ul> |
|
|
list-item; |
|
Let the element behave like a <li> element |
<li><a
href="/html/default.asp"
target="_blank">HTML</a></li> |
|
|
run-in; |
|
Displays an element as either block or inline, depending on
context |
<li><a
href="/css/default.asp"
target="_blank">CSS</a></li> |
|
|
inline-flex; |
|
Displays an element as an inline-level flex container. New in
CSS3 |
<li><a
href="/js/default.asp"
target="_blank">JavaScript</a></li> |
|
|
table; |
|
Let the element behave like a <table> element |
</ul> |
|
|
table-caption |
|
Let the element behave like a <caption> element |
</body> |
|
|
table-caption |
|
Above code shows a horizontal menu. If remove "display: inline;" or
use "display: block;" then, it results a vertical menu. |
|
|
table-column-group |
|
Note: Setting
the display property of an element only changes how the element is displayed, NOT
what kind of element it is. So, an inline element with display: block;is not allowed to
have other block elements inside it. |
|
table-header-group |
|
|
|
|
… |
|
The following example displays <span> elements as block
elements: |
|
|
<head> |
|
|
<style> |
|
|
span { |
|
|
display:
block; |
|
|
border: 2px
orange solid; |
|
|
} |
|
|
</style> |
|
|
<body> |
|
|
<span> A display property with a value of
"block" results in </span> |
|
|
<span>a line break between the two elements.</span> |
|
|
</body> |
|
|
The "display: block;" makes
<span>…</span> have the same effect as <p>…</p> and
show two lines.Otherwise, only one line will be displayed. The added
"border" makes this more clear. |
|
|
|
Hiding an element can be done by setting the display property to none. The element will be hidden,
and the page will be displayed as if the element is not there |
|
|
<head> |
|
|
<style> |
|
|
h2.hidden { |
|
|
display: none; |
|
|
} |
|
|
</style> |
|
|
<head> |
|
|
<body> |
|
|
<h1>This is a visible heading</h1> |
|
|
<h2 class="hidden">This is a hidden heading</h2> |
|
|
<p>Notice that the h2 element with display: none; does not take up
any space.</p> |
|
|
</body> |
|
|
|
visibility:hidden; also hides an
element. However, the element will
still take up the same space as before. The element will be hidden, but still
affect the layout |
|
|
|
h2.hidden { |
|
|
visibility: hidden; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|