Display & Position
Display :
Every element on a web page is a rectangular box. The Display propertyin CSS determines just how that rectangular box behaves. This property defines how the components (div, hyperlink, heading etc.) are going to be placed on the web page.
There are only a handful of values that are commonly used:
- display: inline;
- display: inline-block;
- display: block;
- display: none;
1) Inline : The inline element takes the required width only. It does not start on a new line and only takes up as much width as necessary. We can change other block-level elements to behave as inline element by declaring display: inline property.
Examples of inline elements: <span>, <a>, <img>,<select>,<button> etc.
Example :
This will produce the following result −
2) Block : The CSS display block element takes as much as horizontal space as they can. Means the block element takes the full available width. They make a line break before and after them.
Examples of block-level elements : <p>, <h1>- <h6>, <ul>, <ol>, <div>,<header> etc.
Example :
This will produce the following result −
3) Inline-Block : The CSS display inline-block element is very similar to inline element but the difference is that you are able to set the width and height for element.
Example :
This will produce the following result −
4) None : The display: none value totally removes the element from the page. It will not take any space.
Example :
This will produce the following result −
Note : 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 affects the layout.
Position :
The CSS position property sets how an element is positioned in a document.
The different position values are:
- static
- fixed
- sticky
- relative
- absolute
You can position an element using the top, bottom, left and right properties. These properties can be used only after position property is set first.
Example :
This will produce the following result −