2

I want a border around a <div> with 2 child elements. At the moment the green border is just at the top and not all around the <div> container. Can you tell me how to fix this issue?

Here you can see my code:

.frame {
    border-style: solid;
    border-color: green;
    width: 500px;
}
.left {
    float: left;
    min-height: 20px;
    width: 200px;
    min-height: 20px;
}
.right {
    float: right;
    min-height: 20px;
    width: 300px;
}
.entry {
    height: 20px
}
<div class="frame">
    <div class="left">
        <div class="entry">
            key
        </div>
    </div>
    <div class="right">
        <div class="entry">
            value
        </div>
        <div class="entry">
            value
        </div>
        <div class="entry">
            value
        </div>
    </div>
</div>
Hitesh Misro
  • 3,397
  • 1
  • 21
  • 50
JohnDizzle
  • 1,268
  • 3
  • 24
  • 51

3 Answers3

3

Your .frame div height become zero as there is no other content apart from the floated div childs, and that's how CSS works. Add this part of code to your .frame class

.frame{
    overflow: hidden;
}

Using overflow: hidden; would create a block formatting context. It would render block boxes in which floats interact with each other.

More at: Block formatting context

Hitesh Misro
  • 3,397
  • 1
  • 21
  • 50
1

The problem is that your frame doesn't get a height because all the children are floating. You can solve this by adding overflow: hidden; to the frame class.

Ravenix
  • 1,010
  • 4
  • 15
  • 40
1

While using float on children, don't forget to set layout on parent elements. There are many ways of setting layouts. For example

.frame {
    overflow: hidden;
}

Or you can use :after pseudo element:

.frame:after {
    content: '';
    display: block;
    clear: both;
}

.frame {
  border-style: solid;
  border-color: green;
  overflow: hidden;
  width: 500px;
}
.left {
  float: left;
  min-height: 20px;
  width: 200px;
  min-height: 20px;
}
.right {
  float: right;
  min-height: 20px;
  width: 300px;
}
.entry {
  height: 20px;
}
<div class="frame">
  <div class="left">
    <div class="entry">
      key
    </div>
  </div>
  <div class="right">
    <div class="entry">
      value
    </div>
    <div class="entry">
      value
    </div>
    <div class="entry">
      value
    </div>
  </div>
</div>
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95