基本的布局问题

ios取消默认数字为电话

<meta content="telephone=no" name="format-detection" />

1、div绝对居中
就是固定大小的div在浏览器中垂直、水平都居中,适合用到浮动弹出框、页面居中的登陆框等你想得到的地方。以前我也曾使用window.onload和window.onresize来控制高度自适应,宽度好说,兼容IE和其他主流浏览器的,直接文本居中,内部“margin:0 auto;”。

下面是现在的纯css解决方法。
以宽高为400px*300px的div居中为例,body内只有div,所以简写css如下:

div { 400px; height:300px; position:absolute; left:50%; top:50%; margin-left:-200px; margin-top:-150px; background:#f90;}
完整html源码如下:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>test</title>
<style>
div { 400px; height:300px; position:absolute; left:50%; top:50%; margin-left:-200px; margin-top:-150px; background:#f90;}
</style>
</head>
<body>
<div></div>
</body>
</html>

这样就完美实现了绝对居中,另外,如果设计中最外边框添加了border,div设置了padding等,一定要注意margin值要排除border宽或者padding值。

2、宽高自适应
很多情况下,我们希望页面的宽高可以自适应,宽度自适应很简单,当然,这里所说的自适应不是自适应内容,而是自适应浏览器,在同一行级只有一个层的情况下,宽度自适应直接使用100%;就可以了,但是高度自适应就有些不遂人意了,很多时候设置height:100%;完全不起效,其实,这时完全是因为div所在位置的上级元素没有指定高度。
同样以一个div为例,css如下:

html,body { height:100%; 100%; margin:0; padding:0;}
div { height:100%; 100%; background:#f90;}
完整html源码如下:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>test</title>
<style>
html,body { height:100%; 100%; margin:0; padding:0;}
div { height:100%; 100%; background:#f90;}
</style>
</head>
<body>
<div></div>
</body>
</html>

为什么设置body的height为100%的同时还要设置html呢?其实是为了解决firefox的兼容性,firefox默认body占用的不是100%;,这里有一个设计思想就是,如果某个层要用到100%或者其他值的百分比,上一级必须指定高度,当然,这同样应用到div下的div。

3、多栏宽度自适应
前面第二部分已经将过完全的自适应宽高,但其中的宽度自适应只是在一个div独占一行的情况下,如果出现多个div,如常见的主题部分左右、左中右布局的网站页面,两边固定宽,中间自适应,改怎么写呢?其实只要在中间部分的div下再套一层div即可,示例css如下:

html,body { height:100px; 100%; margin:0; padding:0;}
.left { height:100px; 100px; background:#CC3; float:left;}
.middle { margin:0 50px 0 100px;}
.middle .content { height:100px; 100%; background:#F60; float:left;}
.right { height:100px; 50px; background:#09F; float:left;}
完整html源码如下:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>test</title>
<style>
html,body { height:100px; 100%; margin:0; padding:0;}
.left { height:100px; 100px; background:#CC3; float:left;}
.middle { margin:0 50px 0 100px;}
.middle .content { height:100px; 100%; background:#F60; float:left;}
.right { height:100px; 50px; background:#09F; float:left;}
</style>
</head>
<body>
<div class=”left”></div>
<div class=”middle”>
<div class=”content”></div>
</div>
<div class=”right”></div>
</body>
</html>

4、高度占满全屏

解决方案:在CSS样式中定义

body{height:100%;}

此定义是为了让本身body就具有height为100%

即:要定义一个DIV的高100%纵向满屏,那么请先将其上级的DIV的高定义为100%,上级有多少个DIV,都要定义它们的高为100%。
这样才能达到包含在里面的DIV的高为浏览器高100%的纵向满屏效果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<title>CSS 100% Height</title>

<style>

html {
height: 100%;
_height:100%;
}
body {
margin: 0;
padding: 0;
height: 100%;
_height:100%;
}


#content {
background: #EEE;
margin: auto;

960px;
height: 100%;
_height:100%;


}

</style>
</head>

<body>
<div id="content">content
</div>
</body>
</html>

5、清除浮动

为什么浮动这么难?
因为浮动会使当前标签产生向上浮的效果,同时会影响到前后标签、父级标签的位置及 width height 属性。
而且同样的代码,在各种浏览器中显示效果也有可能不相同,这样让清除浮动更难了。

解决浮动引起的问题有多种方法,但有些方法在浏览器兼容性方面还有问题。

我根据自己的经验总结8种清除浮动的方法(测试已通过 ie chrome firefox opera,后面三种方法只做了解就可以了):

1,父级div定义 height
<style type="text/css">
.div1{background:#000080;border:1px solid red;/*解决代码*/height:200px;}
.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}

.left{float:left;20%;height:200px;background:#DDD}
.right{float:right;30%;height:80px;background:#DDD}
</style>

<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
</div>

<div class="div2">
div2
</div>
原理:父级div手动定义height,就解决了父级div无法自动获取到高度的问题。
优点:简单,代码少,容易掌握
缺点:只适合高度固定的布局,要给出精确的高度,如果高度和父级div不一样时,会产生问题
建议:不推荐使用,只建议高度固定的布局时使用
评分:★★☆☆☆

2,结尾处加空div标签 clear:both
<style type="text/css">
.div1{background:#000080;border:1px solid red}
.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}

.left{float:left;20%;height:200px;background:#DDD}
.right{float:right;30%;height:80px;background:#DDD}

/*清除浮动代码*/
.clearfloat{clear:both}
</style>

<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
<div class="clearfloat"></div>
</div>

<div class="div2">
div2
</div>
原理:添加一个空div,利用css提高的clear:both清除浮动,让父级div能自动获取到高度
优点:简单,代码少,浏览器支持好,不容易出现怪问题
缺点:不少初学者不理解原理;如果页面浮动布局多,就要增加很多空div,让人感觉很不爽
建议:不推荐使用,但此方法是以前主要使用的一种清除浮动方法
评分:★★★☆☆

3,父级div定义 伪类:after 和 zoom
<style type="text/css">
.div1{background:#000080;border:1px solid red;}
.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px}

.left{float:left;20%;height:200px;background:#DDD}
.right{float:right;30%;height:80px;background:#DDD}

/*清除浮动代码*/
.clearfloat:after{display:block;clear:both;content:"";visibility:hidden;height:0}
.clearfloat{zoom:1}

或者
.clear:after{content:'020';display:block;height:0;clear:both}
.clear{*zoom:1}


</style>

<div class="div1 clearfloat">
<div class="left">Left</div>
<div class="right">Right</div>
</div>

<div class="div2">
div2
</div>
原理:IE8以上和非IE浏览器才支持:after,原理和方法2有点类似,zoom(IE转有属性)可解决ie6,ie7浮动问题
优点:浏览器支持好,不容易出现怪问题(目前:大型网站都有使用,如:腾迅,网易,新浪等等)
缺点:代码多,不少初学者不理解原理,要两句代码结合使用,才能让主流浏览器都支持。
建议:推荐使用,建议定义公共类,以减少CSS代码。
评分:★★★★☆

4,父级div定义 overflow:hidden
<style type="text/css">
.div1{background:#000080;border:1px solid red;/*解决代码*/98%;overflow:hidden}
.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px;98%}

.left{float:left;20%;height:200px;background:#DDD}
.right{float:right;30%;height:80px;background:#DDD}
</style>

<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
</div>

<div class="div2">
div2
</div>
原理:必须定义width或zoom:1,同时不能定义height,使用overflow:hidden时,浏览器会自动检查浮动区域的高度
优点:简单,代码少,浏览器支持好
缺点:不能和position配合使用,因为超出的尺寸的会被隐藏。
建议:只推荐没有使用position或对overflow:hidden理解比较深的朋友使用。
评分:★★★☆☆

5,父级div定义 overflow:auto
<style type="text/css">
.div1{background:#000080;border:1px solid red;/*解决代码*/98%;overflow:auto}
.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px;98%}

.left{float:left;20%;height:200px;background:#DDD}
.right{float:right;30%;height:80px;background:#DDD}
</style>

<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
</div>

<div class="div2">
div2
</div>
原理:必须定义width或zoom:1,同时不能定义height,使用overflow:auto时,浏览器会自动检查浮动区域的高度
优点:简单,代码少,浏览器支持好
缺点:内部宽高超过父级div时,会出现滚动条。
建议:不推荐使用,如果你需要出现滚动条或者确保你的代码不会出现滚动条就使用吧。
评分:★★☆☆☆

6,父级div 也一起浮动
<style type="text/css">
.div1{background:#000080;border:1px solid red;/*解决代码*/98%;margin-bottom:10px;float:left}
.div2{background:#800080;border:1px solid red;height:100px;98%;/*解决代码*/clear:both}

.left{float:left;20%;height:200px;background:#DDD}
.right{float:right;30%;height:80px;background:#DDD}
</style>

<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
</div>

<div class="div2">
div2
</div>
原理:所有代码一起浮动,就变成了一个整体
优点:没有优点
缺点:会产生新的浮动问题。
建议:不推荐使用,只作了解。
评分:★☆☆☆☆

7,父级div定义 display:table
<style type="text/css">
.div1{background:#000080;border:1px solid red;/*解决代码*/98%;display:table;margin-bottom:10px;}
.div2{background:#800080;border:1px solid red;height:100px;98%;}

.left{float:left;20%;height:200px;background:#DDD}
.right{float:right;30%;height:80px;background:#DDD}
</style>

<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
</div>

<div class="div2">
div2
</div>
原理:将div属性变成表格
优点:没有优点
缺点:会产生新的未知问题。
建议:不推荐使用,只作了解。
评分:★☆☆☆☆

8,结尾处加 br标签 clear:both
<style type="text/css">
.div1{background:#000080;border:1px solid red;margin-bottom:10px;zoom:1}
.div2{background:#800080;border:1px solid red;height:100px}

.left{float:left;20%;height:200px;background:#DDD}
.right{float:right;30%;height:80px;background:#DDD}

.clearfloat{clear:both}
</style>

<div class="div1">
<div class="left">Left</div>
<div class="right">Right</div>
<br class="clearfloat" />
</div>

<div class="div2">
div2
</div>
原理:父级div定义zoom:1来解决IE浮动问题,结尾处加 br标签 clear:both
建议:不推荐使用,只作了解。
评分:★☆☆☆☆

文字缩略:

移动端字体
body { font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif; }

text-overflow:ellipsis;white-space:nowrap;overflow: hidden;

iframe自适应高度

overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:100%;100%;position:absolute;top:0px;left:0px;right:0px;bottom:0px

A元素点击去年虚线框:

a{
outline: none;
_noFocusLine: expression(this.hideFocus=true);
}

table添加tr边框

table添加样式:border-collapse:collapse;

原文地址:https://www.cnblogs.com/liubei/p/layoutOfCss.html