


Various techniques for using the :before pseudo-element with the position attribute in CSS
通常,我们使用 HTML 向网页添加内容,并使用 CSS 设置内容样式。 CSS 包含一些伪选择器,例如“:before”,我们可以使用它在网页中的任何 HTML 元素之前添加内容。
有时,开发人员不想使用“:before”选择器将内容放在 HTML 元素之前,但他们需要定位内容。例如,如果我们使用‘:before’选择器在文本之前添加图标,则文本和图标之间需要有空格。因此,我们需要使用 CSS 位置属性更改图标的位置。
在本教程中,我们将使用CSS位置属性的“absolute”值来改变内容相对于其父元素位置的位置。
语法
用户可以按照以下语法将position属性与‘:before’伪选择器一起使用。
div:before { content: "arrow"; position: absolute; }
在上述语法中,我们在div元素之前添加了content值。此外,我们将content的位置设置为absolute,并且我们可以使用'left'、'right'、'top'和'bottom' CSS属性来改变其位置。
Example 1
的翻译为:示例 1
在下面的示例中,我们创建了项目列表。在CSS中,我们将列表样式设置为none和相对位置。之后,我们使用“:before”伪选择器在每个列表项之前添加正确的图标。此外,我们设置绝对位置,并将“left”CSS 属性的值设置为“-50px”。
用户可以更改“left”属性的值并观察右侧图标和列表项之间的空间。
<html> <head> <style> li { list-style-type: none; position: relative; } li:before { content: "\2713"; position: absolute; left: -50px; } </style> </head> <body> <h3 id="Adding-the-i-list-icons-using-the-before-pseudo-selector-i-and-changing-its-position"> Adding the <i> list icons using the :before pseudo selector </i> and changing its position </h3> <ul> <li> First </li> <li> Second </li> <li> Third </li> <li> Fourth </li> <li> Fiveth </li> </ul> </body> </html>
示例 2
在下面的示例中,我们使用“img”元素将通知图标添加到网页中。但是,我们在“span”元素内添加了“img”元素。
此外,我们为元素设置了'relative'定位。我们使用了':before'伪选择器在通知图标的顶部添加了通知计数。我们为通知计数内容设置了'absolute'定位,并设置了左侧和顶部位置,以使其看起来很好。
<html> <head> <style> span {position: relative;} span:before { content: "5 NEW"; position: absolute; font-size: 15px; font-weight: bold; color: white; background-color: red; padding: 3px 8px; border-radius: 8px; top: -90px; left: 10px; } </style> </head> <body> <h3 id="Adding-the-i-Notification-count-on-the-notification-icon-i-and-changing-its-position"> Adding the <i> Notification count on the notification icon </i> and changing its position </h3> <span> <img src = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRRFgdEuNyjtHG97YATZHnBXUebNtbHXCDV0pPy8is&s" alt = "Notification"> </span> </body> </html>
示例 3
在下面的示例中,我们演示了使用“:before”伪选择器来创建提示框。
在这里,我们添加了半个文件名作为“”标签的标签,并将完整文件名作为“title”属性的值。在 CSS 中,我们使用 attr() 函数来访问用作内容的属性值。
之后,我们设置工具提示内容的绝对位置,并使用 CSS 变换属性将其位置设置在实际内容之上。在输出中,当用户将鼠标悬停在文件名上时,它会在工具提示中显示完整的文件名。
<html> <head> <style> a:hover:before { content: attr(title); position: absolute; white-space: nowrap; transform: translate(0%, -100%); opacity: 0; transition: all 0.3s ease-in-out; background-color: aqua; color: black; padding: 5px; border-radius: 5px; } a:hover:before {opacity: 1;} </style> </head> <body> <h3 id="Creating-the-tooltip-by-adding-content-before-the-HTML-element"> Creating the tooltip by adding content before the HTML element </h3> <a href = "#" title = "First_File_1.jpg"> First_Fi... </a> <br><br> <a href = "#" title = "Second_File_2.jpg"> Second_F...</a> <br><br> <a href = "#" title = "Third_File_3.jpg"> Third_Fil... </a> </body> </html>
示例 4
在下面的示例中,我们演示了如何使用“:before”伪选择器创建自定义复选框。
首先,我们设置了“display: none”来隐藏默认的复选框。然后,在标签之前添加了内容,并为复选框添加了尺寸和一些CSS样式。接下来,我们添加了CSS来显示选中复选框内部的箭头图标。在这里,我们使用了相对定位来设置复选框的位置。
<html> <head> <style> input[type="checkbox"] { display: none; } label:before { content: ""; display: inline-block; width: 15px; height: 15px; border: 2px solid red; border-radius: 6px; margin-right: 12px; position: relative; top: 3px; } input[type="checkbox"]:checked+label:before { content: "\2713"; font-size: 11px; text-align: center; color: white; background-color: green; } </style> </head> <body> <h3 id="Creating-the-custom-checkbox-using-the-before-pseudo-selector"> Creating the custom checkbox using the :before pseudo selector </h3> <input type = "checkbox" id = "car"> <label for = "car"> Car </label> <br> <br> <input type = "checkbox" id = "Bike"> <label for = "Bike"> Bike </label> </body> </html>
用户学会了使用position CSS属性与‘:before’伪元素。在第一个示例中,我们为列表项添加了自定义图标。在第二个示例中,我们学会了设置通知计数。第三个示例教会了我们使用‘:before’伪选择器和position属性创建工具提示。在最后一个示例中,我们学会了创建自定义复选框。
The above is the detailed content of Various techniques for using the :before pseudo-element with the position attribute in CSS. For more information, please follow other related articles on the PHP Chinese website!

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
