Table of Contents
骨灰级清除浮动
内联元素相连之间存在间隙问题
块级元素包裹内联元素的时候,总会出现几像素的差问题
CSS垂直居中方法
CSS Hack
ie6.7不支持box-sizing: border-box问题
ul中li下面的间隔线用li布局边框问题
ie8及以下的浏览器不支持:befor.:after问题
低版本浏览器下position:fixed闪动问题
IE6双倍margin,padding边距的问题
IE6中设置宽高位10px的时候出现的是长方形问题
IE6无法识别伪对象:first-letter/:first-line问题
IE6下忽略!important问题
父元素与子元素之间的margin-top问题(BFC问题)
元素浮动导致父元素塌陷问题
IE6挨着的p元素产生3像素差值问题
相邻的块状元素margin叠加问题(BFC问题)
Home Web Front-end CSS Tutorial CSS compatibility and BUG handling

CSS compatibility and BUG handling

Feb 18, 2017 pm 03:04 PM

骨灰级清除浮动

.clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
    overflow: hidden;
    *zoom:1; //兼容ie}
Copy after login

内联元素相连之间存在间隙问题

原因:内联元素是当做字体来处理的,字体之间是有间隔的

解决方法:

1.多个标签写在一行

2.将要闭合标签的地方与开始标签的地方重合

3.使用注释头尾相连

4.在父级上写:font-size:0;

5.使用display:block(img是内联元素)

6.使用letter-spacing属性

块级元素包裹内联元素的时候,总会出现几像素的差问题

<!--例子1--><p><img src="http://images.cnblogs.com/cnblogs_com/zqzjs/757818/o_u=3986871593,628400456_fm=21_gp=0.jpg"></p><!--例子2--><ul><li><img src="http://images.cnblogs.com/cnblogs_com/zqzjs/757818/o_u=3986871593,628400456_fm=21_gp=0.jpg"></li></ul><!--例子3--><p><span>asdasdasd</span></p>
Copy after login

解决方法:设置内联元素属性:display:block;

CSS垂直居中方法

使用时一定要给出元素宽高

 width: 200px;
        height: 200px;
        margin: auto;
        position: absolute;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
Copy after login

CSS Hack

指的是针对不同的浏览器写对应的CSS

有三种hack方式

1.html hack(添加不同的类来区别)

<!--[if lt IE 7 ]><html class="ie6" lang="zh-cn"><![endif]--><!--[if IE 7 ]><html class="ie7" lang="zh-cn"><![endif]--><!--[if IE 8 ]><html class="ie8" lang="zh-cn"><![endif]--><!--[if IE 9 ]><html class="ie9" lang="zh-cn"><![endif]-->
Copy after login

2.选择器 hack

* html .test{color:#090;} /* For IE6 */* + html .test{color:#ff0;} /* For IE7 */
Copy after login

3.属性hack

color:#fff\0; /*:选择IE8+和Opera*/color:#090\9; /* For IE浏览器 */*color:#f00; /* For IE7 */_color:#ff0; /* For IE6 */
Copy after login

ie6.7不支持box-sizing: border-box问题

解决:使用https://github.com/Schepp/box-sizing-polyfill这个垫片,稳定性差

注意:*behavior: url(../resource/js/lab/boxsizing.htc);这个URL是相对于HTML页面的!!

ul中li下面的间隔线用li布局边框问题

在IE低版本下有bug,会多出li的宽高

解决:间隔线使用li的border去做

ie8及以下的浏览器不支持:befor.:after问题

使用left:expression(eval(document.documentElement.scrollLeft))与top:expression(eval(document.documentElement.scrollTop))

.leftTop{
    position:absolute;
    left:expression(eval(document.documentElement.scrollLeft));
    top:expression(eval(document.documentElement.scrollTop));}
Copy after login

低版本浏览器下position:fixed闪动问题

解决:

*html{ 
  background-image:url(about:blank); 
  background-attachment:fixed;}
Copy after login

IE6双倍margin,padding边距的问题

内部元素一旦浮动,就会出现双倍的BUG

解决:给内部元素添加display:inline属性

IE6中设置宽高位10px的时候出现的是长方形问题

这个现象的另一种情况是:在IE6中定义比较小的高度问题。

原因:IE6有默认行高

解决:使用font-size:0;line-height:0;

IE6无法识别伪对象:first-letter/:first-line问题

类似这样解决:
p:first-letter {}

在first-letter与"{"间增加空格

IE6下忽略!important问题

如下写法在IE6下不起作用

p{
  color:#f00!important;
  color:#000;}
Copy after login

解决:更改写法

p{color:#f00!important;}p{color:#000;}
Copy after login

父元素与子元素之间的margin-top问题(BFC问题)

现象:给第一个子元素设置margin-top属性后,父元素也会下移

代码示例:

 <style type="text/css">
        .wrapper {
      position: relative;
      width: 500px;
      height: 500px;
      background-color: #ddd;
     }
    .content{
        background-color:#6699FF;
        width:200px;
        height:200px;
    } 
    </style><p class="wrapper">
        <p class="content"></p>
    </p>
Copy after login

解决:

1、修改父元素的高度,增加padding-top样式模拟(padding-top:1px;常用)

2、为父元素添加overflow:hidden;样式即可(完美)

3、为父元素或者子元素声明浮动(float:left;可用)

4、为父元素添加border(border:1px solid transparent可用)

5、为父元素或者子元素声明绝对定位

元素浮动导致父元素塌陷问题

见例子:

<p class=&#39;outer&#39; style="width: 300px;background-color: gray">
        
        <p class=&#39;innner&#39; style="width: 100px;height: 100px;background-color: blue;float: left;"></p>

    </p>
Copy after login

解决方法:

1.给父元素添加overflow:hidden属性

2.给父元素添加清除浮动伪类

.outer:after {
        content: ".";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
        overflow: hidden;
    }
Copy after login

IE6挨着的p元素产生3像素差值问题

解决:使用绝对定位然后内补边

相邻的块状元素margin叠加问题(BFC问题)

见例子:

p{
        margin-bottom: 100px;
        margin-top: 100px;
    }

... <p>
        <p>asdasdasdas中国</p>
        <p>asdasdasdas中国</p>
        <p>asdasdasdas中国</p> 
    </p>
Copy after login

结果p直接的margin发生了合并变成了50px。

解决:给最后一个p元素添加left/right浮动,触发BFC。

更多CSS的兼容性与BUG处理相关文章请关注PHP中文网!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

What the Heck Are npm Commands? What the Heck Are npm Commands? Mar 15, 2025 am 11:36 AM

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

See all articles