div css布局解决浏览器兼容及优化等实用技巧

所属分类: 网页制作 / CSS 阅读数: 559
收藏 0 赞 0 分享
DIV 上下居中用 line-height
Html代码

复制代码
代码如下:

<style type="text/css">
div.v-align {line-height: 200px;height: 200px;border: 1px solid red;}
</style>
<div class="v-align">垂直居中</div>

清除浏览器默认样式
1.用其他有名的库来清除(如:YUI)
2.自已清除 如:
* {padding: 0;margin:0;}

让A支持所有浏览器
Html代码

复制代码
代码如下:

<style type="text/css">
a.agideo:link, a.agideo:visited {font-weight: bold;text-decoration: none;color: green;}
a.agideo:hover, a.agideo:active {font-weight: bold;text-decoration: none;color: red;}
</style>
<a href="http://www.agideo.com" class="agideo">捷道数码</a>
<style type="text/css">
a.google:hover, a.google:active {font-weight: bold;text-decoration: none;color: red;}
a.google:link, a.google:visited {font-weight: bold;text-decoration: none;color: green;}
</style>
<a href="http://www.google.com" class="google">Google</a>

A 伪类定义顺序记忆法
LoVe:HAte(爱恨)
IMG vertical-align
vertical-align : auto | baseline | sub | super | top | text-top | middle | bottom | text-bottom | length
baseline :默认值。将支持 valign 特性的对象的内容与基线对齐
sub 垂直对齐文本的下标
super 垂直对齐文本的上标
top : 将支持 valign 特性的对象的内容对象顶端对齐
text-top : 将支持 valign 特性的对象的文本与对象顶端对齐
middle : 将支持 valign 特性的对象的内容与对象中部对齐
bottom : 将支持 valign 特性的对象的内容与对象底端对齐
text-bottom : 将支持 valign 特性的对象的文本与对象顶端对齐
length : 由浮点数字和单位标识符组成的长度值 | 百分数。可为负数。定义由基线算起的偏移量。基线对于数值来说为0,对于百分数来说就是0%。请参阅 长度单位。目前IE尚未实现此参数
Html代码 

复制代码
代码如下:

<style type="text/css">
div.v-align {border: 1px solid red;line-height: 40px;float: left;font-size: 11px;height: 40px;}
div.v-align img.only-img {float:left;}
div.v-align.has-img {padding-top: 10px;height: 30px;}
div.v-align.has-img-and-text img.has-text {
vertical-align: -4px !important;vertical-align: middle;margin-top: -3px !important;margin-top: 0px;}
div.v-align.has-img-and-text {
padding-top: 0px !important;padding-top: 10px;height: 40px !important;height: 30px;}
</style>
<div class="v-align">正常</div>
<div class="v-align has-img"><img src="http://dl.iteye.com/upload/attachment/162320/1f89f269-580c-304b-975a-e6004b9893ce.png" class="only-img" /></div>
<div class="v-align has-img-and-text"><img src="./accept.png" align="absmiddle" class="has-text" />有图片有字</div>

垂直Margin 叠加
Html代码

复制代码
代码如下:

<style tyep="text/css">
body, div {padding:0;margin:0;}
div, span {padding: 10px;margin: 10px;border: 10px solid #000;}
#div1 {background-color: red;}
#div2 {background-color: green;}
#div3 {background-color: blue;}
</style>
<div id="div1"><div>div1</div></div>
<div id="div2"><div>div2</div></div>
<div id="div3"><span>span1</span><span>span2</span></div>

Background position
Html代码

复制代码
代码如下:

<style tyep="text/css">
body, div {padding:0;margin:0;}
div {border:1px solid #ccc;height: 200px;width: 300px;}
#div1 {background: url(http://dl.iteye.com/upload/attachment/162316/eee477c8-1fcc-3a41-874f-065f0d65a422.jpg) no-repeat 20px 20px;}
#div2 {background: urlhttp://dl.iteye.com/upload/attachment/162316/eee477c8-1fcc-3a41-874f-065f0d65a422.jpg) no-repeat 20% 20%;}
#div3 {background: url(http://dl.iteye.com/upload/attachment/162318/8be0e495-2d0a-3ec2-9f5f-7465eb8c23ae.jpg) no-repeat 20% 20%;}
</style>
<div id="div1">20px</div><br/>
<div id="div2">20%</div><br/>
<div id="div3">20%</div>

Float 占地问题
1.父子TAG都Float以父能包含子
Html代码

复制代码
代码如下:

<style type="text/css">
div.parent {border: 1px solid red;width: 600px;float: left; }
div.child {border: 1px solid green;float: left;}
</style>
<div class="parent">
<div class="child">child</div>
<div class="child">child</div>
<div class="child">child</div>
</div>

Html代码

复制代码
代码如下:

<style type="text/css">
div.parent {border: 1px solid red;width: 600px;float: left; }
div.child {border: 1px solid green;float: left;}
</style>
<div class="parent">
<div class="child">child</div>
<div class="child">child</div>
<div class="child">child</div>
</div>

2.最后一个子TAG后加一clear的无语义tag
Html代码

复制代码
代码如下:

<style type="text/css">
div.parent {border: 1px solid red;width: 600px;}
div.child {border: 1px solid green;float: left;}
div.clear {clear: both;}
</style>
<div class="parent">
<div class="child">child</div>
<div class="child">child</div>
<div class="child">child</div>
<div class="clear"></div>
</div>

例子7:
Html代码

复制代码
代码如下:

<style type="text/css">
div.parent {border: 1px solid red;width: 600px;}
div.child {border: 1px solid green;float: left;}
div.clear {clear: both;}
</style>
<div class="parent">
<div class="child">child</div>
<div class="child">child</div>
<div class="child">child</div>
<div class="clear"></div>
</div>
更多精彩内容其他人还在看

CSS入门教程:计算CSS盒模型宽和高

 出处:当我们布局一个网页的时候,经常会遇到这样的一种情况,那就是最终网页成型的宽度或是高度会超出我们预先的计算,其实就就是所谓的CSS的盒模型造成的。 #test{margin:10px;padding:10px;width:100px;height:100px;}
收藏 0 赞 0 分享

在IE流览器中正确显示PNG透明图片

  png图片有很好的品质。阴影效果也不会有杂边,很流畅。如果插入网页的话可以给网站内容增色不少!更重要的是在不增加图片容量大小的情况下提高了页面的图片的质量。对于有复杂背景,如:在有颜色过度背景上插入不规则边框的图片带来极大很便利!   但目前IE中对于插入
收藏 0 赞 0 分享

CSS教程:DIV底部放置文字

  css对文字的布局上没有靠容器底部对齐的参数,目前使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近的距离还可以精确到像素,自己可以调节,是不是很不错呢?
收藏 0 赞 0 分享

如何用CSS让文字居于div的底部

  这个问题是别人提出的,因为css对文字的布局上没有靠容器底部对齐的参数,(或许有但是我没有发现)不过目前我使用的一个不错的方法也比较好.就是用position属性来解决,看下面的代码,我用position的相对和绝对定位功能也轻松的实现了,文字靠近div低部对齐,并且靠近
收藏 0 赞 0 分享

从A页面连接到B页面后并直接把B页面的隐藏层显示

  这个效果实现的是,在B页面里有两个层,一个显示层,我们暂定名c层,一个是隐藏层,我们暂定名d层,单独进B页面的时候,c层显示,d层隐藏,然而从A页面连接到B页面的时候,则是让d层显示,c层隐藏,我觉得这个效果对网页设计者以后会有很大帮助,现在把代码发出来,
收藏 0 赞 0 分享

CSS样式表定义标签li前面样式

定义LI前面的小点样式 view plaincopy to clipboardprint? 语法: list-style-type : disc | circle | square | decimal | lower-roman | upper-roman | lowe
收藏 0 赞 0 分享

符合标准的div css制作的弹出菜单

本文介绍了五款符合标准的div css制作的弹出菜单,而且不含有js的. NO.1最基本的:二级dropdown弹出菜单 <!DOCTYPE html PUB
收藏 0 赞 0 分享

CSS实现在文章每段后面加入带连接的隐藏文字

代码主要理解3个参数:createElement、createTextNode、appendChild。这3个js参数分别是创建元素、创建字符、追加节点。代码原理:循环页面段落标签<p>,创建连接元素<a>,创建要显示的连接字符,用SetAttribute
收藏 0 赞 0 分享

CSS:浏览器特定选择器介绍

当你想在一个浏览器里改变样式而不像在其他浏览器中改变时,这些选择器很有用。 IE6以下 *html{} IE 7 以下 *:first-child html {} * html {} 只对IE 7 *:first-child html {} 只对IE 7
收藏 0 赞 0 分享

WEB标准学习,认识两种网页声明的含义

即网页标准推出来以后,我们时常会看到两种不同的网页的声明,一个是Dhtml,一个是Xhtml。如下所示: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "ht
收藏 0 赞 0 分享
查看更多