在现在的网站设计中使用reset.css用重置整个站点的标签的CSS属性的做法很常见,但有时候我们已经为了reset而reset,我们经常看到这样的reset代码

div{
padding:0px;
margin:0px;
} span{
margin:0px;
}

其实大部分CSS reset是没必要的,多写了只会增加浏览器在渲染页面是的负担,当然有同学会说CSS reset还是有其意义的,这个我也承认,但是我们可以通过了解一些标签的CSS属性的默认值来避免过度的reset

标签属性默认值

由于大部分的CSS reset都是针对padding、border、margin,我们就先看看常用标签的这三个属性的默认值(Chrome)

标签 padding border margin
html 0 0 0
body 0 0 8
form 0 0 0
div 0 0 0
span 0 0 0
p 0 0 16
th、td 1 0 0
input(text、password) 1 2 2
input(checkbox、radio) 0 0 3 0.5ex
input button 8 0 2
textarea 2 1 2
select 0 0 2
option 0 0 0
h1~h6 0 0 ?px 0
ul、ol 0 0 0 40px 0 16px 0
li 0 0 0
dl 0 0 16px 0
dt 0 0 0
dd 0 0 0 0 0 40px
label 0 0 0
em、strong 0 0 0
label 0 0 0
img 0 0 0
a 0 0 0

虽然只是在Chrome下,但通过上面表可以看出很多标签默认的padding、border、margin就是0,如果还在CSS reset中写一遍岂不是画蛇添足了,除了浏览器的默认值,还有一些标签的属性值得我们注意。

行内元素的width、height、padding、margin

  1. 行内元素不会应用width属性,其长度是由内容撑开的
  2. 行内元素不会应用height属性,其高度也是由内容撑开的,但是高度可以通过line-height调节
  3. 行内元素的padding属性只用padding-left和padding-right生效,padding-top和padding-bottom会改变元素范围,但不会对其它元素造成影响
  4. 行内元素的margin属性只有margin-left和margin-right有效,margin-top和margin-bottom无效
  5. 行内元素的overflow属性无效,这个不用多说了
  6. 行内元素的vertical-align属性无效(height属性无效)

看个例子

<div style="background-color: #a44;">
<span style="padding:4px; margin:8px; height: 500px; width:1000px; background-color:#0e0;">123456789123456789</span>
</div> <div style="background-color: #a44;">
<span style="padding:4px; margin:8px; height: 500px; width:1000px; background-color:#0a0;">123456789</span>
</div>

通过例子可以看出,我们对span设置的width和height属性并没有生效,margin-top和margin-bottom无效,padding-top和padding-bottom会改变元素范围(背景区域变大了),但并没有影响下面元素位置

在CSS reset中我们不应该设置对行内元素无效的属性

一些互斥的属性

  • 对于absolute和fixed定位的固定尺寸(设置了width和height属性值)元素,如果设置了top和left属性,那么设置bottom和right值就没有作用了,应该是top和left优先级高,否则同时写了浏览器怎么知道按照谁定位
  • 对于absolute和fixed定位的元素,如果设置了top、left、bottom、right的值后margin属性也就不起作用了
  • 对于absolute和fixed定位的元素,如果设置了top、left、bottom、right的值后float属性同样会失效
  • 块元素如果设置了float属性或者是absolute、fixed定位,那么vertical-align属性不再起作用

其它

常规情况下块元素的width:100%,pre等很少用到的元素,个人感觉用的时候再页面写就可以,没必要加到reset中,让所有页面都去加载。

例子

看一下CSS reset大神Eric Meyer的写法

/**
* Eric Meyer's Reset CSS v2.0 (http://meyerweb.com/eric/tools/css/reset/)
* http://cssreset.com
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin:;
padding:;
border:;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height:;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing:;
}

写的很精简了,但是我感觉可以把一些不常用的标签去掉,缩写成这样

html, body, div, span, iframe,
h1, h2, h3, h4, h5, h6, p, a,
del, dfn, em, img,
small, strong,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, section, summary
{
margin:;
padding:;
border:;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height:;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing:;
}

如果对CSS reset 有兴趣可以看看http://www.cssreset.com/,上面有很多流行的CSS reset写法

常见标签的默认属性值及相互作用——关于CSS reset的思考的更多相关文章

  1. 查看特定View的默认属性值

    当我在分析focus.touch事件处理代码时发现,有些属性对代码的逻辑有非常重要的影响,比如clickable.focusable 这些属性.这时我们自然而然的想到,那么这些属性的默认值是什么呢?在 ...

  2. How to: Initialize Business Objects with Default Property Values in XPO 如何:在 XPO 中用默认属性值初始化业务对象

    When designing business classes, a common task is to ensure that a newly created business object is ...

  3. How to: Initialize Business Objects with Default Property Values in Entity Framework 如何:在EF中用默认属性值初始化业务对象

    When designing business classes, a common task is to ensure that a newly created business object is ...

  4. html标签默认属性值之margin;padding值

    一.h1~h6标签:有默认margin(top,bottom且相同)值,没有默认padding值. 在chrome中:16,15,14,16,17,19; 在firefox中:16,15,14,16, ...

  5. 在函数中处理html点击事件在标签中增加属性值来解决问题。

  6. java正则表达式获取指定HTML标签的指定属性值

    package com.mmq.regex; import java.util.ArrayList; import java.util.List; import java.util.regex.Mat ...

  7. 使用xpath提取页面所有a标签的href属性值

    # -*- coding: utf-8 -*- #1.选取节点 #获取所有的div元素 //div #/代表获取根节点的直接子元素 #获取所有带有id属性的div //div[@id] #2.谓词(索 ...

  8. js和jquery通过this获取html标签中的属性值

    <html> <head> <script type="text/javascript" src="jquery-1.10.2.min.js ...

  9. 关于li标签的value属性值的获取问题

    在前几天的开发过程中,遇到了这样一个问题. 在li标签中嵌入了一个value属性,如这样滴: <li id="ts1" value="0001">& ...

随机推荐

  1. openoffice

    cmdcd/cd C:\Program Files (x86)\OpenOffice 4\program soffice -headless -accept="socket,host=127 ...

  2. Java 第17章 继承

    继承的概念 继承机制是面向对象程序设计不可缺少的关键概念,是实现软件可重用的根基, 是提高软件系统的可扩展性与可维护性的主要途径. 所谓继承是指一个类的定义可以基于另外一个已经存在的类,即子类基于父类 ...

  3. access查询优化

    一个zirancun 14万数据量,一个 zirancuntd 19万数据,这两个  zirancuntd.distid 与zirancun.id进行关联,查询 zirancuntd.distid不存 ...

  4. 获取基于Internet Explorer_Server的聊天窗口内容

    假设在得到窗体中控件的句柄(通过SPY++)的前提下,如果是像文本框这种控件,只要用SendMessage就可得到文本了,但是对于聊天记录窗口却行不通(返回空值),因为那其实是一个内置浏览器Inter ...

  5. [Python] Removing a non-empty folder

    Removing a non-empty folder You will get an ‘access is denied’ error when you attempt to use os.remo ...

  6. javascript基础知识-数组

    1.javascript创建数组时无需声明数组大小或者在数组大小变化时重新分配 2.javascript数组是无类型的 3.数组元素不一定要连续 4.针对稀疏数组,length比所有元素的索引都要大 ...

  7. javascript 中的事件机制

    1.javascript中的事件. 事件流 javascript中的事件是以一种流的形式存在的. 一个事件会也有多个元素同时响应. 有时候这不是我们想要的效果, 我们只是需要某个特定的元素相应我们的绑 ...

  8. uva-327

    题意:给出一个C语言加减法表达式,求出这个表达式的最终结构,以及各个变量的值,每个变量保证至出现一次,保证输入的字符串合法: 输入:一串包含+.-和小写的26个英文字母: 输出:表达式的结果,以及表达 ...

  9. Windows & Office完美结合,助力办公

    虚拟桌面——休闲工作分开来 Windows 10最令我欣愉的是加入了虚拟桌面的功能. 作为一名拖延症晚期患者,早已病入膏肓.每次工作时总会不知不觉地将实现转移到已经打开的浏览器及聊天工具上,时间就这样 ...

  10. petapoco 使用 MiniProfiler Glimpse监控

    PetaPoco是一款适用于.Net(window) 和Mono( linux )的微小.快速.单文件的微型ORM. MVC MiniProfiler是Stack Overflow团队设计的一款对AS ...