::before Selector in HTML
This article will introduce CSS ::before
pseudo-elements and explore their application in HTML.
CSS ::before pseudo-element
::before
The selector is a CSS pseudo-element that we can use to insert content before one or more selected elements. It is inline by default.
The content property usually takes ::before
a selector. This property allows you ::before
to add content at the location specified by the selector.
Using these attributes, you can add content at a selected location in the HTML document without making any changes to the HTML code. You can apply CSS styles to the content added in the body of the selector.
::before
The selector syntax is as follows:
selector::before{
/*Css Properties*/
}
For example, if you want to add some content before a paragraph, you should write it like this.
p::before{
/*Css Properties*/
}
Suppose you have written a paragraph in HTML but missed the title. In this case, you can use ::before
the <header> pseudo-element to add the title before the paragraph.
You can write the title and add style using the content attribute.
In a later section, you'll actually be able to use ::before
the pseudo-element.
Use ::before to add content before an element in HTML
Let's look at an ::before
example of how to use it to add content before an element.
For example, create a container div1. Then use <ol>
to create an ordered list containing three items.
Use <li>
the tag to write some random items of your choice.
Sample code:
<div class = "div1">
<ol>
<li>Point One</li>
<li>Point Two</li>
<li>Point Three </li>
</ol>
</div>
The output is pretty much predictable. It is an ordered list containing three items.
You will now add some items to the front of the list without changing the HTML document.
In CSS, ::before
create a selector using the pseudo-element with div1 as .div1::before
. In the body of the selector, use the content property and write anything you want as its value.
Let's think of a row of asterisks as content.
Next, let's add some content before each list item. You have to select the li element and use the ::before pseudo-element.
Select the element as .div1 li::before
and use the Content attribute to add '\2192' as its value. The value '\2192' is the entity number of the right arrow.
.div1::before{
content:'*************************';
background: yellow;
}
.div1 li::before{
content: '\2192';
}
Observe how a row of asterisks is formed before the list, before the content of div1 *
. Also, before each point of the ordered list, we inserted a right arrow.
This is ::before
how pseudo-elements behave with the content property in CSS.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Creating a currency input in HTML
Publish Date:2025/04/19 Views:119 Category:HTML
-
In this article, users will learn to input convert a normal HTML field into a currency input field. We will use HTML and jQuery to achieve our goal. input Convert plain to currency input in HTML using JavaScript toFixed() method In HTML, th
HTML Hide Buttons and Show Them Using Onclick
Publish Date:2025/04/19 Views:178 Category:HTML
-
This article will show you several ways to hide HTML buttons and make them visible using the onclick event. Using the CSS display property to show hidden buttons in HTML We can first hide the HTML button by setting its display property to n
Right align button in HTML
Publish Date:2025/04/19 Views:136 Category:HTML
-
Alignment is the positioning of text or buttons in different places like right, left or center on an HTML web page. We can easily use the alignment properties to place our text, images or other elements anywhere on the web page. There may b
Readonly in HTML Select Tag
Publish Date:2025/04/19 Views:176 Category:HTML
-
TML, Hypertext Markup Language, is widely used to format websites. Also, CSS (Cascading Style Sheets) can be used to style websites. Below are some of the attributes used in HTML and CSS to control user and cursor behavior. select readonly
The role attribute in HTML
Publish Date:2025/04/19 Views:142 Category:HTML
-
In this article, we will introduce the HTML role attribute and its uses. Introduction to the role attribute in HTML The role attribute in HTML belongs to the WAI-ARIA (Web Accessibility Initiative - Accessible Rich Internet Applications) sp
Pagination when printing in HTML
Publish Date:2025/04/19 Views:147 Category:HTML
-
This article will introduce several ways to force page breaks when printing HTML content. Set the page-break-after property to always Inside @media print to Page Break in HTML The @media print rule of CSS lets us apply rules when printing H
Displaying Cookie-Based Images in HTML
Publish Date:2025/04/19 Views:95 Category:HTML
-
This article will introduce a method to display images in HTML format based on cookies set in PHP. Display images based on cookies set in HTML The problem statement is that we need to display a specific image based on a cookie passed by the
Creating a download link in HTML
Publish Date:2025/04/19 Views:158 Category:HTML
-
This article will introduce a method to create a download link in HTML. Use the download attribute to create download links in HTML We can create a download link using the download attribute inside the HTML anchor element. In the anchor tag
HTML 中的 role 属性
Publish Date:2023/05/06 Views:354 Category:HTML
-
本篇文章介绍 HTML role属性。HTML中 role 属性介绍,role 属性定义描述语义的 HTML 元素的角色。