2015-04-06 117 views
3

我已经为我的问题寻找解决方案,但是我没有找到它。
我的情况是,我有一个DIV,像一个邮政的容器(我的项目,在这篇文章里面有海报头像,名字,时间戳,文本等的类)。问题是,即使具有文本(POST_TEXT)的DIV自身的大小,这就像一个容器不DIV的,所以出现这种情况:根据孩子内部的内容调整容器DIV大小DIV

打印更好地理解:http://imgur.com/a/j2HVX#0

这是我的实际代码:

<div class="post_container"> 
    <div class="post_who"> 
     <div class="post_avatar"><img src="avatar2.png" /></div> 
     <div class="post_name">Solange Campolina</div> 
     <div class="post_timestamp">03/04/15<br />15:34</div> 
    </div> 
     <div class="post_info"> 
      <div class="post_title">Trabalho sobre fotografia</div> 
      <div class="post_text">RANDOM PORTUGUESE WORDS: É importante questionar o quanto a determinação clara de objetivos estende o alcance e a importância de alternativas às soluções ortodoxas. A prática cotidiana prova que o início da atividade geral de formação de atitudes cumpre um papel essencial na formulação dos relacionamentos verticais entre as hierarquias. No entanto, não podemos esquecer que a contínua expansão de nossa atividade é uma das consequências das posturas dos órgãos dirigentes com relação às suas atribuições. O cuidado em identificar pontos críticos no fenômeno da Internet auxilia a preparação e a composição do orçamento setorial.<span class="post_value">Valor: 3,0 E1AMP</span></div>    
     </div> 
     <div class="post_links"> 
      <a><div class="post_button"> 
        <img src="icon_date.png" /><span>24/04/15</span> 
      </div></a> 
      <a href="#"><div class="post_button"> 
        <img src="icon_attachment.png" /><span>1</span> 
      </div></a> 
      <a href="aluno_disciplinas_disciplina_comentarios.htm"><div class="post_button"> 
       <img src="icon_comment.png" /><span>3</span> 
      </div></a> 
     </div> 
     <div class="post_divider"></div>   
</div> 

这是简化的代码,只有重要的部分:

<div class="post_container"> 
    <div class="post_who"> 
     <!--Content!--> 
    </div> 
    <div class="post_info"> 
     <div class="post_title"><!--Title!--></div> 
     <div class="post_text"><!--Content (text goes here)!--><span class="post_value">Valor: 3,0 E1AMP</span></div>    
    </div> 
    <div class="post_links"> 
     <!--Buttons!--> 
    </div> 
    <div class="post_divider"><!--This is the light gray line that divides the posts!--></div>   
</div> 

的CSS部分:

/*Relevant classes:*/ 

.post_container{ 
position: relative; 
min-height: 140px; 
height: auto; 
}  
.post_info{ 
position: absolute; 
display: inline-block; 
vertical-align: top; 
margin: 20px 20px 0px 20px; 
padding-bottom: 32px; 
width: 550px; 
} 
.post_links{ 
position: absolute; 
bottom: 5; 
left: 110px; 
} 
.post_divider{ 
background-color: #e8e8e8; 
height: 1px; 
width: 85%; 
position: absolute; 
right: 0; 
bottom: 0; 
} 
.post_text{ 
height: auto; 
} 

/*Other classes:*/ 

.post_who{ 
display: inline-block; 
margin: 10px; 
font-style: italic; 
font-size: 1.2em; 
text-align: center; 
width: 75px; 
} 
.post_avatar{ 
border: 1px solid whitesmoke; 
border-radius: 50%; 
overflow: hidden; 
width: 60px; 
height: 60px; 
position: relative; 
bottom: 0px; 
margin: 15px 0 0 5px; 
} 
.post_avatar img{ 
vertical-align: middle; 
display: block; 
width: 60px; 
min-width: 100%; 
min-height: 100%; 
} 
.post_name{ 
white-space: nowrap; 
overflow: hidden; 
text-overflow: ellipsis; 
width: 65px; 
} 

我看到,作为文本在“POST_TEXT” DIV增长的方式,“post_container” DIV应该有它的高度调节,因为高度设置作为自动。根据我在这里看到的另一个问题,我尝试将'overflow:auto'设置为'post_container',但它所做的只是向它添加滚动条。我需要解决这个没有任何脚本。

小提琴:http://jsfiddle.net/3ck990zc/

+1

您的子元素是绝对定位的,因此它们不在内容流中,因此它们不会影响父容器的高度。尝试删除绝对定位。 –

+0

男人我不能相信所有这个问题只是因为这个,我试图纠正这个小时,这是真的。我真的不知道,绝对定位的元素并没有助长父母的高度。谢谢! –

回答

1

这里是你需要做的首先是不绝对定位主容器中的元素,而浮动他们,里面的div可以绝对定位,看调整的CSS:

/*Relevant classes:*/ 

.post_container{ 
position: relative; 
min-height: 140px; 
height: auto; 
}  
.post_info{ 
display: inline-block; 
vertical-align: top; 
margin: 20px 20px 0px 20px; 
padding-bottom: 32px; 
width: 550px; 
float:left; 
} 
.post_links{ 
position: absolute; 
bottom: 5; 
left: 110px; 
} 
.post_divider{ 
background-color: #e8e8e8; 
height: 1px; 
width: 85%; 
right: 0; 
bottom: 0; 
} 
.post_text{ 
height: auto; 
} 

/*Other classes:*/ 

.post_who{ 
display: inline-block; 
margin: 10px; 
font-style: italic; 
font-size: 1.2em; 
text-align: center; 
width: 75px; 
float:left; 
} 
.post_avatar{ 
border: 1px solid whitesmoke; 
border-radius: 50%; 
overflow: hidden; 
width: 60px; 
height: 60px; 
position: relative; 
bottom: 0px; 
margin: 15px 0 0 5px; 
} 
.post_avatar img{ 
vertical-align: middle; 
display: block; 
width: 60px; 
min-width: 100%; 
min-height: 100%; 
} 
.post_name{ 
white-space: nowrap; 
overflow: hidden; 
text-overflow: ellipsis; 
width: 65px; 
} 
+0

谢谢,只删除绝对定位做到了,我没有浮动它,因为'display:inline-block;'做浮球会做什么,并有它的优势(据我所知)。 –

+0

很高兴听到你解决它! –

2

.post_info类有position: absolute这使得股利不宽和高(这就是我想说的)。绝对位置必须仅在必要时使用。在你的情况下,这是可以避免的。但是因为你已经写了很多CSS,所以我可以建议你使用Bootstrap类像行(对于外层容器,它将包含内部具有适当宽度和高度的所有内容)和网格系统(为了将你的列的宽度因此)。相信会,知道现在Bootstrap是必须的,肯定它可以为你节省很多痛苦。

我tryed来修改你的小提琴,并添加一些引导类和删除一些你的,并在年底我的jsfiddle看起来是这样的: https://jsfiddle.net/obwjrbhn/2/

请注意,我在框架已经使用jQuery &扩展在左侧菜单中为bootstrap.js和bootstrap.css添加外部资源中的引用。你可以在你的脑袋将这些它们添加到您的项目:

<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 

但是,这将使你的网站加载速度慢,所以我的建议是下载jQuery和引导,并在项目中添加本地refenrences。

祝你好运!

+0

感谢您的提示,是的,我计划使用bootstrap,但现在我只是创建页面来为我的项目制作示例。权威文件将有更多有组织的编码和可能的引导程序。 –