Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

html - Spacing between inline-block divs, can't solve by removing whitespace

I know this question has been asked on SO before, and the answer is usually to remove any spaces between the markup. In this case, I can't, since I'm programatically printing out divs:

<div class="products row">
  {{#each products}}
    {{> product}}
  {{/each}}
</div>

I won't bore you with the details of the product template, but suffice it to say, the template engine will dump out divs with either a space or newline in between each one.

What I'm trying to do is just close those gaps so I get all six divs in a row (no wrapping). Also, the divs need to be centered within the page (or their container). So whether I have two divs or six, they should always be centered.

Here's a JSFiddle showing the divs with bad spacing:

http://jsfiddle.net/6opf8ybs/

Is there some way to fix this in CSS? I heard the font-size: 0 trick is not ideal.

HTML:

 <div class="container">
    <div class="row row-center">
      <div class="col-xs-2 col-center">b</div>
      <div class="col-xs-2 col-center">d</div>
      <div class="col-xs-2 col-center">b</div>
      <div class="col-xs-2 col-center">d</div>
      <div class="col-xs-2 col-center">d</div>
      <div class="col-xs-2 col-center">d</div>
    </div>
  </div>

CSS:

.row-center {
  text-align: center;
}

.col-center {
  display: inline-block;
  border: 1px solid blue;
  text-align: left;
  float: none;
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You could ty using a negative margin on the inline-block items. Take a look at this jsFiddle

.col-center {
  display: inline-block;
  border: 1px solid blue;
  text-align: left;
  margin: 0 -2px;
  padding: 10px;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...