Maison > interface Web > tutoriel CSS > Diverses techniques d'utilisation du pseudo-élément :before avec l'attribut position en CSS

Diverses techniques d'utilisation du pseudo-élément :before avec l'attribut position en CSS

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Libérer: 2023-09-18 17:29:08
avant
800 Les gens l'ont consulté

Diverses techniques dutilisation du pseudo-élément :before avec lattribut position en CSS

通常,我们使用 HTML 向网页添加内容,并使用 CSS 设置内容样式。 CSS 包含一些伪选择器,例如“:before”,我们可以使用它在网页中的任何 HTML 元素之前添加内容。

有时,开发人员不想使用“:before”选择器将内容放在 HTML 元素之前,但他们需要定位内容。例如,如果我们使用‘:before’选择器在文本之前添加图标,则文本和图标之间需要有空格。因此,我们需要使用 CSS 位置属性更改图标的位置。

在本教程中,我们将使用CSS位置属性的“absolute”值来改变内容相对于其父元素位置的位置。

语法

用户可以按照以下语法将position属性与‘:before’伪选择器一起使用。

div:before {
   content: "arrow";
   position: absolute;
}
Copier après la connexion

在上述语法中,我们在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> 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>
Copier après la connexion

示例 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> 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>
Copier après la connexion

示例 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> 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>
Copier après la connexion

示例 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> 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>
Copier après la connexion

用户学会了使用position CSS属性与‘:before’伪元素。在第一个示例中,我们为列表项添加了自定义图标。在第二个示例中,我们学会了设置通知计数。第三个示例教会了我们使用‘:before’伪选择器和position属性创建工具提示。在最后一个示例中,我们学会了创建自定义复选框。

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal