Fluid Width with Equally Spaced Div using CSS Last Updated : 10 Oct, 2024 Comments Improve Suggest changes Like Article Like Report Fluid width with equally spaced divs in CSS refers to a layout where multiple div elements adjust their width according to the parent container's size, maintaining equal spacing between them. This is typically achieved using Flexbox or CSS Grid, ensuring responsiveness across different screen sizes.Here we have some common approaches:Table of ContentUsing the Flexbox technique in CSSUsing grid property in CSSMethod 1: Using the Flexbox technique in CSSUsing the Flexbox technique, a container is set to display: flex, allowing its child elements to adjust fluidly. By applying justify-content: space-between, equal spacing is achieved among div elements, ensuring a responsive layout that adapts to varying screen sizes.Syntax.container { display: flex; justify-content: space-between;}Example : Here we creates a responsive layout with four equally spaced red boxes inside a black container using Flexbox. Each box has a white border and displays its label in large text. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> #container { width: 98vw; height: 100vh; border: 2px solid black; /*Making the container a flexbox*/ display: flex; /*Making equal spaced divs*/ justify-content: space-between; background-color: black; } .box { width: 20%; height: 25%; background-color: red; color: white; font-size: 2rem; border: 4px solid white; } </style> </head> <body> <div id="container"> <div class="box">Box1</div> <div class="box">Box2</div> <div class="box">Box3</div> <div class="box">Box4</div> </div> </body> </html> Output:Equally Spaced div elements using flex propertyMethod2: Using grid property in CSSUsing the grid property in CSS, set the container's display to grid and define the grid-template-columns for column distribution. Utilize the gap property to create equal spacing between grid items, resulting in a flexible and responsive layout.Syntax.container { display: grid; gap: 1rem;}Example : In this example we are using CSS Grid to create a responsive layout with four red boxes arranged in a grid. Each box has a black border and a consistent height, with gaps between them. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <style> #container { /* Using the grid property */ display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; /* Making equal spaced divs */ gap: 1rem; } .box { border: 4px solid black; background-color: red; height: 30vh; } </style> </head> <body> <div id="container"> <div class="box">Box1</div> <div class="box">Box2</div> <div class="box">Box3</div> <div class="box">Box4</div> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article Fluid Width with Equally Spaced Div using CSS shahbazalam75508 Follow Improve Article Tags : Technical Scripter Web Technologies CSS Technical Scripter 2020 CSS-Questions +1 More Similar Reads How to make div width expand with its content using CSS ? Given an HTML document and the task is to make div width expand with its content using CSS. To do this, the width property is used to set the width of an element excluding padding, margin and border of element. The width property values are listed below:Syntax:width: length|percentage|auto|initial|i 3 min read How to set height equal to dynamic width (CSS fluid layout)? To set an element's height equal to its dynamic width in a CSS fluid layout, you can use the padding-bottom trick or the aspect-ratio property.Padding-Bottom Trick: Set height: 0; and padding-bottom to a percentage relative to the width to achieve the desired aspect ratio.Aspect-Ratio Property: Appl 3 min read Tailwind CSS Divide Width This class accepts lots of values in tailwind CSS in which all the properties are covered as in class form. This class is used to create division between elements as a border. To achieve this CSS border-top-width property or CSS border-bottom-width property are used in CSS. Divide Width Classes: div 2 min read How to Align a Div to Middle (horizontally/width) of Page using CSS ? We are going to see how we can align a div horizontally using CSS. Centering content both vertically and horizontally is one of the most important functions that needs to be done. Here horizontally align refers to the same amount of space to the left and right of the div inside the parent div. Three 5 min read How to Create Equal Width Table Cell Using CSS ? Creating equal-width table cells using CSS refers to adjusting the table's layout so that all columns maintain the same width. This technique ensures a consistent and organized appearance, making the table content easier to read and visually balanced.Here we are following some common approaches:Tabl 3 min read How to Set a Fixed Width Column with CSS Flexbox ? When working with CSS flexbox, setting a fixed width for a column can be achieved by using various approaches to override the default behavior of flex items, which typically grow and shrink to fill available space. Table of Content Using the flex propertyUsing the min-width and max-width propertiesC 4 min read How to place two div side-by-side of the same height using CSS? The two or more different div of same height can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. The used display property are listed below: display:table; This property is used for elements (div) which 2 min read Maintain the aspect ratio of a div with CSS Maintaining the aspect ratio of a div with CSS ensures the div's width and height stay proportional when resizing the browser window. This is important for responsive design, preserving the visual integrity of content such as videos, images, and other elements across different screen sizes. To maint 2 min read Tailwind CSS Divide Style This class accepts lots of value in tailwind CSS in which all the properties are covered in class form. By using this class we can set the divide style of any divide. Divide Style Classes: divide-solid: This class is used to set the divide class in solid form.divide-dashed: This class is used to set 1 min read How to set letter spacing using CSS ? The CSS letter-spacing property controls the space between characters in text, which helps improve both readability and makes the text more visually appealing. By adjusting the space between characters, you control the text's appearance.This property accepts values in units such as pixels, em, or re 2 min read Like