2015-04-06 137 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,但現在我只是創建頁面來爲我的項目製作示例。權威文件將有更多有組織的編碼和可能的引導程序。 –