将文本底部与图像完全对齐

时间:2017-01-02 21:20:20

标签: html css

我的图像显示在其侧面。问题是,较长的字母(例如,' y')越过底部边距,因此,图像不与文本的整个底部对齐。这是描述问题的图像:

enter image description here

如何让文字与图像完全对齐?

JSFiddle



body {
  background-color: lightgreen;
}
#line {
  width: 100%;
  height: 1px;
  background-color: orange;
  position: absolute;
  top: 58px;
}
img {
  height: 50px;
}
#text {
  display: inline-block;
}

<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Book_Hexagonal_Icon.svg/697px-Book_Hexagonal_Icon.svg.png" alt="">
<div id="text">My Texty</div>
<div id="line"></div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

vertical-align:bottom;添加到img

&#13;
&#13;
body {
  background-color: lightgreen;
}
#line {
  width: 100%;
  height: 1px;
  background-color: orange;
  position: absolute;
  top: 58px;
}
img {
  height: 50px;
  vertical-align: bottom;
}
#text {
  display: inline-block;
}
&#13;
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Book_Hexagonal_Icon.svg/697px-Book_Hexagonal_Icon.svg.png" alt="">
<div id="text">My Texty</div>
<div id="line"></div>
&#13;
&#13;
&#13;

默认情况下,内联和内联块元素与文本的基线垂直对齐。

baseline

通过将图片的vertical-align更改为bottom,您可以告诉它与文字的bottom对齐,而不是baseline < / p>

答案 1 :(得分:0)

Flexbox使这很容易

&#13;
&#13;
body {
  background-color: lightgreen;
}
#line {
  width: 100%;
  height: 1px;
  background-color: orange;
  position: absolute;
  top: 58px;
}
img {
  height: 50px;
}
#text {
  /* display: inline-block; */
}
.container {
  display: flex;
  align-items: flex-end;
}
&#13;
<div class="container">
  <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Book_Hexagonal_Icon.svg/697px-Book_Hexagonal_Icon.svg.png" alt="">
  <div id="text">My Texty</div>
  <div id="line"></div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您需要为texty设置底部填充和垂直对齐:

body {
  background-color: lightgreen;
}
#line {
  width: 100%;
  height: 1px;
  background-color: orange;
  position: absolute;
  top: 58px;
}
img {
  height: 50px;
}
#text {
  display: inline-block;
  vertical-align: text-bottom;
  padding-bottom: 3px;
}
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Book_Hexagonal_Icon.svg/697px-Book_Hexagonal_Icon.svg.png" alt="">
<div id="text">My Texty</div>
<div id="line"></div>