0

So I made four simple divs and I will be changing the property of the header div.

The HTML:

   <div class="third">
        Lorem Ipsum
    </div>
    <div class="third">
        Lorem Ipsum
    </div>
    <div class="third">
        <div class="header">Lorem Ipsum</div>
    </div>

The CSS:

.third {
    width:500px;
    display:inline-block;
    border-right:1px solid black;
    height:400px;
}
.header {
    margin-left:16%;
    font-family:Arial;
    font-size:200%;
}

The third div works great but the first two divs are pushed down because of the bigger text. What can I do to prevent this issue?

zsaat14
  • 1,110
  • 2
  • 10
  • 20
123321123321
  • 81
  • 1
  • 3
  • 10
  • 1
    In short, add `vertical-align:top` to `.third`. The default `vertical-align` value is `baseline`. The baseline of an `inline-block` is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its `overflow` property has a computed value other than `visible`, in which case the baseline is the bottom margin edge - see http://stackoverflow.com/questions/12950479/display-inline-block-elements-vertical-aligns-inproperly/12950536#12950536 – andyb Mar 26 '14 at 16:41
  • @CTravel change that width to `100px` (or make your browser width > `1500px`) so the blocks don't wrap to see see the problem. – andyb Mar 26 '14 at 16:53

3 Answers3

2

Adding vertical-align: top will fix your problem.

Fiddle: http://jsfiddle.net/QX7FH/

If you're curious why this works, andyb does a great job explaining why here: Why does this inline-block element have content that is not vertically aligned

Community
  • 1
  • 1
Jack
  • 9,151
  • 2
  • 32
  • 44
  • Please try to actually explain why your answer fixes the problem. – andyb Mar 26 '14 at 16:52
  • @andyb Wow, sorry. I actually thought you did a great job explaining in your comment (which, I gave you an upvote for). I saw your answer and link after posting my answer. I apologize for not being as thorough as your original post. – Jack Mar 26 '14 at 18:09
  • 1
    It's not really about being thorough but about sharing your knowledge of what the problem is and why your solution fixes it. I've seen so many answers downvoted for not explaining so was just trying to help you not suffer the same fate! There is no need to apologize :) – andyb Mar 26 '14 at 20:30
  • 1
    Thanks @andyb. I hear you.. Stack Overflow suffers from "first post!" syndrome where thorough answers are ignored for immediate solutions (and quick reputation). This site is a great resource for knowledge sharing (I've personally learned a lot from thorough answers like yours), and it should definitely be used that way. Thanks again. – Jack Mar 26 '14 at 21:19
1

You can use floats instead of inline-block, you also gain a little bit of browser support (old ie's):

http://jsfiddle.net/aP9Fu/

.third {
    width:500px;
    display:block;
    border-right:1px solid black;
    height:400px;
    float: left;
}

Also, I added a container around all those divs in order to clear the floats.

Jonas Grumann
  • 10,438
  • 2
  • 22
  • 40
0

div's are automatically set to be block elements. This means that they will create a line break. If you also put display:inline-block; in your css for your .header, it should prevent it from creating a new line.

Carl0s1z
  • 4,683
  • 7
  • 32
  • 47
Katrina
  • 1,922
  • 2
  • 24
  • 42