當您將定位到一個元素,它會使用可用的定位上它的父母。如果沒有提供,它將定位到body元素。因此,當您在元素底部獲得文字/鏈接時,您必須爲position: relative
(或position: absolute
)其父母之一,否則它不會知道您希望將其放在哪個位置。相反,無論先發現哪一個,它都會使用該元素進行定位。
我認爲這是可能的,你已經有了太多的標記來完成你在這裏做什麼(什麼是.placeholder
呢?爲什麼不直接使用li
?),以及span
s表示包裹塊長相之一不合適的(如果你真的需要包裝的話,應該是div
)。我不確定,但如果您需要重新使用它,您可能需要將#items
更改爲類。它看起來不像id
。而你的id和class名稱不具有描述性,你的選擇器不夠具體(通常,遠離ul
和li
,因爲這些頁面具有全局效應)。
儘管如此,我認爲這是你要找的。請注意我如何在#items li
上使用padding
,然後補償bottom: 5px
。如果它只是位於塊的頂部,則也不需要定位img
標籤。
HTML(片段)
<li>
<div class="itemplaceholder">
<img src="http://www.els.qut.edu.au/blendedlearning/blackboard/graphics/test_on.gif"/>
<p>
Test title<br/>
Description A
<a href="#">Link</a>
</p>
</div>
</li>
CSS
#items {
display: inline;
position:relative;
margin: 0;
padding: 0;
}
#items li {
float: left;
list-style-type: none;
margin: 0;
padding: 5px 0 0 0;
}
#items .itemplaceholder {
height:200px;
width:160px;
text-align: center;
position: relative;
}
.placeholder {
width:640px;
height:200px;
overflow: hidden;
}
.content {
width:800px;
height:240px;
}
#items .itemplaceholder p {
position: absolute;
bottom: 5px;
width: 100%;
height: 50%;
}
#items .itemplaceholder p a {
position: absolute;
display: block;
bottom: 0;
text-align: center;
width: 100%;
}
http://jsfiddle.net/39bhW/3/
也許你解釋你的問題更實際的小提琴鏈接或東西嗎?你描述的限制是模糊的,你可以將鏈接絕對地設置到DIV的底部嗎? – David 2012-03-03 21:38:11
每當你使用絕對位置記住沒有CSS尺寸的元素有0寬度和高度 – charlietfl 2012-03-03 21:49:52
請粘貼你的代碼在這裏:http://jsfiddle.net/ – Alp 2012-03-03 21:55:18