Creating a Heart Shape with HTML and CSS - IndianTechnoEra
Latest update Android YouTube

Creating a Heart Shape with HTML and CSS

Creating a Heart Shape with HTML and CSS

Want to add a touch of love to your webpage? In this tutorial, we'll show you how to create a heart shape using HTML and CSS. It's a fun and simple way to enhance the visual appeal of your site.

Step 1: Set Up Your HTML

Start by creating the HTML structure for your heart shape. In this example, we'll use a <div> element with a class of heart:

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

Step 2: Style Your Heart with CSS

Now, let's add some CSS to turn that empty <div> into a heart shape. We'll use the ::before and ::after pseudo-elements to create the top parts of the heart:

        
            .heart {
                width: 100px;
                height: 100px;
                background-color: red;
                position: relative;
                transform: rotate(-45deg);
                margin-top: 50px;
            }

            .heart:before,
            .heart:after {
                content: "";
                width: 100px;
                height: 100px;
                background-color: red;
                border-radius: 50%;
                position: absolute;
            }

            .heart:before {
                top: -50px;
                left: 0;
            }

            .heart:after {
                top: 0;
                left: 50px;
            }
        
    

This CSS code uses the transform: rotate(-45deg); property to give the heart shape and the border-radius: 50%; property to make the top parts of the heart circular.

Step 3: Display the Heart

Now that your HTML and CSS are set up, just open your HTML file in a browser, and you'll see a lovely heart shape displayed on your page!

        
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <style>
                    
                </style>
                <title>Heart Shape</title>
            </head>
            <body>
                <div class="heart"></div>
            </body>
            </html>
        
    

That's it! You've successfully created a heart shape using HTML and CSS. Feel free to customize the size and color by adjusting the values in the CSS code.

.

Know the CSS

.heart {

    width: 100px;

    height: 100px;

    background-color: red;

    position: relative;

    transform: rotate(-45deg);

    margin-top: 50px;

}

.heart: This is a class selector that targets the div element with the class of "heart."

width and height: Sets the dimensions of the heart. In this case, both the width and height are set to 100 pixels, creating a square.

background-color: Sets the background color of the heart to red.

position: relative;: Establishes a positioning context for the heart. This is necessary for absolute positioning of pseudo-elements inside it.

transform: rotate(-45deg);: Rotates the heart by -45 degrees, creating the characteristic slanted appearance of the heart shape.

margin-top: 50px;: Provides some top margin to center the heart on the page.


Next, the ::before and ::after pseudo-elements are used to create the top parts of the heart:

.heart:before,

.heart:after {

    content: "";

    width: 100px;

    height: 100px;

    background-color: red;

    border-radius: 50%;

    position: absolute;

}


.heart:before {

    top: -50px;

    left: 0;

}


.heart:after {

    top: 0;

    left: 50px;

}

.heart:before, .heart:after: Targets the ::before and ::after pseudo-elements of the heart.

content: "";: Adds content to the pseudo-elements. In this case, empty content is used because we're creating shapes, not displaying text.

width and height: Sets the dimensions of the pseudo-elements, making them circular with border-radius: 50%;.

background-color: Sets the background color of the pseudo-elements to red.

position: absolute;: Positions the pseudo-elements absolutely within the .heart container.

.heart:before: Positions the first pseudo-element (top-left part of the heart).

top: -50px;: Places the pseudo-element 50 pixels above the top edge of the heart.

left: 0;: Aligns the pseudo-element with the left edge of the heart.

.heart:after: Positions the second pseudo-element (top-right part of the heart).

top: 0;: Aligns the pseudo-element with the top edge of the heart.

left: 50px;: Places the pseudo-element 50 pixels to the right of the left edge of the heart.

Together, these styles create a heart shape by combining a square-shaped base (.heart) with two circular top parts (::before and ::after). The transform: rotate(-45deg); on the base creates the overall heart shape by slanting it at a 45-degree angle. Adjustments to dimensions and positioning can be made to customize the appearance of the heart.



..

إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.