Home  >  Article  >  Web Front-end  >  Introduction to css box model

Introduction to css box model

一个新手
一个新手Original
2017-09-11 10:32:041819browse


Box model

1 Area in the box

f35d6e602fd7d0f0edfa6f7d103c1b57Main properties of the box:
width 宽度,css中width指的是内容的宽度,而不是盒子的宽度。 
height高度 ,css中Height指的是内容的高度,而不是盒子的高度。 
padding 内边距 
border 边框 
margin 外边距
d26dbe428205a1f17fbdff8e2091dd7eIf you want to keep the actual occupied width of a box unchanged, then increase the width and reduce the padding , adding padding means reducing width.

2. Understand padding

f35d6e602fd7d0f0edfa6f7d103c1b57The padding area has a background color. In CSS2.1, the background color must be the same as the content area.
2cc198a1d5eb0d3eb508d858c9f5cbdbPadding is in 4 directions, so we can describe padding in 4 directions respectively.

The first type: small attributes, that is, composite attributes.

padding-top:30px;  上padding-right:20px; 右padding-bottom:40px; 下padding-left:100px; 左

The second type: comprehensive attributes.
Separated by spaces, top, right, bottom and left.

padding:30px 20px 40px 100px;
5bdf4c78156c7953567bb5a0aef2fc53You can use small attributes to stack large attributes (you cannot write small attributes in front of large attributes):
padding:20px;padding-left:30px;

Question 1:

p{   
width:200px;    
height:200px;    
padding-left:10px;    
padding-right:20px;    
padding:40px 50px 60px;    
padding-bottom:30px;    
border:1px  solid #000;
}

Answer: padding -left:10px; and padding-right:20px; are useless, because the subsequent padding attribute overlays them.

23889872c2e8594e0f446a471a78ec4cSome tags have padding by default. For example, ul. So, when we build the website, we will clear this default padding.

3.border border

f35d6e602fd7d0f0edfa6f7d103c1b57The three elements of the border: thickness, line type, and color.
2cc198a1d5eb0d3eb508d858c9f5cbdbAll line types:
none    定义无边框。 
hidden  与 “none” 相同。不过应用于表时除外,对于表,hidden 用于解决边框冲突。 
dotted  定义点状边框。在大多数浏览器中呈现为实线。 
dashed  定义虚线。在大多数浏览器中呈现为实线。 
solid   定义实线。 
double  定义双线。双线的宽度等于 border-width 的值。 
groove  定义 3D 凹槽边框。其效果取决于 border-color 的值。 
ridge   定义 3D 垄状边框。其效果取决于 border-color 的值。 
inset   定义 3D inset 边框。其效果取决于 border-color 的值。 
outset  定义 3D outset 边框。其效果取决于 border-color 的值。 
inherit 规定应该从父元素继承边框样式。 
常用的有:solid 、dashed、 dotted
5bdf4c78156c7953567bb5a0aef2fc53The border attribute can be split, there are two major ways to split it:

First Type: By element

border-width:10px;  
边框宽度border-style:solid;    
线型border-color:red;      
颜色等价于:border:10px solid red;

If a small element is followed by multiple values ​​separated by spaces, then the order is top, right, bottom, left:

border-width:10px 20px;border-style:solid dashed dotted;border-color:red  green blue yellow;

Second type: By direction
The first method of disassembly:

border-top:10px solid red;border-right:10px solid red;border-bottom:10px solid red;border-left:10px solid red;等价于:border:10px solid red

The second method of disassembly is to dismantle each element in each direction:

border-top-width:10px;border-top-style:solid;border-top-color:red;border-right-width:10px;border-right-style:solid;border-right-color:red;border-bottom-width:10px;border-bottom-style:solid;border-bottom-color:red;border-left-width:10px;border-left-style:solid;border-left-color:red;等价于:border:10px solid red;
23889872c2e8594e0f446a471a78ec4c You can use the border to make a triangle.
<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>三角形</title>
    <style type="text/css">
        p{            
        width: 0px;            
        height: 0px;            
        border: 30px solid white;            
        border-top-color: pink;            
        transition:all 1s ease 0s;        
        }
        p:hover{            
            transform: rotate(180deg);        
            }
    </style>
    </head>
    <body>
    <p>
    </p>
    </body>
    </html>

4. Standard document flow

4.1 Block-level elements and inline elements

f35d6e602fd7d0f0edfa6f7d103c1b57From a macro perspective, web pages and design software such as PS are essentially different Avoid:
Web page production, from top to bottom. With design software, you can draw wherever you want.
2cc198a1d5eb0d3eb508d858c9f5cbdbMicroscopic properties of standard flow:
(1) Blank folding phenomenon.
(2) The height is uneven, and the bottom edges are aligned.
(3) Automatically wrap the line. If you can’t finish writing in one line, wrap it in a new line.
5bdf4c78156c7953567bb5a0aef2fc53Block-level elements and inline elements
(1) Tags are divided into two levels: block-level elements and inline elements.
(2) Block-level elements:

霸占一行,不能与其他任何元素并列。
能接受宽高。
如果不设置宽度,那么宽度将默认变为父亲的100%。

(3) Inline elements:

可以与其他行内元素并排。
不能设置宽高。默认的宽度,就是文字的宽度。

(4) Labels are divided into: text level and container level.

文本级:p、span、a、b、i、u、em
容器级:p 、h系列 、li 、dt 、dd

Basically all text-level tags are inline elements. Except for p, it is a block-level element.
All container-level tags are block-level elements.

4.2 Conversion between block-level elements and inline elements

f35d6e602fd7d0f0edfa6f7d103c1b57Block-level elements can be set to inline elements. Inline elements can be set as block-level elements.
2cc198a1d5eb0d3eb508d858c9f5cbdbdisplay is used to change the inline and block-level properties of elements.

display:inline; 这个标签将会变为行内元素。
display:block; 这个标签将会变为块级元素。

5bdf4c78156c7953567bb5a0aef2fc53There are three methods in CSS to separate an element from the standard document flow.
(1) Floating
(2) Absolute positioning
(3) Fixed positioning

5. Floating: It is the most commonly used attribute for layout in CSS.

5.1 Floating elements are off-script
5.2 Floating elements are close to each other

f35d6e602fd7d0f0edfa6f7d103c1b57If there is enough space, they will be close to the second brother. If there is not enough space, he will lean on Brother Yi. If there is not enough space to lean against Brother 1, stick to the left wall yourself.
2cc198a1d5eb0d3eb508d858c9f5cbdbfloat:left/right;

5.3 Floating elements have a "word circumference" effect

The above is the detailed content of Introduction to css box model. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn