css - 如何在 CSS Grid 中使图像居中?

标签 css centering css-grid

如何使图像在左侧垂直居中而文本在右侧垂直居中?

.pricing1box {
  display: grid;
  grid-template-columns: repeat(auto-fit, 300px);
  color: #444;
  margin-top: 150px;
  justify-content: center;
}

.pricing1box-div {
  background-color: orange;
  color: #fff;
  border-radius: 5px;
  font-size: 150%;
}
<div class="pricing1box">
  <div class="pricing1box-div">
    <img src="http://placehold.it/350x150.png" style="float: left; display: inline-block; width: 100px; vertical-align: middle;">
    <h6 style="color: black; padding: 10px;">Title</h6>
    <p style="font: 10px blue;">Powered by me</p>
    <br>
    <p>A dgdfg dfgdhdgfh fdgh dhdfg wertdfg dfgdh ergdfgd egr dfgdhd hdfh </p>
  </div>
</div>

最佳答案

您也可以在图像和文本上使用网格属性。只需将父级设为网格容器即可。

.pricing1box {
  display: grid;
  grid-template-columns: repeat(auto-fit, 300px);
  justify-content: center;  
  color: #444;
}

.pricing1box-div {
  display: grid;         /* new; nested grid container */
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto auto auto;
  background-color: orange;
  color: #fff;
  border-radius: 5px;
  font-size: 150%;
}

.pricing1box-div img {
  grid-column: 1;
  grid-row: 1 / -1;      /* from first line at start, to first line at end */
  align-self: center;    /* vertical centering */
  width: 100px;
}

.pricing1box-div :not(img) {
  grid-column: 2;
}
<div class="pricing1box">
  <div class="pricing1box-div">
    <img src="http://placehold.it/350x150.png">
    <h6 style="color: black; padding: 10px;">Title</h6>
    <p style="font: 10px blue;">Powered by me</p>
    <p>A dgdfg dfgdhdgfh fdgh dhdfg wertdfg dfgdh ergdfgd egr dfgdhd hdfh </p>
  </div>
</div>

关于css - 如何在 CSS Grid 中使图像居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50010006/

相关文章:

html - 如何在两个菜单列表的中间有一个标志?

CSS 网格 - 如何拥有 2、3、4、5 或 6 列

css - 第一行有 2 个感冒,然后使用 css 网格在每行中有 3 列

css - div在IE7中消失

html - 为不同的屏幕尺寸保留屏幕尺寸

html - 内容下方的标签导航栏

html - 在导航栏中隐藏图标并在悬停时仅显示文本

几个并排 div 的 CSS/HTML 居中

html - 我怎样才能使这个提交按钮居中?

css - 具有列顺序的响应式多列列表