-4

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,

  1. only use one div tag
  2. use before/after pseudo-elements
  3. can't use keyboard '+' icon as a base.
Ravi
  • 93
  • 5
  • Where did you get stuck, what went wrong with your attempt(s)? Where's the "*[mcve]*" code of those attempts? – David Thomas Jun 25 '22 at 16:27
  • only these things i could make so far,
    .plusBtn{ width: 40px; height: 15px; background-color: rgb(122, 122, 122); cursor: pointer; transform: scale(2); z-index: 1; } .plusBtn::before{ content: ''; width: 40px; height: 15px; background-color: rgb(122, 122, 122); transform: rotate(90deg); position: absolute; cursor: pointer; z-index: 1; }
    – Ravi Jun 25 '22 at 16:34
  • one element, no pseudo-element: https://stackoverflow.com/q/55281672/8620333 – Temani Afif Jun 25 '22 at 19:43

1 Answers1

0

#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, without 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, without 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>
Laaouatni Anas
  • 4,199
  • 2
  • 7
  • 26
  • 1
    learnt a new thing, What is property names that are prefixed with --? For what kind of thing we can use it? – Ravi Jun 25 '22 at 16:59
  • @Ravi values that are written multiple times the same. just create one CSS var and put it inside everything that needs it. (I suggest to not using it everywhere, only when needed for making the code easier to understand and easier to maintain (less lines of code) https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties – Laaouatni Anas Jun 25 '22 at 17:15
  • by "making the code easier to maintain" I mean this: https://youtube.com/shorts/qADaSdE3sqE?feature=share, he show the problem (in his case he using JavaScript, but remain the same concept also in CSS var) – Laaouatni Anas Jun 25 '22 at 17:18