How to make a square in HTML CSS

Make a square in html css with an example: learn how to use box-sizing & flexbox to create a perfect square!

Making a Square in HTML CSS

Making a square in HTML CSS is a fairly simple process. First, you need to create a div element and assign it a class name. This class name will be used to apply the styling for the square. In the example below, the class name is 'square':


<div class="square"></div>

Next, we need to add the necessary CSS styling to the class 'square' to create the square. This is done by adding a width and height to the class. Both should be equal to create a perfect square. In this example, we are using a width and height of 50 pixels:


.square {
  width: 50px;
  height: 50px;
}

Finally, we need to add a background color to the square. This can be done by adding the 'background-color' property to the class. In this example, we are using a red background color:


.square {
  width: 50px;
  height: 50px;
  background-color: red;
}

And that's it! The HTML and CSS code above will create a perfect square with a red background color. You can easily customize the size and color of the square by changing the values of the 'width', 'height' and 'background-color' properties.

Answers (0)