CSS Box Model

Source: Internet
Author: User


1. Element classification
In CSS, the tag elements in HTML are broadly divided into three different types:block element, inline element (also called inline Element), and inline block element.

the most common block elements are:
<div>, <p>,


the commonly used inline elements are:
<a>, <span>, <br>, <i>, <em>, <strong>, <label>, <q>, <var>, <cite>, <code>


the commonly used inline block elements are:
, <input>


2, block-level elements
in HTML <div>, <p>,


A{display:block;}
block-level element features:


(1) Each block-level element begins with a new row, and the subsequent element also starts another line. (True overbearing, one block-level element exclusive row)
(2) The height, width, row height, and top and bottom margins of the element can be set.
(3) If the element width is not set, it is 100% of its own parent container (and the width of the parent element is the same) unless a width is set.


3. Inline Elements
in HTML,,<span>, <a>, <label>, <strong>, and <em> are typical inline elements (inline elements). Of course, block elements can also be set to inline elements through code display:inline. The code below is to convert the block element div into an inline element, so that the DIV element has an inline element feature.


div{
Display:inline;
 }


......

<div> I want to become an inline element </div>
Inline element Features:

(1) and other elements are on one 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 or Picture it contains, and it cannot be changed.


3. Inline block elements
Inline block Elements (Inline-block) are features that have inline elements, block elements, and code display:inline-block that are set to inline block elements. (css2.1 New),, <input> tag is this inline block tag.


Inline-block Elemental Features:


(1) and other elements are on one line;


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


4, 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;
}
Note:
(1), Border-style (border style) common styles are:

dashed (dashed line) | dotted (dot) | solid (solid).

(2), the color in the Border-color (border color) can be set to hex color, 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).

(4), CSS styles allow the style to be set only for a border of One Direction:

div{border-bottom:1px solid red;}
You can also use the following code to implement other three-side (top, right, left) border settings:


border-top:1px solid red;
border-right:1px solid red;
border-left:1px solid red;


5. 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.



6, 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;}


7, 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.

CSS Box Model

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.