第 16 章 CSS 盒模型[上] - 水之原
学习要点:
1.元素尺寸
2.元素内边距
3.元素外边距
4.处理溢出
主讲教师:李炎恢
本章主要探讨 HTML5 中 CSS 盒模型,学习怎样了解元素的外观配置以及文档的整体布局。
一.元素尺寸
CSS 盒模型中最基础的就是设置一个元素的尺寸大小。有三组样式来配置一个元素的尺寸大小,样式表如下:
属性 |
值 |
说明 |
CSS 版本 |
width |
auto、长度值或百分比 |
设置元素的宽度 |
1 |
height |
auto、长度值或百分比 |
设置元素的高度 |
1 |
min-width |
auto、长度值或百分比 |
设置元素最小宽度 |
2 |
min-height |
auto、长度值或百分比 |
设置元素最小高度 |
2 |
max-width |
auto、长度值或百分比 |
设置元素最大宽度 |
2 |
max-height |
auto、长度值或百分比 |
设置元素最大高度 |
2 |
//设置元素尺寸
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 200px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 200px</span>; }
解释:设置元素的固定尺寸。
//限制元素尺寸
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> min-width</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> min-height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> max-width</span>:<span style="color: #0000ff;"> 300px</span>;<span style="color: #ff0000;"> max-height</span>:<span style="color: #0000ff;"> 300px</span>; }
解释:这一组主要是应对可能动态产生元素尺寸变大变小的问题,从而限制它最大和最小的值。
//auto 自适应
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> auto</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> auto</span>; }
解释:auto 是默认值,width 在 auto 下是 100%的值;height 在 auto 下是自适应。
//百分比方式
<span style="color: #800000;">#a </span>{<span style="color: #ff0000;"> background</span>:<span style="color: #0000ff;"> silver</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 200px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 200px</span>; }<span style="color: #800000;"> #b </span>{<span style="color: #ff0000;"> background</span>:<span style="color: #0000ff;"> gray</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 80%</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 80%</span>; }
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="a"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="b"</span><span style="color: #0000ff;">></span><span style="color: #000000;"> 我是 html5 </span><span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span>
解释:百分比就是相对于父元素长度来衡定的。
二.元素内边距
CSS 盒模型中可以设置元素内部边缘填充空白的大小,我们成为内边距。样式表如下:
属性 |
值 |
说明 |
CSS 版本 |
padding-top |
长度值或百分比 |
设置顶部内边距 |
1 |
padding-bottom |
长度值或百分比 |
设置底部内边距 |
1 |
padding-left |
长度值或百分比 |
设置左边内边距 |
1 |
padding-right |
长度值或百分比 |
设置右边内边距 |
1 |
padding |
1 ~ 4 个长度值或百分比 |
简写属性 |
1 |
//设置四个内边距
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> padding-top</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> padding-bottom</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> padding-left</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> padding-right</span>:<span style="color: #0000ff;"> 10px</span>; }
//简写形式,分别为上 10px、右 10px、下 10px、左 10px
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 10px 10px 10px 10px</span>; }
//简写形式,分别为上 10px,左右 50px,下 200px
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 10px 50px 200px</span>; }
//简写形式,分别是上下 10px,左右 20px
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 10px 20px</span>; }
//简写形式:上下左右均 10px
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 10px</span>; }
三.元素外边距
CSS 盒模型中可以设置元素外部边缘填充空白的大小,我们成为外边距。样式表如下:
属性 |
值 |
说明 |
CSS 版本 |
margin-top |
长度值或百分比 |
设置顶部内边距 |
1 |
margin-bottom |
长度值或百分比 |
设置底部内边距 |
1 |
margin-left |
长度值或百分比 |
设置左边内边距 |
1 |
margin-right |
长度值或百分比 |
设置右边内边距 |
1 |
margin |
1 ~ 4 长度值或百分比 |
简写属性 |
1 |
//设置四个内边距
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> margin-top</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> margin-bottom</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> margin-left</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> margin-right</span>:<span style="color: #0000ff;"> 10px</span>; }
//简写形式,分别为上 10px、右 10px、下 10px、左 10px
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 10px 10px 10px 10px</span>; }
//简写形式,分别为上 10px,左右 50px,下 200px
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 10px 50px 200px</span>; }
//简写形式,分别是上下 10px,左边 20px
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 10px 20px</span>; }
//简写形式:上下左右均 10px
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 10px</span>; }
四.处理溢出
当设置了元素固定尺寸且内容过大时,就会出现溢出的问题。溢出主要朝两个方向:右侧和底部。我们可以通过 overflow 系列样式来控制它。
溢出处理主要有四种处理值:
//设置溢出为 auto 值
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> overflow-x</span>:<span style="color: #0000ff;"> auto</span>; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit
