The Wayback Machine - https://web.archive.org/web/20240712110328/https://www.geeksforgeeks.org/how-to-make-div-elements-display-inline-using-css/
Open In App

How to make div elements display inline using CSS ?

Last Updated : 04 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn the different approaches to displaying div elements inline. This will allow them to take available space horizontally on the screen in a line.

First, we will create a basic HTML code for the div elements and apply different CSS styling to make it display inline.

CSS properties:

In this article, we will use below CSS properties.

  • Display: We will use display: flex and display: inline-block property to show div elements inline.
  • Float: We will use the float: left property to show div elements side by side.

Approach 1: In this approach, we have set the display: flex and flex-direction: row to the parent div of all the div elements. Due to the flex-direction: row property all the elements inside the parent div will be shown in a single row.

Example: In this example, we are using the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .main {
            display: flex;
            flex-direction: row;
            font-size: 30px;
            color: green;
            border: 4px dashed green;
            padding: 5px;
            width: 400px;
        }
 
        .main div {
            border: 2px solid red;
            margin: 10px 20px;
            width: 100px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <div>Geeks</div>
        <div>for</div>
        <div>Geeks</div>
    </div>
</body>
</html>


Output:

 

Users can see that all div elements inside the parent div are displaying inline.

Approach 2: In this approach, we will apply the display: inline-block property to all the div we need to display inline. The display:inline-block property will set all div elements side by side.

Example: In this example, we are using the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        div {
            display: inline-block;
            color: green;
            border: 2px solid red;
            margin: 10px 20px;
            width: 120px;
            font-size: 40px;
        }
    </style>
</head>
 
<body>
    <div>Geeks</div>
    <div>for</div>
    <div>Geeks</div>
</body>
</html>


Output: 

 

Users can see all div elements displayed inline. 

Approach 3: In this approach, we will apply the float: left property to all the div elements to display them inline. Also, users can use the float: right CSS property to show all div elements in reverse order from the right side.

Example: In this example, we are using the above approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        div {
            float: left;
            color: green;
            border: 2px solid red;
            margin: 10px 20px;
            width: 120px;
            font-size: 40px;
        }
    </style>
</head>
 
<body>
    <div>Geeks</div>
    <div>for</div>
    <div>Geeks</div>
</body>
</html>


Output: 

 

Users can see in the below output image how all div elements look when we apply the float: left CSS property to all div elements.

Approach 4: In this approach, we will use the span element instead of the div element. When the user needs to write only text inside the div tag, they can use the span tag as all span elements render inline by default.

Example: In this example, we are using the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        span {
            color: green;
            border: 2px solid red;
            margin: 10px 20px;
            width: 100px;
            font-size: 30px;
        }
    </style>
</head>
 
<body>
    <span>Geeks</span>
    <span>for</span>
    <span>Geeks</span>
</body>
</html>


Output: In the below output image, users can see how the span element renders inline.

 

In the below output image, users can see how the span element renders inline.



Similar Reads

What is the difference between display: inline and display: inline-block in CSS?
The display property in CSS is a very useful and commonly used property that contains many values. The display property defines how an HTML element should be displayed on the webpage. The property also specifies the type of box used for an HTML element and how it should be laid out on the page. If we need to display the elements that are laid out a
4 min read
How display: inline is different from display: inline-block in CSS ?
In this article, we will know about the display property in CSS, along with understanding the 2 different property values for display property, i.e., display: inline &amp; display: inline-block properties, &amp; will understand their basic differences &amp; implementation through the examples. The display property facilitates setting the element by
3 min read
What is the difference between display:inline-flex and display:flex in CSS ?
In this article, we will see the display property &amp; its values, i.e. the inline-flex &amp; flex, in order to specify the display behavior of an element, along with understanding their basic differences &amp; will illustrate them through the examples. Both CSS display: inline-flex and display: flex properties are used to create flexible layouts.
3 min read
How to Make a Child Div Element Wider than Parent Div using CSS ?
When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to
2 min read
How to display paragraph elements as inline using CSS ?
The purpose of this article is to display paragraph elements as inline elements using CSS. The display property in CSS is used for placing the components ("div", "hyperlink", "heading", etc) on the web page. The display property is set to inline. It has the default property of "anchor" tags. It is used to place the "div" inline i.e. in a horizontal
1 min read
What is the difference between inline-flex and inline-block in CSS?
The display property specifies how an element should be displayed on a webpage. There can be many values, related to this property in CSS. Inline-block and inline-flex are two such properties. Although there are several values that this property can have, to understand the aforementioned, let us first look into three other values: inline, block, an
3 min read
How to overlay one div over another div using CSS
Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS
2 min read
How to make Parent Div Activate Styling of Child Div for Hover and Active ?
In this article, we will see how to make parent div activate styling of child div for hover and active. We can do this by using Pseudo-classes like hover and active. Approaches to make parent div activate styling of child div for hover and activeUsing Pure CSS: with pseudo-classesUsing CSS and JavaScript: without pseudo-classesMethod 1: Using Pure
2 min read
How to make div fixed after scroll to that div ?
In this article, we will see how to make a div fixed after you scroll to it by using the position property with a sticky value. This effect is generally used on websites when an important message needs to be shown. Set the property position to sticky to create elements that behave as fixed positions whenever the user scrolls to that position. Fixin
4 min read
Display div element on hovering over &lt;a&gt; tag using CSS
In this article, we will know how to render the div element by hovering over the &lt;a&gt; tag using CSS, &amp; will understand its implementation through the example. We can apply an adjacent sibling CSS selector property to the &lt;a&gt; tag that will display the hidden div element's content while hovering over it. The Adjacent sibling selector i
2 min read
three90RightbarBannerImg