W3C-Box model
- 格式:wps
- 大小:265.05 KB
- 文档页数:26
什么叫盒⼦模型?有⼏种模式?什么是盒⼦模型?把所有的⽹页元素都看成⼀个盒⼦,它具有: content,padding,border,margin 四个属性,这就是盒⼦模型。
盒⼦模型有⼏种模式?1、W3C标准盒⼦模型2、IE盒⼦模型W3C标准盒⼦模型标准模式下,⼀个块的宽度 = width+padding(内边距)+border(边框)+margin(外边距);W3C模型中: CSS中的宽(width)=内容(content)的宽 CSS中的⾼(height)=内容(content)的⾼IE盒⼦模型怪异模式下,⼀个块的宽度 = width+margin(外边距) (即怪异模式下,width包含了border以及padding)。
IE模型中: CSS中的宽(width)=内容(content)的宽+(border+padding)*2 CSS中的⾼(height)=内容(content)的⾼+(border+padding)*2CSS3 box-sizingbox-sizing:content-box||border-box||inheritbox-sizing:content-box; 将采⽤标准模式的盒⼦模型标准box-sizing:border-box; 将采⽤怪异模式的盒⼦模型标准box-sizing:inherit; 规定应从⽗元素继承 box-sizing 属性的值。
总结IE5.5及更早的版本使⽤的是IE盒模型。
IE6及其以上的版本在标准兼容模式下使⽤的是W3C的盒模型标准。
我们说这是⼀个好消息因为这意味着此盒模型问题只会出现在IE5.5及其更早的版本中。
只要为⽂档设置⼀个DOCTYPE,就会使得IE遵循标准兼容模式的⽅式⼯作。
我们知道ie会在一段时间内仍然流行,但是我们仍然可以支持浏览器并且避免hacks和条件css吗?这里有十条使用有效的html和css代码修复可以兼容ie6常见的问题。
1、使用一个声明你必须经常在html网页头部放置一个声明,推荐使用严格的标准。
例如或者xhtml使用最后你需要是ie6进入兼容模式,这已经足够兼容的了。
2、使用position: relative设置一个标签position: relative可以解决很多问题,特别是曾经有过看不见的经历或者奇怪布局的框架。
明显的,你需要小心,绝对位置放置的子元素是否都参照找到新位置。
3、为浮动元素使用display:inline浮动元素会有一个著名的ie6双边距margin bug。
假如你设置了左边距5px但实际上得到了10px左边距。
display:inline可以解决这个问题,尽管它不是必需的,但是css仍然有效。
4、设置元素启动hasLayout大部分ie6(ie7)的渲染问题都可以通过起来元素的hasLayout属性来兼容。
这是ie内置的设定,确定一个内容块相对其它内容块是有界限和位置的。
当你需要设置一个行内元素例如一个连接变成块状元素或者是透明效果,设置hasLayout也是必须的。
5、修复重复字符的bug复杂的布局会触发一个bug:浮动对象的最后字符会出现在已经清除浮动的元素后面。
这里有几种解决的办法,部分是理想的,并且一些测试和出错是必须的。
a、确保浮动元素都使用:display:inline;b、最后一个浮动元素使用margin-right:-3px;c、在浮动对象最后一个元素后使用一个条件注释。
例如这里输入注释…<![endif]d、在容器内的最后使用一个div空标签(它也必须设置90%宽度甚至更小)6、使用a标签完成可点击和hover原理Ie6只支持a标签的css定义hover效果你可以使用它去控制javascript启动的widgets,使得他们仍然保持键盘操作。
CSS 框模型概述CSS 框模型 (Box Model) 规定了元素框处理元素内容、内边距、边框 和 外边距 的方式。
规定了元素框处理元素内容、内边距、 的方式。
一、CSS 框模型概述元素框的最内部分是实际的内容,直接包围内容的是内边距。
内边距呈现了元素的背景。
内边距的边缘是 边框。
边框以外是外边距,外边距默认是透明的,因此不会遮挡其后的任何元素。
提示: 提示:背景应用于由内容和内边距组成的区域。
内边距、边框和外边距都是可选的,默认值是零。
但是,许多元素将由用户代理样式表设置外边距和内边 距。
可以通过将元素的 margin 和 padding 设置为零来覆盖这些浏览器样式。
这可以分别进行,也可以 使用通用选择器对所有元素进行设置:* {margin: 0; padding: 0; }在 CSS 中,width 和 height 指的是内容区域的宽度和高度。
增加内边距、边框和外边距不会影响内容 区域的尺寸,但是会增加元素框的总尺寸。
假设框的每个边上有 10 个像素的外边距和 5 个像素的内边距。
如果希望这个元素框达到 100 个像素, 就需要将内容的宽度设置为 70 像素,请看下图:#box { width: 70px; margin: 10px; padding: 5px; }提示: 提示:内边距、边框和外边距可以应用于一个元素的所有边,也可以应用于单独的边。
提示: 提示:外边距可以是负值,而且在很多情况下都要使用负值的外边距。
浏览器兼容性 浏览器兼容性一旦为页面设置了恰当的 DTD,大多数浏览器都会按照上面的图示来呈现内容。
然而 IE 5 和 6 的呈现 却是不正确的。
根据 W3C 的规范, 元素内容占据的空间是由 width 属性设置的, 而内容周围的 padding 和 border 值是另外计算的。
不幸的是,IE5.X 和 6 在怪异模式中使用自己的非标准模型。
这些浏览器 的 width 属性不是内容的宽度,而是内容、内边距和边框的宽度的总和。
盒⼦模型详解盒⼦模型盒⼦模型,英⽂即box model。
⽆论是div、span、还是a都是盒⼦。
但是,图⽚、表单元素⼀律看作是⽂本,它们并不是盒⼦。
这个很好理解,⽐如说,⼀张图⽚⾥并不能放东西,它⾃⼰就是⾃⼰的内容。
⼀个盒⼦中主要的属性就5个:width、height、padding、border、margin。
如下:width和height:内容的宽度、⾼度(不是盒⼦的宽度、⾼度)。
padding:内边距。
border:边框。
margin:外边距。
盒⼦模型的⽰意图:代码演⽰:上⾯这个盒⼦,width:200px; height:200px; 但是真实占有的宽⾼是302*302。
这是因为还要加上padding、border。
注意:宽度和真实占有宽度,不是⼀个概念!来看下⾯这例⼦。
我们⽬前所学习的知识中,以标准盒⼦模型为准。
标准盒⼦模型:IE盒⼦模型:上图显⽰:在 CSS 盒⼦模型 (Box Model) 规定了元素处理元素的⼏种⽅式:width和height:内容的宽度、⾼度(不是盒⼦的宽度、⾼度)。
padding:内边距。
border:边框。
margin:外边距。
CSS盒模型和IE盒模型的区别:在标准盒⼦模型中,width 和 height 指的是内容区域的宽度和⾼度。
增加内边距、边框和外边距不会影响内容区域的尺⼨,但是会增加元素框的总尺⼨。
IE盒⼦模型中,width 和 height 指的是内容区域+border+padding的宽度和⾼度。
注:Android中也有margin和padding的概念,意思是差不多的,如果你会⼀点Android,应该⽐较好理解吧。
区别在于,Android中没有border这个东西,⽽且在Android中,margin并不是控件的⼀部分,我觉得这样做更合理⼀些,呵呵。
<body>标签有必要强调⼀下。
很多⼈以为<body>标签占据的是整个页⾯的全部区域,其实是错误的,正确的理解是这样的:整个⽹页最⼤的盒⼦是<document>,即浏览器。
margin属性margin在中文中我们翻译成外边距或者外补白(本文中引用外边距)。
他是元素盒模型(box model)的基础属性。
一、margin的基本特性margin属性包括margin-top,margin-right,margin-bottom,margin-left,margin,可以用来设置box的margin area。
属性margin可以用来同时设置box的四边外边距,而其他的margin属性只能设置其自各的外边距。
margin属性可以应用于几乎所有的元素,除了表格显示类型(不包括table-caption, table and inline-table)的元素,而且垂直外边距对非置换内联元素(non-replaced inline element)不起作用。
或许有朋友对非置换元素(non-replaced element)有点疑惑,稍微帮助大家理解一下。
非置换元素,W3C中没有给出明确的定义,但我们从字面可以理解到,非置换元素对应着置换元素(replaced element),也就是说我们搞懂了置换元素的含义,就懂了非置换元素。
置换元素,W3C中给出了定义:引用:“An element that is outside the scope of the CSS formatter, such as an image, embedded docu ment, or applet”从定义中我们可以理解到,置换元素(replaced element)主要是指img,input,textarea,select,object等这类默认就有CSS格式化外表范围的元素。
进而可知,非置换元素(non-replaced element)就是除了img,input,textarea,select,object等置换元素以外的元素。
margin始终是透明的。
二、margin的基本写法外边距的margin-width的值类型有:auto | length | percentagepercentage:百分比是由被应用box的containing block(注:一个元素的containing block 是该元素产生的box(es)在计算位置和大小时参考的一个矩形,详细阅读可看:《Containing Block》)的大小所决定。
epub的制作--CSS的使用方式一、前言什么是CSS? 先看一下同一个epub在不同的阅读器上的呈现效果。
同一个档案,在书苑中的阅读器中和calibre中的呈现方式。
由于繁体字的笔画较多,在书苑中的阅读经验并不是很好。
因此,适当的加大字与字、行与行之间的距离是有必要的。
请再检查calibre中的阅读效果。
测试用的档案中又加上了背景颜色、文字颜色的设定,虽然书苑中的阅读器并不能呈现这种效果,但是在支持较多的CSS阅读器上,你可以看到变化的情形。
这就是CSS,用来设定文字、图片…在epub上的呈现方式。
目前,epub书籍中的样式设定是藉由CSS(cascading style sheets, 连接样式表)来设定的。
CSS,由W3C (world wideweb consortium)所公布,主要的用途是在加强网页中文字控制及版面编辑,它是用来扩充 HTML 的功能。
由于epub 仍是网页的格式,因此,CSS语法也可以使用在epub书籍的排版上。
但是,目前CSS在epub的应用上,仍是以段落样式为主,主要是设定边界、首行缩排、行距、文字对齐..等基本的排版功能,使用上仍有其不足。
甚至于,在现在的epub 书籍中,能找到具有文绕图效果的eupb书籍还是不容易。
这并不是开发水平的问题,而是目前的阅读器对CSS的支持仍有限。
但是,随着epub3标准的普及,未来的epub书籍,不仅对CSS有更大的支持性,而且,在epub上,也能利用javaScript、jQuery等语法来设计互动的效果。
因此,在未来的epub书籍中,大量的使用CSS语法是指日可待的。
CSS的学习并不容易,因为它包含了大量的语法。
幸运的是,如果你使用支持intelliSense功能的编辑器,例如:DreamWeaver,撰写CSS并不是难事。
因此,大量的属性并不需要你去记忆…相反的,在开始学习CSS时,你要将重点放在使用的方式上,这才是学习的重点。
border-box——⼀种改变盒⼦尺⼨的⽅法语法:box-sizing:content-box|border-box|padding-boxIE6中的传统盒⼦模型: CSS样式声明的 width 和 hieght 包含 content、padding 和 border。
这就是 border-box 盒⼦模型。
W3C 的盒⼦模型是 content-box,即CSS样式声明的⾼度和宽度是 content 的尺⼨。
padding-box 盒⼦样式声明中的⾼度是和宽度是 paddin和content的宽度和⾼度。
如何⽤好他们?W3C的盒⼦是反⾃觉的,也就说我们把⼀样东西放到⼀个盒⼦⾥,再把盒⼦放到仓库⾥,东西占的尺⼨是盒⼦的尺⼨,⽽不是东西的尺⼨。
border-box 不反⾃觉。
很多现代框架如bootstrap 就是这么处理的。
流式布局和border-box:这个属性⼴泛⽤于流式布局,尤其是以百分数为基础的布局。
想象⼀个场景:导航栏 有5个项⽬,每个项⽬占 20% 的宽度,加上⼀条⽤于分割他们的固定尺⼨的边框,1nav li{2width:20%;3display:inline-block;4border-left:0.25em solid #fff;5 }那个第五个项⽬将换⾏。
为了避免它换⾏,我们告诉浏览器⽤ border-box 去计算盒⼦宽度。
nav li {box-sizing:border-box;width:20%;display:inline-block;border-left:0.25em solid #fff;}⽤ border-box 统⼀表单控件的尺⼨不同浏览器对表单控件的处理有的⽤ content-box,有的⽤ border-box,如input type="sumbmit" VS select。
⽤ border-box 就统⼀处理了。
padding-box 很不常见,只有FF⽀持。
css属性列表和属性值含义css属性列表属性名称字体属性(Font)font-familyfont-stylefont-variantfont-weightfont-size颜色和背景属性ColorBackground-color Background-image Background-repeat Background-attachment Background-position文本属性Word-spacingLetter-spacingText-decoration Vertical-alignText-transformText-alignText-indentLine-height边距属性Margin-topMargin-rightMargin-bottomMargin-leftMargin 属性含义使用什么字体字体是否斜体是否用小体大写字体的粗细字体的大小定义前景色定义背景色定义背景图案重复方式设置滚动初始位置单词之间的间距字母之间的间距文字的装饰样式垂直方向的位置文本转换对齐方式首行的缩进方式文本的行高分别设置上右下左,一个值属性值所有的字体Normal、italic、obliqueNormal、small-capsNormal、bold、bolder、lithter等Absolute-size、relative-size、length、percentage等颜色颜色路径Repeat-x、repeat-y、no-repeatScroll、FixedPercentage、length、top、left、right、bottom等Normal <length>同上None|underline|overline|line-through|blinkBaseline|sub|super|top|text-top|middle|bottom|text-bottom|Capitalize|uppercase|lowercase|noneLeft|right|center|justify<length>|<percentage>Normal|<number>|<length>|<percentage>Length|percentage|auto同上同上同上填充距属性Padding-top Padding-right Padding-bottom Padding-left Padding边框属性Border-top-width Border-right-width Border-bottom-width Border-left-width Border-width Border-color Border-style Border-topBorder-rightBorder-bottom Border-leftWidthHeightFloatClear分级属性DisplayWhite-spaceList-style-typeList-style-imageList-style-position List-style鼠标(Cursor)属性设置全部顶端边距右侧边距底端边距左侧边距同Margin顶端填充距右侧填充距底端填充距左侧填充距顶端边框宽度右侧边框宽度底端边框宽度左侧边框宽度一次定义宽度设置边框颜色设置边框样式一次定义顶端一次定义右侧一次定义底端一次定义左侧定义宽度属性定义高度属性文字环绕哪一边环绕定义是否显示怎样处理空白加项目编号加图案第二行起始位置一次定义列表自动定位“十”字默认指针Length|percentage同上同上同上同MarginThin|medium|thick|length同上同上同上同上ColorNone|dotted|dashed|dash|solid|double|groove|ridge|inset|outset等Border-top-width|color等同上同上同上Length|percentage|autoLength|autoLeft|right|noneLeft|right|none|bothBlock、inline、list-item、noneNormal、pre、nowrapDisc、circle、square等<url>|noneInside、outside<keyword>|<position> |<url>AutoCrosshairDefaultHandMovee-resizeNe-resizeNw-resizen-resize手形移动箭头朝右方箭头朝右上方箭头朝左上方箭头朝上方箭头朝右下方箭头朝左下方箭头朝下方箭头朝左方文本“I”形等待帮助Se-resize Sw-resize s-resize w-resize TextWait HelpCSS属性:1、媒体(Media)类型样式单的一个最重要的特点就是它可以作用于多种媒体,比如页面、屏幕、电子合成器等等。
Doctype严格模式与混杂模式-如何触发这两种模式,区分它们有何意义?Doctype:(Document Type)⽂档类型,它位于⽂档中最前⾯的位置,处于标签之前。
如果你想制作符合标准的页⾯,⼀个必不可少的关键组成部分就是DOCTYPE的声明。
确定了正确的Doctype,xhtml⾥⾯的标识和css才能正常⽣效(也就是说它会很负责地告诉浏览器怎么解释你的这个标签和你写的css)。
既然它是⼀种声明,它的责任就是告诉浏览器⽂档使⽤哪种html或者xhtml规范。
为了获取正确的Doctype声明,关键就是让dtd与⽂档所遵循的标准对应。
例如:假定⽂档遵循的是xhtml 1.0 strict,那么⽂档的Doctype声明就应该是相应的dtd,如果Doctype声明指定的是xhtml dtd,但⽂档包含的依旧是html标记就是不恰当的。
类似的,如果⽂档包含的是xhtml 1.0 strict标记,但是Doctype声明指定是html dtd也是不恰当的。
⼀、选择什么样的DOCTYPE? xhtml 1.0中有3种dtd声明可以选择,过渡性的(Transitional)、严格的(Strict)、框架的(Frameset)。
下⾯我们来分别介绍: 1、过渡的:⼀种要求不很严格的,允许在html中使⽤html 4.01的标识(符合xhtml语法标准),过渡的dtd写法如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2、严格的:⼀种要求严格的dtd,不允许使⽤任何表现层的标识和属性,严格的dtd写法如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "/TR/xhtml1/DTD/xhtml1-strict.dtd"> 3、框架的:⼀种专门针对框架页⾯所使⽤的dtd,当页⾯中含有框架元素时,就要采⽤这种dtd,写法如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-frameset.dtd"> PS:使⽤严格的dtd来制作页⾯当然是最理想的⽅式,但是对于还没有深⼊了解web标准的⽹页设计者,⽐较适合⽤过渡的dtd,因为这种dtd还允许使⽤表现层的标识、元素和属性,⽐较适合⼤多数的⽹页制作⼈员(当然量⼒⽽为了!)⼆、什么是标准模式与混杂模式?不同⽂档模式主要影响CSS内容的呈现,尤其是浏览器对盒模型的解析,但在某些情况下也会影响到JavaScript的解释执⾏。
css3发展历史CSS3是CSS技术的一个版本,是CSS2的后继版本。
CSS3的发展历史可以分为以下几个阶段:1. CSS2.1CSS2.1是CSS技术的一个重要版本。
它于2006年6月被W3C发布,该版本主要进行了一些修订和纠错的工作,并对CSS2做了一些增强。
CSS2.1的发布为CSS3的发展打下了坚实的基础。
2. CSS3 SelectorsCSS3 Selectors是CSS3中的一个模块,用于增强CSS选择器的功能。
这个模块分为多个小组件,包括基础选择器、属性选择器、伪类选择器等等。
CSS3 Selectors的增强为CSS3的发展奠定了基础。
3. CSS3 Media QueriesCSS3 Media Queries是CSS3中的另一个模块,它允许开发者针对设备的不同屏幕尺寸和分辨率来指定不同的样式规则。
这项技术在响应式网站开发中得到广泛的应用。
4. CSS3 Box ModelCSS3 Box Model是CSS3中的一个模块,它提出了一种新的盒子模型,能够更好地处理边框、内边距和外边距等属性的计算。
这项技术为开发者提供了更加方便的布局方式。
5. CSS3 Backgrounds and BordersCSS3 Backgrounds and Borders是CSS3中的一个模块,它提供了一些新的背景和边框的样式属性,包括多重背景、边框图片、圆角边框等等。
这项技术使得网页设计变得更加美观。
6. CSS3 Transitions and AnimationsCSS3 Transitions和Animations是CSS3中的一个模块,它们允许开发者在元素属性变化时制作平滑的动画效果。
这项技术使得网页动画效果更加丰富多彩,吸引人眼球。
7. CSS3 FontsCSS3 Fonts是CSS3中的一个模块,它为网页设计师提供了更多的字体选择。
这项技术在设计过程中提供了更多的创作可能性。
总之,CSS3是CSS技术的一个重要版本,它对CSS2进行了增强和改进。
W3C标准W3C(World Wide Web Consortium)是一个国际标准制定组织,致力于促进互联网技术的发展和标准化。
W3C标准是互联网的基石,广泛应用于网页设计、网站开发以及互联网相关的各个领域。
本文将介绍W3C标准的概念、分类和重要性。
概念W3C标准指的是由W3C制定和推广的一系列规范和指南,这些标准旨在统一互联网技术的开发和实现方式,确保不同平台和设备间的互操作性和兼容性。
W3C标准通常采用开放的、非专有的方式制定,以促进技术的普及和创新。
分类根据领域和功能的不同,W3C标准可以分为以下几类:1.HTML和CSS标准:HTML(Hypertext MarkupLanguage)和CSS(Cascading Style Sheets)是构建网页的基础技术。
W3C制定了HTML和CSS的规范,定义了网页的结构、样式和布局,确保不同浏览器对网页的解析和渲染结果一致。
2.DOM标准:DOM(Document Object Model)是描述网页文档结构和元素之间关系的标准模型。
W3C制定了DOM标准,使开发者可以通过操作DOM树来动态修改网页内容和行为。
3.XML标准:XML(eXtensible Markup Language)是一种标记语言,用于描述数据结构和语义。
W3C制定了XML的规范,使数据在不同平台和应用间的传输和共享变得更加方便和可靠。
4.Web服务标准:Web服务是一种基于互联网的软件系统。
W3C制定了Web服务相关的标准,定义了协议和接口,使得不同系统和应用之间可以进行互联互通。
5.移动Web标准:移动互联网的发展使得移动设备上的Web应用越来越重要。
W3C制定了一系列移动Web 标准,包括响应式设计、高效的图片和视频压缩,以及针对移动设备的交互和导航优化等。
重要性W3C标准的重要性体现在以下几个方面:1.保证互操作性:W3C标准的制定和推广能够使不同平台和设备之间实现互操作,确保内容在不同环境中的一致性和可访问性。
css盒⼦模型(BoxModel)css盒⼦模型(Box Model)盒⼦的组成⼀个盒⼦由外到内可以分成四个部分:margin(外边距)、border(边框)、padding(内边距)、content(内容)。
会发现margin、border、padding是CSS属性,因此可以通过这三个属性来控制盒⼦的这三个部分。
⽽content则是HTML元素的内容。
盒⼦的⼤⼩盒⼦的⼤⼩指的是盒⼦的宽度和⾼度。
⼤多数初学者容易将宽度和⾼度误解为width和height属性,然⽽默认情况下width和height属性只是设置content(内容)部分的宽和⾼。
盒⼦真正的宽和⾼按下⾯公式计算:盒⼦的宽度 = 内容宽度 + 左填充 + 右填充 + 左边框 + 右边框 + 左边距 + 右边距盒⼦的⾼度 = 内容⾼度 + 上填充 + 下填充 + 上边框 + 下边框 + 上边距 + 下边距为了显得专业⼀点,我们还可以⽤带属性的公式表⽰:盒⼦的宽度 = width + padding-left + padding-right + border-left + border-right + margin-left + margin-right盒⼦的⾼度 = height + padding-top + padding-bottom + border-top + border-bottom + margin-top + margin-bottom上⾯说到的是默认情况下的计算⽅法,另外⼀种情况下,width和height属性设置的就是盒⼦的宽度和⾼度。
盒⼦的宽度和⾼度的计算⽅式由box-sizing属性控制。
box-sizing属性值content-box:默认值,width和height属性分别应⽤到元素的内容框。
在宽度和⾼度之外绘制元素的内边距、边框、外边距。
border-box:为元素设定的width和height属性决定了元素的边框盒。
2009-03-22 04:55图解CSS的padding,margin,border属性(详细介绍及举例说明)图解CSS的padding,margin,border属性W3C组织建议把所有网页上的对像都放在一个盒(box)中,设计师可以通过创建定义来控制这个盒的属性,这些对像包括段落、列表、标题、图片以及层。
盒模型主要定义四个区域:内容(content)、边框距(padding)、边界(border)和边距(margin)。
对于初学者,经常会搞不清楚margin,background-color,background-image,padding,content,border之间的层次、关系和相互影响。
这里提供一张盒模型的3D示意图,希望便于你的理解和记忆。
∙margin:层的边框以外留的空白∙background-color:背景颜色∙background-image:背景图片∙padding:层的边框到层的内容之间的空白∙border:边框∙content:内容padding、margin两个重要属性的详细介绍及举例说明本文将讲述HTML和CSS的关键—盒子模型(Box model). 理解Box model的关键便是margin和padding属性, 而正确理解这两个属性也是学习用css布局的关键.注: 为什么不翻译margin和padding? 原因一, 在汉语中并没有与之相对应的词语; 原因二: 即使有这样的词语, 由于在编写css代码时, 必须使用margin 和padding, 如果我们总用汉语词语代替其来解释的话, 到了实际应用时容易混淆margin和padding的概念.如果有一点Html基础的话, 就应该了解一些基本元素(Element), 如p, h1~h6, br, div, li, ul, img等. 如果将这些元素细分, 又可以分别归为顶级(top-level)元素,块级(block-level)元素和内联(inline)元素.1. Block-level element: 指能够独立存在, 一般的块级元素之间以换行(如一个段落结束后另起一行)分隔. 常用的块级元素包括: p, h1~h6, div, ul等;2. Inline element: 指依附其他块级元素存在, 紧接于被联元素之间显示, 而不换行. 常用的内联元素包括: img, span, li, br等;3. Top-level element: 包括html, body, frameset, 表现如Block-level element, 属于高级块级元素.块级元素是构成一个html的主要和关键元素, 而任意一个块级元素均可以用Box model来解释说明.Box Model: 任意一个块级元素均由content(内容), padding, background(包括背景颜色和图片), border(边框), margin五个部分组成. 立体图如下(Fig.1):该立体图引自:/url.asp?url=/ (Under the Creative Commons License)平面图如下(Fig. 2):根据以上两图, 相信大家对于Box model会有个直观的认识.以下说明margin和padding属性:1. Margin: 包括margin-top, margin-right, margin-bottom, margin-left, 控制块级元素之间的距离, 它们是透明不可见的, 对于Fig. 2所示的上右下左margin值均为40px, 因此代码为:margin-top: 40px;margin-right: 40px;margin-bottom: 40px;margin-left: 40px;根据上, 右, 下, 左的顺时针规则, 简写为margin: 40px 40px 40px 40px;为便于记忆, 请参考下图:当上下, 左右margin值分别一致, 可简写为:margin: 40px 40px;前一个40px代表上下margin值, 后一个40px代表左右margin值.当上下左右margin值均一致, 可简写为:margin: 40px;2. Padding: 包括padding-top, padding-right, padding-bottom,padding-left, 控制块级元素内部, content与border之间的距离, 其代码, 简写请参考margin属性的写法.至此, 我们已经基本了解margin和padding属性的基本用法. 但是, 在实际应用中, 却总是发生一些让你琢磨不透的事, 而它们又或多或少的与margin有关.注: 当你想让两个元素的content在垂直方向(vertically)分隔时, 既可以选择padding-top/bottom, 也可以选择margin-top/bottom, 再此Jorux建议你尽量使用padding-top/bottom来达到你的目的, 这是因为css中存在Collapsing margins(折叠的margins)的现象.Collapsing margins: margins折叠现象只存在于临近或有从属关系的元素, 垂直方向的margin中. 文字说明可能让人费解, 下面用一个例子说明margin-collapsing现象.例: 在html文件的<body></body>之间写入如下代码:<div id=”ID1″><h1 id=”ID2″>Margins of ID1 and ID2 collapse vertically.<br/>元素ID1与ID2的margins在垂直方向折叠.</h1></div>在与其外联的css文件中写入:* {padding:0;margin:0;}#ID1 {background-color: #333;color: #FFF;margin-top: 10px;margin-bottom: 10px;}#ID2 {font: normal 14px/1.5 Verdana, sans-serif;margin-top: 30px;margin-bottom: 30px;border: 1px solid #F00;}代码解释:1. 在html写入的代码表示, 在html中插入id分别为ID1和ID2的两个块级元素div, h1;2. *{padding:0; margin:0;}: 使浏览器默认的元素padding和margin值均归零;3. #ID1{…}: 使id为ID1的元素div的背景颜色为#333, 字体颜色为#FFF, margin-top/bottom为10px;4. #ID2{…}: 使id为ID2的元素h1的字体大小为14px, verdana字体, 行高为字体高的150%, 正常粗细. margin-top/bottom为30px, 边框为1px宽, 红色实线.依据以上解释, 我们应该得到如下效果(Fig. 3):即ID1的margin-top/bottom=ab=ef=10px;ID2的margin-top/bottom=bc=de=30px;但用浏览器打开html文件, 却得到Example4的效果, 如下图(Fig. 4):即ab=cd=30px, ID1的margin-top/bottom=10px被折叠了, 而且ID1应有的margin黑色背景也一同被折叠消失了.为什么会折叠: 造成以上现象的原因是, 我们在css中并没有声明id为ID1的元素div的height(高), 因此它的高便被设为auto(自动)了. 一旦其值被设为auto, 那么浏览器就会认为它的高为子元素ID2的border-top到border-bottom之间的距离, 即Fig. 4中bc的长度, 所以子元素ID2的margin-top/bottom(30px)就伸出到了父元素ID1之外, 出现了Fig. 4中ab与cd之间的空白区域. 因此父元素ID1的margin-top/bottom因子元素的”红杏出墙”而被折叠消失了.如何解决折叠问题: 可能大家最初想到的办法就是根据折叠发生的原因—auto, 来解决问题. 但是, 在实际操作中, 某些元素如div, h1, p等, 我们是不可能预先知道它的高是多少的, 因此在css文件中是不能常规通过声明元素的高来解决折叠问题.我们需要在css文件中加入如下代码(红色部分):#ID1 {background-color: #333;color: #FFF;margin-top: 10px;margin-bottom: 10px;padding-top:1px;padding-bottom:1px;}或是:#ID1 {background-color: #333;color: #FFF;margin-top: 10px;margin-bottom: 10px;border-top:1px solid #333;border-bottom:1px solid #333;}通过增加以上代码, 便可使浏览器重新计算ID1的高, 使其为子元素ID2的margin-top/bottom外缘(outer top/bottom)之间的距离, 即Fig. 3中be的距离.附加:CSS中Padding 属性中参数个数的定义,例如:body { padding: 20px;}body { padding: 20px 30px; }body { padding: 20px 30px 10px; }body { padding: 20px 30px 10px 20px; }详细说明如下:如果只提供一个,将用于全部的四条边;如果提供两个,第一个用于上-下,第二个用于左-右;如果提供三个,第一个用于上,第二个用于左-右,第三个用于下;如果提供全部四个参数值,将按上-右-下-左的顺序作用于四边。
深⼊理解css中的margin属性之前我⼀直认为margin属性是⼀个⾮常简单的属性,但是最近做项⽬时遇到了⼀些问题,才发现margin属性还是有⼀些“坑”的,下⾯我会介绍margin的基本知识以及那些“坑”。
这篇博⽂主要分为以下⼏个部分:第⼀部分:margin--基础知识 要介绍margin的基础知识,我们不可回避地要谈到css盒⼦模型(Box Model),⼀般⽽⾔,css盒⼦模型是⽤来设计和布局的。
它本质上是⼀个盒⼦,包括:外边距(margin)、边框(border)、内边距(padding)以及最中间的内容(content)。
下图即为盒⼦模型(这⾥只谈W3C规范的标准盒模型,⽽不谈IE5和IE6在怪异模式中使⽤的⾮标准的盒⼦模型): 我们要介绍的margin在最外层,因为margin(外边距)⼀定是透明的,所以它可以⽤来使得不同的盒⼦之间留有⼀定的间隙从⽽达到布局美观等效果。
从上⾯的盒⼦模型中我们可以看到,margin在四周均存在,我们可以使⽤margin-top、margin-right、margin-bottom、margin-left分别设置这四个⽅向的margin值。
(注:由于这部分知识较为基础,所以我不再在这部分不做更多介绍)第⼆部分:margin--在同级元素(⾮⽗⼦关系)之间应⽤ 这⼀部分主要介绍⽔平⽅向和竖直⽅向的外边距的合并问题。
(1)⽔平⽅向的外边距合并 两个⽔平⽅向的盒⼦相遇,那么最终两者之间的距离为左边盒⼦的右外边距和右边盒⼦的做外边距之和。
例1: 代码如下:display: inline-block;margin-right: 50px;font-size: 20px;}.right{width: 100px;height: 100px;background: yellow;display: inline-block;margin-left: 50px;font-size: 20px;}</style></head><body><div class="left">宽为100px,右边距为50px</div><div class="right">宽为100px,左边距为50px</div></body></html>效果如下:这时两者之间的距离刚好为100px。
⽹页布局——Box盒⼦在移动端开发的时候,圣杯布局,弹性盒,是我们经常会⽤得到的,W3C很久以前就有⼀个display:box属性flex是最新的,但是在实际的浏览器测试中,display: flex 不能完全替代display: -webkit-boxdisplay: box 使⽤可以参考display: flex 使⽤可以参考兼容性的问题可以参考Android UC浏览器只⽀持display: box语法。
⽽iOS UC浏览器则⽀持两种⽅式需要注意的是如果要使⽤line-clamp时需要⽤display:boxdisplay:-webkit-box;⽗元素设置该属性后,作⽤与display:flex类似,⼦元素可在⼀⾏显⽰,且实现⾃适应。
演⽰:1<style>2 *{3 margin: 0;4 padding: 0;5 }6 .parent{7 width: 400px;8 height: 600px;9 display: -webkit-box;10 -webkit-box-orient: vertical;/* 竖向排列 */11 }12 .child-one{13 background: lightblue;14 -webkit-box-flex: 1;15 }16 .child-two{17 background: lightgray;18 -webkit-box-flex: 2;19 }20 .child-three{21 background: lightgreen;22/* 加了固定的⾼度和边距 */23 height: 200px;24 margin: 15px 0;25 }26</style>2728<div style="display: -webkit-box;-webkit-box-pack: center;border: 1px solid #000">29<div class="parent">30<div class="child-one">1</div>31<div class="child-two">2</div>32<div class="child-three">3</div>33</div>34</div>效果如下:那么到这⾥有⼈会说,display:-webkit-box;与display:flex究竟有什么区别呢?如果要说区别,display:box;是⽼规范,要兼顾古董机⼦就加上它。
w3c教程W3C是World Wide Web Consortium的缩写,是一个由全球范围内的组织和个人组成的机构。
它的目标是制定一系列的标准和指南,以确保互联网上的各种技术和标准的互操作性和互联性。
W3C成立于1994年,是由互联网先驱 Tim Berners-Lee 创立的。
作为Web的创始人之一,他希望通过W3C来推动Web的发展和标准化。
W3C的总部设在美国麻省理工学院(MIT)实验室。
它的成员包括组织和公司,涵盖了互联网行业的各个领域,如IT公司、大学、研究机构、政府等。
W3C致力于制定一系列的技术规范和标准,以确保Web的开放性和可访问性。
其中最为人所熟知的是HTML和CSS标准。
HTML(超文本标记语言)是用于创建Web页面的标记语言,它定义了页面的结构和内容。
CSS(层叠样式表)是用于控制页面样式和布局的语言。
通过HTML和CSS的标准化,W3C确保了不同浏览器和设备上的Web页面的一致性。
除了HTML和CSS,W3C还制定了许多其他的标准。
其中包括XML(可扩展标记语言),它是一种通用的标记语言,用于存储和传输结构化数据;SVG(可缩放矢量图形),它是一种描述二维图形和图像的XML语言;Web标准化API,用于在Web浏览器中开发富媒体应用程序等等。
W3C的标准不仅仅局限于Web技术,它还涉及到隐私和安全、电子商务、多媒体等方面。
例如,标准化的认证和加密机制,可以确保用户的个人信息和交易数据在互联网上的安全传输;多媒体标准,如音频格式和视频编解码等,为互联网上的音视频传输提供了一致的标准。
除了制定标准,W3C还致力于推广标准的普及和应用。
它通过组织各种研讨会、会议和培训课程,向Web开发人员和设计师传授各种标准的知识和技能。
W3C还提供了一系列的工具和测试套件,帮助开发人员遵循标准并验证自己的代码。
总的来说,W3C是一个全球性的组织,致力于制定Web技术和标准的规范和指导。
它的工作对于确保互联网上的各种技术的互操作性和互联性非常重要。
CSS盒⼦模型(标准盒、怪异盒模型)CSS盒⼦模型(Box Model)所有HTML元素可以看作盒⼦,在CSS中,"box model"这⼀术语是⽤来设计和布局时使⽤。
CSS盒模型本质上是⼀个盒⼦,封装周围的HTML元素,它包括:边距,边框,填充,和实际内容。
盒模型允许我们在其它元素和周围元素边框之间的空间放置元素。
下图为盒⼦模型图:margin(外边距) - 清除边框外的区域,外边距是透明的。
border(边框) - 围绕在内边距和内容外的边框。
padding(内边距) - 清除内容周围的区域,内边距是透明的。
content(内容) - 盒⼦的内容,显⽰⽂本和图像。
盒⼦模型分为两种:⼀种为标准盒⼦模型(W3C标准),另⼀种为怪异盒⼦模型(IE标准)⼀、标准盒⼦模型标准盒⼦模型的总宽、⾼度计算公式为:总宽度 = 宽度+左填充+右填充+左边框+右边框+左边距+右边距总⾼度 = ⾼度+顶部填充+底部填充+上边框+下边框+上边距+下边距在浏览器中默认选择的就是标准盒模型⼆、怪异盒⼦模型怪异盒⼦模型的总宽、⾼计算是与标准盒模型不同的:总宽度 = 宽度(内容+边框+左右填充)+左右边距与标准盒不同的是,怪异盒中的宽度不是只有盒⼦内容的宽度,⽽是盒⼦内容的宽度加上左右边框再加上左右填充,这是怪异盒模的标准宽度盒模型解析编辑器建⼀个html页⾯的时候,其实最顶上都会有⼀个DOCTYPE标签,如果定义了DOCTYPE,就会触发标准盒模式,使⽤W3C的标准。
但是在IE9以下,DOCTYPE协议缺失,那么浏览器就会⾃⼰界定触发怪异模式定义盒模型属性:box-sizingbox-sizing属性的三个值:content-box:默认值,border和padding不算到width范围内,采⽤标准模式解析计算,是W3c的标准模型(default)border-box:border和padding划归到width范围内,采⽤怪异模式解析计算,是IE的怪异盒模型padding-box:将padding算⼊width范围box-sizing使⽤需要加前缀:webkit-box-sizing: content-box;moz-box-sizing: content-box;。
8 Box modelContents∙8.1 Box dimensions∙8.2 Example of margins, padding, and borders8.3 Margin properties: 'margin-top', 'margin-right', 'margin-bottom', 'margin-left', and 'margin'o8.3.1 Collapsing margins∙8.4 Padding properties: 'padding-top', 'padding-right', 'padding-bottom', 'padding-left', and 'padding'8.5 Border propertieso8.5.1 Border width: 'border-top-width', 'border-right-width','border-bottom-width', 'border-left-width', and 'border-width'o8.5.2 Border color: 'border-top-color', 'border-right-color','border-bottom-color', 'border-left-color', and 'border-color'o8.5.3 Border style: 'border-top-style', 'border-right-style','border-bottom-style', 'border-left-style', and 'border-style'o8.5.4 Border shorthand properties: 'border-top', 'border-right','border-bottom', 'border-left', and 'border'∙8.6 The box model for inline elements in bidirectional contextThe CSS box model describes the rectangular boxes that are generated for elements in the document tree and laid out according to the visual formatting model.8.1 Box dimensionsEach box has a content area (e.g., text, an image, etc.) and optional surrounding padding, border, and margin areas; the size of each area is specified by properties defined below. The following diagram shows how these areas relate and the terminology used to refer to pieces of margin, border, and padding:The margin, border, and padding can be broken down into top, right, bottom, and left segments (e.g., in the diagram, "LM" for left margin, "RP" for right padding, "TB" for top border, etc.).The perimeter of each of the four areas (content, padding, border, and margin) is called an "edge", so each box has four edges:content edge or inner edgeThe content edge surrounds the rectangle given by the width and height of the box, which often depend on the element's rendered content. The four content edges define the box's content box.padding edgeThe padding edge surrounds the box padding. If the padding has 0 width, the padding edge is the same as the content edge. The four padding edges define the box's padding box.border edgeThe border edge surrounds the box's border. If the border has 0 width, the border edge is the same as the padding edge. The four border edges define the box's border box.margin edge or outer edgeThe margin edge surrounds the box margin. If the margin has 0 width, the margin edge is the same as the border edge. The four margin edges define the box's margin box.Each edge may be broken down into a top, right, bottom, and left edge.The dimensions of the content area of a box — the content width and content height —depend on several factors: whether the element generating the box has the 'width' or 'height' property set, whether the box contains text or other boxes, whether the box is a table, etc. Box widths and heights are discussed in the chapter on visual formatting model details.The background style of the content, padding, and border areas of a box is specified by the 'background' property of the generating element. Margin backgrounds are always transparent.8.2 Example of margins, padding, and bordersThis example illustrates how margins, padding, and borders interact. The example HTML document:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML><HEAD><TITLE>Examples of margins, padding, and borders</TITLE><STYLE type="text/css">UL {background: yellow;margin: 12px 12px 12px 12px;padding: 3px 3px 3px 3px;/* No borders set */}LI {color: white; /* text color is white */background: blue; /* Content, padding will be blue */margin: 12px 12px 12px 12px;padding: 12px 0px 12px 12px; /* Note 0px padding right */list-style: none /* no glyphs before a list item *//* No borders set */}LI.withborder {border-style: dashed;border-width: medium; /* sets border width on all sides */border-color: lime;}</STYLE></HEAD><BODY><UL><LI>First element of list<LI class="withborder">Second element of list isa bit longer to illustrate wrapping.</UL></BODY></HTML>results in a document tree with (among other relationships) a UL element that has two LI children.The first of the following diagrams illustrates what this example would produce. The second illustrates the relationship between the margins, padding, and borders of the UL elements and those of its children LI elements. (Image is not to scale.)Note that:∙The content width for each LI box is calculated top-down; the containing block for each LI box is established by the UL element.∙The margin box height of each LI box depends on its content height, plus top and bottom padding, borders, and margins. Note that vertical margins between the LI boxes collapse.∙The right padding of the LI boxes has been set to zero width (the 'padding' property). The effect is apparent in the second illustration.∙The margins of the LI boxes are transparent — margins are always transparent —so the background color (yellow) of the UL padding and content areas shines through them.∙The second LI element specifies a dashed border (the 'border-style' property). 8.3 Margin properties: 'margin-top', 'margin-right', 'margin-bottom', 'margin-left', and'margin'Margin properties specify the width of the margin area of a box. The 'margin' shorthand property sets the margin for all four sides while the other margin properties only set their respective side. These properties apply to all elements, but vertical margins will not have any effect on non-replaced inline elements.The properties defined in this section refer to the <margin-width> value type, which may take one of the following values:<length>Specifies a fixed width.<percentage>The percentage is calculated with respect to the width of the generated box's containing block. Note that this is true for 'margin-top' and 'margin-bottom' as well. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1.autoSee the section on calculating widths and margins for behavior.Negative values for margin properties are allowed, but there may beimplementation-specific limits.'margin-top', 'margin-bottom'Value:<margin-width>| inheritInitial:0all elements except elements with table display types other than Applies to:table-caption, table and inline-tableInherited:noPercentages:refer to width of containing blockMedia:visualComputedthe percentage as specified or the absolute lengthvalue:These properties have no effect on non-replaced inline elements.'margin-right', 'margin-left'Value:<margin-width>| inheritInitial:0all elements except elements with table display types other than Applies to:table-caption, table and inline-tableInherited:noPercentagesrefer to width of containing block:Media:visualComputedthe percentage as specified or the absolute lengthvalue:These properties set the top, right, bottom, and left margin of a box.h1 { margin-top: 2em }'margin'Value:<margin-width>{1,4} | inheritInitial:see individual propertiesall elements except elements with table display types other than Applies to:table-caption, table and inline-tableInherited:noPercentagesrefer to width of containing block:Media:visualComputedsee individual propertiesvalue:The 'margin' property is a shorthand property for setting 'margin-top', 'margin-right', 'margin-bottom', and 'margin-left' at the same place in the style sheet.If there is only one value, it applies to all sides. If there are two values, the top and bottom margins are set to the first value and the right and left margins are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top,right, bottom, and left, respectively.body { margin: 2em } /* all margins set to 2em */body { margin: 1em 2em } /* top & bottom = 1em, right & left = 2em */body { margin: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */The last rule of the example above is equivalent to the example below:body {margin-top: 1em;margin-right: 2em;margin-bottom: 3em;margin-left: 2em; /* copied from opposite side (right) */}8.3.1 Collapsing marginsIn this specification, the expression collapsing margins means that adjoining margins (no non-empty content, padding or border areas or clearance separate them) of two or more boxes (which may be next to one another or nested) combine to form a single margin.In CSS 2.1, horizontal margins never collapse.Vertical margins may collapse between certain boxes:Two or more adjoining vertical margins of block boxes in the normal flow collapse.The resulting margin width is the maximum of the adjoining margin widths. In the caseof negative margins, the maximum of the absolute values of the negative adjoining margins is deducted from the maximum of the positive adjoining margins. If there are no positive margins, the absolute maximum of the negative adjoining margins is deducted from zero. Note. Adjoining boxes may be generated by elements that are not related as siblings or ancestors.∙Vertical margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).∙Vertical margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.∙Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).∙Margins of inline-block elements do not collapse (not even with their in-flow children).∙If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it. In this case, the position of the element depends on its relationship with the other elements whose margins are being collapsed.o If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.o Otherwise, either the element's parent is not taking part in the margin collapsing, or only the parent's bottom margin is involved. The position of the element's top border edge is the same as it would have been if the elementhad a non-zero bottom border.An element that has had clearance applied to it never collapses its top margin with its parent block's bottom margin.Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements.Margins of the root element's box do not collapse.The bottom margin of an in-flow block-level element is always adjoining to the top margin of its next in-flow block-level sibling, unless that sibling has clearance.The top margin of an in-flow block-level element is adjoining to its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.The bottom margin of an in-flow block-level element with a 'height' of 'auto' is adjoining to its last in-flow block-level child's bottom margin if the element has no bottom padding or border.An element's own margins are adjoining if the 'min-height' property is zero, and it has neither top or bottom borders nor top or bottom padding, and it has a 'height' of either 0 or 'auto', and it does not contain a line box, and all of its in-flow children's margins (if any) are adjoining.When an element's own margins collapse, and that element has had clearance applied to it, its top margin collapses with the adjoining margins of subsequent siblings but that resulting margin does not collapse with the bottom margin of the parent block.Collapsing is based on the used value of 'padding', 'margin', and 'border' (i.e., after resolving any percentages). The collapsed margin is calculated over the used value of the various margins.Please consult the examples of margin, padding, and borders for an illustration of collapsed margins.8.4 Padding properties: 'padding-top', 'padding-right', 'padding-bottom', 'padding-left', and 'padding'The padding properties specify the width of the padding area of a box. The 'padding' shorthand property sets the padding for all four sides while the other padding properties only set their respective side.The properties defined in this section refer to the <padding-width>value type, which may take one of the following values:<length>Specifies a fixed width.<percentage>The percentage is calculated with respect to the width of the generated box's containing block, even for 'padding-top' and 'padding-bottom'. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1. Unlike margin properties, values for padding values cannot be negative. Like margin properties, percentage values for padding properties refer to the width of the generated box's containing block.'padding-top', 'padding-right', 'padding-bottom', 'padding-left'Value:<padding-width>| inheritInitial:0all elements except table-row-group, table-header-group, Applies to:table-footer-group, table-row, table-column-group and table-column Inherited:noPercentagesrefer to width of containing block:Media:visualComputedthe percentage as specified or the absolute lengthvalue:These properties set the top, right, bottom, and left padding of a box.blockquote { padding-top: 0.3em }'padding'Value:<padding-width>{1,4} | inheritInitial:see individual propertiesall elements except table-row-group, table-header-group, Applies to:table-footer-group, table-row, table-column-group and table-column Inherited:noPercentagesrefer to width of containing block:Media:visualComputedsee individual propertiesvalue:The 'padding' property is a shorthand property for setting 'padding-top', 'padding-right', 'padding-bottom', and 'padding-left' at the same place in the style sheet.If there is only one value, it applies to all sides. If there are two values, the top and bottom paddings are set to the first value and the right and left paddings are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top, right, bottom, and left, respectively.The surface color or image of the padding area is specified via the 'background' property: h1 {background: white;padding: 1em 2em;}The example above specifies a '1em' vertical padding ('padding-top' and 'padding-bottom') and a '2em' horizontal padding ('padding-right' and 'padding-left'). The 'em' unit is relative to the element's font size: '1em' is equal to the size of the font in use.8.5 Border propertiesThe border properties specify the width, color, and style of the border area of a box. These properties apply to all elements.Note.Notably for HTML, user agents may render borders for certain user interfaceelements (e.g., buttons, menus, etc.) differently than for "ordinary" elements.8.5.1 Border width: 'border-top-width', 'border-right-width', 'border-bottom-width','border-left-width', and 'border-width'The border width properties specify the width of the border area. The properties defined in this section refer to the <border-width> value type, which may take one of the following values:thinA thin border.mediumA medium border.thickA thick border.<length>The border's thickness has an explicit value. Explicit border widths cannot be negative.The interpretation of the first three values depends on the user agent. The following relationships must hold, however:'thin' <='medium' <= 'thick'.Furthermore, these widths must be constant throughout a document.'border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width'Value:<border-width>| inheritInitial:medium Applies to:all elements Inherited:no Percentages:N/A Media:visualComputed value:absolute length; '0' if the border style is 'none' or 'hidden'These properties set the width of the top, right, bottom, and left border of a box.'border-width'Value:<border-width>{1,4} |inheritInitial:see individual propertiesApplies to:all elementsInherited:noPercentages:N/AMedia:visualComputedvalue:see individual propertiesThis property is a shorthand property for setting 'border-top-width', 'border-right-width', 'border-bottom-width', and 'border-left-width' at the same place in the style sheet.If there is only one value, it applies to all sides. If there are two values, the top and bottom borders are set to the first value and the right and left are set to the second. If there arethree values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top, right, bottom, and left, respectively.In the examples below, the comments indicate the resulting widths of the top, right, bottom, and left borders:h1 { border-width: thin } /* thin thin thin thin */h1 { border-width: thin thick } /* thin thick thin thick */h1 { border-width: thin thick medium } /* thin thick medium thick */8.5.2 Border color: 'border-top-color', 'border-right-color', 'border-bottom-color','border-left-color', and 'border-color'The border color properties specify the color of a box's border.'border-top-color', 'border-right-color', 'border-bottom-color', 'border-left-color'Value:<color>| transparent | inheritInitial:the value of the 'color' propertyApplies to:all elementsInherited:noPercentages: N/AMedia:visualComputed value:when taken from the 'color' property, the computed value of 'color'; otherwise, as specified'border-color'[ <color> | transparent ]{1,4} |Value:inheritInitial:see individual propertiesApplies to:all elementsInherited:noPercentages:N/AMedia:visualComputedsee individual propertiesvalue:The 'border-color' property sets the color of the four borders. Values have the following meanings:<color>Specifies a color value.transparentThe border is transparent (though it may have width).The 'border-color' property can have from one to four values, and the values are set on the different sides as for 'border-width'.If an element's border color is not specified with a border property, user agents must use the value of the element's 'color' property as the computed value for the border color.In this example, the border will be a solid black line.p {color: black;background: white;border: solid;}8.5.3 Border style: 'border-top-style', 'border-right-style', 'border-bottom-style','border-left-style', and 'border-style'The border style properties specify the line style of a box's border (solid, double, dashed, etc.). The properties defined in this section refer to the <border-style> value type, which may take one of the following values:noneNo border; the computed border width is zero.hiddenSame as 'none', except in terms of border conflict resolution for table elements. dottedThe border is a series of dots.dashedThe border is a series of short line segments.solidThe border is a single line segment.doubleThe border is two solid lines. The sum of the two lines and the space between them equals the value of 'border-width'.grooveThe border looks as though it were carved into the canvas.ridgeThe opposite of 'groove': the border looks as though it were coming out of the canvas. insetThe border makes the box look as though it were embedded in the canvas.outsetThe opposite of 'inset': the border makes the box look as though it were coming out of the canvas.All borders are drawn on top of the box's background. The color of borders drawn for values of 'groove', 'ridge', 'inset', and 'outset' depends on the element's border color properties, but UAs may choose their own algorithm to calculate the actual colors used. For instance, if the 'border-color' has the value 'silver', then a UA could use a gradient of colors from white to dark gray to indicate a sloping border.'border-top-style', 'border-right-style', 'border-bottom-style', 'border-left-style'<border-style>|Value:inheritInitial:noneApplies to:all elementsInherited:noPercentages:N/AMedia:visualComputedas specifiedvalue:'border-style'<border-style>{1,4} |Value:inheritInitial:see individual propertiesApplies to:all elementsInherited:noPercentages:N/AMedia:visualComputedsee individual propertiesvalue:The 'border-style' property sets the style of the four borders. It can have from one to four values, and the values are set on the different sides as for 'border-width' above.#xy34 { border-style: solid dotted }In the above example, the horizontal borders will be 'solid' and the vertical borders will be 'dotted'.Since the initial value of the border styles is 'none', no borders will be visible unless the border style is set.8.5.4 Border shorthand properties: 'border-top', 'border-right', 'border-bottom', 'border-left', and 'border''border-top', 'border-right', 'border-bottom', 'border-left'[ <border-width> || <border-style> || <'border-top-color'> ] | Value:inheritInitial:see individual propertiesApplies to:all elementsInherited:noPercentages:N/AMedia:visualComputedsee individual propertiesvalue:This is a shorthand property for setting the width, style, and color of the top, right, bottom, and left border of a box.h1 { border-bottom: thick solid red }The above rule will set the width, style, and color of the border below the H1 element. Omitted values are set to their initial values. Since the following rule does not specify a border color, the border will have the color specified by the 'color' property: H1 { border-bottom: thick solid }'border'[ <border-width> || <border-style> || <'border-top-color'> ] | Value:inheritInitial:see individual propertiesApplies to:all elementsInherited:noPercentages:N/AMedia:visualComputedsee individual propertiesvalue:The 'border' property is a shorthand property for setting the same width, color, and style for all four borders of a box. Unlike the shorthand 'margin' and 'padding' properties, the 'border' property cannot set different values on the four borders. To do so, one or more of the other border properties must be used.For example, the first rule below is equivalent to the set of four rules shown after it: p { border: solid red }p {border-top: solid red;border-right: solid red;border-bottom: solid red;border-left: solid red}Since, to some extent, the properties have overlapping functionality, the order in which the rules are specified is important.Consider this example:blockquote {border: solid red;border-left: double;color: black;}In the above example, the color of the left border is black, while the other borders are red. This is due to 'border-left' setting the width, style, and color. Since the color value is not given by the 'border-left' property, it will be taken from the 'color' property. The fact that the 'color' property is set after the 'border-left' property is not relevant.8.6 The box model for inline elements in bidirectional contextFor each line box, UAs must take the inline boxes generated for each element and render the margins, borders and padding in visual order (not logical order).When the element's 'direction' property is 'ltr', the left-most generated box of the first line box in which the element appears has the left margin, left border and left padding, and the right-most generated box of the last line box in which the element appears has the right padding, right border and right margin.When the element's 'direction' property is 'rtl', the right-most generated box of the first line box in which the element appears has the right padding, right border and right margin, andthe left-most generated box of the last line box in which the element appears has the left margin, left border and left padding.。