Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

CSS making inner div match outer div height

I'm basically making a form (screenshot: http://mason.gmu.edu/~vnguyenl/form.jpg) where there are 2 columns. The label column and the input column. The label side is in a div that has its own bg color, and same with the input field side. I'm trying to make it so that if there's to much text on one side than the other column will match it's height. Right now, if one side is larger than the other, there are gaps. Would appreciate any help! Thanks.

The html for the form looks like the following:

<div class="formRow">
<div class="labelColumn">Last Name: Last Name: Last Name: Last Name: Last Name: Last Name: Last Name: Last Name: Last Name: Last Name: Last Name: Last Name: <span class="required">*</span></div>
<div class="contentcolumn">
    <input class="textBox300" type="text" id="last_name"  tabindex="1"  />
</div>
</div>

The css is the following:

#pt-profile-form .labelColumn, .labelColumn2 {
font-weight:bold;
float:left;
width:300px; /* Width of left column */
margin-left:0px;
background:#f0f4f7;
text-align:left;
padding:5px;
padding-left:14px;
display:block;
white-space:normal;
position:relative;
clear:both;
}

#pt-profile-form .formRow { clear:both; height:100%;  }

#pt-profile-form .contentcolumn, .contentcolumn2 {
margin-left:320px; /* Set left margin to LeftColumnWidth */
background-color:#eae9d7;
padding:5px;
text-align:left;
vertical-align:middle;
position:relative;
}

#pt-profile-form .labelColumn, .contentcolumn {
/*height:30px;*/
min-height:30px;
height:100%;
}

#pt-profile-form .labelColumn2, .contentcolumn2 { /* column properties for <textarea>     */
/*height:100px;*/
height:100%;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simple solution

As far as I can see the only way to achieve this is to put the background color on the wrapping element (in your case <div class="formRow">) and then ensure that wrapping element is pushed to the full height by use of clearing, as @MarcB mentioned in his comment ( http://quirksmode.org/css/clearing.html ).

So the following CSS should fix it:

.formRow {
    overflow: auto;
    background-color: #EAE9D7;
}

Demo:
http://jsfiddle.net/nottrobin/qnRgL/

The one caveat with this solution is that it will hide overflowing content. If you need overflowing content to be displayed try the alternate solution instead.


Alternate clearing solution

If changing the overflow property causes problems for any reason, another solution is to add generated content using the :after pseudo-element. E.g.:

.formRow:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
    background-color: #EAE9D7;
}

See under Generating Content Through CSS:
http://robertnyman.com/2007/04/12/how-to-clear-css-floats-without-extra-markup-different-techniques-explained/

Note however that this solution will not work in IEs 6 and 7.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...