Table of Contents
Box model
1. Box Model
2. Box border (border)
The spacing between the border and the content
The margin is the control between the box and the box Distance
5.盒子阴影
浮动
1. 浮动是什么
2. 浮动使用
3. 浮动与其他盒子关系
4.清除浮动
Home Web Front-end CSS Tutorial Summary of the use of CSS standard box model and floating

Summary of the use of CSS standard box model and floating

Aug 03, 2022 am 10:17 AM
css

This article brings you relevant knowledge about css, which mainly introduces the issues related to the box model and floating. The essence of web page layout is to place the box model through css layout. Then put the required elements in the appropriate position. Let’s take a look at it. I hope it will be helpful to everyone.

Summary of the use of CSS standard box model and floating

(Learning video sharing: css video tutorial, html video tutorial)

Box model

The essence of web page layout is to place the box model in the appropriate position through css layout, and then add the required elements

1. Box Model

Box Model It consists of element content, inner margin, outer margin, and border

Standard box model
Summary of the use of CSS standard box model and floating

2. Box border (border)

border : border-width || border-style || border-color
Copy after login
##border-widthDefine the border Thickness, unit pxborder-styleDefinition of border styleborder-colorDefinition Border color
Attribute Function

Border style

##none: No border
  • solid: Solid border
  • dashed:dotted border
  • dotted:dotted border
  • Separately set the style of one side of the border
 border-top-style:样式; border-top-width:宽度;border-top-color:颜色;border-top:宽度 样式 颜色;
Copy after login

bottom,left,right

And so on

Table border

By

cellspacing=0

The cell distance is 0, but the distance between two cells Overlapping borders will make the borders thicker. Use the css attribute <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">table{border-collapse:collapse}</pre><div class="contentsignin">Copy after login</div></div> to merge the borders without making them thicker.

Rounded border

border-radius:length;/*length 可以数字也可以百分比*/
Copy after login
The square box will become thicker. It is circular, and rectangles usually use half the height

3. Padding

The spacing between the border and the content


Summary of the use of CSS standard box model and floating

Attributepadding-leftpadding-rightpadding-toppadding-bottom can also be abbreviated as
Function
Left padding
Padding-right
Padding-top
Lower padding

Number of values1234
Meaning
padding: top, bottom, left and right padding
padding: up, down, left and right
padding: up, left, right, down
padding: top right, bottom left (clockwise)
Inner box size calculation

    Width
  • Element Height = content height padding border (Height is the content height)

  • Height
  • Element Width = content width padding border (Width is the content width)

  • The actual size of the box = the width and height of the content padding border
When width is not given directly, padding will not open the box, otherwise it will

4. Margin (margin)

The margin is the control between the box and the box Distance

Attributemargin-leftmargin-rightmargin-topmargin-bottom
  • 同样类似于padding有类似的写法

块级盒子水平居中

盒子必须指定宽度,然后左右外边距设置为auto

.nav{width:960px;margin:0 auto;}
Copy after login

常见的写法,以下下三种都可以。

  • margin-left: auto; margin-right: auto;
  • margin: auto;
  • margin: 0 auto;

文字居中与盒子居中的区别

  • 盒子内的文字水平居中是 text-align: center, 而且还可以让 行内元素和行内块居中对齐

  • 块级盒子水平居中 左右margin 改为 auto

插入图片与背景图片区别

  • 插入图片移动位置只能靠 padding与margin

  • 背景图则使用background-position

清楚元素内外边距

* {
	padding:0;
	margin:0;
}
Copy after login

外边距合并

  • 当上下相邻的两个块元素相遇时,如果上面的元素有下外边距margin-bottom

  • 下面的元素有上外边距margin-top,则他们之间的垂直间距不是margin-bottom与margin-top之和

  • 取两个值中的较大者这种现象被称为相邻块元素垂直外边距的合并(也称外边距塌陷)。
    Summary of the use of CSS standard box model and floating

解决方案:尽量给只给一个盒子添加margin值

嵌套块元素垂直外边距的合并

对于父子块元素,父元素会与子元素合并,父元素没有上padding和边框,合并时外边距取较大值
Summary of the use of CSS standard box model and floating

解决方案:

  • 可以为父元素定义上边框。

  • 可以为父元素定义上内边距

  • 可以为父元素添加overflow:hidden。

区分出父子元素

盒子布局稳定性

width>padding>margin
Copy after login

稳定性依次减小,padding会撑开盒子,margin在合并时也会有问题

5.盒子阴影

box-shadow:水平阴影 垂直阴影 模糊距离(虚实)  阴影尺寸(影子大小)  阴影颜色  内/外阴影;/*单位px*/
Copy after login
Function
Left margin
Right margin
Top margin
Bottom margin
描述
h-shadow 必需,水平阴影,负值在左边
v-shadow 必需,垂直阴影,负值在下
blur 可选,模糊距离
spread 可选,阴影尺寸
color 可选,阴影颜色
inset 可选,内阴影

注意

水平垂直阴影必须,其余可以省略,外阴影(outset)默认不写

p {
	box-shadow:0 15px 20px rgba(0,0,0,.5)
}
Copy after login

浮动

1. 浮动是什么

css布局的三种机制

1. 普通

  • 块级元素独占一行,由上到下排列
  • 行内元素从左到右依次排列,父元素换行

2. 浮动

使得盒子浮动起来。让多个块级盒子一行展示

3. 定位

将盒子定位于浏览器中的某一位置

2. 浮动使用

通过浮动可以使得多个p水平排列一行,且之间没有空白缝隙,实现左右对齐,最早使用于图片,实现文字环绕效果

选择器 {float: 值;}
Copy after login
属性值 描述
none 不浮动(默认)
left 元素向左浮动
right 元素向右浮动

注意

浮动托标使用浮动后,元素会脱离标准流,后续的标准流会移动至浮动盒子底下,浮动元素会“漂浮”

Summary of the use of CSS standard box model and floating

浮动会改变 元素display属性,任何元素都可以浮动,浮动元素相互紧靠,父级宽度装不下时,多出盒子调至下一行

浮动与标准流搭配

给浮动元素添加一个标准流父亲,在子元素使用浮动,从而较少对其他标准流的影响

3. 浮动与其他盒子关系

  • 浮动元素与父盒子关系

子盒子会与父盒子对齐,但不会与边框重叠,也不会超过父盒子的内边距

  • 浮动元素与兄弟盒子关系

浮动元素只会影响当前以及后面的标准流盒子

如果一个盒子里面有多个子盒子,如果其中一个盒子浮动了,其他兄弟也应该浮动。防止引起问题

4.清除浮动

很多子元素浮动后,父元素很难直接给出高度,最后父级盒子高度为0,对于以后的标准流盒子会有影响,对于标准流,子盒子会撑开盒子,而浮动不会。

清除就是为了消除浮动布局对于后续排版影响,主要是解决父元素高度为0的问题

选择器 {clear: 属性值;}
Copy after login
属性值 描述
left 不允许左侧有浮动元素(消除左侧浮动影响)
right 不允许右侧有浮动元素(消除右侧浮动影响)
both 同时清除左右浮动元素

额外标签法

是W3C推荐的做法是通过在浮动元素末尾添加一个空的标签例如 <p></p>,或则其他标签br等亦可。
Copy after login
  • 优点: 通俗易懂,书写方便

  • 缺点: 添加许多无意义的标签,结构化较差。

父级添加overflow属性

overflow : hidden|auto|scroll
Copy after login

缺点: 内容增多时候容易造成不会自动换行导致内容被隐藏掉,无法显示需要溢出的元素。

使用after为元素清除浮动

:after方式是额外标签升级方式

.clearfix:after{ 
	content:""; display:block;height: 0;clear:both; visibility: hidden;
}

.clearfix {*zoom: 1;}
Copy after login

 

  • 优点: 符合闭合浮动思想 结构语义化正确
  • 缺点: 由于IE6-7不支持:after,使用 zoom:1触发 hasLayout。

双伪元素

.clearfix:before,.clearfix:after { 
  content:"";
  display:table; 
}
.clearfix:after {
 clear:both;
}
.clearfix {
  *zoom:1;
}
Copy after login

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

The above is the detailed content of Summary of the use of CSS standard box model and floating. For more information, please follow other related articles on the PHP Chinese website!

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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

See all articles