【问题标题】:Centering text in html while keeping it left justified?在html中居中文本,同时保持左对齐?
【发布时间】:2020-08-15 02:13:36
【问题描述】:

我浏览了很多关于这个主题的帖子,但传统的解决方案似乎不起作用。我很可能做错了什么,但我看不出是什么。我试图将文本置于 div 中,同时保持左对齐。这是我的代码/标记:

HTML

<div class="home">
    <div class="title">
        Hi, I'm <a href="#">name</a>.  This is some of my <a href="#">work</a>.
    </div>
</div>

CSS

.home {
    padding: 120px 75px 0px 75px;
    margin: 0 0 200px 0;
    width: calc(100% - 150px);
    height: auto;
    text-align: center;
}
   .title {
   display: inline-block;
   text-align: left;
   color: var(--text-primary);
}

目前我已经设置好了,所以标题 div 中的文本左对齐,标题 div 在主 div 中居中(至少我认为这是正在发生的事情。)我也尝试过弹性框,但无济于事。我认为这个问题与 h1 标签中的文本换行有关,在右侧留下空间(在下面以红色选择)。这就是我的意思:

感谢您的关注,感谢您的任何建议!

【问题讨论】:

  • 尝试在标题中添加一个额外的包装器,并设置 display:flex;证明内容:中心;在 .title;
  • 这能回答你的问题吗? CSS: Center block, but align contents to the left
  • PythonMaster202 我查看了它并尝试复制它,但它没有用。谢谢你的评论。
  • @SeanDoherty 我试过了,但没有骰子,文本仍然没有居中。不过还是谢谢你的建议。
  • 你能建立一个codepen吗?因为我复制了你的代码并且它仍然可以工作,但看起来不像你的例子 - 所以我认为其他地方的风格有冲突?

标签: html css text


【解决方案1】:

我设法通过“收缩包装”标题 div 到里面的文本,使文本在其中居中,然后将主 div 用作标题 div 的弹性框居中。

.home {
    padding: 5% 0;
    margin-top: 10%;
    width: 100%;
    height: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 5.5rem;
    font-weight: 800;
    color: var(--text-primary);
}

.title {
    width: max-content;
}

希望这可以帮助其他遇到此问题的人!

【讨论】:

    【解决方案2】:

    尝试给 "home" width:100% 和 "title" margin-left:auto;边距右:自动

    【讨论】:

      【解决方案3】:
      .title {
          display: inline-block;
          text-align: center;
          color: var(--text-primary);
          float:left;
      }
      

      【讨论】:

      • 不幸的是,这并没有解决我的问题。不过谢谢!
      【解决方案4】:

      当我想把任何东西居中时,我通常会尝试这个,所以我希望它对你有用。

      .home{
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
      }
      

      【讨论】:

        【解决方案5】:

        我想你需要这个。

        .home {
            padding: 50px 0;
            margin: 0 0 200px 0;
            width: calc(100% - 150px);
            height: auto;
            display: flex;
            justify-content: center;
            background:red;
        }
        .title {
           display: inline-block;
           color: var(--text-primary);
           background: yellow;
           width: calc(100% - 100px);
           text-align: center;
        }
        <div class="home">
            <div class="title">
                Hi, I'm <a href="#">name</a>.  This is some of my <a href="#">work</a>.
            </div>
        </div>

        【讨论】:

        • 感谢您的回复!我试过了,但仍然有同样的问题。文本所在的 div 居中,但文本本身不居中。
        • 已编辑。现在看看......让我知道这是否可以。还有另一种方法。 @TheRedTopHat
        • 不幸的是,同样的想法发生了。 div 红色 div 居中,黄色 div 居中,但文本不在 div 中居中。
        • 但它正在寻找中心
        猜你喜欢
        • 2016-10-11
        • 2012-11-18
        • 1970-01-01
        • 2014-05-27
        • 2013-08-23
        • 2017-06-29
        • 2016-10-06
        • 1970-01-01
        • 2020-12-19
        相关资源
        最近更新 更多