CSS Tips Test: Do you really understand CSS?
Review of key points
- The best way to set double line height is to use unitless numbers (e.g. 2), so that child elements with different font sizes can inherit the correct line height ratio.
- Although the general misconception that
z-index
itself causes overlap, HTML elements can actually be overlapped by themargin
attribute (especially setting negative margins). - Pseudo-elements and pseudo-classes are very different features in CSS: pseudo-classes are applied to actual HTML elements under specific conditions, and pseudo-elements allow styling of parts of a document that are not actual HTML elements.
- W3C attempts to distinguish between pseudo-elements and pseudo-classes in the CSS3 selector specification by using two colons (
::first-line
) for pseudo-element selectors and one colon (:hover
) for pseudo-element selectors, but in order to Backcompatibility, the browser must support both versions.
Do you think you are proficient in CSS? If the free CSS test results I've provided online for the past six months are of any reference, many experienced developers don't know as much about CSS as they think. Of the more than 3,000 people who have participated in the test so far, the average score is only 55%.
However, the average itself is not that interesting. I want to know more about where people are wrong. To write this article, I analyzed the data and focused on three issues where people scored particularly low. I will explain each question one by one, show you the answers most people choose, and explain the correct answers.
It is safe to say that if you take the test yourself after reading this article, you will have an unfair advantage!
Problem 1: The best way to set line-height
The first question should be easy for people who work on text styles regularly:
You want the text on the website to be double-line high by default. Which of the following
line-height
values is the best way to achieve this?
200%
2em
2
double
There are four answers to choose from, and you expect 25% of people will answer correctly by luck, but only 31% will answer this question correctly! Take some time to choose an answer for yourself and continue reading.
First of all, double
is a distraction. line-height
The only accepted keyword value is normal
. I'm happy to say that only 9% of people chose this option. The other three answers are very popular.
The answer most people chose is 2em
(39% chose this answer). In fact, 2em
will certainly provide double line heights for the elements that apply it; but 200%
will, too, and only 21% of people like this answer! Either em is more popular than percentages or people don't really understand them.
However, the correct answer is 2
.
I was instilled with this lesson long ago when I first learned CSS. Always specify line-height
as a unitless number; this way, specifying child elements of different font sizes will inherit the number instead of a fixed line height.
Suppose the page has a default font size of 12pt, but it also contains a title with a font size of 24pt. If you set the line-height
of the body to 2em
(or 200%
) then you will get exactly 24pt (twice the size of the body font) line height throughout the document - even in that title . So the title will be a double line height instead of a double line height!
Instead, setting line-height
to 2
will tell the browser to maintain the font size/line ratio even if the font size changes. The line height of the body will be 24pt, but for the 24pt font of the title, the line height will automatically increase to 48pt.
Question 2: How to make elements overlap
This question is a bit tricky. It requires some "skill" experience that CSS layout often requires:
Which of the following CSS attributes can cause HTML elements to overlap?
z-index
margin
overflow
background
Have you chosen the answer? OK, let's go into the deeper.
Again, there is an option that is easy to rule out: background
. Everyone except 2% of the testers avoided it because they knew it controlled the background color and image.
Unfortunately, most people choose z-index
directly. Up to 46% chose this answer. I guess it's because no one really understands how z-index
works. In fact, setting the z-index
attribute alone has no effect; you also need to set the position
attribute of the element to make z-index
work. In short, z-index
allows you to control the stacking order of elements that do overlap, but they need to overlap first. MDN has a very good article titled “Understanding CSS z-index”, which is worth reading for more details.
If you have ever used overflow
, it should also be easy to rule out. It controls the behavior of content that is not suitable for the size of the box: whether it is truncated, whether it flows out of the box's edge, etc. Again, this depends on whether the box size is constrained by other properties; it does not cause overlap by itself. Still, 22% think it might.
This leaves us with only margin
, which is the correct answer. Only 30% of people answered correctly. You may be curious how an attribute that creates distances between elements can cause them to overlap. If you've done any actual CSS layout, the answer should be obvious: Negative margins will cause elements to overlap.
To demonstrate this, create a page with two div elements. Set the margin-top
of the second div to a negative value, such as -100px
. Bang! The second div now covers the bottom one hundred pixels of the first div.
In practice, you hardly intentionally overlap blocks like this, but negative margins are very useful for squeezing HTML elements into places they usually don't go. I often use them to push elements that float left or right into the fill area of their parent box.
For web design history enthusiasts, the use of negative margin overlap elements in 2005 made three column page layouts, including the so-called One True Layout (and later Holy Grail layout).
Problem 3: Pseudo-elements and pseudo-classes
I admit, the last question is a bit despicable. But only 23% of the testers were able to answer this question correctly (this is worse than luck!), and it obviously touched a confusing point:
Which of the following effects is most suitable for using pseudo-elements to implement?
- Add a shadow to the user when he hovers over the hyperlink.
- When the check box is selected, the labels of the check box are displayed in different colors.
- Assign different background colors to odd and even rows of the table.
- In a flexible page layout, display the first line of the paragraph as bold text.
These three choices are all effects implemented using pseudo-classes; only one involves pseudo-elements. Can you distinguish them?
Pseudo-classes are specific states that the actual HTML element may be in. Think of it as a virtual class that the browser will automatically apply to elements under certain conditions.
The pseudo-element is part of the document, and even if it is not an actual HTML element, CSS allows you to style it. It's like a virtual HTML element - you can style it even if there is no actual HTML tag around it.
With this difference, let's take a look at these options:
Add a shadow to the user when he hovers over the hyperlink.
The hyperlink is an actual HTML element. Applying styles to it only in specific cases (when the mouse is hovering over it) means we are using pseudo-classes. In this case, the pseudo-class you will use is :hover
.
22% of the testers considered this to be a pseudo-element.
When the check box is selected, the labels of the check box are displayed in different colors.
Similarly, a tag is an actual HTML element, not a virtual element. When the check box is selected, the browser applies the :checked
pseudo-class to it. You can then use it in your selector to style the checkbox, or even style the label next to it (for example, using the adjacent sibling selector
).
20% of the testers considered this to be a pseudo-element.
Assign different background colors to odd and even rows of the table.
This is a question that really fools people, but again, we're talking about applying styles to actual HTML elements (in this case tr
elements). tr
Is it odd or even in the set of child elements of its parent element, which is just another case where you can match with pseudo-classes.
In this case, the pseudo-class is :nth-child(even)
(or :nth-child(2n)
) for even elements and :nth-child(odd)
(or :nth-child(2n 1)
) for odd elements.
I guess this is simply because :nth-child
and pseudo-elements usually sound like very obscure CSS features, but 36% of testers chose this as a pseudo-element.
In a flexible page layout, display the first line of the paragraph as bold text.
Of course, this is the correct answer. So far, hopefully the difference has been clear. In a flexible page layout, you can't view the HTML code of the page and say "the elements there contain only the first line of the paragraph text". The browser wraps the line according to the width of the paragraph, which you can't control in a flexible page layout.
:first-line
is a pseudo-element that allows you to apply styles to the first line of text in a block, regardless of where the first line is broken to the second line.
If you're thinking "Okay, that sounds reasonable, but no one knows the difference between pseudo-elements and pseudo-classes", then W3C agrees with you. In the CSS3 selector specification, in order to distinguish between the two, it changed the syntax so that the pseudo-element selector uses two colons (::first-line
), while the pseudo-class still uses one colon (:hover
). Of course, for backward compatibility, the browser must support both versions.
So yes, like I said: mean question. But hey, if you're a CSS geek like me, I think you'll know the difference between your pseudo-element and pseudo-class.
How are your scores?
It's like this: three puzzles in the test. If you answer one of these questions confidently, you're doing a good job. Did you answer two correctly? not bad. If you answered all three correctly, I would love to hear what you think! Especially after I've given these answers, I really need some more tricky CSS questions. Please post them in the comments!
If you like these questions, maybe you'd like to try the rest of the tests. Rest assured, other questions are much easier than these…most of them!
The above is the detailed content of 3 Things (Almost) No One Knows About CSS. For more information, please follow other related articles on the PHP Chinese website!

The rise of Chinese women's tech power in the field of AI: The story behind Honor's collaboration with DeepSeek women's contribution to the field of technology is becoming increasingly significant. Data from the Ministry of Science and Technology of China shows that the number of female science and technology workers is huge and shows unique social value sensitivity in the development of AI algorithms. This article will focus on Honor mobile phones and explore the strength of the female team behind it being the first to connect to the DeepSeek big model, showing how they can promote technological progress and reshape the value coordinate system of technological development. On February 8, 2024, Honor officially launched the DeepSeek-R1 full-blood version big model, becoming the first manufacturer in the Android camp to connect to DeepSeek, arousing enthusiastic response from users. Behind this success, female team members are making product decisions, technical breakthroughs and users

DeepSeek released a technical article on Zhihu, introducing its DeepSeek-V3/R1 inference system in detail, and disclosed key financial data for the first time, which attracted industry attention. The article shows that the system's daily cost profit margin is as high as 545%, setting a new high in global AI big model profit. DeepSeek's low-cost strategy gives it an advantage in market competition. The cost of its model training is only 1%-5% of similar products, and the cost of V3 model training is only US$5.576 million, far lower than that of its competitors. Meanwhile, R1's API pricing is only 1/7 to 1/2 of OpenAIo3-mini. These data prove the commercial feasibility of the DeepSeek technology route and also establish the efficient profitability of AI models.

Website construction is just the first step: the importance of SEO and backlinks Building a website is just the first step to converting it into a valuable marketing asset. You need to do SEO optimization to improve the visibility of your website in search engines and attract potential customers. Backlinks are the key to improving your website rankings, and it shows Google and other search engines the authority and credibility of your website. Not all backlinks are beneficial: Identify and avoid harmful links Not all backlinks are beneficial. Harmful links can harm your ranking. Excellent free backlink checking tool monitors the source of links to your website and reminds you of harmful links. In addition, you can also analyze your competitors’ link strategies and learn from them. Free backlink checking tool: Your SEO intelligence officer

Midea will soon release its first air conditioner equipped with a DeepSeek big model - Midea fresh and clean air machine T6. The press conference is scheduled to be held at 1:30 pm on March 1. This air conditioner is equipped with an advanced air intelligent driving system, which can intelligently adjust parameters such as temperature, humidity and wind speed according to the environment. More importantly, it integrates the DeepSeek big model and supports more than 400,000 AI voice commands. Midea's move has caused heated discussions in the industry, and is particularly concerned about the significance of combining white goods and large models. Unlike the simple temperature settings of traditional air conditioners, Midea fresh and clean air machine T6 can understand more complex and vague instructions and intelligently adjust humidity according to the home environment, significantly improving the user experience.

DeepSeek-R1 empowers Baidu Library and Netdisk: The perfect integration of deep thinking and action has quickly integrated into many platforms in just one month. With its bold strategic layout, Baidu integrates DeepSeek as a third-party model partner and integrates it into its ecosystem, which marks a major progress in its "big model search" ecological strategy. Baidu Search and Wenxin Intelligent Intelligent Platform are the first to connect to the deep search functions of DeepSeek and Wenxin big models, providing users with a free AI search experience. At the same time, the classic slogan of "You will know when you go to Baidu", and the new version of Baidu APP also integrates the capabilities of Wenxin's big model and DeepSeek, launching "AI search" and "wide network information refinement"

This Go-based network vulnerability scanner efficiently identifies potential security weaknesses. It leverages Go's concurrency features for speed and includes service detection and vulnerability matching. Let's explore its capabilities and ethical

AI Prompt Engineering for Code Generation: A Developer's Guide The landscape of code development is poised for a significant shift. Mastering Large Language Models (LLMs) and prompt engineering will be crucial for developers in the coming years. Th


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool