content
PropertyThe content
property in CSS is used to insert generated content into the document. It can be used with the ::before
and ::after
pseudo - elements. Pseudo - elements are used to style specific parts of an element or to insert content before or after an element.
The basic syntax of the content
property is as follows:
selector::pseudo-element {
content: value;
}
The value
can be a string, an image URL, a counter, or other special values.
::before
and ::after
)::before
: This pseudo - element is used to insert content before the content of the selected element.::after
: This pseudo - element is used to insert content after the content of the selected element.To add text content to an empty HTML element, you can use the ::before
or ::after
pseudo - elements along with the content
property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<style>
.empty-element::before {
content: "This is added content";
}
</style>
</head>
<body>
<div class="empty-element"></div>
</body>
</html>
In this example, the text “This is added content” is inserted before the content of the <div>
element with the class empty-element
.
You can also add an image to an empty HTML element using the content
property.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<style>
.empty-element::after {
content: url('example.jpg');
}
</style>
</head>
<body>
<div class="empty-element"></div>
</body>
</html>
Here, the image example.jpg
is inserted after the content of the <div>
element with the class empty-element
.
One common use case is to add icons to buttons or links. For example, you can use Font Awesome icons.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font - awesome/5.15.3/css/all.min.css">
<style>
.icon - button::after {
content: "\f004";
font-family: "Font Awesome 5 Free";
font-weight: 900;
margin-left: 5px;
}
</style>
</head>
<body>
<button class="icon - button">Like</button>
</body>
</html>
In this example, a heart icon is added after the text of the button.
You can use CSS to create placeholders for empty elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<style>
.input - placeholder::before {
content: "Enter your text here";
color: #999;
}
</style>
</head>
<body>
<input type="text" class="input - placeholder">
</body>
</html>
This creates a placeholder - like effect for the input field.
content
property, make sure to provide text alternatives for screen readers. For example, you can use the aria - label
attribute in HTML to describe the added content.content
property, make sure the images are optimized for the web to reduce page load times.content
property and pseudo - elements. Test your code in multiple browsers to ensure consistent behavior.Adding content to empty HTML elements using CSS can be a powerful technique for enhancing the visual appearance of a web page without cluttering the HTML code. By understanding the fundamental concepts of the content
property and pseudo - elements, you can use various usage methods to add text, images, icons, and placeholders. However, it is important to follow best practices related to accessibility, performance, and cross - browser compatibility to ensure a high - quality web experience for all users.