how to use before and after in Scss

How To Use before And After In CSS – Creative Uses and Examples

The CSS :before and :after properties are what are also known as pseudo-elements. They are used to add something before or after the content of an element. There are a lot of great uses for these pseudo-elements, and we are here to explore some of them.

The ::before pseudo-element is used to insert content before the content of an element. For example, you can use it to add an icon or a bullet point before a list item.

The ::after pseudo-element is used to insert content after the content of an element. For example, you can use it to add a signature or a copyright notice after an article.

The Syntax of before and after in CSS

If we have an element like this one:

We can add a pseudo-element before it using CSS, like this:

/* Add a bullet point before each list item */
ul li::before {
  content: "\2022"; /* Unicode value for a bullet point */
  margin-right: 10px;
}

/* Add a signature after each article */
article::after {
  content: "By John Smith";
  font-style: italic;
}

Here’s an example of how to use the ::before and ::after pseudo-elements in CSS:

Example of Custom Tooltip by CSS before  property

In this example, we have a button with a class of tooltip. The position: relative property is applied to the button to create a positioning context for the tooltip.

 

<button class="tooltip">Hover over me</button>

 

tooltip {
  position: relative;
}

.tooltip::before {
  content: attr(data-tooltip);
  position: absolute;
  top: -30px;
  left: 50%;
  transform: translateX(-50%);
  background-color: black;
  color: white;
  padding: 5px;
  border-radius: 5px;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.tooltip:hover::before {
  opacity: 1;
}

Creating a custom checkbox using the ::before and ::after pseudo-elements:

<input type="checkbox" id="custom-checkbox">
<label for="custom-checkbox">Check me!</label>
input[type="checkbox"] {
  position: absolute;
  opacity: 0;
}

label {
  position: relative;
  padding-left: 30px;
  cursor: pointer;
}

label::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 20px;
  height: 20px;
  border: 1px solid #333;
}

label::after {
  content: "";
  position: absolute;
  left: 5px;
  top: 5px;
  width: 10px;
  height: 10px;
  background-color: #333;
  opacity: 0;
  transition: opacity 0.2s;
}

input[type="checkbox"]:checked + label::after {
  opacity: 1;
}

Custom Text Highlighting Effects: By using the ::before and ::after

<p>Here is some text that we want to highlight.</p>
p {
  position: relative;
}

p::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, #ff8a00 0%, #e52e71 100%);
  z-index: -1;
  opacity: 0.5;
  pointer-events: none;
  mix-blend-mode: screen;
}

p::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to bottom, #fff 0%, #fff 80%, rgba(255, 255, 255, 0) 100%);
  z-index: -1;
  pointer-events: none;
}