Home Web Front-end CSS Tutorial Bugs in CSS that you must know and how to fix them

Bugs in CSS that you must know and how to fix them

Apr 25, 2017 pm 02:39 PM

Compared to many programming languages, CSS is a fairly easy language to learn. Its syntax is simple and clear, and due to its expressive nature, developers don't need to deal with complex logic. However, difficulties arise when testing code in different browsers. Why browser bugs are displayed in different ways is a major problem faced by most CSS developers. Your design may look great in one browser, but the layout may fall apart in another.

The misunderstanding that "CSS is difficult to control" does not come from the language itself, but from a series of necessary measures to make the site work properly on all major browsers. Below we will explain some situations of bugs.

1. How to catch bugs

We all know that browsers have bugs, and some browsers have more bugs than others. When CSS developers encounter problems in their own code, some people will blame the error on browser bugs and take appropriate measures. In fact, everyone exaggerates bugs, and bugs are not as common as people say. The most common CSS problems do not come from browser bugs, but from an incomplete understanding of CSS specifications.

Many developers are self-taught and build their own mental models of the effects. When something doesn't meet their expectations they point to the browser as the culprit. In order to avoid this problem, when dealing with css bugs, it is best to assume that you have made a mistake somewhere, check the code with doubts about yourself, and scrutinize each code. In this way, you will naturally be able to find your own syntax errors. Continuously improved. When you really can’t find it, consider whether it’s a browser bug.

Common CSS Problems

Some of the simplest CSS problems are caused by typing and syntax errors in the code. The best way to prevent this bug is to run your code through a css validator (ttp://jigsaw.w3.org/css-validator/). This should find all syntax errors and show you the line and a short description of each error.

But also remember that the checker is just an automatic checking tool and is not completely reliable. It may report errors that stun you, which is also a bug of the checker, but you should be able to tell whether the errors it reports are real errors.

1. Problems with specificity and classification order

In addition to grammatical errors, one of the more common problems is the design of specificity and classification order. There is often a specificity problem when you apply a rule to an element and find that it has no effect. Other rules can be applied and they work fine, but some rules just don't work, which is very annoying. For example:

I want the following code to display orange, but it was originally written to be transparent. Apply the rules like this:

#content p{background-color:transparent;}
.intro{background-color:#feeca9}
Copy after login

When tested in the browser, it still displays transparent . This is because selecting content p is more specific than intor's selector. In this case, the best way to deal with it is to add the id of the content element at the beginning of the intor paragraph selector:

#content p{background-color: transparent;}
#content .intro{background-color: #feeca9;}
Copy after login

I’m writing this first, I want to go out for a while.

We have finished talking about the "issues of particularity and classification order" in the previous chapter. Now let's start talking about it

2. The issue of overlapping blank edges

Blank Edge overlay is another CSS peculiarity that can cause a lot of trouble if misunderstood. Let’s take an example:

<p id="box">
<p>This paragraph has a 20px margin.</p>
</p>
Copy after login

The p box is set with a 10 pixel margin

#box{margin:10px;background-color:#d5d5d5;}
p{margin:20px;background-color:#6699ff;}
Copy after login

In this way, your ideal p margin should be 10 pixels and the p label should produce 20 pixels. The margins, in fact, are that only the 10-pixel margin of p is generated. The p tag only appears with 20-pixel margins on the left and right sides, and no margins are generated on the top and bottom of p.

This is caused by two reasons. First, the 20 pixels of the top and bottom margins of the paragraph overlap with the 10 pixels of the p, forming a single 20 pixel vertical margin. Second, these blank edges are not surrounded by p, but protrude beyond the top and bottom edges of p. This happens because the child element's height is calculated by the element. If an element has no vertical border or padding, its height is the distance between the top and bottom border edges of its containing child elements. Therefore, the top and bottom margins of the containing child element protrude outside the container element. However, there is a simple solution. By adding a vertical border or padding, the whitespace no longer overlaps, and the element's height is the distance between the top and bottom whitespace edges of its containing child elements. The code is as follows:

#box{margin:10px;padding:1px;background-color:#d5d5d5;}
p{margin:20px;background-color:#6699ff;}
Copy after login

ok the problem is solved, in the next chapter I will talk about the basic knowledge of bug capture.

2. Bug isolation problem

接下来需要尝试隔离问题。通过隔离问题和识别症状,有可能查明是什么导致了这个问题并修复它。隔离问题的一种方法是在相关的元素上应用边框或轮廓,看看它们的反应:

1 #promo1
 2 {
 3 float:left;
 4 margin-right:5px;
 5 border:1px solid red;
 6 }
 7 #promo2
 8 {
 9 float:left;
10 border:1px solid green;
11 }
Copy after login

(我一般喜欢把边框直接在也页面添中加,这样善后处理的话就比较好处理)可以使用firefox开发人员工具条插件中的轮廓选项,或者使用用来给不同元素加轮廓的bookmarklet之一。有时候,仅仅添加边框就会修复问题,这往往就说明这个就是空白边叠加的问题。

尝试修改几个属性后,看看它们是否影响bug,如果有影响,那么是哪一个元素,那一个样式产生的影响找到这个元素,就ok了。例如,如果在两个框架之间的间隙在IE中比你想想的大,那么加大空白边,来测试,看一下会有什么变化。如果边框之间的间隙加倍了,那么可能是遇到了IE的双空白边浮动bug。

 1  #promo1
 2   {
 3  float:left;
 4  margin-right:40px;
 5  border:1px solid red;
 6  }
 7  #promo2
 8  {
 9  float:left;
10  border:1px solid green;
11  }
Copy after login

       尝试一些常见的解决方案。例如,将position属性设置为relatively、将display属性设置为inline(在浮动元素上)或者设置宽度或者高度的属性,让它增高或者缩短,都可以修复许多IE bug。这样的话你就可以找到很多css问题,从而达到对其了解和修复它们!

The above is the detailed content of Bugs in CSS that you must know and how to fix them. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles