0

I have two div elements. Second is inside in first element. In second I display some text. For second div I set height to auto and when I put more text in div height is greater. Also I set height for first div to auto, but first div has always same height.

How I can set height of DIV to be dependable of number of text rows?

<div class="first-div">
  <div class="second-div">
  </div>
</div>

.first-div {
    background-color: #f5f5f5;
    margin-top: 5px;
    margin-left: 5px;
    margin-right: 5px;
    border-radius: 6px;
    border: 1px solid #b8b8b8;
    text-align: justify;
    padding: 4px 4px 4px 4px;
    word-wrap: break-word;
    height: auto;
    min-height: 75px;
}
.second-div {
    width: 30%;
    float: right;
    font-size: 9px;
    height: auto;
}
whale_steward
  • 2,088
  • 2
  • 25
  • 37
kat1330
  • 5,134
  • 7
  • 38
  • 61

3 Answers3

2

Add overflow:hidden to .first-div.

You may want to check out this question: How does CSS 'overflow:hidden' work to force an element (containing floated elements) to wrap around floated elements?

Community
  • 1
  • 1
greener
  • 4,989
  • 13
  • 52
  • 93
1

Demo 1

add overflow: auto to outer div (.first-div)

css

.first-div {
    background-color: #f5f5f5;
    margin-top: 5px;
    margin-left: 5px;
    margin-right: 5px;
    border-radius: 6px;
    border: 1px solid #b8b8b8;
    text-align: justify;
    padding: 4px 4px 4px 4px;
    word-wrap: break-word;
    height: 100%;
    min-height: 75px;
    overflow:auto; /* added */
}
.second-div {
    width: 30%;
    float: right;
    font-size: 9px;
    height: auto;
}

Demo 2

or you can add div to the html and set its style as clear: both

css

.clear {
    clear: both;
}

html

<div class="first-div">
  <div class="second-div"></div>
     <div class="clear"></div>
</div>
4dgaurav
  • 11,360
  • 4
  • 32
  • 59
0

You can remove the min-height from your .first-div and apply overflow: hidden check out the fiddle, I think this is what you want.

In the .second-div you can change the height with min-height. In the fiddle I have it at 300px.

http://jsfiddle.net/wcnbq9xc/

ValleyDigital
  • 1,460
  • 4
  • 21
  • 37