【问题标题】:How to horizontally center the contents of a media object in Bootstrap 4如何在 Bootstrap 4 中水平居中媒体对象的内容
【发布时间】:2019-01-15 21:27:39
【问题描述】:

我有一个模态窗口,它在媒体对象中显示带有对话气泡的图像,如下所示:

.profile {
  max-width: 7em;
}

.speech {
  color: #808080;
  position: relative;
  background: #f1f1f1;
  border: 1px solid #efefef;
  border-radius: 3px;
  padding: 15px;
}

.speech::after {
  content: "";
  width: 15px;
  height: 15px;
  display: block;
  background: #f1f1f1;
  border: 1px solid #efefef;
  border-width: 0 0 1px 1px;
  position: absolute;
  left: -8px;
  bottom: 20px;
  transform: rotateZ(-46deg);
}

.close {
  position: absolute;
  right: 20px;
}

.media img {
  margin-right: 30px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">

      <div class="modal-header">
        <h4 class="modal-title col-12 text-center" id="exampleModalLabel">Modal Title</h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>

      <div class="modal-body">
        <div class="media">
          <img class="profile rounded-circle align-self-center" src="https://blackrockdigital.github.io/startbootstrap-landing-page/img/testimonials-1.jpg">
          <p class="speech align-self-end">
            Blah blah blah.
          </p>
        </div>
      </div>
    
    </div>
  </div>
</div>
    
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

此代码导致图像和对话气泡相对于其父媒体对象显示为左对齐。

如何使图像和对话气泡在媒体对象中居中显示?

【问题讨论】:

    标签: css flexbox bootstrap-4 alignment


    【解决方案1】:

    您可以将此规则添加到 CSS 中,以使弹性项目在其容器内居中:

    .media {
      justify-content: center;
    }
    

    .profile {
      max-width: 7em;
    }
    
    .speech {
      color: #808080;
      position: relative;
      background: #f1f1f1;
      border: 1px solid #efefef;
      border-radius: 3px;
      padding: 15px;
    }
    
    .speech::after {
      content: "";
      width: 15px;
      height: 15px;
      display: block;
      background: #f1f1f1;
      border: 1px solid #efefef;
      border-width: 0 0 1px 1px;
      position: absolute;
      left: -8px;
      bottom: 20px;
      transform: rotateZ(-46deg);
    }
    
    .close {
      position: absolute;
      right: 20px;
    }
    
    .media img {
      margin-right: 30px;
    }
    .media {
      justify-content: center;
    }
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
    
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Launch demo modal
    </button>
    
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
    
          <div class="modal-header">
            <h4 class="modal-title col-12 text-center" id="exampleModalLabel">Modal Title</h4>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
    
          <div class="modal-body">
            <div class="media">
              <img class="profile rounded-circle align-self-center" src="https://blackrockdigital.github.io/startbootstrap-landing-page/img/testimonials-1.jpg">
              <p class="speech align-self-end">
                Blah blah blah.
              </p>
            </div>
          </div>
        
        </div>
      </div>
    </div>
        
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

    【讨论】:

      猜你喜欢
      • 2019-01-13
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      相关资源
      最近更新 更多