简体   繁体   English

如何在div元素中将文本与图像垂直居中?

[英]How do you vertically center text with an image in a div element?

I am trying to make a small segment where I have an image on the left side with text vertically centered to the right of the image. 我正在尝试制作一小段,在其中左侧有图像,文字垂直居中于图像的右侧。 For the second image below, I want the image on the right, and the text vertically centered to the left of the image. 对于下面的第二个图像,我希望图像在右侧,文本垂直居中于图像左侧。

<div id="Segment2">
    <h2>Fig Tree is a discussion tool that helps organizations innovate</h2>
    <h3>We took the basics of a discussion thread and added...</h3>

    <div style="height:259px;" id="Branching">
        <section class=LeftBoundPic>
            <h4>Discussion Branching</h4>
            <img src="SomePhoto.png" alt="FigTree"/>
        </section>
        <span>
        Bunch of text <br> 
        that should be next to the image.
        </span>
    </div>

    <div style="height: 259px; margin-top: 50px;" id="ContentHubs">
        <section class=RightBoundPic>
            <h4>Content Hub</h4>
            <img src="SomePhoto.png" alt="FigTree"/>
        </section>
        <span>
            More text that should <br>
            be on the left of the photo now.
        </span>
    </div>
</div>

That is the html portion of it, but I cannot figure a way to style it with css. 那是它的html部分,但是我无法找到一种使用CSS设置样式的方法。

If you don't need to support IE9, flexbox is the perfect fit for this situation (it's very well supported now). 如果您不需要支持IE9,则flexbox非常适合这种情况(现在已得到很好的支持 )。

#Branching, #ContentHubs {
  display: -webkit-flex;
  display: flex;
  -webkit-flex-direction: row;
  flex-direction: row;
  -webkit-align-items: center;
  align-items: center;
}

.RightBoundPic {
  -webkit-order: 1;
  order: 1;
}

Make that image your background of a div and inside that div this css suggested from howtocenterincss.com or you can search for your other queries at same. 使该图像成为div的背景,并在howtocenterincss.com中 建议该CSS在该div内,或者您可以同时搜索其他查询。

This css is compatible down to IE8 此CSS向下兼容IE8

 .div-table{ display:table-cell; border:1px solid BLACK; } .div-cell{ display:table-cell; padding:5px; vertical-align:middle; } .div-row{ display:table-row; vertical-align:middle; } 
 <div class='div-table'> <div class='table-row'> <div class='div-cell'> <h4>Discussion Branching</h4> </div> </div> <div class='table-row'> <div class='div-cell'> <img src="SomePhoto.png" alt="FigTree"/> </div> <div class='div-cell'> <span> Bunch of text that should be next to the image. <br> More text and yet more and more.<br> Even more text text. </span> </div> </div> </div> <br> <div class='div-table'> <div class='table-row'> <div class='div-cell'> <h4>Discussion Branching</h4> </div> </div> <div class='table-row'> <div class='div-cell'> <span> Bunch of text that should be next to the image. </span> </div> <div class='div-cell'> <img src="SomePhoto.png" alt="FigTree"/> </div> </div> </div> 

<section class='TextBox RightBoundPic'>
    <h4>Content Hub</h4>
    <img src="SomePhoto.png" alt="FigTree"/>
</section>
.TextBox > h4, .TextBox > img { display: inline-block; }
.TextBox > img { vertical-align: middle; }

I would recomend using display:table and display:table-cell which is the mostly supported layout in almost all browsers 我建议使用display:table和display:table-cell,这是几乎所有浏览器中最受支持的布局

<style>
#Branching {display: table;}
#Branching .RightCenteredText{
   display: table-cell;
   text-align: center;
   vertical-align: middle;
}
#Branching .LeftBoundPic {display: table-cell;}
#ContentHubs {display: table;}
#ContentHubs .LeftCenteredText{
   display: table-cell;
   text-align: center;
   vertical-align: middle;
}
#ContentHubs .RightBoundPic {display: table-cell;}
</style>
<div id="Segment2">
  <h2>Fig Tree is a discussion tool that helps organizations innovate</h2>
  <h3>We took the basics of a discussion thread and added...</h3>

  <div id="Branching">
    <section class=LeftBoundPic>
        <h4>Discussion Branching</h4>
        <img src="SomePhoto.png" alt="FigTree"/>
    </section>
    <span class="RightCenteredText">
    Bunch of text <br> 
    that should be next to the image.
    </span>
  </div>

  <div id="ContentHubs">
    <span class="LeftCenteredText">
        More text that should <br>
        be on the left of the photo now.
    </span>
    <section class=RightBoundPic>
        <h4>Content Hub</h4>
        <img src="SomePhoto.png" alt="FigTree"/>
    </section>
  </div>
</div>

Atleast we can use this until all browsers started supporting css3 flex and flex box layout 至少在所有浏览器开始支持CSS3 flex和flex box布局之前,我们都可以使用它

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

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