Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does display:block; do to an element?

Tags:

css

What are the exact specs of display:block? I know quite a few of the specific things applying this to an element will do but I would like to hear them all.

Thanks.

like image 323
Sam152 Avatar asked Mar 11 '09 09:03

Sam152


People also ask

Why do we use display block in HTML?

Elements in HTML are mostly "inline" or "block" elements: An inline element has floating content on its left and right side. A block element fills the entire line, and nothing can be displayed on its left or right side. The display property also allows the author to show or hide an element.

What is the work of display block in CSS?

The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex. Formally, the display property sets an element's inner and outer display types.

What does display block do for an inline element?

The display: inline-block Value Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.

What is used to display a block element?

Two commonly used block elements are: <p> and <div> . The <p> element defines a paragraph in an HTML document. The <div> element defines a division or a section in an HTML document. The <p> element is a block-level element.


2 Answers

Quote from W3C

In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block. The vertical distance between two sibling boxes is determined by the 'margin' properties. Vertical margins between adjacent block boxes in a block formatting context collapse.

In a block formatting context, each box's left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch). This is true even in the presence of floats (although a box's content area may shrink due to the floats).

like image 200
Ólafur Waage Avatar answered Oct 23 '22 12:10

Ólafur Waage


Autistic Cuckoo's article about display properties and their differences (part 1, part 2, part 3) is also a recommended read.

like image 42
kkyy Avatar answered Oct 23 '22 14:10

kkyy