日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > CSS >内容正文

CSS

再发些CSS常用技巧和兼容方案

發布時間:2025/7/25 CSS 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 再发些CSS常用技巧和兼容方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

基本上配合我以前所發的:Html+css小技巧收集,制作網頁中經常 .. 和瀏覽器兼容性測試工具 可以做到所向無敵。

CSS HACK以下兩種方法幾乎能解決現今所有HACK.
1, !important
隨著IE7對!important的支持, !important 方法現在只針對IE6的HACK.(注意寫法.記得該聲明位置需要提前.)
CSS代碼

引用

<style>
#wrapper {
width: 100px!important; /* IE7+FF */
width: 80px; /* IE6 */
}
</style>

?

2, IE6/IE77對FireFox
*+html 與 *html 是IE特有的標簽, firefox 暫不支持.而*+html 又為 IE7特有標簽.
CSS代碼

引用

<style>
#wrapper { width: 120px; } /* FireFox */
*html #wrapper { width: 80px;} /* ie6 fixed */
*+html #wrapper { width: 60px;} /* ie7 fixed, 注意順序 */
</style>


注意:
*+html 對IE7的HACK 必須保證HTML頂部有如下聲明:
XML/HTML代碼
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” ”http://www.w3.org/TR/html4/loose.dtd”>?

萬能 float 閉合(非常重要!)關于 clear float 的原理可參見 [How To Clear Floats Without Structural Markup]
將以下代碼加入Global CSS 中,給需要閉合的div加上 class=”clearfix” 即可,屢試不爽.
CSS代碼

引用

<style>
/* Clear Fix */
.clearfix:after {
content:“.”;
display:block;
height:0;
clear:both;
visibility:hidden;
}
.clearfix {
display:inline-block;
}
/* Hide from IE Mac \*/
.clearfix {display:block;}
/* End hide from IE Mac */
/* end of clearfix */
</style>



三、其他兼容技巧(再次啰嗦)
1, FF下給 div 設置 padding 后會導致 width 和 height 增加, 但IE不會.(可用!important解決)
2, 居中問題.
1).垂直居中.將 line-height 設置為 當前 div 相同的高度, 再通過 vertical-align: middle.( 注意內容不要換行.)
2).水平居中. margin: 0 auto;(當然不是萬能)
3, 若需給 a 標簽內內容加上 樣式, 需要設置 display: block;(常見于導航標簽)
4, FF 和 IE 對 BOX 理解的差異導致相差 2px 的還有設為 float的div在ie下 margin加倍等問題.
5, ul 標簽在 FF 下面默認有 list-style 和 padding . 最好事先聲明, 以避免不必要的麻煩. (常見于導航標簽和內容列表)
6, 作為外部 wrapper 的 div 不要定死高度, 最好還加上 overflow: hidden.以達到高度自適應.
7, 關于手形光標. cursor: pointer. 而hand 只適用于 IE.
PS:搞公司的站,IE6,IE7,FF下這些問題頭大死了,后來經過這個文章的介紹終于解決了!
貼上代碼:
CSS代碼

引用

/* FF */
.submitbutton {
float:left;
width: 40px;
height: 57px;
margin-top: 24px;
margin-right: 12px;
}
/* IE6 */
*html .submitbutton {
margin-top: 21px;
}
/* IE7 */
*+html .submitbutton {
margin-top: 21px;
}



css完美解決圖片尺寸問題

經測試有效,設定圖片最寬最高max-width,max-height在高版本的瀏覽器中都支持,但愚蒙的IE6不支持,這個css很好的解決了各個瀏覽器下圖片尺寸比例的控制問題

定義css樣式如下

引用

img{vertical-align: middle;max-width: 630px; width: expression(this.width >630 && this.height < this.width ? 630: true); }



png圖片背景透明[修正全瀏覽器]

如果在網頁中直接插入png圖片想使其透明只需加入以下js代碼,整個頁面內的所有直接插入的png圖片都可以實現透明:

引用

<script language="JavaScript">
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
if ((version >= 5.5) && (document.body.filters))
{
for(var j=0; j<document.imagess.length; j++)
{
var img = document.imagess[j]
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
{
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
var imgStyle = "display:inline-block;" + img.style.cssText
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
j = j-1
}
}
}
}
window.attachEvent("onload", correctPNG);
</script>



--------------------------------------------------------------------

如果是想使用png做背景透明的話,需要用到css濾鏡和hack:

引用

html>body .png { background:url(1.png); width:300px; height:100px; border:#000 solid 1px;}

/* ie6 */* html .png { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='1.png');
background:none; width:300px; height:100px; border:#000 solid 1px;}



============================================

IE6不支持PNG半透明圖片的缺陷為web設計帶來了極大的不便,之前曾經介紹過用濾鏡+hack的方法實現顯示PNG,不過實現起來相當繁瑣。還有一種網上比較流行的方法,更加簡便,下面詳細介紹這種方法:

把以下代碼保存為correctpng.js

引用

function correctPNG()
{
for(var i=0; i<document.imagess.length; i++)
{
var img = document.imagess
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
{
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
var imgStyle = "display:inline-block;" + img.style.cssText
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
i = i-1
};
};
};

if(navigator.userAgent.indexOf("MSIE")>-1)
{
window.attachEvent("onload", correctPNG);
};

在網頁的頭部引用一下

<SCRIPT language=JavaScript
src="correctpng.js"
type=text/javascript></SCRIPT> ?



使用的時候直接用img標簽即可。注:使用此代碼后,PNG透明的邊會變成空,所以為了防止頁面錯亂,應設置好png的寬和高

轉載于:https://www.cnblogs.com/Miton/archive/2011/06/01/2067131.html

總結

以上是生活随笔為你收集整理的再发些CSS常用技巧和兼容方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。