Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Vertical Positioning

Featured Replies

So a few times I've run into this problem and I simply want to know why this happens...

 

post-31960-0-82395700-1436896880_thumb.png

 

Why won't boxes 4 and 6 position themselves under boxes 1 and 3 respectively with just the margin separating them? It appears as empty space but it's as though the baseline of the largest box (box 2) sets the vertical precedence for the other boxes. Of course, changing the vertical align of boxes 1 and 3 to bottom positions them at the base of the largest box but I can't seem to lift the lower boxes up. Is there a way to do this? Or is this just the way the DOM works? and if so - why?

 

Here's a Fiddle if anyone wants to have a play around...

 

https://jsfiddle.net/3wpc6agm/

It's just how inline-blocks work,there are JavaScript workarounds for this, or you could look into flexbox but that won't work on <IE10.

 

By the way there's no need to qualify all your selectors with element identifiers, instead of div.container just do .container. Qualifying selectors limits reuse, makes the selector fragile (if the div changed to a section it would break) and needlessly increases specificity.

The ONLY time you use element identifiers is when you are setting global styles. like H1s are going to have a font-size of 4rems.

  • Author

But why? Why do they refuse to use that extra space? Having used HTML and CSS for over a decade I constantly feel that it's so far behind its own requirements.

 

I used elements identifiers just to make it easier to identify which styles are affecting which elements but I often use them in development anyway. I'll often use different classes with the same names for different elements to prevent having to remember or document more classes than are necessary; just my way doing things. I would also never change a div to a section, if I found myself needing to do that then it would mean that I'd made a mistake in the planning phase. Nevertheless, thanks for the reply :)

Because it's more often that designers want each new row to begin with a new context. It's how things were decided back in the day and we've just got to live with that, I actually think that is good behaviour. There's many bad decisions that were made like CSS not having any concept of scope that we still have to live with.

 

Regarding your CSS have a read of this: If you are doing these things you aren't gonna get that far as a front-end developer in the industry. Good CSS has to be robust and and reuseable. These are things you should be avoiding in your css, one of them being qualifying your selectors.

http://csswizardry.com/2012/11/code-smells-in-css/

 

This section about qualified selectors http://csswizardry.com/2012/11/code-smells-in-css/#qualified-selectors

Edited by rbrtsmith

Adding a couple of classes will make it work

<div class="container">
    <div class="size1">1</div>
    <div class="size2">2</div>
    <div class="size3">3</div>
    <div class="size1 foo">4</div>
    <div class="size1">5</div>
    <div class="size4 bar">6</div>
</div>

and the CSS

div {
    box-sizing: border-box;
}

div.container {
    padding: 20px;
    background: #ddd;
    width: 600px;
    border: 2px solid #aaa
}

div.container > div {
    display: inline-block;
    width: 26%;
    background: #4679bd;
    vertical-align:top;
    margin: 3%;
    
    /* identifiers */
    text-align: center;
    font-size: 1.2em;
    color: white;
    border: 1px solid #333;
}

div.size1 {
    min-height: 120px;
}
div.size2 {
    height: 200px;
}
div.size3 {
    height: 50px;
}
div.size4 {
    height: 170px;
}
.size1.foo {
    margin-top:-60px;
}
.size4.bar {
    margin-top:-130px;
}

Not sure how useful it would be in a real world situation though.

 

However if box 4 is to be placed below box 1 it would suggest the vertical context is greater than the horizontal.

 

So wrapping box 1 and box 4 in a sub-container would be a better course of action. Then boxes 2 and 5 and boxes 3 and 6 would go into their own sub-containers.

 

So doing it that way this CSS should work

div {
    box-sizing: border-box;
}

.container {
    padding: 20px;
    background: #ddd;
    width: 600px;
    border: 2px solid #aaa
}

.sub {
    display: inline-block;
    width: 26%;
    vertical-align:top;
    margin: 3%;
}
.sub div {
    margin-bottom:50px;
    background: #4679bd;
    text-align: center;
    font-size: 1.2em;
    color: white;
    border: 1px solid #333;
}
.sub div:last-of-type {
    margin-bottom:0;
}

div.size1 {
    height: 120px;
}
div.size2 {
    height: 200px;
}
div.size3 {
    height: 50px;
}
div.size4 {
    height: 170px;
}

And can be used with either

<div class="container">
    <div class="sub">
        <div class="size1">1</div>
        <div class="size1">4</div>
    </div>
    <div class="sub">
        <div class="size2">2</div>
        <div class="size1">5</div>
    </div>
    <div class="sub">     
        <div class="size3">3</div>
        <div class="size4">6</div>
    </div>
</div>

or

<div class="container">
    <div class="sub">
        <div class="size1">1</div>
        <div class="size1">1</div>
        <div class="size1">4</div>
    </div>
    <div class="sub">
        <div class="size2">2</div>
        <div class="size1">1</div>
        <div class="size1">5</div>
    </div>
    <div class="sub">
        
        <div class="size3">3</div>
        <div class="size2">2</div>
        <div class="size1">1</div>
        <div class="size4">6</div>
    </div>
</div>

magic numbers like predefined heights are generally bad news, what happens when the content changes? if you absolutely need equal heights then use an equal heights script.

You'll need something like Masonry to achieve a layout like that, without Flexbox anyway.

 

The alternative, would be to use Flexbox, and just display the items like they current are in older browsers.

magic numbers like predefined heights are generally bad news, what happens when the content changes? if you absolutely need equal heights then use an equal heights script.

 

Did you actually read the question and look at the very illustrative diagram the OP so helpfully provided?

 

He didn't ask for equal heights he asked for the lower boxes to be equally spaced below the upper boxes. I for one took it that this is a hypothetical scenario and I did say that in a real world situation it probably wouldn't be much use.

 

However this seems to work quite well and gives me a craving for a bacon buttie.

div {
    box-sizing: border-box;
}

.container {
    padding: 20px;
    background: #ddd;
    width: 100%;
    border: 2px solid #aaa
}

.sub {
    display: inline-block;
    width: 26%;
    vertical-align:top;
    margin: 3%;
}
.sub div {
    margin-bottom:1em;
    padding:10px;
    background: #4679bd;
    text-align: center;
    font-size: 1.2em;
    color: white;
    border: 1px solid #333;
}
.sub div:last-of-type {
    margin-bottom:0;
}
<div class="container">
    <div class="sub">
        <div class="size1">This is some content</div>
        <div class="size1">This is some more content for testing purposes</div>
        <div class="size1">Short loin cupim tenderloin sausage pork loin bacon. Ham venison meatball fatback tri-tip salami brisket meatloaf bacon. Meatloaf cow filet mignon, rump strip steak</div>
    </div>
    <div class="sub">
        <div class="size2">Bacon ipsum dolor amet jerky ham landjaeger strip steak ham hock bresaola picanha turkey short ribs. Cow sausage bresaola pork loin cupim short loin venison boudin porchetta leberkas chuck corned beef shank salami. </div>
        <div class="size1">Alcatra ribeye porchetta, kielbasa frankfurter meatloaf cow landjaeger boudin beef andouille. Cow prosciutto cupim bresaola salami pig rump</div>
        <div class="size1">Hamburger leberkas swine flank tongue short loin ball tip strip steak tenderloin, brisket frankfurter tri-tip meatloaf. Fatback drumstick landjaeger,</div>
    </div>
    <div class="sub">
        
        <div class="size3">Meatloaf filet mignon landjaeger beef ribs</div>
        <div class="size2">Short ribs pastrami swine, jowl turkey ball tip capicola ribeye short loin ham pancetta shoulder andouille strip steak. Sirloin ground round shankle cupim fatback tongue turkey.</div>
        <div class="size4">Fatback pork loin rump kevin pork belly turkey hamburger tenderloin, salami ground round prosciutto bacon pig. Ball tip pancetta cow sirloin chicken.</div>
    </div>
</div>

Hope I've got brown sauce.

 

Did you actually read the question and look at the very illustrative diagram the OP so helpfully provided?

 

He didn't ask for equal heights he asked for the lower boxes to be equally spaced below the upper boxes. I for one took it that this is a hypothetical scenario and I did say that in a real world situation it probably wouldn't be much use.

 

However this seems to work quite well and gives me a craving for a bacon buttie.

div {
    box-sizing: border-box;
}

.container {
    padding: 20px;
    background: #ddd;
    width: 100%;
    border: 2px solid #aaa
}

.sub {
    display: inline-block;
    width: 26%;
    vertical-align:top;
    margin: 3%;
}
.sub div {
    margin-bottom:1em;
    padding:10px;
    background: #4679bd;
    text-align: center;
    font-size: 1.2em;
    color: white;
    border: 1px solid #333;
}
.sub div:last-of-type {
    margin-bottom:0;
}
<div class="container">
    <div class="sub">
        <div class="size1">This is some content</div>
        <div class="size1">This is some more content for testing purposes</div>
        <div class="size1">Short loin cupim tenderloin sausage pork loin bacon. Ham venison meatball fatback tri-tip salami brisket meatloaf bacon. Meatloaf cow filet mignon, rump strip steak</div>
    </div>
    <div class="sub">
        <div class="size2">Bacon ipsum dolor amet jerky ham landjaeger strip steak ham hock bresaola picanha turkey short ribs. Cow sausage bresaola pork loin cupim short loin venison boudin porchetta leberkas chuck corned beef shank salami. </div>
        <div class="size1">Alcatra ribeye porchetta, kielbasa frankfurter meatloaf cow landjaeger boudin beef andouille. Cow prosciutto cupim bresaola salami pig rump</div>
        <div class="size1">Hamburger leberkas swine flank tongue short loin ball tip strip steak tenderloin, brisket frankfurter tri-tip meatloaf. Fatback drumstick landjaeger,</div>
    </div>
    <div class="sub">
        
        <div class="size3">Meatloaf filet mignon landjaeger beef ribs</div>
        <div class="size2">Short ribs pastrami swine, jowl turkey ball tip capicola ribeye short loin ham pancetta shoulder andouille strip steak. Sirloin ground round shankle cupim fatback tongue turkey.</div>
        <div class="size4">Fatback pork loin rump kevin pork belly turkey hamburger tenderloin, salami ground round prosciutto bacon pig. Ball tip pancetta cow sirloin chicken.</div>
    </div>
</div>

Hope I've got brown sauce.

 

On second glance you're right equal heights doesnt do what he asked, but something like Masonry that @@Jack spoke of will. However magic numbers should ALWAYS be scrutinized they are one of the biggest pain points when it comes to CSS maintenance, especially on projects that are likely to grow over time.

 

The last solution you posted is pretty cool, it will work in some scenarios, if you want the order to be top to bottom like

1  5  9
2  6  10
3  7  11
4  8  12

but if not then masonry will help - although even with Masonry you don't have full controll over the order of the posts, but it generally tries to arrange them left to right.

Edited by rbrtsmith

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.