gpt4 book ai didi

javascript - 将div与父级垂直放置在中间

转载 作者:行者123 更新时间:2023-11-30 15:37:55 25 4
gpt4 key购买 nike

我想将子元素与父元素垂直放置

<!DOCTYPE html>
<html>
<head>
<title>Jajal</title>
<style type="text/css">
#body {
font-family: sans-serif, arial, 'Roboto';
}
#outer {
width: 280px;
background-color: white;
height: 253px;
background-color: #f0f0f0;
}
.imgbox {
height: 174px;
width: 270px;
overflow: hidden;
display: table;
margin: 0 auto;
background-color: black;
text-align: center;
}
.img_content {
max-height: 174px;
max-width: 270px;
margin: 0 auto;
vertical-align: middle;
display: table-cell;
}
.titlebox {
max-width: 270px;
text-align: center;
}
.title {
font-weight: 900;
font-size: 14px;
}
</style>
</head>
<body>
<div id="outer">
<div class="imgbox">
<img class="img_content" src="http://lorempixel.com/700/100" alt="coba">
</div>
<div class="titlebox">
<p class="title">Lorem ipsum Amet</p>
</div>
</div>
</body>
</html>

为什么当图片太宽时 img_content 类没有将 imgbox 类放在垂直中间?

最佳答案

添加以下 CSS:

.imgbox {
height: 174px;
width: 270px;
overflow: hidden;
margin: 0 auto;
background-color: black;
text-align: center;
position: relative;
}

.img_content {
max-height: 174px;
max-width: 270px;
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
}

关于javascript - 将div与父级垂直放置在中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41236912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com