All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Float Center CSS

Last Updated : Mar 11, 2024

How To Float Center CSS

In this article we will show you the solution of how to float center CSS, we will see the use of the float property of CSS to center an image. We will use three different properties of float.

Float: left; : this will make the image float to left.

Float:right; : using this we can make the image float into right

Float: middle; : this will make the image float into the middle.

Step By Step Guide On How To Float Center CSS :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> how to float center css </title>
    <style>
           body{
            background-color: rgb(51, 50, 50);
            font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
        }
        h1{
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
        h3{
            color : rgb(95, 194, 95) ;
            font-size: 20px;
        }
        .container{
            color: darkblue;
            display: flex;
        }
        .box{
            height: 200px;
            width: 300px;
            background-color: bisque;
            border: 2px solid orange;
            margin: 2px;
        }
        img {
            height: 50%;
            width: 50%;
            background-position: cover;
            display: block;
            margin: auto;
        }
       #sunflower{
        float: left;
       }
       #rose{
        float: right;
       }
       #daisy{
        float: middle;
       }
    </style>
</head>
<body>
    <center>
        <h1> TALKERSCODE </h1>
        <h3> how to float center css </h3>
    </center>
    <div class="container">
        <div class="box">
            <img src="https://images.unsplash.com/photo-1580046154419-2ab638e3be8e?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=387&q=80" alt="" id="sunflower">
            Large flower heads or blooms on sunflowers make them beneficial plants. They got their name because they follow the Sun by turning their blossoms from east to west. The cultivation of sunflowers began throughout North and South America.
        </div>
        <div class="box">
            <img src="https://images.unsplash.com/photo-1496062031456-07b8f162a322?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=465&q=80" alt="" id="rose">
            The stem of a rose plant is covered in tiny, pointed thorns. When the flower is in bloom in the garden, it is gorgeous. The rose represents grace, compassion, and love.
        </div>
        <div class="box">
            <img src="https://images.unsplash.com/photo-1560717789-0ac7c58ac90a?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=870&q=80" alt="" id="daisy">
            A daisy flower typically has white petals and a yellow centre, though it occasionally has pink or rose hues. There are several flowers that make up a daisy.
        </div>
    </div>
</body>
</html>
  1. writing HTML code <!DOCTYPE html> is used to specify the HTML version that a file is written in.
  2. The <HTML> tag used to specify the root of an HTML document must then be written. Additionally, add the </html> tag at the end.
  3. The metadata for the HTML file is contained in the <head> tag, which is closed with the </head> tag.
  4. The HTML document's <title> is then established using the title tag, followed by the < /title> tag.
  5. We'll use an external CSS file to style the HTML page.
  6. The <h1> element is used to add a heading, and the </h1> tag is used to close it.
  7. creating a <div> with the class of container.
  8. Now into the <div> create three another <div> using class of box. Into every div add some images with different id and some paragraphs.
  9. using <style> tag to add CSS.
  10. For the class box, add some height, width, background-color, and border.
  11. Now for the image tag <img>, adding the height, width to 50%. Using background-position to cover. Set the display to block and the margin to auto.
  12. To the first image, set the float property to the left.
  13. To the second image, set the float property to the right.
  14. Lastly in the third image, set the float property to middle.

Conclusion :-

Last, here in conclusion, we can say that with this article’s help, we know how to float center in CSS.

I hope this article on how to float center CSS helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪