Is it possible to make a crosshair with a single div?

Issue

I’m trying to create a cross hair like this:

<div class="crosshair"></div>

enter image description here

using a single element and pure css?

I thought of using ::after & ::before along with the div
but that would only result in 3 bars whereas the image has 4 bars,

I’m not sure if it’s even possible but maybe I missed something?

Edit

btw the transparency of the crosshair as a whole changes to see stuff behind it

Edit 2

on @tacoshy request in the comments I’m also adding my "research"

:root{
  --bar_thickness:0.5rem;
  --bar_length:5rem;
}

.crosshair::before{
  content:"";
  display:block;
  background-color:black;
  height:var(--bar_length);
  width:var(--bar_thickness);
  position:relative;
  transform:rotate(90deg);
}

.crosshair {
  margin:3rem;
  background-color:black;
  height:var(--bar_length);
  width:var(--bar_thickness);
}

.crosshair::after{
  content:"";
  display:block;
  background-color:white;
  height:calc( var(--bar_length) * 0.4);
  width:calc( var(--bar_length) * 0.4);
  position:relative;
  bottom:50%;
  left:50%;
  transform:translate(-50%,-50%)
}
<div class="crosshair"></div>

but the problem with my approach is when I need it to be transparent as mentioned in the first edit it fails as such:

:root{
  --bar_thickness:0.5rem;
  --bar_length:5rem;
}

.crosshair::before{
  content:"";
  display:block;
  background-color:black;
  height:var(--bar_length);
  width:var(--bar_thickness);
  position:relative;
  transform:rotate(90deg);
}

.crosshair {
  
  position:absolute;
  opacity:50%;
  margin:3rem;
  background-color:black;
  height:var(--bar_length);
  width:var(--bar_thickness);
}

.crosshair::after{
  content:"";
  display:block;
  background-color:white;
  height:calc( var(--bar_length) * 0.4);
  width:calc( var(--bar_length) * 0.4);
  position:relative;
  bottom:50%;
  left:50%;
  transform:translate(-50%,-50%)
}
<div id="background" style="background-color:red;height:10rem;width:10rem">
  <div class="crosshair"></div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitatio

</div>

Solution

You can do something with box-shadow:

#el {
  position: relative;
}

#el::after, #el::before {
  content: "";
  background: black;
  border-radius: 2px;
  position: absolute;
}

#el::after {
  left: 30px;
  width: 10px;
  height: 20px;
  box-shadow: 0 50px black;
}

#el::before {
  top: 30px;
  width: 20px;
  height: 10px;
  box-shadow: 50px 0 black;
}
<div id=el></div>

Or you might use the border instead of box-shadow:

#el {
  position: relative;
  margin-top: 40px;
  height: 10px;
  width: 30px;
  border-left: 20px solid black;
  border-right: 20px solid black;
}

#el::after, #el::before {
  content: "";
  width: 10px;
  height: 20px;
  background: black;
  position: absolute;
  left: 10px;
}

#el::before {
  top: -30px;
}

#el::after {
  top: 20px;
}
<div id=el></div>

Or use only the ::before and ::after borders:

#el {
  position: relative;
}

#el::after, #el::before {
  content: "";
  position: absolute;
  box-sizing: border-box;
}

#el::before {
  width: 10px;
  height: 70px;
  left: 30px;
  border-top: 20px solid black;
  border-bottom: 20px solid black;
}

#el::after {
  width: 70px;
  height: 10px;
  top: 30px;
  border-left: 20px solid black;
  border-right: 20px solid black;
}
<div id=el></div>

Or use gradients instead of borders:

#el {
  position: relative;
}

#el::after, #el::before {
  content: "";
  position: absolute;
}

#el::before {
  width: 10px;
  height: 70px;
  left: 30px;
  background: linear-gradient(0deg, rgba(0,0,0,1) 28.57%, rgba(0,0,0,0) 28.57%, rgba(0,0,0,0) 71.43%, rgba(0,0,0,1) 71.43%);
}

#el::after {
  width: 70px;
  height: 10px;
  top: 30px;
  background: linear-gradient(90deg, rgba(0,0,0,1) 28.57%, rgba(0,0,0,0) 28.57%, rgba(0,0,0,0) 71.43%, rgba(0,0,0,1) 71.43%);
}
<div id=el></div>

Answered By – Sandro

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.