"CSS3"---box model margin, padding and border

Source: Internet
Author: User
Tags border color dashed line

Box model--border

The border of the box model is the line around the content and the filter, which you can set its thickness, style and color (border three properties).

The following code is div to set the border thickness to 2px, the style is solid, the color is red border:

Div {    border:2px  solid  red;}

The above is an abbreviated form of the border code, which can be written separately:

Div {    border-width:2px;     border-style:solid;     border-color:red;}


1. Border-style (border Style) common styles are:

Dashed (dashed line) | Dotted (point line) | Solid (solid line).

2. Colors in Border-color (border color) can be set to hexadecimal colors, such as:

Border-color: #888;//Don't forget the well in front.

3, the width of the border-width (border width) can also be set to:

Thin | Medium | Thick (but not very common), most often with pixels (px).

Box Model--fill

You can set the distance between the element content and the border, which is called "padding". Fills can also be divided into top, right, bottom, and left (clockwise). The following code:

Div {padding:20px 10px 15px 30px;}

The order must not be confused. You can write the above code separately:

Div {   padding-top:20px;    padding-right:10px;    padding-bottom:15px;    padding-left:30px;}

If the top, right, bottom, and left fills are 10px; you can write this.

Div {padding:10px;}

If the upper and lower fills are the same as 10px, about the same as 20px, you can write:

Div {padding:10px 20px;}
Box model--boundary

The distance between the element and other elements can be set using the boundary (margin). The boundary can also be divided into upper, right, lower and left. The following code:

Div {margin:20px 10px 15px 30px;}

You can also write separately:

Div {   margin-top:20px;    margin-right:10px;    margin-bottom:15px;    margin-left:30px;}

If the upper-right bottom-left boundary is 10px, you can write this:

Div { margin:10px;}

If the upper and lower bounds are the same as 10px, and about the same 20px, you can write:

Div { margin:10px 20px;}

Summary: The difference between padding and margin, padding in the border, margin outside the border.

Box model--width and height

The width and height of the box model is not the same as the width and height of the object we normally speak of, the width (width) and high (height) defined within the CSS, which refers to the content range filled in.

So the actual width of an element (width of box) = left border + Left box + Left padding + content width + right padding + right Border + right edge.

The height of the element is also the same.

Like what:

CSS code:

Div {    width:200px;     padding:20px;     Border:1px solid red;     margin:10px;    }

HTML code:

< Body >   < Div > text content </div></body>

The actual length of the element is: 10px+1px+20px+200px+20px+1px+10px=262px. The element box model can be viewed under Chrome browser, such as:

"CSS3"---box model margin, padding and border

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.