Block-level elements and row-level elements in CSS

Source: Internet
Author: User

Recently, when learning CSS, block-level elements and row-level elements are sometimes confused, write a blog to record their understanding of it.

First of all, from a conceptual point of view:

Block-level elements

Features: 1. Each block-level element is a single row, followed by an element that can only be another row, and cannot have two elements that share a single row.

2. The height, width, row height, and top and bottom margins of the element can be set.

   3. If the width of the element is not set, the default is the width of the parent element.

Common block-level elements:<div>, <p>,

Row-level elements

Features: 1. You can be in a row with other elements without having to start another line.

2. The height, width, and top and bottom margins of the element are not set.

3. The width of the element is the width of the text and the image it contains, and it cannot be changed.

Say a half-day concept for an example to illustrate:

<HTML><Head>    <title>Difference</title>    <styletype= "Text/css">. Div1{Background-color:Red;        }. Span1{Background-color:Yellow;        }    </style></Head><Body><Divclass= "Div1">I'm a block-level element. 1</Div>
<div class= "Div1" > I am a block-level element 2</div>

<class= "Span1"> I am a row-level element 1</span>
<span class= "Span1" > I'm a row-level element 2</span>
</ Body > </ HTML >

The results are as follows:

If you can clearly see that the block-level element directly takes up a row and the width is the same as the width of the parent element, the row-level element can occupy two lines together, and the width of the row-level element and the text within it.

Conversion of row-level elements to block-level elements

What if you want to convert block-level and row-level elements to each other?

You can use Display:inline to set block-level elements as row-level elements in CSS styles

Similarly, you can use Display:block to set row-level elements as block-level elements

The code examples are as follows:

<HTML><Head>    <title></title>    <styletype= "Text/css">. Div1{Background-color:Red;Display:inline;; }. Span1{Background-color:Yellow;Display:Block;        }    </style></Head><Body><Divclass= "Div1">I'm a block-level element. 1</Div><Divclass= "Div1">I'm a block-level element. 2</Div><spanclass= "Span1">I'm a row-level element 1</span><spanclass= "Span1">I'm a row-level element 2</span></Body></HTML>

The results obtained

Visible, the original block-level elements are shown in order to row-level elements, the original row-level elements are shown for block-level elements.

Row-level-block-level elements

What if you want to set the height, width, row height, and top and bottom margins, as well as the element in one row?

You can now set the element as a row-level-block-level element with Display:inline-block.

Block-level elements and row-level elements in CSS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.