垂直对齐时,Div和图像高度为100vh不对齐

huangapple go评论75阅读模式
英文:

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;">

对我来说,网站看起来像这样:

图像

(我为此选择了一张黑色的图像。)

如果您能告诉我原因,也能告诉我如何更正它,使它们都从相同的高度开始,就像以下网站一样:

petermckinnon

我也愿意尝试其他方法。

英文:

Can someone please tell me why the div and the image are vertically separated? <br><br>

&lt;div style=&quot;height: 100vh; display: inline-block;width: 50%; background-color: blue&quot;&gt;TEST
&lt;/div&gt;&lt;!--
--&gt;&lt;img src=&quot;photos/openening.jpg&quot; alt=&quot;SICK SHOT&quot; style=&quot;height:100vh; display: inline-block; width: 50%; object-fit:cover;&quot;&gt;

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 -->

&lt;div style=&quot;height: 100vh; display: inline-block;width: 50%; background-color: blue&quot;&gt;TEST
&lt;/div&gt;&lt;!--
--&gt;&lt;img src=&quot;photos/openening.jpg&quot; alt=&quot;SICK SHOT&quot; style=&quot;height:100vh; display: inline-block; width: 50%; vertical-align:bottom; background: black; object-fit:cover;&quot;&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月7日 02:46:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定