haley
haley

Reputation: 909

CSS Floating Boxes Unexpected Corner Bump

I have a page layout which employs floating boxes with constant width and variable height, inside a variable-width container (which I'm going to make constant for the sake of this question). This is my page's code:

CSS:
#main {
    width: 640px;
    margin: 0 auto;
    min-height: 100%;
}
.profile {
    width: 300px;
    min-height: 160px;
    margin: 10px;
    float: left;
}

HTML:
<body>
  <div id="main">
    <div class="profile">1: I'm tall. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    <div class="profile">2: I'm short.</div>
    <div class="profile">3: I'm also short.</div>
    <div class="profile">4: I'm short too.</div>
  </div>
</body>

When I apply this code, div 4 seems like it's "stuck" in the corner, instead of to the left of div 3 like it should be: (Codepen preview).

What am I doing wrong, and how should I fix this glitch?

Upvotes: 2

Views: 79

Answers (1)

Danield
Danield

Reputation: 125443

You could clear the odd divs:

.profile:nth-child(odd) {
  clear:left;
}

Updated Codepen

#main {
  width: 640px;
  margin: 0 auto;
  background: #444;
}
.profile {
  width: 300px;
  min-height: 160px;
  margin: 10px;
  float: left;
  background: silver;
}
.profile:nth-child(odd) {
  clear: left;
}
<body>
  <div id="main">
    <div class="profile">1: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
      dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    <div class="profile">2: I'm short.</div>
    <div class="profile">3: I'm also short.</div>
    <div class="profile">4: I'm short too.</div>
  </div>
</body>

Upvotes: 2

Related Questions