How to avoid a new line with a tag?

When working with HTML <div> tags, browsers typically display them as block-level elements, meaning each div starts on a new line. However, there are situations where you need multiple divs to appear on the same line, such as creating navigation menus, horizontal layouts, or side-by-side content sections.

This article demonstrates three effective methods to prevent div elements from creating new lines: using the display: inline property, display: inline-block property, and CSS Flexbox.

Understanding Block vs Inline Behavior

By default, <div> elements have display: block, which means they

  • Take up the full width available

  • Start on a new line

  • Force subsequent elements to the next line

To change this behavior, we modify the CSS display property.

Using display: inline Property

The display: inline property makes div elements behave like inline elements such as <span>. They flow horizontally and only take up the width of their content.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Inline Div Elements</title>
   <style>
      .inline-container {
         display: inline;
         padding: 10px;
         margin: 5px;
         border: 2px solid orange;
         background-color: #fff3e0;
      }
      .second-inline {
         display: inline;
         padding: 10px;
         margin: 5px;
         border: 2px solid purple;
         background-color: #f3e5f5;
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <div class="inline-container">First Container</div>
   <div class="second-inline">Second Container</div>
   <div class="inline-container">Third Container</div>
</body>
</html>

The output shows all three div elements displayed horizontally on the same line

[First Container] [Second Container] [Third Container]
(All containers appear side by side with colored borders)

Limitation: Inline elements cannot have width, height, or vertical margins applied to them effectively.

Using display: inline-block Property

The display: inline-block property combines the best of both worlds. Elements flow horizontally like inline elements but retain the ability to have width, height, and margins like block elements.

Key differences between inline and inline-block

inline inline-block
Cannot set width or height Can set width and height
Vertical margins have no effect Vertical margins work properly
Only takes content width Respects specified dimensions
Cannot create block formatting context Creates block formatting context

Example

<!DOCTYPE html>
<html>
<head>
   <title>Inline-Block Div Elements</title>
   <style>
      .inline-block-container {
         display: inline-block;
         width: 150px;
         height: 80px;
         padding: 15px;
         margin: 10px;
         border: 3px solid #2196f3;
         background-color: #e3f2fd;
         text-align: center;
         vertical-align: top;
      }
      .second-block {
         display: inline-block;
         width: 200px;
         height: 60px;
         padding: 15px;
         margin: 10px;
         border: 3px solid #e91e63;
         background-color: #fce4ec;
         text-align: center;
         vertical-align: top;
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <div class="inline-block-container">Container with fixed width</div>
   <div class="second-block">Another container with different dimensions</div>
</body>
</html>

The output demonstrates that inline-block containers can have different sizes while still appearing on the same line

[Container with fixed width]     [Another container with different dimensions]
(Two boxes side by side with different widths and heights but same line alignment)

Using CSS Flexbox

CSS Flexbox is the most modern and flexible approach for creating horizontal layouts. It provides better control over alignment, spacing, and responsive behavior.

Example

<!DOCTYPE html>
<html>
<head>
   <title>Flexbox Layout</title>
   <style>
      .flex-container {
         display: flex;
         flex-direction: row;
         gap: 15px;
         align-items: center;
      }
      .flex-item {
         flex: 1;
         padding: 20px;
         text-align: center;
         border-radius: 5px;
      }
      .item-one {
         background-color: #4caf50;
         color: white;
         border: 2px solid #388e3c;
      }
      .item-two {
         background-color: #ff9800;
         color: white;
         border: 2px solid #f57c00;
      }
      .item-three {
         background-color: #9c27b0;
         color: white;
         border: 2px solid #7b1fa2;
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <div class="flex-container">
      <div class="flex-item item-one">Flex Item 1</div>
      <div class="flex-item item-two">Flex Item 2</div>
      <div class="flex-item item-three">Flex Item 3</div>
   </div>
</body>
</html>

The flexbox layout creates three equally-sized containers in a row with consistent spacing

[Flex Item 1]  [Flex Item 2]  [Flex Item 3]
(Three colored boxes of equal width with gaps between them)

Advanced Flexbox Example

Flexbox offers additional properties for more sophisticated layouts

<!DOCTYPE html>
<html>
<head>
   <title>Advanced Flexbox Layout</title>
   <style>
      .advanced-flex {
         display: flex;
         justify-content: space-between;
         align-items: center;
         flex-wrap: wrap;
         min-height: 100px;
         background-color: #f5f5f5;
         padding: 10px;
         border: 1px solid #ddd;
      }
      .nav-item {
         background-color: #2196f3;
         color: white;
         padding: 10px 20px;
         border-radius: 4px;
         text-decoration: none;
         margin: 5px;
      }
      .nav-item:hover {
         background-color: #1976d2;
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <div class="advanced-flex">
      <div class="nav-item">Home</div>
      <div class="nav-item">About</div>
      <div class="nav-item">Services</div>
      <div class="nav-item">Contact</div>
   </div>
</body>
</html>

This creates a navigation-like layout where items are evenly distributed with space between them.

Comparison of Methods

Method Best For Limitations
display: inline Simple text-like content alignment No width/height control, limited styling
display: inline-block Fixed-size elements in a row Whitespace issues, less flexible
Flexbox Modern responsive layouts, complex alignment Requires understanding of flex properties

Conclusion

To avoid new lines with div tags, use display: inline for simple text-like content, display: inline-block for elements needing specific dimensions, and CSS Flexbox for modern, flexible layouts. Flexbox is generally recommended for its superior control over alignment, spacing, and responsive behavior.

Updated on: 2026-03-16T21:38:54+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements