En CSS, comment créer un carré bombé [dupliquer]

Nov 28 2020

Est-il possible d'y parvenir en pur css, sans avoir besoin d'utiliser un SVG intégré dans votre page.

Obvs border-radius: 10pxne le coupe pas tout à fait.

Réponses

cornonthecob Nov 28 2020 at 16:43

Voici un exemple rapide où nous changeons les border-radiuses du haut et du bas . Nous pouvons utiliser l'autre valeur comme une sorte de hauteur étrange qui contrôlera la hauteur de notre rayon.

.box {
    background: #62f;
    color: white;
    border-radius: 50% / 10px;
    padding: 30px;
    width: 1em;
    height: 1em;
}
<div class="box">8</div>

Maintenant, nous pouvons utiliser le sélecteur ::beforeet ::afterpour en faire deux:

.box::after {
    background: #62f;
    color: white;
    border-radius: 50% / 10px;
    padding: 30px;
    width: 1em;
    height: 1.75em;
    content: "";
    display: block;
    position: absolute;
    top: 2px;
    left: 8px;
}

.box::before {
    background: #62f;
    color: white;
    border-radius: 10px / 50%;
    padding: 30px;
    width: 1.75em;
    height: 1em;
    content: "";
    display: block;
    position: absolute;
    top: 8px;
    left: 2px;
}
<div class="box">8</div>

Maintenant, je vous entends demander, qu'en est-il de nos 8? Où est-il passé? Eh bien, mes amis, il est en fait passé derrière notre étrange carré. Nous pouvons l'envelopper dans un intervalle et le styliser pour résoudre ce problème.

.box::after {
    background: #62f;
    color: white;
    border-radius: 50% / 10px;
    padding: 30px;
    width: 1em;
    height: 1.75em;
    content: "";
    display: block;
    position: absolute;
    top: 2px;
    left: 8px;
}

.box::before {
    background: #62f;
    color: white;
    border-radius: 10px / 50%;
    padding: 30px;
    width: 1.75em;
    height: 1em;
    content: "";
    display: block;
    position: absolute;
    top: 8px;
    left: 2px;
}

.box > span {
    position: absolute;
    top: 32px;
    left: 36px;
    z-index: 5;
    color: white;
    font-size: 1.5em;
    font-family: sans-serif;
}
<div class="box"><span>8</span></div>

Nous pouvons également échanger quelques chiffres pour le rendre encore plus parfait:

.box::after {
    background: #62f;
    color: white;
    border-radius: 50% / 20px;
    padding: 30px;
    width: 1em;
    height: 1.75em;
    content: "";
    display: block;
    position: absolute;
    top: 2px;
    left: 8px;
}

.box::before {
    background: #62f;
    color: white;
    border-radius: 20px / 50%;
    padding: 30px;
    width: 1.7em;
    height: 1em;
    content: "";
    display: block;
    position: absolute;
    top: 8px;
    left: 2px;
}

.box > span {
    position: absolute;
    top: 32px;
    left: 36px;
    z-index: 5;
    color: white;
    font-size: 1.5em;
    font-family: sans-serif;
}
<div class="box"><span>8</span></div>

ImranRafiqRather Nov 28 2020 at 16:36

J'espère que cela t'aides:

.square{
  height:80px;
  width:80px;
  background:blue;
  display:flex;
  justify-content:center;
  align-items:center;
  color:#fff;
  border-radius:2rem;
  font-size: 2rem;
  opacity: 0.5;
  box-shadow:-1px 1px 1px 1px #fff;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="square">8</div>
</body>
</html>