The Wayback Machine - https://web.archive.org/web/20241107162337/https://www.geeksforgeeks.org/how-to-create-a-box-in-html/
Open In App

How To Create A Box in HTML?

Last Updated : 04 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
        &lt;div&gt; element.
    </div>
</body>

</html>


Output:

boxx1
Using the div Element with CSS

2. 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:

boxx2

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:

boxx3
Using the HTML5 Semantic Elements

4. 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:

boxx4
Using the CSS Flexbox and Grid

Similar Reads

How Create a Text Box Where User Can't Type Inside the Text Box ?
To create a text box where the user can't type inside the text box, we have different approaches. In this article, we are going to learn how to create a text box where the user can't type inside the text box in JavaScript. Below are the approaches to creating a text box where the user can't type inside the text box: Table of Content Using Readonly
3 min read
Difference between -webkit-box-shadow & box-shadow in CSS
In this article, we will find difference between 'webkit-box-shadow' & 'box-shadow' in CSS. Webkit-box-shadow: The webkit-box-shadow property in CSS is used to apply a box-shadow. The webkit-box-shadow is a browser-specific implementation for WebKit browsers like Google Chrome and Safari. Syntax: webkit-box-shadow: h-offset v-offset blur; Advan
1 min read
How to change Input box borders after filling the box using JavaScript ?
In this article, we will change the input border after filling in the text on the input text field. The onchange event attribute works when the value of the element changes and selects the new value from the list. Approach: Whenever the input value is changed with some value by the user, the onchange event attribute is triggered. After the event is
2 min read
Difference between alert box and prompt box in JavaScript
1. Alert box : An alert box is one type of popup boxes in JavaScript which is often used to make sure that information have come through the user. So, the user will have to click "OK" to proceed when an alert box pops up on the window. Syntax : window.alert("message"); Another syntax to use alert box is : alert("message"); 2. Prompt box : It is als
2 min read
How is border-box different from content-box ?
CSS (Cascading Style Sheets) is a stylesheet language used to design a webpage to make it attractive. The reason for using this is to simplify the process of making web pages presentable. It allows you to apply styles on web pages. More importantly, it enables you to do this independent of the HTML that makes up each web page. In this article, we w
3 min read
Which property specifies the distance between nearest border edges of marker box and principal box ?
In this article, we will see the property that specifies the distance between the nearest border edges of the marker box and the principal axis. The marker-offset is the property that specifies the distance between the nearest border edges of the marker box and the principal axis. Syntax: style=" marker-offset:em/cm"; Example 1: In this example, we
2 min read
Difference between alert box and confirmation box in JavaScript
In JavaScript, alert and confirm are two built-in functions used to interact with users via dialogue boxes. Both serve different purposes and provide distinct functionalities. Understanding the difference between an alert box and a confirmation box is crucial for effective user interface design and user interaction management. Table of Content Aler
3 min read
How to Create Floating Box Effect using HTML and CSS ?
The floating box effect is a classical example of a custom box-shadow technique. In this technique, we create realistic-looking shadows without using the box-shadow property that is provided by the CSS. Approach: The approach is to use after selector to use create shadow using the blur function. HTML Code: In this section, we have created the basic
2 min read
How to create a dialog box or window in HTML ?
In this article, we will create a dialog box or window using the <dialog> tag in the document. This tag is used to create popup dialog and models on a web page. This tag is new in HTML5. Syntax: <dialog open> Contents... </dialog> Example 1: <!DOCTYPE html> <html> <head> <title> How to define a dialog box o
1 min read
How to Create an Animated Search Box using HTML and CSS ?
The task is to create an Animated Search Box using HTML and CSS. The search bar is one of the most important components of the website. Basically, it is used for connecting people with websites. In the face of complicated web content, users express their needs by searching keywords, expecting to obtain accurate information and quick result. Approac
2 min read
How to Create Color Picker input box in HTML ?
In this article, we will know about the HTML color picker & will understand its implementation through the example. The <input> element of type color in HTML provides the user with an interface to select a color from the default color-picker to interface or design their own color by giving the desired value in the field of RGB. Syntax:
2 min read
Create A Custom Select Menu with Search Box using HTML CSS and JavaScript
This article will show you how to create a custom select menu with a search box using HTML, CSS, and JavaScript. First, we create an option list for the select box, and we can search for text in the text input box. The search result displays the matched option value. Additionally, we have added a "clear" button to remove your latest search. Approac
5 min read
Create a Facebook Post Box Clone in HTML CSS & JavaScript
We will see how can we implement a Facebook post box clone with the help of HTML, CSS, and JavaScript. A Facebook post is a message or update shared by a user on their profile or page. It typically includes text, photos, videos, or links. Users can tag locations, and friends, express feelings, and set privacy settings. Other users can react, commen
8 min read
Create a Testimonial box switcher using HTML CSS & JavaScript
In this article, we will develop an interactive Testimonial box switcher application using HTML CSS & JavaScript Language. In this application, we have a Card component that has the message given by the testimonial, also there is information about the person and his/her designation. The card component displays the new testimonial message when t
4 min read
How to create Scrollable HTML Comment box ?
The purpose of this article is to create a scrollable comment box using HTML and CSS3 property. An HTML scrollbox is a box with expanding scroll bars when the contents of the box are too huge to fit. Approach: For the scope of this article, we will make a div element that displays the comments, and we will create a scrollable div element. We will c
2 min read
How to create Cookie Consent Box using HTML CSS and JavaScript ?
In this article, we will see how cookies are stored using the cookie consent box using HTML, CSS, & JavaScript. Cookies are small pieces of data that are stored on a user's device while browsing the website. Cookies are used to store information for faster access to data and provide a better user experience. Also, it is used for session managem
4 min read
How to Create Tags Input Box in HTML CSS and JavaScript ?
In this article, we are going to create a tag input box using HTML, CSS, and JavaScript. A tags input box is a user interface element that enables users to insert a variety of tags into an input field. To make tag input, we use some javascript methods createElement(), addEventListener(), preventDefault(), value, keydown. JavaScript Methods: The cre
5 min read
How to Create a Box with HTML and CSS?
A box is a basic building block in web design, used to contain text, images, or other content. Understanding how to create and style a box is crucial for building responsive and visually appealing web pages. These are the below approaches to creating a box in HTML and CSS: Table of Content Using Basic CSS PropertiesUsing CSS FlexboxUsing CSS GridUs
2 min read
How to Create Popup Box using HTML and CSS?
A popup box is a graphical user interface element that appears on top of the main content to capture user attention. Using HTML and CSS, you can create stylish and interactive popup boxes for alerts, forms, or notifications, enhancing user experience without leaving the current page. Below are the approaches to create a Popup box in HTML and CSS: T
4 min read
How to create Popup Box using HTML CSS and JavaScript?
Creating a popup box with HTML, CSS, and JavaScript improves user interaction on a website. A responsive popup appears when a button is clicked, featuring an HTML structure, CSS for styling, and JavaScript functions to manage visibility. ApproachCreate the Popup structure using HTML tags, Some tags are used in this project like <h1>,<div
3 min read
How to Create a Modal Box using HTML CSS and JavaScript?
We will learn how to create a modal dialog box using HTML, CSS, and JavaScript. A modal is a pop-up dialog that appears on top of the main content and requires the user to interact with it before returning to the main screen. Approach to Create Modal BoxHTML Structure:Create a button that opens the modal.Design the modal with two main parts: the ov
2 min read
How to create a Color-Box App using ReactJS?
Basically we want to build an app that shows the number of boxes which has different colors assigned to each of them. Each time the app loads different random colors are assigned. when a user clicks any of the boxes, it changes its color to some different random color that does not equal to its previous color value. We create three components 'App'
5 min read
How to create a pop-up to print dialog box using JavaScript?
Given an HTML document and the task is to design a button that would pop-up a print dialog box. We are going to use JavaScript to do the assigned task: Approach:: Add a button which links to a JavaScript Function. Inside the JavaScript Function, use the JavaScript default function to call the print dialog box Syntax: window.print Example: <!DOCT
2 min read
How to create a Textarea Input Box using jQuery Mobile ?
jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be a Textarea Input box using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=”stylesheet” href=”http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.
1 min read
How to create dynamic search box in ReactJS?
The dynamic search box is a search bar with a text field to take the user input and then perform some operation on the user input to show him the dynamic results based on his input. API calls are made whenever a user starts typing in order to show him the dynamic options. For example Youtube search box, Google Searchbox, etc. Material UI for React
2 min read
How to Create Card with Box Shadow in React Native ?
Creating Card with box shadow in react native makes it stand out within the interface and display information in an appealing way. It will be done in React Native by using the expo cli. Expo simplifies cross-platform app development by providing a unified codebase for iOS, Android, and the web. With its tools and services, developers can create hig
4 min read
How to Create a Box Shadow in CSS ?
A box shadow in CSS is a visual effect that adds a shadow to an HTML element, creating a sense of depth and dimension. It is a styling technique commonly used to enhance the visual appearance of elements on a webpage. Below are the approaches to creating a box shadow in CSS: Table of Content Using box-shadow propertyUsing the filter propertyCreate
2 min read
How to Create an Image Overlay Fade in Box Effect on Hover ?
The image overlay fade in box effect on hover is a visual effect where a box fades in over an image when the user hovers their mouse over the image. It can be used to display additional information about the image or simply add a stylish touch to your website. ApproachCreate an HTML document and create the image container element with the class ima
2 min read
How to create an accordion hover effect with box-shadows in CSS ?
The purpose of this article is to use box shadows for creating an accordion hover effect. Approach: The CSS box-shadow property is used to draw shadows around an element. CSS box-shadow property has the following syntax. Syntax: box-shadow : x-offset y-offset blur-radius spread-radius colorExample 1:  [GFGTABS] HTML <!DOCTYPE html> <html
2 min read
How to create OTP Input Box Using React ?
Let us explore the implementation of OTP (One-Time Password) login functionality using ReactJS. OTP-based logins add an extra layer of security to your applications, making them more resilient against unauthorized access. Prerequisites:ReactHTML, CSS and JavaScriptApproach:Create a React functional component managing the OTP input state using useSt
3 min read
Article Tags :
three90RightbarBannerImg