简体   繁体   English

文字换行时,如何在浮动div中将此图像保持在垂直居中位置?

[英]How do I keep this image in the floated div centered vertically when the text wraps?

I've done some research to figure out how to keep the text aligned with the red box (which will be replaced with an icon) after it wraps. 我进行了一些研究,以弄清楚在换行后如何使文本与红色框(将被图标替换)对齐。

But now because of the float, the icon is raised to the top of the container <td> . 但是现在由于浮动,图标被提升到容器<td>的顶部。 Unfortunately now I'm stuck with how to center the div.imgwrap vertically within its parent, after the link wraps to two lines. 不幸的是,在链接换行为两行之后,我现在div.imgwrap停留在如何将div.imgwrap垂直居中的父级中。

Is it possible to do this just with CSS, without having to modify the HTML? 是否可以仅使用CSS来执行此操作,而无需修改HTML? The obvious solution is to just add an extra column for the icons, but that would take a lot more work. 显而易见的解决方案是为图标添加一个额外的列,但这将需要更多的工作。

http://jsfiddle.net/fzvy5oca/ http://jsfiddle.net/fzvy5oca/

<table>
  <tr class="">
    <td>
      <div class="imgwrap">
        <img src="https://i.imgur.com/H2Vulqm.png" alt="[   ]" width="16" height="16">
      </div>

        <a href="test">test really long name....</a>

    </td>
    <td>smaller column</td>
    <td>small column</td>
    <td>medium size column</td>
  </tr>
</table>

CSS: CSS:

table,
tr,
td {
  border: solid 1px black;
  padding: 1em;
  text-align: left;
  vertical-align: middle;
  line-height: 1.125rem;
  font-size: 0.875rem;
  font-family: sans-serif;
}

.imgwrap {
  vertical-align: middle;
  display: inline-block;
  margin-right: 8px;
  float: left;
}

.imgwrap + a {
  vertical-align: middle;
  display: block;
  margin-left: 24px;
}

.imgwrap img {
  display: block;
}

a {
  text-decoration: none;
}

is this what u want? 这就是你想要的吗?
http://jsfiddle.net/fzvy5oca/22/ http://jsfiddle.net/fzvy5oca/22/

Css 的CSS

td {
  position: relative;
  border: solid 1px black;
  padding: 1em;
  text-align: left;
  vertical-align: middle;
  line-height: 1.125rem;
  font-size: 0.875rem;
  font-family: sans-serif;
}

.imgwrap {
  position: absolute;
  top: 50%;
  margin-top: -8px; /* half of #imgwrap's height */

  vertical-align: middle;
  display: inline-block;
  margin-right: 8px;
  float: left;
}

Is this what you are looking for? 这是你想要的?

 table, tr, td { border: solid 1px black; padding: 1em; text-align: left; vertical-align: middle; line-height: 1.125rem; font-size: 0.875rem; font-family: sans-serif; /*Added*/ position: relative; } .imgwrap { vertical-align: middle; /*display: inline-block; removed */ margin-right: 8px; /*float: left; removed */ /*Added*/ position: absolute; top: 50%; transform: translateY(-50%); } .imgwrap + a { vertical-align: middle; display: block; margin-left: 24px; } .imgwrap img { display: block; } a { text-decoration: none; } 
 <table> <tr class=""> <td> <div class="imgwrap"> <img src="https://i.imgur.com/H2Vulqm.png" alt="[ ]" width="16" height="16"> </div> <a href="test">test really long name.test really long name.test really long name.test really long name.test really long name.test really long name.test really long name.test really long name....</a> </td> <td>smaller column</td> <td>small column</td> <td>medium size column</td> </tr> </table> 

 .imgwrap { display: grid; align-items: center; grid-template-columns: auto 1fr; margin-right: 8px; } 
  <table> <tr class=""> <td> <div class="imgwrap"> <img src="https://i.imgur.com/H2Vulqm.png" alt="[ ]" width="16" height="16"> <a href="test">test really long name....</a> </div> </td> <td>smaller column</td> <td>small column</td> <td>medium size column</td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM