html - 居中内容 - 然后左对齐 Bootstrap 4

标签 html css twitter-bootstrap bootstrap-4

目标:使用 Bootstrap 4 将文本置于屏幕中央,但仍向左对齐。无论元素的长度如何,它都应该有效。

                            List Item
                            Longer List Item
                            Longer List Item With Lots of Info
                            Short

让元素居中很简单,但让他们左对齐就很困难了。有什么建议吗?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet"/>
<div class="col text-center">
<p> List Item </p> 
<p> Longer List Item </p> 
<p> Longer List Item With Lots of Info </p> 
<p> Short </p> 
</div>

最佳答案

创建一个动态或固定宽度的内部div(外部容器的百分比是一个不错的选择)

.inner {
  text-align: left;
  margin: 0 auto;
  width: 25%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet" />

<div class="col text-center">
  <div class="inner">
    <p> List Item </p>
    <p> Longer List Item </p>
    <p> Longer List Item With Lots of Info </p>
    <p> Short </p>
  </div>
</div>

关于html - 居中内容 - 然后左对齐 Bootstrap 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57860117/

相关文章:

html - 背景图像高度在较低分辨率 View 中被裁剪

css - div 不随文本扩展

css - 制作 div 网格 Bootstrap

html - 如何不重叠表和行?

html - HTML 文件中的 &lt;style&gt; 标签未显示在浏览器中

css - 在导航栏内将按钮向右对齐,在右侧留出边距

javascript - 根据字段值更改页面加载时的按钮颜色

像 xaml 一样的 html 元素绑定(bind)

javascript - 滚动经过元素时更改 div/span 颜色?

html - 控制和对齐 CSS 中的段落