简体   繁体   English

如何在DIV内垂直对齐文本和图像?

[英]How do I align text and an image vertically inside a DIV?

I am trying to vertically center an image and text. 我试图垂直居中图像和文字。 Here is the code I have in a wokring jsfiddle http://jsfiddle.net/4a9XX/ as you can see the text is lower than the image how can I bring them to be level? 这是我在一个wkring jsfiddle http://jsfiddle.net/4a9XX/中的代码,因为你可以看到文字低于图像我怎么能把它们带到水平?

CSS CSS

#display {
position:fixed !important;
bottom:0;
z-index:999;
background-color:#D41924;
width:100%;
padding:5px;
color:#FFF;
font-weight:bold;
vertical-align:middle;
}

HTML HTML

<div id="display"><img src="http://i46.tinypic.com/2jd4due.gif" /> SOME TEXT </div>

Use this ( vertical-align ): 使用此( vertical-align ):

​#display img, #display span {vertical-align: middle;}​'

And Enclose the text inside the <span> tags. 并将文本包含在<span>标记内。

Updated Fiddle: http://jsfiddle.net/4a9XX/10/ 更新小提琴: http//jsfiddle.net/4a9XX/10/

http://jsfiddle.net/4a9XX/4/ http://jsfiddle.net/4a9XX/4/

Added a span tag to the text. 在文本中添加了span标记。

<div id="display"><img src="http://i46.tinypic.com/2jd4due.gif" /> <span>SOME TEXT</span> </div>

Shift it using CSS position: relative; 使用CSS position: relative;移动它position: relative;

#display span {
   position: relative;
   top: -3px;    
}

#display span selects a span tag that's a child of an element id display . #display span选择一个span标签,它是元素id display的子元素。 Everything else is pretty self explanatory. 其他一切都非常自我解释。

Css CSS

   #display {
position:fixed !important;
bottom:0;
z-index:999;
background-color:#D41924;
width:100%;
padding:5px;
color:#FFF;
font-weight:bold;
}

img{
vertical-align:top;
}
p{
display:inline-block;
    vertical-align:top;
}

HTML HTML

<div id="display"><img src="http://i46.tinypic.com/2jd4due.gif" /> <p>SOME TEXT </p></div>

because img tag is inline-block element 因为img 标签inline-block 元素

Live demo http://jsfiddle.net/4a9XX/8/ 现场演示http://jsfiddle.net/4a9XX/8/

the vertical-align attribute should be applied on inner elements instead of the container. vertical-align属性应该应用于内部元素而不是容器。 You can put your text in a span and apply vertical-align on both span and img : 您可以将文本放在span并在spanimg上应用vertical-align

​#display img, #display span {
    vertical-align:middle;
}​

Here's the fiddle . 这是小提琴

对齐内部的图像和文本<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> 如何在TD单元格内垂直对齐div文本? - How do I vertically align div text inside of a td cell? 如何垂直对齐div中的文本? - How do I vertically align text in a div? 垂直对齐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 如何在另一个div内垂直对齐div? - How do I vertically align a div inside of another div? 如何在表格单元格内垂直对齐div? - How do I vertically align a div inside a table cell? <a>在div中</a>垂直对齐<a>文本</a> - Vertically Align <a> text inside a div 如何使用CSS在图像旁边垂直对齐多个文本? - How do I vertically align multiple text next to an image with CSS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM