Maison > interface Web > tutoriel HTML > 后端码农谈前端(CSS篇)第二课:CSS的5个来源_html/css_WEB-ITnose

后端码农谈前端(CSS篇)第二课:CSS的5个来源_html/css_WEB-ITnose

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2016-06-24 11:47:44
original
1183 Les gens l'ont consulté

0、浏览器默认样式

当你不为html元素设置任何样式时,显示在浏览器上的(比如:元素会显示粗体、

元素有纵向margin、

元素字号比

元素大一倍……)这是为什么呢?

因为浏览器自带一个默认的样式,在html元素未被设置样式时,浏览器会按照自己默认的样式来显示。但是浏览器默认样式的级别是最低的,一旦有其他地方设置了样式,浏览器默认样式就会被覆盖掉。

注意,不同浏览器的默认样式有些地方是不一样的。例如,我们在写css时,都会首先设置 * {margin:0; padding:0;},这是为何?就是因为有浏览器兼容性问题。干脆,全部弄成0,这样各个浏览器就都统一了。

下面,我们贴出默认样式的代码:

html, address,blockquote,body, dd, div,dl, dt, fieldset, form,frame, frameset,h1, h2, h3, h4,h5, h6, noframes,ol, p, ul, center,dir, hr, menu, pre { display: block }/*以上列表元素默认状态下一块状显示,未显示的将以内联元素显示,该列表针对HTML4版本,部分元素在XHTML1中将废弃*/
Copier après la connexion
li { display: list-item }/*默认以列表显示*/
Copier après la connexion
Copier après la connexion
head { display: none }/*默认不显示*/
Copier après la connexion
table { display: table }/*默认为表格显示*/
Copier après la connexion
Copier après la connexion
tr { display: table-row }/*默认为表格行显示*/
Copier après la connexion
thead { display: table-header-group }/*默认为表格头部分组显示*/
Copier après la connexion
tbody { display: table-row-group }/*默认为表格行分组显示*/
Copier après la connexion
tfoot { display: table-footer-group }/*默认为表格底部分组显示*/
Copier après la connexion
col { display: table-column }/*默认为表格列显示*/
Copier après la connexion
colgroup { display: table-column-group }/*默认为表格列分组显示*/
Copier après la connexion
td, th { display: table-cell; }/*默认为单元格显示*/
Copier après la connexion
Copier après la connexion
caption { display: table-caption }/*默认为表格标题显示*/
Copier après la connexion
th { font-weight: bolder; text-align: center }/*默认为表格标题显示,呈现加粗居中状态*/
Copier après la connexion
caption { text-align: center }/*默认为表格标题显示,呈现居中状态*/
Copier après la connexion
body { margin: 8px; line-height: 1.12 }
Copier après la connexion
h1 { font-size: 2em; margin: .67em 0 }
Copier après la connexion
h2 { font-size: 1.5em; margin: .75em 0 }
Copier après la connexion
h3 { font-size: 1.17em; margin: .83em 0 }
Copier après la connexion
h4, p, blockquote, ul, fieldset, form, ol, dl, dir, menu { margin: 1.12em 0 }
Copier après la connexion
h5 { font-size: .83em; margin: 1.5em 0 }
Copier après la connexion
h6 { font-size: .75em; margin: 1.67em 0 }
Copier après la connexion
h1, h2, h3, h4, h5, h6, b,strong { font-weight: bolder }
Copier après la connexion
blockquote { margin-left: 40px; margin-right: 40px }
Copier après la connexion
i, cite, em,var, address { font-style: italic }
Copier après la connexion
pre, tt, code, kbd, samp { font-family: monospace }
Copier après la connexion
pre { white-space: pre }
Copier après la connexion
button, textarea, input, object, select { display:inline-block; }
Copier après la connexion
big { font-size: 1.17em }
Copier après la connexion
small, sub, sup { font-size: .83em }
Copier après la connexion
sub { vertical-align: sub }/*定义sub元素默认为下标显示*/
Copier après la connexion
sup { vertical-align: super }/*定义sub元素默认为上标显示*/
Copier après la connexion
table { border-spacing: 2px; }
Copier après la connexion
thead, tbody, tfoot { vertical-align: middle }/*定义表头、主体表、表脚元素默认为垂直对齐*/
Copier après la connexion
td, th { vertical-align: inherit }/*定义单元格、列标题默认为垂直对齐默认为继承*/
Copier après la connexion
s, strike, del { text-decoration: line-through }/*定义这些元素默认为删除线显示*/
Copier après la connexion
hr { border: 1px inset }/*定义分割线默认为1px宽的3D凹边效果*/
Copier après la connexion
ol, ul, dir, menu, dd { margin-left: 40px }
Copier après la connexion
ol { list-style-type: decimal }
Copier après la connexion
ol ul, ul ol, ul ul, ol ol { margin-top: 0; margin-bottom: 0 }
Copier après la connexion
u, ins { text-decoration: underline }
Copier après la connexion
br:before { content: "A" }/*定义换行元素的伪对象内容样式*/
Copier après la connexion
:before, :after { white-space: pre-line }/*定义伪对象空格字符的默认样式*/
Copier après la connexion
center { text-align: center }
Copier après la connexion
abbr, acronym { font-variant: small-caps; letter-spacing: 0.1em }
Copier après la connexion
:link, :visited { text-decoration: underline }
Copier après la connexion
:focus { outline: thin dotted invert }
Copier après la connexion
 /* Begin bidirectionality settings (do not change) */
Copier après la connexion
BDO[DIR="ltr"] { direction: ltr; unicode-bidi: bidi-override }/*定义BDO元素当其属性为DIR="ltr"时的默认文本读写显示顺序*/
Copier après la connexion
BDO[DIR="rtl"] { direction: rtl; unicode-bidi: bidi-override }/*定义BDO元素当其属性为DIR="rtl"时的默认文本读写显示顺序*/
Copier après la connexion
*[DIR="ltr"] { direction: ltr; unicode-bidi: embed }/*定义任何元素当其属性为DIR="ltr"时的默认文本读写显示顺序*/
Copier après la connexion
*[DIR="rtl"] { direction: rtl; unicode-bidi: embed }/*定义任何元素当其属性为DIR="rtl"时的默认文本读写显示顺序*/
Copier après la connexion
@media print { /*定义标题和列表默认的打印样式*/
Copier après la connexion
    h1 { page-break-before: always }
Copier après la connexion
    h1, h2, h3, h4, h5, h6 { page-break-after: avoid }
Copier après la connexion
    ul, ol, dl { page-break-before: avoid }
Copier après la connexion
}
Copier après la connexion

(1)、display: block

html, address,blockquote,body, dd, div,dl, dt, fieldset, form,frame, frameset,h1, h2, h3, h4,h5, h6, noframes,ol, p, ul, center,dir, hr, menu, pre { display: block }/*以上列表元素默认状态下一块状显示,未显示的将以内联元素显示,该列表针对HTML4版本,部分元素在XHTML1中将废弃*/ 
Copier après la connexion

为何默认情况下p、h1、ul、div都是block显示,就是这里定义的。所以,不要再说div天生就是block??这句话应该换成:浏览器默认样式规定了div是block!是块元素还是内联元素是由默认样式规定的,不是浏览器的内核规定的。

没有设置block的元素,默认为inline元素。

(2)、display: list-item

li { display: list-item }/*默认以列表显示*/
Copier après la connexion
Copier après la connexion

我们在使用display时,常用的值一般是:inline/block/inline-block,用不到list-item。那这里的list-item到底有什么作用?我们不妨亲自试一试:

看到没有,出现了ul-li中的效果了吧,如果再加一个margin-left是不是就跟ul-li一样了?

所以,ul-li为什么会默认显示成那种样子???list-item才是“罪魁祸首”。

(3)、diplay:table

table { display: table }/*默认为表格显示*/
Copier après la connexion
Copier après la connexion

举一个例子:

上图中,第一个div默认是block,宽度撑满整个页面。第二个div设置了display:table,宽度根据内容而定。这就是“包裹性”。

(4)、display: table-cell

td, th { display: table-cell; }/*默认为单元格显示*/
Copier après la connexion
Copier après la connexion

举个例子:

记得刚学html时候,不会用div + css做多列布局,我就用table做多列布局。而今,你可以用table-cell,像用table一样做多列布局,做出来的效果和table做出来的效果是一模一样的。

(5)、display:inline-block

button, textarea, input, object, select { display:inline-block; } 
Copier après la connexion

能被父容器居中、能设置高度宽度和margin、不会像table或div那样占一正行……??这就是inline-block。(具体可看看设置了该样式元素的表现。)

1、内联样式

中编写的样式代码。

2、内部样式

3、引用样式

引用的css文件。

4、浏览器用户自定义样式

在一些新闻网站中,经常看到可以设置字体大小的快捷菜单,例如下图就是搜狐新闻中的设置。

这些是给一些有视觉障碍的人看的,反正我是没用过,我也有视觉障碍,不过我是通过近视镜来解决的。

其实浏览器也有这样的设置,例如chrome浏览器中,我们就可以设置字号和字体。

用户在这里设置了字体和字号之后,它们会覆盖掉浏览器默认的样式。

本篇内容节选自《CSS知多少》

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal