css - 如何使文本与 block 居中但左对齐?

标签 css text flexbox alignment

我希望它具有 css flex 属性(“图像内容” 文本中心与 block 对齐但左对齐)就像下图一样。

enter image description here

但我明白了

.wrapper {
  max-width:300px;
}
.main {
  width:300px;
  display:flex;
  justify-content:center;
  align-items:center;
  flex-flow:column nowrap;
  background:#458bc3;
  color:#fff;
  height:300px;
  overflow:hidden;
}
.content {
  color:#fff;
  background:#a6d8ff;
  height:100px;
  display:flex;
  justify-content:center;
  align-items:center;
  flex-flow:column nowrap;
}
.content p {
  margin:0px;
}
<div class="wrapper">
   <div class="main">
    IMAGE
  </div>
  <div class="content">
    <p>
       Image
    </p>
     <p>
       Content
     </p> 
   </div>
</div>

我需要(“图像内容” 文本与 block 居中对齐,但像图像一样向左对齐)。 任何人都可以帮助我如何使用 flex 获得这种方法?

最佳答案

只需使用 text-align: left 将您的内容 imagecontent para 包裹在 div 中。

试试这个:

查看演示 here

HTML:

<div class="wrapper">
  <div class="main">
    IMAGE
  </div>
  <div class="content">
    <div class="text-left">
      <p>
        Image
      </p>
      <p>
        Content
      </p>
    </div>

  </div>
</div>

CSS:

.wrapper {
  max-width:300px;
}
.text-left {
  text-align: left;
}
.main {
  width:300px;
  display:flex;
  justify-content:center;
  align-items:center;
  flex-flow:column nowrap;
  background:#458bc3;
  color:#fff;
  height:300px;
  overflow:hidden;
}
.content {
  color:#fff;
  background:#a6d8ff;
  height:100px;
  display:flex;
  justify-content:center;
  align-items:center;
  flex-flow:column nowrap;
}
.content p {
  margin:0px;
}

关于css - 如何使文本与 block 居中但左对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44253060/

相关文章:

html - SVG - 剪辑路径仅在相同的 <svg> 标签中定义时才有效

css - 变换比例和边界半径

java - 将文本文件中的输入保存到数组

CSS 无用的供应商前缀

html - 内部 div 不会垂直居中或左对齐

css - webkit 渐变在 lesscss 中产生语法错误

html - 使图像定位在另一个容器上

mysql - 将base64编码的数据存储为BLOB或TEXT数据类型

text - flutter 下拉文本字段

html - 根据 child 的尺寸自动调整 parent 的高度( flex 元素)