简体   繁体   English

在粘性 div 内垂直对齐图像

[英]Vertically align image inside a sticky div

I know this is a question that has been answered before but for some reason, I can't get anything to work.我知道这是一个以前已经回答过的问题,但由于某种原因,我什么都做不了。 I have an image inside a div that is sticky.我在一个 div 中有一个粘性的图像。 The image is currently aligned in the center horizontally but I can't get it to align in the center vertically.图像当前水平居中对齐,但我无法让它垂直居中对齐。 The image is stuck to the top of the div.图像粘在 div 的顶部。

This is my HTML code:这是我的 HTML 代码:

<section>
    <div class="stickyImg img-1">
        <img src="MainImgCrop.jpeg" class="stickyImg">
    </div>
</section>

And this is my CSS code:这是我的 CSS 代码:

section {
    display: flex;
    flex-direction: row;
}

section div.stickyImg {
    height: 92vh;
    background-color: purple;
    background-size: cover;
    background-position: center;
    position: sticky;
    position: -webkit-sticky;
    top: 8vh;
    width: 50vw;
    margin: 0;
}

.stickyImg {
    width: 90%;
    height: auto;
    display: block;
    margin-right: auto;
    margin-left: auto;
    margin: auto;
    vertical-align: middle;
    top: 50%;
}

Any help would be appreciated, thank you!任何帮助将不胜感激,谢谢!

 section { display: flex; flex-direction: row; } section div.stickyImg { height: 92vh; background-color: purple; background-size: cover; background-position: center; position: sticky; position: -webkit-sticky; top: 8vh; width: 50vw; margin: 0; display:flex; flex-direction:column; justify-content:center; align-items:center; }.stickyImg { width: 90%; height: auto; display: block; margin-right: auto; margin-left: auto; margin: auto; vertical-align: middle; top: 50%; }
 <section> <div class="stickyImg img-1"> <img src="MainImgCrop.jpeg" class="stickyImg"> </div> </section>

对齐内部的图像和文本<div>水平和垂直</div><div id="text_translate"><p>我想水平和垂直对齐 div 内的图像和文本。 我的代码:</p><pre> &lt;div style="white-space:nowrap" (click)="sidenav.toggle()"&gt; &lt;img alt='image' style="margin-top: 1vw;display: inline;" width="25" height="35" src="../assets/menu-white-18dp.svg"&gt; &lt;span style="display:inline; white-space:nowrap;" &gt; KRON&lt;/span&gt; &lt;/div&gt;</pre><p> 目前它看起来像这样: <a href="https://i.stack.imgur.com/VP0Ld.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VP0Ld.png" alt="在此处输入图像描述"></a></p><p> 我希望“KRON”与图像对齐。 我做错了什么?</p></div> - Align image and text inside <div> horizontally and vertically

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使div和图像在div内垂直对齐 - make div and image vertically align inside a div 在DIV中水平和垂直对齐图像 - Align image inside DIV horizontally and vertically 将跨度与 div 内的图像垂直对齐 - Vertically align span with image inside div 垂直对齐div内的图像和响应高度 - Vertically align an image inside a div with responsive height 如何将文字与图片中的图片垂直对齐<div> - How to align text vertically with an image inside a <div> 在div内水平和垂直居中对齐图像 - Center align image inside div horizontally and vertically 将div中的图像垂直对齐到中间 - Align image inside div to the middle vertically 垂直对齐Div内的图像和文本 - Vertically Align Image and Text Inside a Div 将文本与div中的图像垂直对齐 - Vertically align a text with an image inside a div 对齐内部的图像和文本<div>水平和垂直</div><div id="text_translate"><p>我想水平和垂直对齐 div 内的图像和文本。 我的代码:</p><pre> &lt;div style="white-space:nowrap" (click)="sidenav.toggle()"&gt; &lt;img alt='image' style="margin-top: 1vw;display: inline;" width="25" height="35" src="../assets/menu-white-18dp.svg"&gt; &lt;span style="display:inline; white-space:nowrap;" &gt; KRON&lt;/span&gt; &lt;/div&gt;</pre><p> 目前它看起来像这样: <a href="https://i.stack.imgur.com/VP0Ld.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VP0Ld.png" alt="在此处输入图像描述"></a></p><p> 我希望“KRON”与图像对齐。 我做错了什么?</p></div> - Align image and text inside <div> horizontally and vertically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM