简体   繁体   English

在多条H1标签线上对齐图像

[英]Aligning image on multiple H1 tag lines

I have an image in an h1 tag which is vertically aligned in the middle. 我在h1标​​签中有一张图片,该图片在中间垂直对齐。 It works well with a short sentence. 短句子很有效。 However, when the sentence is on multiple lines, I'd like the image to be aligned in the middle of the entire sentence rather than just the first line. 但是,当句子在多行中时,我希望图像在整个句子的中间对齐,而不仅仅是第一行。 Is there anyway to do this without creating two columns? 无论如何,不​​创建两列就可以做到这一点?

 <h1><img src="https://images-na.ssl-images-amazon.com/images/I/4109Z2o0HuL._SX522_.jpg" height="35" style="padding-right:20px; vertical-align: middle;">Hello, how are you?</h1> <h1><img src="https://images-na.ssl-images-amazon.com/images/I/4109Z2o0HuL._SX522_.jpg" height="35" style="padding-right:20px; vertical-align: middle;">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</h1> 

You can achieve this with flex 您可以通过flex实现

https://jsfiddle.net/yc9umsop/ https://jsfiddle.net/yc9umsop/

h1 {
  display: flex;
  align-items: center;
}

HTML 的HTML

<h1>
   <img src="https://images-na.ssl-images-amazon.com/images/I/4109Z2o0HuL._SX522_.jpg" height="35" style="padding-right:20px; vertical-align: middle;">
   <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</span>
</h1>

Note I wrapped your text with a span 请注意,我用跨距包裹了您的文字

Learn how to use flex here https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 在此处了解如何使用Flex, 网址为https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Well if you absolutely don't want to create multiple columns then you could go with this pure css approach : 好吧,如果您绝对不想创建多个列,那么可以使用这种纯CSS方法:

h1{
  position:relative;
  padding-left:80px;
}

h1 img{
  position:absolute;
  left:5px;
  top:50%;
  margin-top:-17.5px; /* IMAGE HEIGHT DIVIDED BY 2 */
}

https://jsfiddle.net/cndj27q3/4/ https://jsfiddle.net/cndj27q3/4/

My suggestion would be to go with an additional html element instead using a multi column approach if at all possible in your scenario. 我的建议是,在您的情况下尽可能使用附加的html元素,而不是使用多列方法。

Hope that helps. 希望能有所帮助。

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

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