英文:
Div and Image vertically missalligned when height: 100vh
问题
可以有人告诉我为什么 div 和图像垂直分开吗?
<div style="height: 100vh; display: inline-block;width: 50%; background-color: blue">测试</div><!-- --><img src="photos/openening.jpg" alt="酷炫镜头" style="height:100vh; display: inline-block; width: 50%; object-fit:cover;">
对我来说,网站看起来像这样:
(我为此选择了一张黑色的图像。)
如果您能告诉我原因,也能告诉我如何更正它,使它们都从相同的高度开始,就像以下网站一样:
我也愿意尝试其他方法。
英文:
Can someone please tell me why the div and the image are vertically separated? <br><br>
<div style="height: 100vh; display: inline-block;width: 50%; background-color: blue">TEST
</div><!--
--><img src="photos/openening.jpg" alt="SICK SHOT" style="height:100vh; display: inline-block; width: 50%; object-fit:cover;">
For me the website looks like that:<br>
Image
(I took a black Image for this.)
<br>
And if you are able to tell me the reason behind that, can you please also tell me a way to correct it, so both of those start at the same height. That it looks somehow like the following website:<br>petermckinnon<br><br> I would also be open for some different approaches to this.
答案1
得分: 2
这是因为图像的垂直对齐属性使其与 div 顶部内联。我已在下面的示例中进行了更正:
<div style="height: 100vh; display: inline-block; width: 50%; background-color: blue">TEST</div><!--
--><img src="photos/openening.jpg" alt="SICK SHOT" style="height: 100vh; display: inline-block; width: 50%; vertical-align: bottom; background: black; object-fit: cover;">
英文:
It is because the image's vertical alignment property puts it inline with the top of the div. I've corrected it in the example below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<div style="height: 100vh; display: inline-block;width: 50%; background-color: blue">TEST
</div><!--
--><img src="photos/openening.jpg" alt="SICK SHOT" style="height:100vh; display: inline-block; width: 50%; vertical-align:bottom; background: black; object-fit:cover;">
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论