如何给CSS进行初始化样式?

原创 阁主  2018-11-30 06:56:00  阅读 4669 次 评论 2 条
摘要:

作为初学者,掌握了HTML后的学习任务就是CSS,但是平时在学习中相信大家会遇到很多时候相同的HTML标签和CSS样式并不能完美展现我们预期想要的结果,比如:相同的标签样式在多个浏览器中打开会有不一样的间距、行距、边距等……这就是不同浏览器的显示效果可能不一样,为杜绝这种情况,通过css强制让所有元素的属性值都一样,这样浏览器显示就一致了,减少了不兼容情况的发生,这个过程叫做css的初始化。通俗讲就是把所有常用HTML标签或者样式需要统一CSS样式,主流浏览器打开网页都会有一样的效果。我们通过给它初始化来解决这个问题。

准备工具:

前端IDE:HBuilderX(不一定这个)

教程开始:

  1. 开始之前先带大家学习一下BAT公司都是对哪些HTML元素和标签进行初始化的。

下面是取自百度贴吧一部分的CSS初始化代码:

table {
	border-collapse: collapse;
	border-spacing: 0;
}
td,th {
	padding: 0;
}
html,body,div,p,ul,li,ol,dl,dt,dd,pre,code,table,tr,
td,form,fieldset,legend,button,input,textarea,h1,h2,
h3,h4,h5,h6,hr,blockquote {
	margin: 0;
	padding: 0;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
ul,ol,li {
	list-style: none;
}

下面是取自淘宝一部分的CSS初始化代码:

body,button,dd,dl,dt,fieldset,form,h1,h2,h3,h4,h5,h6,hr,input,legend,li,ol,p,pre,td,textarea,th,ul {
	margin: 0;
	padding: 0
}

body,button,input,select,textarea {
	font: 12px/1.5 tahoma, arial, 'Hiragino Sans GB', '\5b8b\4f53', sans-serif
}

h1,h2,h3,h4,h5,h6 {
	font-size: 100%
}

address,cite,dfn,em,var {
	font-style: normal
}

code,kbd,pre,samp {
	font-family: courier new, courier, monospace
}

small {
	font-size: 12px
}

ol,ul {
	list-style: none
}

a {
	text-decoration: none
}

下面是取自腾讯网一部分的CSS初始化代码:

body,ol,ul,li,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select {
	margin: 0;
	padding: 0
}

img {
	border: 0;

2.参考了BAT三家公司的常见初始化代码,我们可以发现大多都是对常用标签如:h1-h6,body,table等进行边距(margin)和填充(padding)大小还有块元素(display)的初始化。所以这边综合了一下一些常见的初始化代码给大家,大家可以根据需要增删减。

/*css 初始化 */
html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4, h5, h6, form, fieldset, legend, img {
    margin: 0;
    padding: 0;
}

h1, h2, h3, h4, h5, h6 {
    text-decoration: none;
    font-weight: normal;
    font-size: 100%;
}

img, input, button {
    border: none;
    padding: 0;
    margin: 0;
    outline-style: none;
}

ul, ol {
    list-style: none;
}
/*css 设置文本输入框统一字体 */
input {
    padding-top: 0;
    padding-bottom: 0;
    font-family: "SimSun", "宋体";
}

/*防止拖动*/
img {
    border: 0;
    vertical-align: middle;
}

/*  去掉图片低测默认的3像素空白缝隙*/
table {
    border-collapse: collapse;
}

s, i, em {
    font-style: normal;
    text-decoration: none;
}

table {
	border-collapse: collapse;
	border-spacing: 0;
}
td,th {
	padding: 0;
}

a {
	text-decoration: none
}

3.上述初始化也存在不完整性,可以参考下列的标签默认样式进行筛选初始化。

//块级元素
html, body,  div,ol, p, ul,  h1, h2,h3,h4,h5, h6,
address,blockquote, form,
dd,dl, dt, fieldset, frame, frameset,noframes,center, dir, hr, menu, pre  
{ display: block }
 
//列表元素类
li{ display:list-item }
ol{list-style-type: decimal }
ol ul, ul ol,ul ul, ol ol  { margin-top: 0; margin-bottom: 0 }
ol, ul{ margin-left: 40px }
 
//预格式文本类
i, cite, em,var, address{ font-style: italic }
big{ font-size:1.17em }
small, sub, sup{ font-size: .83em }
sub{ vertical-align:sub }
sup{ vertical-align:super }
s, strike, del{ text-decoration: line-through }
u, ins{ text-decoration:underline }
 
//标题类
h1{ font-size:2em; margin: .67em 0 }
h2{ font-size:1.5em; margin: .75em 0 }
h3{ font-size:1.17em; margin: .83em 0 }
h4, p,blockquote, ul,fieldset, form,ol, dl, dir,menu { margin: 1.12em 0}
h5 { font-size:.83em; margin: 1.5em 0 }
h6{ font-size:.75em; margin: 1.67em 0 }
h1, h2, h3, h4,h5, h6, b,strong  { font-weight: bolder }
 
 
//伪类
br:before{ content: ”\A” }
:before, :after{ white-space: pre-line }
:link, :visited { text-decoration: underline }
:focus{ outline: thin dotted invert }
 
//表格类
table{ display: table }
tr{ display:table-row }
thead{ display:table-header-group }
tbody{ display:table-row-group }
tfoot{ display:table-footer-group }
col{ display:table-column }
colgroup{ display:table-column-group }
td, th{ display: table-cell;}
caption{ display: table-caption }
th{font-weight: bolder; text-align: center }
caption{ text-align: center }
table{ border-spacing: 2px;}
thead, tbody,tfoot { vertical-align:middle }
td, th { vertical-align:inherit }
 
//其它元素
head{ display: none }
body{ margin: 8px;line-height: 1.12 }     
button, textarea,input, object,select  { display:inline-block;}
blockquote{ margin-left: 40px;margin-right: 40px }
pre, tt, code,kbd, samp  { font-family: monospace }
pre{ white-space: pre}
hr{ border: 1px inset }
center{ text-align: center }
abbr, acronym{ font-variant: small-caps; letter-spacing:0.1em }
 
 BDO[DIR="ltr"]  { direction: ltr; unicode-bidi:bidi-override }
 BDO[DIR="rtl"]  { direction: rtl; unicode-bidi:bidi-override }
 /*定义BDO元素当其属性为DIR="ltr/rtl"时的默认文本读写显示顺序*/
 *[DIR="ltr"]{ direction: ltr;unicode-bidi: embed }
 *[DIR="rtl"] { direction: rtl;unicode-bidi: embed }
 /*定义任何元素当其属性为DIR="rtl/rtl"时的默认文本读写显示顺序*/
 @media print {
       h1{page-break-before: always }
       h1, h2, h3,h4, h5, h6    { page-break-after: avoid }
       ul, ol, dl{ page-break-before: avoid }
  } /*定义标题和列表默认的打印样式*/

完!

暂时就这些吧,若想起少了什么再来补充………………

本文地址:https://www.mainblog.cn/127.html
版权声明:本文为原创文章,版权归 阁主 所有,欢迎分享本文,转载请保留出处!
免责申明:有些内容源于网络,没能联系到作者。如侵犯到你的权益请告知,我们会尽快删除相关内容。

评论已关闭!