0

I have some css and html code to uniformly separate the buttons (or group of buttons). Justification works great but for some reason the container div .justified height is notably taller than its contents.

enter image description here

Why this happens? I want it to be the same height as the buttons. How can i do it? (I have a jsfiddle that shows my problem equivalent to the following code)

/* Approach based on these two: */
/* http://stackoverflow.com/questions/6865194/ */
/* http://jsfiddle.net/thirtydot/EDp8R/ */
div.justified {
    text-align: justify;
    -ms-text-justify: distribute-all-lines;
    text-justify: distribute-all-lines;
    border: thin solid purple;
}
div.justified > div {
    border: thin solid green;
    display: inline-block;
    *display: inline;
    zoom: 1;
}
div.justified div.spacer {
    width: 1px;
    display: inline-block;
}
div.justified > span.stretch {
    width: 100%;
    display: inline-block;
    font-size: 0;
    line-height: 0;
}
<br/>
<br/>
example 1:
<div class='justified'>
    <div>
        <button>button1</button>
    </div>
    <div>
        <button>button2</button>
    </div>
    <span class='stretch'></span>
</div>
example 2:
<div class='justified'>
    <div class='spacer'></div>
    <div>
        <button>button1</button>
    </div>
    <span class='stretch'></span>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
nacho4d
  • 43,720
  • 45
  • 157
  • 240

4 Answers4

1

The reason this is happening is because of the stretch element. It is creating a new inline line break. The break is causing another line to appear below the content. If you want to get this completely right, try looking into using floats and a clearfix, but a quick fix is to add this:

.justified { font-size: 0; }

Matthew R.
  • 4,332
  • 1
  • 24
  • 39
  • This worked. But in my case `font-size: 0` causes inner buttons font size to be 0 too. I am work-arounding for now. Probably I should check floats and clearfix as you said. – nacho4d Aug 03 '15 at 13:43
1

Very simple with flexbox, no extra divs or spans.

div.justified {
  border: thin solid purple;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
      -ms-flex-pack: justify;
          justify-content: space-between;
}
<div class='justified'>
  <button>button1</button>
  <button>button2</button>
</div>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

You have a div arround your button while you should modify your button. The following snipplet shows how I would do it.

/* Based on: */
/* http://stackoverflow.com/questions/6865194/ */
/* http://jsfiddle.net/thirtydot/EDp8R/ */
div.justified {
    text-align: justify;
    -ms-text-justify: distribute-all-lines;
    text-justify: distribute-all-lines;
    border: thin solid purple;
    
}
div.justified > button {
    border: thin solid green;
    display: inline-block;
    *display: inline;
    zoom: 1;
}
div.justified div.spacer {
    width: 1px;
    display: inline-block;
}
div.justified > span.stretch {
    width: 100%;
    display: inline-block;
    font-size: 0;
    line-height: 0;
}
<br/>
<br/>
example 1:
<div class='justified'>
        <button>button1</button>
        <button>button2</button>
    <span class='stretch'></span>
</div>
example 2:
<div class='justified'>
    <div class='spacer'></div>
        <button>button1</button>
    <span class='stretch'></span>
</div>
Silve2611
  • 2,198
  • 2
  • 34
  • 55
0

add this css -

div.justified {
    text-align: justify;
    -ms-text-justify: distribute-all-lines;
    text-justify: distribute-all-lines;
    border: thin solid purple;
    line-height: 0; /* This line */
}

http://jsfiddle.net/usqp2qxf/1/

Rohit Kumar
  • 2,048
  • 1
  • 14
  • 28