kaxigt.com

Jag skriver om webben för webben

CSS 3 – del 6: Shapes border-radius cirkel rektangel triangel

Postad: 5 februari 2021 | CSS-guider | No Comments
Lästid: 3 minuter

Den här artikeln handlar om CSS-former höjd bredd border-radius cirkel rektangel triangel

I CSS kan vi skapa olika sorters former. Lägg till höjd och bredd, border-radius, och låt den växa fram som en cirkel, rektangel eller triangel. Med ::before and ::after pseudo-element så kan vi ytterligare göra våra former mer ”formbara”.

Härunder har jag lagt upp några exempel.

Den här artikeln innehåller kodsnippar du kan använda.

Cirkulär border med bakgrundsfärg

.selector {background-color:#ccc;border:1px solid #000; border-radius: 50%;width: 100px;height:100px;}

Fler koder till cirklar

Blue
Green
Red
.circle {
width: 200px;
height: 200px;
line-height: 200px;
border-radius: 50%; /* the magic */
text-align: center;
color: white;
font-size: 16px;
text-transform: uppercase;
font-weight: 700;
margin: 0 auto 40px;
}

.blue {background-color: #3498db;}
.green {background-color: #16a085;}
.red {background-color: #e74c3c;}

HTML

<div class="circle blue">Blue</div>
<div class="circle green">Green</div>
<div class="circle red">Red</div>

Källa: https://codepen.io/tomdurkin/pen/HvbEB

CSS Triangle Shapes

Triangel upp och ned

#triangle-up {
      width: 0;
      height: 0;
      border-left: 50px solid transparent;
      border-right: 50px solid transparent;
      border-bottom: 100px solid red;
	  display: inline-block;
    }
#triangle-down {
      width: 0;
      height: 0;
      border-left: 50px solid transparent;
      border-right: 50px solid transparent;
      border-top: 100px solid red;
	 display: inline-block;
    }

Triangel vänster och höger

#triangle-left {
      width: 0;
      height: 0;
      border-top: 50px solid transparent;
      border-right: 100px solid red;
      border-bottom: 50px solid transparent;
	 display: inline-block;
    }

#triangle-right {
      width: 0;
      height: 0;
      border-top: 50px solid transparent;
      border-left: 100px solid red;
      border-bottom: 50px solid transparent;
	 display: inline-block;
    }

Triangel upp ned vänster höger hörn

#triangle-topleft {
      width: 0;
      height: 0;
      border-top: 100px solid red;
      border-right: 100px solid transparent;
    }

#triangle-topright {
      width: 0;
      height: 0;
      border-top: 100px solid red;
      border-left: 100px solid transparent;
    }
  
#triangle-bottomleft {
      width: 0;
      height: 0;
      border-bottom: 100px solid red;
      border-right: 100px solid transparent;
    }  

#triangle-bottomright {
      width: 0;
      height: 0;
      border-bottom: 100px solid red;
      border-left: 100px solid transparent;
    }

Parallelogram

 #parallelogram {
      width: 150px;
      height: 100px;
      transform: skew(20deg);
      background: red;
    }

Hjärta

 #heart {
      position: relative;
      width: 100px;
      height: 90px;
    }
    #heart:before,
    #heart:after {
      position: absolute;
      content: "";
      left: 50px;
      top: 0;
      width: 50px;
      height: 80px;
      background: red;
      border-radius: 50px 50px 0 0;
      transform: rotate(-45deg);
      transform-origin: 0 100%;
    }
    #heart:after {
      left: 0;
      transform: rotate(45deg);
      transform-origin: 100% 100%;
    }

Ruter Äss

#diamond {
      width: 0;
      height: 0;
      border: 50px solid transparent;
      border-bottom-color: red;
      position: relative;
      top: -50px;
    }
    #diamond:after {
      content: '';
      position: absolute;
      left: -50px;
      top: 50px;
      width: 0;
      height: 0;
      border: 50px solid transparent;
      border-top-color: red;
    }

Pacman

   #pacman {
      width: 0px;
      height: 0px;
      border-right: 60px solid transparent;
      border-top: 60px solid red;
      border-left: 60px solid red;
      border-bottom: 60px solid red;
      border-top-left-radius: 60px;
      border-top-right-radius: 60px;
      border-bottom-left-radius: 60px;
      border-bottom-right-radius: 60px;
    }

Flag

  #flag {
      width: 110px;
      height: 56px;
      box-sizing: content-box;
      padding-top: 15px;
      position: relative;
      background: red;
      color: white;
      font-size: 11px;
      letter-spacing: 0.2em;
      text-align: center;
      text-transform: uppercase;
    }
    #flag:after {
      content: "";
      position: absolute;
      left: 0;
      bottom: 0;
      width: 0;
      height: 0;
      border-bottom: 13px solid #eee;
      border-left: 55px solid transparent;
      border-right: 55px solid transparent;
    }

Pointer

#pointer {
  width: 200px;
  height: 40px;
  position: relative;
  background: red;
}
#pointer:after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 0;
  border-left: 20px solid white;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
}
#pointer:before {
  content: "";
  position: absolute;
  right: -20px;
  bottom: 0;
  width: 0;
  height: 0;
  border-left: 20px solid red;
  border-top: 20px solid transparent;
  border-bottom: 20px solid transparent;
}

Omvända hörn

#curved-corner-bottomleft,
#curved-corner-bottomright,
#curved-corner-topleft,
#curved-corner-topright {
  width: 100px;
  height: 100px;
  overflow: hidden;
  position: relative;
}
#curved-corner-bottomleft:before,
#curved-corner-bottomright:before,
#curved-corner-topleft:before,
#curved-corner-topright:before {
  content: "";
  display: block;
  width: 200%;
  height: 200%;
  position: absolute;
  border-radius: 50%;
}
#curved-corner-bottomleft:before {
  bottom: 0;
  left: 0;
  box-shadow: -50px 50px 0 0 red;
}
#curved-corner-bottomright:before {
  bottom: 0;
  right: 0;
  box-shadow: 50px 50px 0 0 red;
}
#curved-corner-topleft:before {
  top: 0;
  left: 0;
  box-shadow: -50px -50px 0 0 red;
}
#curved-corner-topright:before {
  top: 0;
  right: 0;
  box-shadow: 50px -50px 0 0 red;
}
  

Källa: https://css-tricks.com/the-shapes-of-css/