How many types of css box models are there?

青灯夜游
Release: 2021-10-12 16:08:39
Original
16093 people have browsed it

There are two types of css box models, namely: 1. W3c standard box model (standard box model), width and height refer to the width and height of the content area; 2. IE standard box model (Weird box model), width and height refer to the total width and height of the content area, borders, and padding.

How many types of css box models are there?

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

Box Model

The CSS box model is a thinking model used by CSS technology that is often used in web design. The Box Model can be used to lay out elements, including padding, borders, margins, and actual content.

Area in the box

There are only 5 main attributes in a box: width, height, padding, border, and margin. As follows:

  • width and height:The width and height of the content(not the width and height of the box).
  • padding: padding.
  • border: border.
  • margin: Margin.

Schematic diagram of the box model:

Code demonstration:

The box above , width:200px; height:200px; But the actual width and height occupied are 302*302. This is because padding and border need to be added.

Note:Width and real occupied width are not the same concept!Look at the example below.

Standard box model and IE box model

The box model is divided into two types:

  • The first is the W3c standard box model (standard box Model)

  • The second IE standard box model (weird box model)

In the knowledge we have learned so far, Based on the standard box model.

Standard box model:

IE box model:

The above picture shows :

The CSS Box Model (Box Model) stipulates several ways for elements to be processed:

  • width and height:The width and height of the content( Not the width and height of the box).
  • padding: padding.
  • border: border.
  • margin: Margin.

The difference between CSS box model and IE box model:

  • Instandard box model,width and height refer to the width and height of the content area. Increasing padding, borders, and margins will not affect the size of the content area, but it will increase the overall size of the element's box.

  • In the IE box model,width and height refer to the content area border paddingThe width and height.

Note: Android also has the concepts of margin and padding, which have similar meanings. If you know a little bit about Android, it should be easier to understand. The difference is that there is no such thing as border in Android, and in Android, margin is not part of the control. I think this is more reasonable, haha.

tags also have margin

tags need to be emphasized. Many people think that thetag occupies the entire area of the entire page. This is actually wrong. The correct understanding is this: the largest box on the entire web page is, which is the browser. Andis the son of. The default margin size given by the browser tois 8 pixels. At this time,occupies a large area of the entire page, not the entire area. Let’s look at a piece of code.

        Document  
有生之年
狭路相逢
Copy after login

In the above code, we set margins and other information on the div tag. Open Google Chrome and hold down F12. The display effect is as follows:

Understanding width and height

You must know that in the eyes of front-end development engineers, in the world Everything is different.

For example, when measuring the manuscript paper, the front-end development engineer will only measure the content width:

The actual width and height of the two boxes below are both 302 *302:

Box 1:

.box1{ width: 100px; height: 100px; padding: 100px; border: 1px solid red; }
Copy after login

盒子2:

.box2{ width: 250px; height: 250px; padding: 25px; border: 1px solid red; }
Copy after login

真实占有宽度 = 左border + 左padding + width + 右padding + 右border

上面这两个盒子的盒模型图如下:

如果想保持一个盒子的真实占有宽度不变,那么加width的时候就要减padding。加padding的时候就要减width。因为盒子变胖了是灾难性的,这会把别的盒子挤下去。

认识padding

padding区域也有颜色

padding就是内边距。padding的区域有背景颜色,css2.1前提下,并且背景颜色一定和内容区域的相同。也就是说,background-color将填充所有border以内的区域。

效果如下:

padding有四个方向

padding是4个方向的,所以我们能够分别描述4个方向的padding。

方法有两种,第一种写小属性;第二种写综合属性,用空格隔开。

小属性的写法:

padding-top: 30px; padding-right: 20px; padding-bottom: 40px; padding-left: 100px;
Copy after login

综合属性的写法:(上、右、下、左)(顺时针方向,用空格隔开。margin的道理也是一样的)

padding:30px 20px 40px 100px;
Copy after login

如果写了四个值,则顺序为:上、右、下、左。

如果只写了三个值,则顺序为:上、右、下。??和右一样。

如果只写了两个值,比如说:

padding: 30px 40px;
Copy after login

则顺序等价于:30px 40px 30px 40px;

要懂得,用小属性层叠大属性。比如:

padding: 20px; padding-left: 30px;
Copy after login

上面的padding对应盒子模型为:

下面的写法:

padding-left: 30px; padding: 20px;
Copy after login

第一行的小属性无效,因为被第二行的大属性层叠掉了。

(学习视频分享:css视频教程

The above is the detailed content of How many types of css box models are there?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!