Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...