简体   繁体   English

使父母超越孩子

[英]Making parent grow beyond child

I have a parent container which has a div containing text: 我有一个包含文本的div的父容器:

<div id="parent">
<div id="text"></div>
</div>

I have the following CSS rule for them: 我对他们有以下CSS规则:

parent{
clear:both;
background:#f3f3f3;
min-height:180px;
}
text{
float:left;
overflow:auto;
min-height:44px;
width:400px;
margin-left:10px;
margin-top:12px;
}

Thing is, the parent div grows only upto the length of the text. 事实是,父div仅增长到文本的长度。 I want it to grow a bit beyond the text say 20% more. 我希望它超出文本所说的20%。 How do I achieve this? 我该如何实现?

see this: http://jsfiddle.net/VJ6rg/1 看到这个: http : //jsfiddle.net/VJ6rg/1

#parent{
   padding:3%;
   float:left;
   overflow:auto;

   clear:both;
   background:#f3f3f3;
   min-height:180px;
}
#text{
   float:left;
   overflow:auto;
   min-height:44px;
}

Looks like it works as you're expecting it too. 看起来它像您期望的那样工作。 Apart from the fact that you're missing hashes in front of your CSS ids. 除了您在CSS ID前面缺少哈希的事实之外。

ie #text or #parent. 即#text或#parent。

See the following fiddle: http://jsfiddle.net/ZTtwv/ 请参见以下提琴: http : //jsfiddle.net/ZTtwv/

#parent {
    clear:both;
    background:#f3f3f3;
    min-height:180px;
    border:1px solid green;
}
#text {
    float:left;
    overflow:auto;
    min-height:44px;
    width:400px;
    margin-left:10px;
    margin-top:12px;
    border:1px solid red;
}

Give it some padding like this: 给它一些填充,如下所示:

For all sides: padding: 20%; 对于所有方面: padding: 20%;

For top: padding-top: 20%; 顶部: padding-top: 20%;

For bottom: padding-bottom: 20%; 底部:底部 padding-bottom: 20%;

For left: padding-left: 20%; 左: padding-left: 20%;

For right: padding-right: 20%; 右: padding-right: 20%;

Since you're floating #text that element's dimension will no longer affect #parent . 由于您浮动的是#text ,该元素的尺寸将不再影响#parent The min-height will still apply to #parent regardless of its content. min-height将仍然适用于#parent而不管其内容如何。

Setting overflow: hidden/auto on #parent will cause it to pick up #text 's dimension once again. 设置overflow: hidden/auto #parent上的overflow: hidden/auto将导致它再次获得#text的尺寸。 You may also use the .clearfix solution if overflow isn't suitable for you: 如果overflow不适合您,您也可以使用.clearfix解决方案

/**
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    contenteditable attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that are clearfixed.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */
.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.cf {
    *zoom: 1;
}

To use this, simply apply the .cf class to #parent 要使用此功能,只需将.cf类应用于#parent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM