Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add space between elements so they fill their container div?

Tags:

css

I want to add space between spans so that the leftmost and rightmost spans will be close to the edges of the inner div. I've tried to add the following rule, but it had no effect.

span.icon-square {
    margin: 0 auto;
}

span.icon-square:first-child {
    margin-left: 0;
}

span.icon-square:last-child {
    margin-right: 0;
}

The illustration of what I'm trying to achieve is given below:

enter image description here enter image description here

So, what am I missing?

like image 538
Alex Lomia Avatar asked Mar 15 '16 13:03

Alex Lomia


People also ask

How to make a <Div> fill the remaining space?

Another way of making a <div> fill the remaining space is to use the CSS position property. Just set the position to “absolute” to stretch the <div>.

How do I add a space between two elements in CSS?

Add CSS ¶ Set the justify-content property to "space-around" for the.flex2 element. Set the justify-content property to "space between" for the.flex3 element. Set the display property to “flex” for both elements.

How to stretch a Div inside a container using CSS?

A child div inside a container can be made to take the complete width and height of the parent div. There are two methods to stretch the div to fit the container using CSS tat are discussed below: Method 1: First method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div.

How to fill the height of the remaining space in HTML?

If you need the content to fill the height of the full screen, you’re in the right place. In this snippet, we’ll demonstrate four methods of making a <div> fill the height of the remaining space. So, let’s start! 1. The most usual solution to this problem is to use Flexbox.


2 Answers

You can do this with Flexbox and justify-content: space-between.

.content {
  display: flex;
  justify-content: space-between;
  max-width: 400px;
  margin: 0 auto;
  background: #A0C5E8;
  padding: 10px 0;
}

span {
  width: 50px;
  height: 50px;
  background: black;
}
<div class="content">
  <span></span>
  <span></span>
  <span></span>
  <span></span>
</div>
like image 146
Nenad Vracar Avatar answered Sep 25 '22 03:09

Nenad Vracar


For Infos and older browser, text-align:justify + a pseudo element to generate an extra line can still be usefull. For really old browser (IE5) , turn the pseudo into a tag (span), technic is quiet old but still efficient where flex is not avalaible or unwanted.

edit : there is nothing here about text-justify if you ever read too fast ;)

div {
  background:#C3DEB7;
  padding:1px;
}
p {
  background:#A0C5E8;
  margin:1em auto;
  width:80%;
  text-align:justify;
}
p:after {
  content:'';
  width:100%;
}
span, p:after {
  display:inline-block;
}
span {
  border-radius: 15px;
  background:#7A9FC1;
  line-height:60px;
  width:60px;
  margin-top:1em;
  text-align:center;
  color:white;
  box-shadow:inset 0 0 0 1px ;
}
span:nth-child(1) {
  background:#709AC2;
}
span:nth-child(3) {
  background:#6D93B7;
}
span:last-child {
  background:#948798;
}
<div>
  <p>
    <span> span</span>
    <span> span</span>
    <span> span</span>
    <span> span</span>
  </p>
</div>

http://codepen.io/anon/pen/NNbXEm

like image 30
G-Cyrillus Avatar answered Sep 24 '22 03:09

G-Cyrillus