css - 如何垂直对齐图像,即div旁边的动态高度

标签 css image vertical-alignment

就像标题说的那样,我想将一张图像放在垂直中心,该图像位于具有动态高度的 div 旁边。 这是代码:http://jsfiddle.net/txuxn4c1/2/

<div class="element">
    <div class="logo_div">
        <img src="http://www.iconsdb.com/icons/download/orange/stackoverflow-4-32.jpg" width="30px" height="30px" />
    </div>
    <div class="text_div"> 
        text here<br />
        text here<br />
        text here<br />
        text here<br />
        text here<br />
    </div>
    <div style="clear:both"></div>
</div>

.element {
    border: 1px solid green;
}
.logo_div {
    float: left;
    border: 1px solid blue;
}
.text_div {
    float: left;
    width: 105px;
    border: 1px solid red;
}

我尝试将 div 样式显示设置为 inline-block 或 table(-cell) 和 vertical-align: middle 就像其他一些线程答案中建议的那样,但它没有用。

问候

最佳答案

一个解决方案是使用 display: table-cellvertical-align: middle:

.element {
  border: 1px solid green;
}
.logo_div {
  display: table-cell;
  border: 1px solid blue;
  vertical-align: middle;
}
.text_div {
  display: table-cell;
  width: 105px;
  border: 1px solid red;
  vertical-align: middle;
}
<div class="element">
  <div class="logo_div">
    <img src="http://www.iconsdb.com/icons/download/orange/stackoverflow-4-32.jpg" width="30px" height="30px" />
  </div>
  <div class="text_div">text here
    <br />text here
    <br />text here
    <br />text here
    <br />text here
    <br />
  </div>
  <div style="clear:both"></div>
</div>
<div class="element">
  <div class="logo_div">
    <img src="http://www.iconsdb.com/icons/download/orange/stackoverflow-4-32.jpg" width="30px" height="30px" />
  </div>
  <div class="text_div">text here
    <br />text here
    <br />text here
    <br />text here
    <br />text here
    <br />
  </div>
  <div style="clear:both"></div>
</div>

关于css - 如何垂直对齐图像,即div旁边的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486575/

相关文章:

css - 带有表格单元格父级和绝对定位伪元素的 IE 垂直居中错误

javascript - Carousel slick.js + bootstrap 列宽问题

javascript - Vue.js - 将类添加到单击的按钮

javascript - 100% 宽度容器内的中心 div,左右浮动宽度

javascript - 使用 toggleClass 淡入淡出图像

css - 图像左侧的 Div

java - 在 Java 中读/写 Tiff

text - 将 SVG 文本与其他 SVG 对象垂直对齐以与 Inkscape 一起使用

html - 由于向描述中添加图标按钮而导致未对齐后垂直对齐垫扩展面板标题

html - 竖排文字方向