css中用于設(shè)置行高的屬性,line-height屬性。
第一,對CSS3的選擇器和類似header、nav、footer等標(biāo)簽不兼容,在不使用插件和JS處理的情況下,從純CSS的角度來切入,可以采用類名來做定義,這是常用的替代方案。
項目中,針對元素背景不支持顏色漸變的情況,折中的方案是給其一個最合適的背景色,這樣使得背景色和文字顏色能有個基本的對比和區(qū)分,不至于影響用戶的閱讀和正常瀏覽。
例如:
復(fù)制代碼代碼如下:
header.sub-hd{
position:relative;
height:40px;
background-image:-moz-linear-gradient(top, #13b9fd, #0183c3);
background-image:-o-linear-gradient(top, #13b9fd, #0183c3);
background-image:-webkit-gradient(linear,left top,left bottom,color-stop(0,#13b9fd),color-stop(1,#0183c3));
box-shadow:inset 0 1px 4px #6cd5ff;
-moz-box-shadow:inset 0 1px 4px #6cd5ff;
-webkit-box-shadow:inset 0 1px 4px #6cd5ff;
text-align:center;
font-size:15px;
background-color:#099ddf;/*opera mobile不支持漸變的折中方案*/
}
針對IE6不識別html5標(biāo)簽的方法
復(fù)制代碼代碼如下:
<script>
(function(){
var e =”abbr,article,aside,audio,canvas,datalist,details,dialog,eventsource,figure,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,time,video”.split(‘,’),i=e.length;
while(i–){
document.createElement(e[i]);
}
})();
</script>
第二,我們經(jīng)常使用line-height屬性,定義行高,尤其是需要設(shè)置垂直居中的時候,常常讓height屬性與line-height屬性相同。而且其是可以繼承的,一篇文章的父標(biāo)簽定義了行高屬性,其子段落元素就不需要再次進行聲明。可是,也有例外的情況。比如在Opera Mobile瀏覽器,就必須要再次聲明,才能生效。
例子:
1,html代碼
復(fù)制代碼代碼如下:
<div class=”test”><h2>測試標(biāo)題在Opera Mobile下的寬高</h2></div>
2,CSS代碼
復(fù)制代碼代碼如下:
<style>
.test{width:100%;height:40px;line-height:40px!important;background-color:black;color:#eee; opacity:0.5;}
.test h2{border:1px solid red;}
</style>
從手機上看頁面效果:不居中!
通過觀察紅色邊框大小,知道內(nèi)標(biāo)簽h2的呈現(xiàn)高度與實際呈現(xiàn)的line-height一致,都不是我們父div定義的數(shù)值。
然后,我們給h2加上line-height屬性值,可以設(shè)置為line-height:inherit或者line-height:40px;
1,CSS代碼:
復(fù)制代碼代碼如下:
<style>
.test{width:100%;height:40px;line-height:40px!important;background-color:black;color:#eee; opacity:0.5;}
.test h2{line-height:inherit;border:1px solid red;}
</style>
新聞熱點
疑難解答