Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically add spacing between divs without using percentage?

Tags:

html

css

I have a few divs aligned horizontally. How do I make the spacing between them automatic so that if I resize the screen or add another div, there will be equal spacing between divs.

Example when screen width is 600px:

This is an image description

Example when screen width is 330px:

enter image description here

Hopefully my explanation is good enough.

Thanks for any help!

like image 627
David Callanan Avatar asked Jan 17 '26 22:01

David Callanan


2 Answers

Flexbox can do that https://jsfiddle.net/2Lzo9vfc/210/

HTML

<div class="content">
  <div class="box">Box</div>
  <div class="box">Box</div>
  <div class="box">Box</div>
</div>

CSS

.content {
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: space-around; 
    justify-content: space-around;
    width: 100%;
}

.box {
    background: black;
    padding: 25px;
    color: white;
}
like image 150
Nenad Vracar Avatar answered Jan 20 '26 15:01

Nenad Vracar


Here you can find a solution with flexbox:

.container {
    display:flex;
    justify-content:space-between;
}
.item {
    background:#000;
    height:50px;
    width:120px;
}
<div class="container">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

More information about using flexbox you can find here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

like image 38
Sebastian Brosch Avatar answered Jan 20 '26 14:01

Sebastian Brosch