How To Create A Box in HTML?
Last Updated :
04 Sep, 2024
A box in HTML can be created using the various CSS properties and HTML elements. Boxes can be styled in multiple ways using CSS to define their width, height, background color, border, padding, margin, and other properties.
In this article, we will explore the various methods to create the box in HTML ranging from simple approaches using HTML tags to the more advanced techniques using CSS.
Below are the following approaches to create a box in HTML:
1. Using the div Element with CSS
The <div> element is one of the most commonly used HTML elements for creating boxes. It is a block-level container that can be used to group the content. By using CSS, we can style the <div> element to create a variety of boxes, ranging from simple to complex.
Syntax:
<div class="box">Content goes here</div>
Example: Here, we will create the simple box using the <div> element. We can apply some CSS styles to define the width, height, background color, border, padding and margin of the box. This example can demonstrate how to encapsulate the content within a styled container.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightblue;
border: 2px solid blue;
padding: 10px;
margin: 10px;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<div class="box">This is a box created using a
<div> element.
</div>
</body>
</html>
Output:
Using the div Element with CSS2. Using the span Element with CSS
The <span> element is the inline container typically used to style parts of the text or other inline elements. However, we can use it to create the box by changing its display property to the inline-block or block using CSS. This approach can be ideal when you need the box within the line of the text or when you don't want the box to take up the full width of its container.
Syntax:
<span class="inline-box">Inline box content</span>
Example: Here, we will create the inline box using the <span> element: By default, <span> is inline, but using the display: inline-block; in the CSS allows us to give it a width and height, making it behave like a box.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.inline-box {
display: inline-block;
width: 150px;
height: 50px;
background-color: pink;
border: 2px dashed red;
padding: 5px;
margin: 5px;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<span class="inline-box">Inline box</span>
<span class="inline-box">Another inline box</span>
</body>
</html>
Output:

3. Using the HTML5 Semantic Elements
HTML5 introduced the new semantic elements like <section>, <article>, <aside> and <header>. These elements are meant to represent the different sections of the web page semantically. It can also be styled with CSS to look like boxes. This approach is great for defining the meaning of the different content areas while also giving them a visual boundary.
Syntax:
<section class="styled-section">Section content</section>
Example: Here, we will use the <section> element to create the box. We will style it using CSS to define its size, color, and border. This example shows how to use the semantic HTML elements to create the boxes while providing meaning to the content.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.styled-section {
width: 250px;
height: 120px;
background-color: lightgreen;
border: 2px solid green;
padding: 15px;
margin: 15px;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<section class="styled-section">This is a styled section box.</section>
</body>
</html>
Output:
Using the HTML5 Semantic Elements4. Using the CSS Flexbox and Grid
CSS Flexbox and Grid are powerful layout model in the CSS3 that allow you to create the flexible and responsive box layouts. Flexbox is the one-dimensional layout system for arranging the items in rows or columns, while Grid is the two-dimensional system that handles the both rows and columns. These approaches can be ideal for creating the complex layouts that adapt to the different screen sizes.
Syntax:
<div class="flex-container">
<div class="flex-box">Flexbox item 1</div>
<div class="flex-box">Flexbox item 2</div>
</div>
Example: Here, we will create the responsive layout using the CSS Flexbox. We can define the container with display: flex; and add the multiple box items inside it. This example can demonstrates how Flexbox can be used to create the boxes that align the horizontally and adjust their sizes based on the container.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
justify-content: space-around;
background-color: lightgrey;
padding: 10px;
}
.flex-box {
width: 100px;
height: 50px;
background-color: coral;
border: 2px solid brown;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<div class="flex-container">
<div class="flex-box">Item 1</div>
<div class="flex-box">Item 2</div>
</div>
</body>
</html>
Output:
Using the CSS Flexbox and Grid