简体   繁体   English

如何居中对齐div标签中包含的标题和图像

[英]How to center-align heading and image contained in a div tag

I have one image and heading tag in a div, How can I align them like beloved images. 我在div中有一个图像和标题标签,如何像喜欢的图像一样对齐它们。

<div class="clearfix">
    <img src="http://localhost/chifley-acf/wp-content/uploads/2017/03/icon-01.png" alt="Icon" class="pull-left">
    <h4>Refinancing</h4>
</div>

Current output like this 这样的电流输出

在此处输入图片说明

But I want Like This 但是我想要这样

在此处输入图片说明

A better solution for this would be 一个更好的解决方案是

 .wrapper { text-align: center; } .heading, img { display: inline-block; } .heading { vertical-align: top; } 
 <div class="clearfix wrapper"> <img src="http://placehold.it/80x60/0fa" alt="Icon" class="pull-left"> <h4 class="heading">Refinancing</h4> </div> 

Make the display of both the elements in the div inline-block and vertically align the heading text. 在div内联块中同时显示两个元素,并使标题文本垂直对齐。

If you assign classes to the element, you can apply the following CSS to them. 如果为元素分配类,则可以将以下CSS应用于它们。 The essential thing is that the child elements are inline-blocks, which can be centered in their container both horizontally and vertically. 重要的是子元素是内联块,可以在其容器中水平和垂直居中。 Hoizontally they only take as much space as their contents need. 通常,它们仅占用其内容所需的空间。

 .x { text-align: center; } .y { display: inline-block; } .x img, .y { vertical-align: middle; } 
 <div class="clearfix x"> <img src="http://placehold.it/80x60/0fa" alt="Icon" class="pull-left"> <h4 class="y">Refinancing</h4> </div> 

Try This 尝试这个

  <div class="clearfix text-center">
    <img src="https://unsplash.it/400/180" alt="Icon" class="pull-left">
<h4>Refinancing</h4>
  </div>

this work good : 这项工作很好:

<div class="clearfix">
    <div class="block">

          <img src="your_icon.png" class="pull-left">
          <h4>Refinancing</h4>

    </div>
</div>

in your CSS file : 在您的CSS文件中:

.clearfix{
/*your code ...*/
position:relative;
}

.block{
display:inline-block;
position:absolute;
left:50%;
transform:TranslateX(-50%);
}

Thanks All, Find a solution using yours comment and answers, I removed pull-left class and added beloved CSS line 谢谢大家,使用您的注释和答案找到解决方案,我删除了pull-left类并添加了心爱的CSS行

       .x {
        text-align: center;
        }
        .y, img {
        display: inline-block;
        }

    <div class="clearfix x">
        <img src="http://localhost/chifley-acf/wp-content/uploads/2017/03/icon-01.png" alt="Icon" class="">
        <h4 class="y">Refinancing</h4>
   </div>

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

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