简体   繁体   English

将文本与div中的图像垂直对齐

[英]Vertically align a text with an image inside a div

I saw a similar post to this which is here but I noticed that no one answered and that when I do press the jsfiddle, it doesn't center it. 我在这里看到了与此类似的帖子,但我注意到没有人回答,当我按jsfiddle时,它不会居中。

I have this current code and what I'm trying to accomplish is to have the image directly in the middle vertically inside the div. 我有当前代码,我要完成的工作是将图像直接直接放在div内部的中间位置。 I've tried vertical-align and putting it in span and top % but nothing seems to work. 我试过垂直对齐并将其放在span和top%中,但似乎没有任何效果。 I'm also using Chrome so I don't know if that matters. 我也在使用Chrome,所以我不知道这是否重要。 http://jsfiddle.net/y84mx/ http://jsfiddle.net/y84mx/

    <div class="hours">
    <div id="sun">
        <img src="http://jsfiddle.net/img/logo.png" alt="Opening" height="auto" width="20%" align="left" vertical-align="middle" />
        <span>Hours</span>
    </div>
    </div>

Thanks! 谢谢!

You can use: 您可以使用:

#sun {
    position:relative;
}

img {
    background: #3A6F9A;
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    margin:auto;
}

Updated Fiddle 更新小提琴

For both items set display: inline-block; 对于这两个项目,都display: inline-block;
Then make sure to match the line-height of the <span> the height of the image. 然后确保以匹配line-height的的<span>的图像的高度。

Demo 演示

HTML HTML

<div id="hours"> 
    <span id="sun"> 
        <img src="http://jsfiddle.net/img/logo.png" alt="twibuffer"  /> 
    </span>  
    <span> Hours</span>
</div>

CSS CSS

#hours {
    margin-bottom:2%;
    text-align: center;
}
#hours span {
    display: inline-block;
    vertical-align: middle;
}
#hours #sun {
    border: 1px solid red;
    /* to show the centering */
}

I have updated the fiddle , Please check 我已经更新了小提琴 ,请检查

#sun{
 line-height : auto;
 border: 1px solid;
 text-align : center;  
}

.hours{
padding-left: 2%;
padding-right: 2%;
text-align: right;
color: #000;
font-size: 40px;
}

img {
  background: #3A6F9A;
}

Check out this http://jsfiddle.net/chetangawai/y84mx/9/ 看看这个http://jsfiddle.net/chetangawai/y84mx/9/

#sun{
  text-align:center;
    display:inline-block;
    border:solid
}

.hours{
    color: #000;
    font-size: 40px;
    padding-left: 2%;
    padding-right: 2%;

}

img {
     background: #3A6F9A;
}

对齐内部的图像和文本<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> - How to align text vertically with an image inside a <div> 垂直对齐Div内的图像和文本 - Vertically Align Image and Text 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 <a>在div中</a>垂直对齐<a>文本</a> - Vertically Align <a> text inside a div 如何在DIV内垂直对齐文本和图像? - How do I align text and an image vertically inside a DIV? 使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 内垂直对齐图像 - Vertically align image inside a sticky div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM