【发布时间】:2018-02-26 18:59:45
【问题描述】:
我有一个高度为 100vh 的 div,因此它始终是浏览器屏幕的高度。在这个 div 中,我想放置一个图像并将其垂直于其父级居中。
高度是可变的,所以我不能使用固定边距。我目前的标记如下:
HTML
<div class="textportfolio">
<h1>This is a title</h1>
<p class="textbio-small">
The Roosevelt dime is the current ten-cent piece of the United States.
</p>
<img class="portfolio-slides-img" src="https://i.imgur.com/iheO43X.png">
</div>
CSS:
.textportfolio {
font-family: "Lora", serif;
margin: 5%;
background: #e9cca3;
height: 100vh;
}
img.portfolio-slides-img {
max-width: 40%;
max-height: 40%;
margin: 0 auto;
display: block;
}
有人知道如何根据浏览器高度垂直居中图像吗?
这里是代码sn-p
.textportfolio {
font-family: "Lora", serif;
margin: 5%;
background: #e9cca3;
height: 100vh
}
img.portfolio-slides-img {
margin-top: 15%;
max-width: 40%;
max-height: 40%;
margin: 0 auto;
display: block;
}
<div class="textportfolio">
<h1>This is a title</h1>
<p class="textbio-small">
The Roosevelt dime is the current ten-cent piece of the United States.
</p>
<img class="portfolio-slides-img" src="https://i.imgur.com/iheO43X.png">
</div>
【问题讨论】:
-
你试过 position: absolute attibute 吗?
-
不太清楚你在追求什么。图像是否应该居中,并且文本位于居中图像的垂直上方或您是否要使整个文本/图像组合居中?
-
只有图像应该在(橙色)div内垂直居中。
标签: html css image center vertical-alignment