Some examples that show how to use CSS3 to create geometric shapes of various types, to be included in a web page.
The HTML , CSS and Javascript specifications contain enormous potential, but they are not always put into practice. Thanks to the power of CSS 3 , for example, it is possible to create animations and effects that previously were not minimally conceivable. Mixing some of the CSS property best known as width, height, border, marginand position, along with the more complex and “new” as border-radius, box-shadow, transformand contentas well as taking advantage of the new pseudo selectors ::after, and ::beforeyou can create real geometric shapes using only HTML elements and CSS selectors.
In this article posted on CSS Tricks we can admire how it is possible to realize practically all the figures we can imagine: rectangles, circles, ovals, triangles, trapezoids, parallelograms, stars, pentagrams, parallepids are just some of the possible results.
Consider a simple HTML code, in which a div is assigned a id:
<div id="nomeforma"></div>
We can animate the appearance of this element, adding a little ‘appropriate CSS. Let’s see some examples.
Rectangle
#rectangle {
width: 200px;
height: 100px;
background: red;
}
Circle
#circle {
width: 100px;
height: 100px;
background: red;
border-radius: 50%
}
Triangle pointing upwards
#triangle-up {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
}
Triangle pointing to the right
#triangle-right {
width: 0;
height: 0;
border-top: 50px solid transparent;
border-left: 100px solid red;
border-bottom: 50px solid transparent;
}
Curved arrow pointing downwards
#curvedarrow {
position: relative;
width: 0;
height: 0;
border-top: 9px solid transparent;
border-right: 9px solid red;
transform: rotate(10deg);
}
#curvedarrow:after {
content: "";
position: absolute;
border: 0 solid transparent;
border-top: 3px solid red;
border-radius: 20px 0 0 0;
top: -12px;
left: -9px;
width: 12px;
height: 12px;
transform: rotate(45deg);
}
Six-pointed star
#star-six {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
position: relative;
}
#star-six:after {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 100px solid red;
position: absolute;
content: "";
top: 30px;
left: -50px;
}
Five-pointed star
#star-five {
margin: 50px 0;
position: relative;
display: block;
color: red;
width: 0px;
height: 0px;
border-right: 100px solid transparent;
border-bottom: 70px solid red;
border-left: 100px solid transparent;
transform: rotate(35deg);
}
#star-five:before {
border-bottom: 80px solid red;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
position: absolute;
height: 0;
width: 0;
top: -45px;
left: -65px;
display: block;
content: '';
transform: rotate(-35deg);
}
#star-five:after {
position: absolute;
display: block;
color: red;
top: 3px;
left: -105px;
width: 0px;
height: 0px;
border-right: 100px solid transparent;
border-bottom: 70px solid red;
border-left: 100px solid transparent;
transform: rotate(-70deg);
content: '';
}
Octagon
#octagon {
width: 100px;
height: 100px;
background: red;
position: relative;
}
#octagon:before {
content: "";
width: 100px;
height: 0;
position: absolute;
top: 0;
left: 0;
border-bottom: 29px solid red;
border-left: 29px solid #eee;
border-right: 29px solid #eee;
}
#octagon:after {
content: "";
width: 100px;
height: 0;
position: absolute;
bottom: 0;
left: 0;
border-top: 29px solid red;
border-left: 29px solid #eee;
border-right: 29px solid #eee;
}
These examples are obviously a limited sample, but demonstrate the ability to generate geometric shapes using only CSS3.