我希望紅色虛線位於黑色實線內,即我不希望內容溢出黑色實線。我也爲容器類添加了最小高度,但內容溢出。你可以在我的代碼中修改相同的代碼。謝謝!
這裏的HTML代碼 -
.container{
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 100%;
min-height: 300px;
border: 1px solid black;
}
.item_1{
position: absolute;
left: 10%;
height: 210px;
width: 210px;
border: 1px dashed green;
float: left;
}
.item_2{
position: absolute;
left: 50%;
width: 210px;
transform: translate(-50%);
height: 210px;
border: 1px dashed green;
float: left;
}
.item_3{
position: absolute;
right: 10%;
width: 210px;
height: 210px;
border: 1px dashed green;
float: left;
}
.img{
padding: 5px;
}
.content{
text-align: center;
font-family: Roboto;
font-weight: bold;
min-height: 90px;
border: 1px dashed red;
}
<html>
<head>
<link rel="stylesheet" href="second.css">
</head>
<body>
<div class="container">
<div class="item_1">
<img src="1.jpg" alt="Arya Stark" class="img">
<div class="content">
<h3>Arya Stark</h3>
<p>I am Arya Stark of House Winterfell.</p>
</div>
</div>
<div class="item_2">
<img src="2.jpg" alt="Jon Snow" class="img">
<div class="content">
<h3>Jon Snow</h3>
<p>I am a bastard and the king in the north.</p>
</div>
</div>
<div class="item_3">
<img src="3.jpg" alt="Tyrion Lannister" class="img">
<div class="content">
<h3>Tyrion Lannister</h3>
<p>I am the dwarf of Casterly Rocks and the hand to Danerys Targaryen</p>
</div>
</div>
</div>
</body>
</html>
第1步:停止使用絕對定位釘住一切。這不是你想要的工具。去做一些關於如何以正確方式實現CSS列或網格佈局的研究。 – CBroe
絕對定位是佈局網頁的非常糟糕的方法。它非常不靈活,並且有更好更快的響應選項。查看[** LearnLayout.com **](http://learnlayout.com/) –