目录
语法
Example 1
示例 4
Adding the list icons using the :before pseudo selector and changing its position
Adding the Notification count on the notification icon and changing its position
Creating the tooltip by adding content before the HTML element
Creating the custom checkbox using the :before pseudo selector
首页 web前端 css教程 在CSS中使用position属性的:before伪元素的各种技巧

在CSS中使用position属性的:before伪元素的各种技巧

Sep 18, 2023 pm 05:29 PM

在CSS中使用position属性的:before伪元素的各种技巧

通常,我们使用 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属性创建工具提示。在最后一个示例中,我们学会了创建自定义复选框。

以上是在CSS中使用position属性的:before伪元素的各种技巧的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

使用GraphQL缓存 使用GraphQL缓存 Mar 19, 2025 am 09:36 AM

如果您最近开始使用GraphQL或审查了其优点和缺点,那么您毫无疑问听到了诸如“ GraphQl不支持缓存”或

使用Redwood.js和Fauna构建以太坊应用 使用Redwood.js和Fauna构建以太坊应用 Mar 28, 2025 am 09:18 AM

随着最近比特币价格超过20k美元的攀升,最近打破了3万美元,我认为值得深入研究创建以太坊

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

它的出局!恭喜Vue团队完成了完成,我知道这是一项巨大的努力,而且很长时间。所有新文档也是如此。

您可以从浏览器获得有效的CSS属性值吗? 您可以从浏览器获得有效的CSS属性值吗? Apr 02, 2025 pm 06:17 PM

我有人写了这个非常合法的问题。 Lea只是在博客上介绍了如何从浏览器中获得有效的CSS属性。那样的是这样。

在CI/CD上有点 在CI/CD上有点 Apr 02, 2025 pm 06:21 PM

我说的“网站”比“移动应用程序”更合适,但我喜欢Max Lynch的框架:

比较浏览器的响应式设计 比较浏览器的响应式设计 Apr 02, 2025 pm 06:25 PM

这些桌面应用程序中有许多目标是同时在不同的维度上显示您的网站。因此,例如,您可以写作

在WordPress块编辑器中使用Markdown和本地化 在WordPress块编辑器中使用Markdown和本地化 Apr 02, 2025 am 04:27 AM

如果我们需要直接在WordPress编辑器中向用户显示文档,那么最佳方法是什么?

带有粘性定位的堆叠卡和一点点的杂物 带有粘性定位的堆叠卡和一点点的杂物 Apr 03, 2025 am 10:30 AM

前几天,我发现了科里·金尼文(Corey Ginnivan)网站上的这一点,当您滚动时,彼此之间的卡片堆放集。

See all articles