About bugs in css and how to fix them
与许多编程语言相比,css是一种相当容易学习的语言。它的语法简单明了,而且由于它的表现本质,开发人员并不需要处理复杂的逻辑。但是,当在不同的浏览器中测试代码时,困难就会随之出现。浏览器bug何不一至的显示方式是大多css开发人员面临的主要难题。你的设计在一种浏览器上显示的很好,但在另一种浏览器上布局可能就会支离破碎。
“css难以掌控”的误解并不来源于语言本身,而是由于为了让站点在所有主流浏览器上工作正常而采取一系列必要的措施。下面我们就来讲解一下bug的一些情况。
一、如何捕捉bug
我们都知道浏览器是有bug的,而且一些浏览器的bug比其他浏览器多。当css开发人员在自己代码中遇到了难题时,一些人就会把错误归咎于浏览器的bug,采取适当的招数。其实,大家把bug太夸大了,bug并没有人们说的那么常见。最常见的css问题并非来源于浏览器bug,而是对css规范的理解不完整。
许多开发人员是自学的,他们自行建立对效果的思维模型。当某些东西不符合他们的预期时就会把浏览器的当成罪魁祸首。为了避免这个问题,在处理css bug时最好假设自己是不是哪里写错了,带着对自己的怀疑来检查代码,每个代码推敲一下,这样的话在找出自己语法错误的时候自然就能不断提高了。当实在找不到时,再来考虑是不是浏览器bug的问题。
常见的css问题
最简单的一些css问题是由代码中的打字和语法错误造成的。防止这种bug的最好方法一是通过css检查器运行代码(ttp://jigsaw.w3.org/css-validator/)。这应该会发现所有语法错误,并且向你显示所在的行和对每个错误的简短描述。
但是也要记住,检查器只是一个自动检查的工具,并不是完全可靠。它有可能会报出让你目瞪口呆的错误,这也是检查器的bug,但是你应该能够分清楚他报出的是不是真错误。
1.特殊性和分类次序的问题
除了语法错误之外,比较常见的问题之一设计特殊性和分类次序。在将一个规则应用于元素时,却发现没有任何效果,这是往往存在特殊性问题。可以应用其他规则而且它们工作正常,但是某些规则就是不起作用,很是气人。打个比方:
我想要如下代码显示橙色的,但是它原来写的是透明的,这样运用规则:
#content p
{
background-color:transparent;
}
.intro
{
background-color:#feeca9
}
览器中测试的时候,这里仍然显示透明。这是因为于选择content p比intor的选择器的特殊性更强,在这种情况下,最好的处理方式是在intor段落选择器的开头添加内容元素的id:
#content p
{
background-color: transparent;
}
#content .intro
{
background-color: #feeca9;
}
先写到这里,要出去一下。
上一章已经讲完了“特殊性和分类次序的问题”,金额下来我们开始讲
2.空白边叠加的问题
空白边叠加是另一个如果误解就会导致很多麻烦的css特殊性。下面我们来举个例子:
This paragraph has a 20px margin.
div box is set with 10 pixels of margin
#box{
margin:10px;
background-color:#d5d5d5;
}
p
{
margin:20px;
background-color:#6699ff;
}
In this way, your ideal ideal should be that the div's outer margin is 10 pixels and the p tag produces a 20 pixel outer margin. In fact, only the div's 10 pixel outer margin is generated, and the p tag only appears on the left and right sides. There is a 20 pixel margin on the side, and there is no margin on the top and bottom of the div.
This is caused by two reasons. First, the 20 pixels of top and bottom margins of the paragraph overlap with the 10 pixels of the div, forming a single 20 pixel vertical margin. Second, these whitespace edges are not surrounded by the div, but protrude beyond the top and bottom edges of the div. 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 children. The code is as follows:
#box{
margin:10px;
padding:1px;
background-color:#d5d5d5;
}
p
{
margin:20px;
background-color:#6699ff;
}
ok The problem is solved. In the next chapter, I will talk about the basic knowledge of bug catching. G u b b b
The next time you need to try the problem of isolation. By isolating the problem and identifying the symptoms, it is possible to figure out what is causing the problem and fix it. One way to isolate the problem is to apply a border or outline to the relevant elements and see how they react:
1 #promo1
2 {
3 float:left;
4 margin-right:5px;
5 border:1px solid red;
6 }
7 #promo2
8 {
9 float:left;
11 }
(I usually like to add the border directly to the page, so that the aftermath can be dealt with easily (easier to deal with) You can use the outline option in the Firefox developer toolbar plug-in, or use one of the bookmarklets used to outline different elements. Sometimes, simply adding a border will fix the problem, which often indicates that the problem is white space overlay.个 After trying to modify a few attributes, see if they affect BUG, if there is an influence, then which element, the impact of that style to find this element is OK. For example, if the gap between two frames is larger than you think in IE, then increase the margin and test it to see what happens. If the gap between borders doubles, you may be running into IE's double-margin floating 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 }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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

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.

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.

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.

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.

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 the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
