【问题标题】:html middle text alignmenthtml中间文本对齐
【发布时间】:2019-02-07 10:11:12
【问题描述】:

大家好,我正在我的网站上创建一个目录,希望文本位于中间而不是中心,而不是顶部,我希望它位于文档的中间

<center>
  <button href="#">Download!</button>
</center>

【问题讨论】:

  • 对于现代方法,请使用 flexbox。
  • 请在问题中添加相关代码。没有人会在您的网站中挖掘 CSS 来确定问题所在
  • 它只有 html 还没有 css
  • @Ski 所以你没有花任何精力寻找解决方案?
  • 我做了,但它们似乎都不起作用

标签: html css centering


【解决方案1】:

为你的 DOM 赋予 css 样式

.wrapper {
    display: table;
    margin: 0 auto;
}

.box {
  background-color: blue;
  color: white;
  text-align: center;
  vertical-align: middle;
  width: 100px;
  height: 100px;
  display: table-cell;
}
<div class="wrapper">
   <div class="box">
       Center
   </div>
</div>

【讨论】:

    【解决方案2】:

    您可以使用以下内容:

    .box {
      width: var(--box-size);
      height: var(--box-size);
      line-height: var(--box-size);
      background: red;
      color: white;
      text-align: center;
    }
    
    :root {
      --box-size: 100px;
    }
    <div class="box">
      Center
    </div>

    或者按照 flexbox 的建议:

    .box {
      width: var(--box-size);
      height: var(--box-size);
      background: red;
      color: white;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    :root {
      --box-size: 100px;
    }
    <div class="box">
      Center
    </div>

    【讨论】:

    • 看起来它对我不起作用我在开头添加了 div 标签,并在我所有的标题和段落中结束了它,它只是在左边逐字逐句
    • 如果您想要一个真正的解决方案,请将您的代码添加到问题中,并使用您已经尝试过的 CSS
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 2011-06-03
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2023-04-06
    • 2010-09-30
    相关资源
    最近更新 更多