Home > Web Front-end > CSS Tutorial > How do you create a box filled with color in HTML/CSS?

How do you create a box filled with color in HTML/CSS?

WBOY
Release: 2023-09-07 13:29:02
forward
1558 people have browsed it

How do you create a box filled with color in HTML/CSS?

Overview

HTML and CSS are technologies with which we can create any shape and any frame. To create a box filled with color can be achieved using HTML as we can create a simple box frame with the help of HTML and can fill it with color using CSS properties. We can also use the HTML "svg" (scalar vector graphics) attribute to draw a box and the fill color attribute to fill the box with color.

grammar

The syntax for using svg to create a box and fill it with color is as follows. It contains four attributes: x, y, width, height and padding. So, the x-axis sets the horizontal position of the box relative to the screen, the y-axis sets the vertical position, the height sets the height of the box, the width sets the width of the box, and the fill property sets the color of the box within the box.

<svg>
   <rect x=“” y=“” width=“” height=“” fill=“” />
</svg>
Copy after login

Algorithm 1

  • Create an HTML file in a text editor that contains HTML boilerplate.

  • Now add the svg tag to the HTML body.

<svg></svg>
Copy after login
  • Now creates a box inside the svg tag using semantic markup.

<rect/>
Copy after login
  • Now set the properties in the tag such as x, y, width, height and padding.

<rect width="100" height="100" fill="green" />
Copy after login
  • Now open your browser and you will see a box filled with color.

Example 1

In this example, we will use HTML svg tag to draw a box with the help of attribute.



     create box using svg 


    

Created using svg

<rect width="100" height="100" fill="green" />
Copy after login

Algorithm 2

  • Create an index.html file on a text editor and add HTML boilerplate to the file

  • Now add the div container to the HTML body

<div></div>
Copy after login
  • Now add the inner style tag to the HTML head tag.

<style></style>
Copy after login
  • Now use CSS style properties to set the height, width and color of the box.

<style>
   div{
      width: 10rem;
      height: 10rem;
      background-color: green;
   }	
</style>
Copy after login
  • Open the browser, the box is created successfully and filled with color.

Example 2

In this example, we will create a box with an HTML div container and will use CSS style properties to fill the box with color.



     create u=box using simple HTML and CSS
    


    <div></div>

Copy after login

in conclusion

Just like we use svg (scalar vector graphics) to create a box, we can also use it to create many different shapes. The best way to create a color-filled box is to use a simple div container with some CSS style properties, because with CSS you can easily customize the box in a separate stylesheet. In small projects, working with svg may not seem difficult, but if you have to build the same box multiple times, writing the same code multiple times is a daunting task. If you don't want to change the position of the box, you don't have to use the "x" and "y" attributes with "". These types of boxes are used for loading animations or can be used as cards to display information

The above is the detailed content of How do you create a box filled with color in HTML/CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template