【问题标题】:Vertical alignment of image and text, while centering text and floating image to the left图像和文本垂直对齐,同时将文本和浮动图像向左居中
【发布时间】:2015-03-03 12:51:24
【问题描述】:

我查看了一些已发布的用于将图像和文本的垂直对齐居中的解决方案(例如:Vertically align text next to an image?

但是,我也尝试将图像固定在左侧,并将文本水平居中。就像在页脚中一样,图像将“固定”在左侧,导航(或免责声明文本)将在该页脚中水平居中(并且它的位置会根据视口的大小进行调整)。

这是我目前所拥有的:

    <div style="width:100%; text-align: center;">
        <img src="logo.png" alt="my logo" style="vertical-align:middle; float: left;" />
        <span>Copyright 2015 by Company, LLC. All Rights Reserved</span>
    </div>

添加“浮动:左;”禁用“垂直对齐:中间;”。但是,如果我删除浮动属性,我不确定如何将徽标固定在左侧。

任何想法我可以如何实现这一目标。只是寻找一个简单的答案,没有什么太复杂的。

谢谢!

【问题讨论】:

  • 请发布您尝试过的代码。
  • 你试过的代码在哪里?
  • 你知道图片的高度吗?会修复吗?
  • 更新了问题并添加了我一直在处理的代码。
  • @JoeSager 是的,图像的高度是固定的。

标签: html image footer vertical-alignment text-align


【解决方案1】:

似乎适用于 float 的解决方案(至少在 chrome 中)

<div class="container">
    <img src="http://lorempixel.com/400/200/"/>
    <div class="text">this is some text</div>
</div>

* {
box-sizing: border-box;
}
.container {
width: 100%;
text-align: center;
border: 1px solid red;
display: table;
}

img {
float: left;
}

.text {
border: 1px solid blue;
display: table-cell;
vertical-align: middle;
height: 100%;
}

小提琴:http://jsfiddle.net/u7cjLL1f/

如果您希望文本占据整个父 div 并遍历图像:http://jsfiddle.net/u7cjLL1f/1/

【讨论】:

  • 感谢分享!有没有办法使文本在整个容器内居中.. 现在,看起来有两个单元格,文本在自己的单元格中居中。所以我想使第一个单元格(图像所在的位置)的宽度与图像的宽度相同。
  • 如果在.text中加上width: 100%,文字会占据图片未占用的空间,你想让文字在图片上方吗?
  • ^ 我在答案中实现了另一个解决方案(文本覆盖图像)
  • 谢谢!添加 width: 100% 到 .text 做到了!非常感谢!
【解决方案2】:

设置宽度和高度,不要使用浮动示例:

<div style="width:100%; text-align: center;">
    <img src="logo.png" alt="my logo" style="width:200px; height:200px;vertical-align:middle;display:inline-block" />
    <span>Copyright 2015 by Company, LLC. All Rights Reserved</span>
</div>

【讨论】:

  • 感谢代码!但是,图像也居中(水平)。有没有办法让它固定在最左边,同时保持文本居中?
猜你喜欢
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 2014-04-24
相关资源
最近更新 更多