HTML Shapes Design

Creating shapes using HTML and CSS is an exciting way to enhance the visual appeal of your web pages. By utilizing the power of CSS, you can create various shapes like circles, rectangles, triangles, and more. This article will guide you through the process of designing different shapes using HTML and CSS, complete with code examples.

Basic HTML Shapes with CSS

CSS allows you to create basic geometric shapes by manipulating properties like width, height, border-radius, and border. Here, we’ll cover some common shapes: squares, rectangles, circles, ovals, triangles, and stars.

Square and Rectangle Shape

Squares and rectangles are the simplest shapes to create. They require only the width and height properties.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Square and Rectangle</title>
    <style>
        .square {
            width: 100px;
            height: 100px;
            background-color: #3498db;
        }
        .rectangle {
            width: 150px;
            height: 100px;
            background-color: #e74c3c;
        }
    </style>
</head>

<body>
    <div class="square"></div><br>
    <div class="rectangle"></div>
</body>

</html>

Circle and Oval Shape

Circles and ovals are created using the border-radius property. Setting border-radius to 50% on a square creates a circle, while different width and height values create an oval.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Circle and Oval</title>
    <style>
        .circle {
            width: 100px;
            height: 100px;
            background-color: #2ecc71;
            border-radius: 50%;
        }
        .oval {
            width: 150px;
            height: 100px;
            background-color: #f1c40f;
            border-radius: 50%;
        }
    </style>
</head>

<body>
    <div class="circle"></div><br>
    <div class="oval"></div>
</body>

</html>

Triangle Shape

Triangles are created using the border property. By setting the border properties creatively, you can create different types of triangles.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Triangle</title>
    <style>
        .triangle {
            width: 0;
            height: 0;
            border-left: 50px solid transparent;
            border-right: 50px solid transparent;
            border-bottom: 100px solid #9b59b6;
        }
    </style>
</head>

<body>
    <div class="triangle"></div>
</body>

</html>

Star Shape

Stars are a bit more complex and require more creativity with CSS. Here is an example of a simple star.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Star Pattern Design</title>
    <style>
        .star {
            margin: 50px;
            display: inline-block;
            width: 0;
            height: 0;
            border-right:  100px solid transparent;
            border-bottom: 70px  solid #f39c12;
            border-left:   100px solid transparent;
            transform: rotate(35deg);
        }
        
        .star:before {
            border-bottom: 80px solid #f39c12;
            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:after {
            position: absolute;
            display: block;
            color: #f39c12;
            top: 3px;
            left: -105px;
            width: 0;
            height: 0;
            border-right: 100px solid transparent;
            border-bottom: 70px  solid #f39c12;
            border-left:   100px solid transparent;
            transform: rotate(-70deg);
            content: '';
        }
    </style>
</head>

<body>
    <div class="star"></div>
</body>

</html>

Heart Shape

Creating a heart shape involves combining two circles and a square.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Heart Shape</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        body {
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100vh;
        }
        .heart {
            position: relative;
            width: 100px;
            height: 100px;
            background-color: red;
            transform: rotate(-45deg);
        }
        .heart:before, .heart:after {
            content: "";
            position: absolute;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: red;
        }
        .heart:before {
            top: -50px;
            left: 0;
        }
        .heart:after {
            left: 50px;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="heart"></div>
</body>

</html>

Conclusion

Creating shapes using HTML and CSS opens up a world of design possibilities. From basic shapes like squares and circles to more complex shapes like stars and hearts, understanding how to manipulate CSS properties allows you to create visually appealing web designs. Experiment with different shapes and combine them to create unique designs for your web projects.

tools

Computer Science

Related Articles