Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSS :after elements overlapped

HTML

<div class="star"></div>
<div class="star"></div>

CSS

.star {
  font-size: 1em;
}

.star:before,
.star:after {
  content: "";
  position: absolute;
  background: red;
  width: 1em;
  height: 1em;
}

.star:before {
  transform: rotate(0deg) skew(22.5deg, 22.5deg);
}

.star:after {
  transform: rotate(90deg) skew(22.5deg, 22.5deg);
}

I tried this code but it would only show one element in the end. How to prevent element with star class from being overlapped?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your two elements are both overlapping. You need to offset the second element.

You can use .star:nth-of-type(2) to target the second element, and margin-left to offset it.

This can be seen in the following:

.star {
  font-size: 1em;
}

.star:before,
.star:after {
  content: "";
  position: absolute;
  background: red;
  width: 1em;
  height: 1em;
}

.star:before {
  transform: rotate(0deg) skew(22.5deg, 22.5deg);
}

.star:after {
  transform: rotate(90deg) skew(22.5deg, 22.5deg);
}

.star:nth-of-type(2) {
  margin-left: 30px;
}
<div class="star"></div>
<div class="star"></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading