首頁  >  文章  >  web前端  >  html和css中常見的瀏覽器相容性問題

html和css中常見的瀏覽器相容性問題

小云云
小云云原創
2017-11-16 17:01:163475瀏覽

在html開發中我們一定會用到css,但是有時候開發中,並不是我們所想的那麼順利,比如說html和css中常見的瀏覽器兼容性問題,那麼針對這個瀏覽器兼容性問題我們這節內容就來教大家如何去解決。

1、居中問題

div裡的內容,IE預設為居中,而FF預設為左對齊,可以嘗試增加程式碼:

margin: 0 auto;

2、高度問題

兩上下排列或嵌套的div,上面的div設定高度(height),如果div裡的實際內容大於所設高度,在FF中會出現兩個div重疊的現象;但在IE中,下面的div會自動給上面的div讓出空間所以為避免出現層的重疊,高度一定要控制恰當,或者乾脆不寫高度,讓他自動調節,比較好的方法是height:100% ;但當這個div裡面一級的元素都float了的時候,則需要在div塊的最後,閉和前加一個沉底的空div,對應CSS是:

float_bottom {clear:both;height:0px;font-size:0px;padding:0;margin:0;border:0;line-height:0px;overflow:hidden;}

3、clear: both;

不想受到float浮動的,就在div中寫入clear:both;

4、IE浮動margin產生的雙倍距離

01#box {
 float:left;
width:100px;
 margin:0 0 0 100px; //这种情况之下IE会产生200px的距离
display:inline; //使浮动忽略
}

5、padding問題

FF設定padding 後,div會增加height 和width,但IE不會(* 標準的XHTML1.0 定義dtd 好像一致了)高度控制恰當,或嘗試使用height:100%;寬度減少使用padding但根據實際經驗,一般FF和IE的padding 不會有太大區別,div 的實際寬= width + padding ,所以div寫全width 和padding,width用實際想要的寬度減去padding 定義。
 

6、div嵌套時y軸上padding和marign的問題
 

FF裡y 軸上子div 到父div 的距離為父padding + 子marign

IE裡y 軸上子div 到父div 的距離為父padding 和子marign 里大的一個

FF裡y 軸上父padding=0 且border=0 時,子div到父div 的距離為0,子marign 作用到父div 外面
 

7、padding,marign,height,width的傻瓜式解決技巧

#注意是技巧,不是方法:

寫好標準頭

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=” 
高尽量用padding,慎用margin,height尽量补上100%,父级height有定值子级height不用100%,子级全为浮动时底部补个空clear:both的div宽尽量用margin,慎用padding,width算准实际要的减去padding

8、列表類別

1. ul標籤在FF中預設是有padding值的,而在IE中只有margin有值

先定義ul {margin:0;padding:0;}

#2.ul和ol列表縮排問題消除ul、ol等列表的縮排時,樣式要寫成: {list-style:none;margin:0px;padding:0px;}

9、顯示類別(display:block,inline)

# 1. display:block,inline兩個元素

    display:block; //可以模擬內嵌元素為區塊元素

    display:inline; //實現同一行排列的效果

    display:table; //for FF,類比table的效果

    display:block 區塊元素,元素的特徵是:

    總是在新行上開始;高度,行高以及頂和底邊距都可控制;寬度缺省是它的容器的100%,除非設定一個寬度

    dc6dce4a544fdca2df29d5ac0ea9906b,e388a4556c0f65e1904146cc1a846bee,4a249f0d628e2318394fd9b75b4636b1 ,ff9c23ada1bcecdd1a0fb5d5a0f18437,ff6d136ddc5fdfeffaf53ff6ee95f185 和25edfb22a4f469ecb59f1190150159c6 是塊元素的例子

    display:inline 是將元素顯示為行內元素,元素的特徵是:和其他元素都在一行上;高,行高及頂和底邊距不可改變;寬度就是它的文字或圖片的寬度,不可改變。 45a2772a6b6107b401db3c9b82c049c2,3499910bf9dac5ae3c52d5ede7383485,2e1cf0710519d5598b1f0f14c36ba674,d5fd7aea971a85678ba271703566ebfd,a1f02c36ba31691bcfe87b2722de723b,8e99a69fbe029cd4e2b854e244eab143 和907fae80ddef53131f3292ee4f81644b 是inline 元素的例子

2.滑鼠手指狀顯示

全部用標準的寫法cursor: pointer;

10、背景、圖片類別

1. background顯示問題

#全部注意補齊width,height 屬性

2.背景透明問題

    IE: filter: progid: DXImageTransform.Microsoft.Alpha(style=0,opacity=60);

#    IE: filter: alpha(opacity=10);

    FF: opacity:0.6;

    FF: -moz-opacity:0.10;

#    FF: -moz-opacity:0.10;

c   # 個都寫,並將opacity屬性放在下面

11、min-height最小高度的實作(相容IE6、IE7、FF)

作用是:當容器的內容較少時,能保持一個最小的高度,以免破壞了佈局或UI設計效果。而當容器內的內容增加的時候,容器能夠自動的伸展以適應內容的變化。

#mrjin {
               background:#ccc;
               min-height:100px;
               height:auto !important;
                height:100px;
               overflow:visible;
}

12、著名的Meyer Reset(重設)

html, body, div, span, applet, object, iframe,h1, h2, h3, h4, h5, h6,p, blockquote, pre,a, abbr,
 acronym, address, big, cite, code,del, dfn, em, font, img, ins, kbd, q, s, samp,small, strike, 
strong, sub, sup, tt, var,dl, dt, dd, ol, ul, li,fieldset, form, label, legend,table, caption, tbody, tfoot, thead, tr, th, td
{ 
  margin: 0; 
  padding: 0; 
  border: 0; 
  outline: 0; 
  font-weight: inherit; 
  font-style: inherit; 
  font-size: 100%; 
  font-family: inherit; 
  vertical-align: baseline; 
  } 
  :focus { 
   outline: 0; 
  } 
  body { 
   line-height: 1; 
   color: black; 
   background: white; 
  } 
  ol, ul { 
   list-style: none; 
  } 
  table { 
   border-collapse: separate; 
   border-spacing: 0; 
  } 
  caption, th, td { 
   text-align: left; 
   font-weight: normal; 
  } 
blockquote:before, blockquote:after, 
q:before, q:after { 
  content: ""; 
} 
blockquote, q { 
  quotes: "" ""; 
}

13、跨瀏覽器的CSS透明度

.transparent { 
  opacity: 0.7; 
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=70)";
  filter: alpha(opacity=70); 
  -moz-opacity: 0.7; 
  -khtml-opacity: 0.7;

14、文字陰影( CSS3)

.text { text-shadow: 1px 1px 1px #666; filter: Shadow(Color=#666666, Direction=135, Strength=5); }

15、Box陰影(CSS3)

box { box-shadow: 5px 5px 5px #666;  -moz-box-shadow: 5px 5px 5px #666;  -webkit-box-shadow: 5px 5px 5px #666; }

16、Sticky Footer (讓頁腳永遠停靠在頁面底部,而不是根據絕對位置)

<div id="wrap"> 

 <div id="main" class="clearfix"></div> 

</div> 

  <div id="footer"> </div>

CSS:

 * { margin:0; padding:0; }  

 html, body, #wrap { height: 100%; } 

 body > #wrap {height: auto; min-height: 100%;} 

 #main { padding-bottom: 150px; }   

 #footer { 

  position: relative; 

     margin-top: -150px;  

     height: 150px; 

   clear:both;}  

  .clearfix:after {content: "."; 

     display: block; 

         height: 0; 

         clear: both; 

         visibility: hidden;} 

  .clearfix {display: inline-block;} 

* html .clearfix { height: 1%;} 

 .clearfix {display: block;}

17、iframe元素內嵌頁面如何去除繼承的html及body背景色/背景圖片
 

iframe元素的功能是在一个文档里内嵌一个文档,创建一个浮动的帧。内嵌文档时一个完整的页面,有HTML,BODY等属性。这样遇到了一个问题,如果样式表中对BODY定义过背景色/背景图片,那么内嵌文档将全部继承过来。所以如何去掉背景色和背景图片:

【1】去掉背景色:filter:Chroma(Color=white);

举例:

<iframe width="100%" height="400" marginwidth="0" marginheight="0"
 scrolling="no" frameborder="0" leftmargin="0" topmargin="0" style="filter:Chroma(Color=white);" ></iframe>

【2】去掉背景图片:

举例:

<iframe width="100%" height="400" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" leftmargin="0"
 topmargin="0" style="filter:Chroma(Color=white);" allowTransparency="true" ></iframe>

注意:内嵌页面同时也要增加BODY属性:

<body bgcolor="transparent" style=&#39;background:transparent&#39;>

18、为什么web标准中无法设置IE浏览器滚动条颜色了?

原来样式设置:

body{2scrollbar-face-color:#f6f6f6; 3scrollbar-highlight-color:#fff;
 4scrollbar-shadow-color:#eeeeee; 5scrollbar-3dlight-color:#eeeeee;
  6scrollbar-arrow-color:#000; 7scrollbar-track-color:#fff; 8scrollbar-darkshadow-color:#fff; }

解决办法是将body换成html。

19、为什么中火狐浏览器下文本无法撑开容器的高度?

标准浏览器中固定高度值的容器是不会象IE6里那样被撑开的,那我又想固定高度,又想能被撑开需要怎样设置呢?办法就是去掉height设置min-height:200px; 这里为了照顾不认识min-height的IE6 可以这样定义:

div { height:auto!important; height:200px; min-height:200px; }

20、如何定义1px左右高度的容器?
 

IE6下这个问题是因为默认的行高造成的,解决的方法也有很多,例如:overflow:hidden | zoom:0.08 | line-height:1px
 

21、怎么样才能让层显示在FLASH之上呢?
 

解决的办法是给FLASH设置透明:

<a href="http://www.chinaz.com/">:</a>
<pre line="1">
<param name="wmode" value="transparent" />

22、怎样使一个div层居中于浏览器中?

div {
position:absolute;
top:50%;
left:50%;
margin:-100px 0 0 -100px;
width:200px;
height:200px;
border:1px solid red;
}

23、怎样使div背景透明?

首先,需要这两个层都是兄弟关系,其次,这两个层都需要绝对定位。举个例子:

 body {
              font:normal 12px/30px Verdana;   }
       #test {
              position:relative;
              width:400px;
              height:50px;
                     }
       #inner {
              z-index:2;
              position:absolute;
              top:10px;
              left:10px;
              width:380px;
              height:30px;
              color:#003;
       font-weight:bold;
              text-align:center;
       }
       #transbox {
              z-index:1;
              position:absolute;
              top:0px;
              left:0px;
              width:400px;
              height:50px;
        background:#eef;
border:1px solid #a00;
              filter:alpha(opacity=40);
              -moz-opacity:0.4;
        opacity:0.4;
       }

e388a4556c0f65e1904146cc1a846bee这里很多的文字,这里很多的文字,这里很多的文字,这里很多的文字,94b3e26ee717c64999d7867364b1b4a3

24、怎样去掉选中时的虚线框?

利用onfocus="this.blur();"例如:dd8b186726c0b45d7b00732cf0e1fe3e测试5db79b134e9f6b82c0b36e0489ee08ed

25、ie6下png背景显示问题?

针对ie6下png背景显示问题,CSS中可以这样解决:

_background:none;_filter:progid
:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=crop, src=’http://www.0351zhuangxiu.com/uploads/images/bj.jpg’);

26、文字与表单对齐方法?
 

设置表单元素第一字体为Tahoma(Verdana系列也可),并设置vertical-align:middle.建设大家把这个约定写入CSS RESET中,能减小很多麻烦:

body,button,input,select,textarea{font:12px/1.5 tahoma,arial,sans-serif; vertical-align:middle}


27、optgroup标签的用法?

optgroup标签,鲜为人知,它对提升选择表单用户体验很有帮助。就是可以在有很多选项时,对这些选项分组:例子:

<select id="selectId">
<optgroup label="GROUP ONE">
<option value="1">one select</option>
<option value="2">two select</option>
</optgroup>
<optgroup label="GROUP TWO">
<option value="3">three select</option>
<option value="4">four select</option>
</optgroup>
</select>

28、文字与图片垂直居中对齐方法?

为图片与文字的共同父元素所有的后代元素定义*{vertical-align:middle};例如:

<p>我要的坚强<img src="i/image.gif" /></p>

只需定义p*{vertical-align:middle}即可使文字与图片同行垂直居中.

29、文章标题列表中日期居右显示的两种方法?

方法A相对方法B省资源,但比方法B要多写两句代码,使用时请视情况而定:

方法A:

<p>这是文章标题<span>2010-10-10</span></p>

然后定义p和span的样式:

p{ position:relative}
p span{ position:absolute; right:0}

方法B:

<p><span>2010-10-10</span>这是文章标题</p>

然后定义span右浮动:

p span{float:right}

30、ie6下max/min-width/height实现?
 

ie6下max/min-width/height实现,_width: expression_r(this.width >600 ? “600px” : true);,height同理.
 

31、空白外边距互相叠加的问题?

一般通过添加透明边框或者1px的内边距避免;

其一,为外围元素定义透明边框.具体到本例,即在样式div中加入border:1px solid transparent;

其二,为外围元素定义内边距填充..具体到本例,即在样式div中加入padding:1px;

例如:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>空白边距叠加demo@Mr.Think</title>
<style>
body{width:300px; font-family:&#39;微软雅黑&#39;; font-size:1em; text-indent:10px; line-height:1.25}
div{background:#a40000;margin:10px}
div p{background:#eee;margin:15px}
</style>
</head>
<body>
<div><p>空白边距叠加demo@Mr.Think</p></div>
</body>
</html>

32、网页设计中的默认字体

font: 12px/1.5 Tahoma, Helvetica, Arial, sans-serif;

说明:line-height采用1.5, 也就是18px. 这是淘宝视觉规范中定义的行高,对于12px字体,这个行高看起来很舒服。font-family默认采用Tahoma. Tahoma是英文Windows操作系统的默认字体,这个字体比较均衡,显示中英文混排很不错,是经久耐看的一款字体。

33、浏览器兼容——常用的css hack

/*第一种*/
.title{ height:200px;

    *height:200px;

    _height:200px; }

/*第二种*/
.title{ height:200px;

    *height:200px !important;

    *height:200px; }

/*第三种*/
.title{ height:200px; }

    *html.title{ height:200px;}

    *+html.title{ height:200px;}

虽然篇幅有点长,但是这里面的内容对我们真的很有用,大家赶紧收藏起来吧。

相关教程:

用html5和css3写出登录页面教程

CSS如何实现文字颜色渐变的实例

CSS3中关于“渐变”兼容性解决方案详解

以上是html和css中常見的瀏覽器相容性問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn