search
HomeWeb Front-endCSS TutorialIntroduction to css box model

Introduction to css box model

Sep 11, 2017 am 10:32 AM
cssIntroduction


Box model

1 Area in the box

Main properties of the box:
width 宽度,css中width指的是内容的宽度,而不是盒子的宽度。 
height高度 ,css中Height指的是内容的高度,而不是盒子的高度。 
padding 内边距 
border 边框 
margin 外边距
.box1{
    width:100px;
    height:100px;
    padding:100px;
    border:1px solid red;}
计算如下:1+100+100+100+1=302px.box2{
    width:250px;
    height:250px;
    padding:25px;
    border:1px solid red;}
计算如下:1+25+250+25+1=302px
上面代码中盒子的真实占有宽度计算公式:
真实占有宽度=左border+左padding+width+右padding+右border
If 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

The padding area has a background color. In CSS2.1, the background color must be the same as the content area.
Padding 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;
You 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.

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

3.border border

The three elements of the border: thickness, line type, and color.
All 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
The 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;
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

From 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.
Microscopic 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.
Block-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

Block-level elements can be set to inline elements. Inline elements can be set as block-level elements.
display is used to change the inline and block-level properties of elements.

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

There 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

If 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.
float: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
Two Images and an API: Everything We Need for Recoloring ProductsTwo Images and an API: Everything We Need for Recoloring ProductsApr 15, 2025 am 11:27 AM

I recently found a solution to dynamically update the color of any product image. So with just one of a product, we can colorize it in different ways to show

Weekly Platform News: Impact of Third-Party Code, Passive Mixed Content, Countries with the Slowest ConnectionsWeekly Platform News: Impact of Third-Party Code, Passive Mixed Content, Countries with the Slowest ConnectionsApr 15, 2025 am 11:19 AM

In this week's roundup, Lighthouse sheds light on third-party scripts, insecure resources will get blocked on secure sites, and many country connection speeds

Options for Hosting Your Own Non-JavaScript-Based AnalyticsOptions for Hosting Your Own Non-JavaScript-Based AnalyticsApr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

It's All In the Head: Managing the Document Head of a React Powered Site With React HelmetIt's All In the Head: Managing the Document Head of a React Powered Site With React HelmetApr 15, 2025 am 11:01 AM

The document head might not be the most glamorous part of a website, but what goes into it is arguably just as important to the success of your website as its

What is super() in JavaScript?What is super() in JavaScript?Apr 15, 2025 am 10:59 AM

What's happening when you see some JavaScript that calls super()?.In a child class, you use super() to call its parent’s constructor and super. to access its

Comparing the Different Types of Native JavaScript PopupsComparing the Different Types of Native JavaScript PopupsApr 15, 2025 am 10:48 AM

JavaScript has a variety of built-in popup APIs that display special UI for user interaction. Famously:

Why Are Accessible Websites so Hard to Build?Why Are Accessible Websites so Hard to Build?Apr 15, 2025 am 10:45 AM

I was chatting with some front-end folks the other day about why so many companies struggle at making accessible websites. Why are accessible websites so hard

The `hidden` Attribute is Visibly WeakThe `hidden` Attribute is Visibly WeakApr 15, 2025 am 10:43 AM

There is an HTML attribute that does exactly what you think it should do:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.