简体   繁体   English

在容器引导程序中垂直和水平对齐图像和文本

[英]Vertical and horizontal align image and text in container bootstrap

I have an image with some text below it, however, I am trying to get them to align vertically and horizontally in the middle. 我的图像下方带有一些文本,但是,我试图使它们在中间垂直和水平对齐。 It must also be responsive to both mobile and web view, is it possible? 它还必须同时响应移动和Web视图,这可能吗?

Here's what I have tried 这是我尝试过的

<style>
        .img-responsive {
            display: block;
            margin-left: auto;
            margin-right: auto;
            width: 50%;
        }
</style>

<div class="container">
       <img src="..." class="img-responsive"/>
       <h4 style="text-align: center;">Sample Text 1</h4>
       <p style="text-align: center;">Sample Text 2</p>
</div>

Try this 尝试这个

Html HTML

<div class="container">
  <img src="https://s33.postimg.cc/ud7gljfb3/ripple_Bg.jpg" class="img-responsive"/>
     <div class="textblock">
       <h4 style="text-align: center;">Sample Text 1</h4>
       <p style="text-align: center;">Sample Text 2</p>
     </div>
</div>

Css ( 2 ways to make center ) CSS (使中心居中的两种方法)

1. modern browser supported 1.支持现代浏览器

    .container {
      width: 500px;
      height: 500px;
      position: relative;
     }

    .img-responsive {
      z-index: -1;
      position: absolute;
      display: block;
      width: 100%;
    }

    .textblock {
        width: 109px; /*need to give width*/
        display: inline-table; /*ie not supported*/
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        right: 0;
        margin: auto;
    }

1. IE not supported 1.不支持IE

    .container {
      width: 500px;
      height: 500px;
      position: relative;
     }

    .img-responsive {
      z-index: -1;
      position: absolute;
      display: block;
      width: 100%;
    }

    .textblock {
      width: 109px; /*need to give width*/
      height: 100px; /*need to give height*/
      position: absolute;
      left: 0;
      top: 0;
      bottom: 0;
      right: 0;
      margin: auto;
    }

Hope it helps:) 希望能帮助到你:)

You can use the media object in bootstrap 您可以在引导程序中使用媒体对象

<div class="media">
<img class="align-self-center mr-3" src=".../64x64" alt="Generic placeholder image">
<div class="media-body">
<h5 class="mt-0">Center-aligned media</h5>
<p>Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.</p>
<p class="mb-0">Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
</div>
</div>

The documentation link above show you how to align images in different positions. 上面的文档链接显示了如何在不同位置对齐图像。 The grid in Bootstrap4 is based on Flex , which allow you to do many advanced layouts. Bootstrap4中网格基于Flex ,它允许您执行许多高级布局。 Check it out. 看看这个。

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

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