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

How to Create plus symbol with background-color in CSS?

I want to create plus symbol using CSS.
this is the final output need to be shown.

enter image description here

But the challenge is, to create this icon using,

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

  1. only use one div tag
  2. use before/after pseudo-elements
  3. can’t use keyboard ‘+’ icon as a base.

>Solution :

#plus {
  /* change this value to the desired width */
  --width: 40px;
  /* setting the background color */
  background-color: black;
  /* setting height and width with the value of css variable */
  width: var(--width);
  height: var(--width);
  /* perfect circle */
  border-radius: var(--width);
  /* centrering */
  display: grid;
  place-items: center;
  /* don't delete this, is important for the ::before and ::after pseudoelements */
  position: relative;
}

#plus::before,
#plus::after {
  content: "";
  /* using css calc() https://developer.mozilla.org/en-US/docs/Web/CSS/calc */
  /* height and width relative to css variable, change to what you feel is good */
  height: calc(var(--width) / 1.5);
  width: calc(var(--width) / 5);
  /* coloring the pseudoelements */
  background-color: white;
  /* here the TRICK, using calc() we easily canculate the rotation, with hardcoding this value */
  transform: rotate(calc(var(--i) * 90deg));
  /* important don't delete */
  position: absolute;
}

#plus::before {
  --i: 1;
}

#plus::after {
  --i: 2;
}
<div id="plus"></div>

this code is relative to the width of the element, so just change the width and all the elements inside will be changed automatically!

#plus {
  --width: 5rem; /* change this value if you need */
}

or here directly on your element (if you have multiple same buttons)

 <div id="plus" style="--width: 100px;"></div>

for this I am using CSS variables, make sure to see this before https://developer.mozilla.org/en-US/docs/Web/CSS/var


Example:

  1. smaller value

enter image description here

  1. higher value:

enter image description here

so now you can see, the div remain always the same as with big values


the code:

#plus {
  /* change this value to the desired width */
  --width: 5rem;
  /* setting the background color */
  background-color: black;
  /* setting height and width with the value of css variable */
  width: var(--width);
  height: var(--width);
  /* perfect circle */
  border-radius: var(--width);
  /* centrering */
  display: grid;
  place-items: center;
  /* don't delete this, is important for the ::before and ::after pseudoelements */
  position: relative;
}

#plus::before,
#plus::after {
  content: "";
  /* using css calc() https://developer.mozilla.org/en-US/docs/Web/CSS/calc */
  /* height and width relative to css variable, change to what you feel is good */
  height: calc(var(--width) / 1.5);
  width: calc(var(--width) / 5);
  /* coloring the pseudoelements */
  background-color: white;
  /* here the TRICK, using calc() we easily canculate the rotation, with hardcoding this value */
  transform: rotate(calc(var(--i) * 90deg));
  /* important don't delete */
  position: absolute;
}

#plus::before {
  --i: 1;
}

#plus::after {
  --i: 2;
}
<div id="plus"></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