【问题标题】:How to center an element where the width is not 100%如何使宽度不是 100% 的元素居中
【发布时间】:2017-06-11 01:53:52
【问题描述】:

我创建了一个title 并创建了width: 30%,以便我可以在它周围创建一个框,但现在它没有居中。我尝试了许多不同的方法来使其居中,但都没有奏效。我可以通过添加left: 35% 自己将其居中。但这并不完全集中。关于如何将title 完美居中,而width 不在100% 的任何想法?

【问题讨论】:

  • 请向我们展示您到目前为止所做的尝试。
  • 添加 html 代码,(也可以是 CSS)

标签: html css text web center


【解决方案1】:

您可以使用左右边距设置为自动来实现此目的。

margin: 0 auto;

h1{
	width: 30%;
	border: 2px solid black;
	margin: 0 auto;
}
<h1>Title</h1>

【讨论】:

    【解决方案2】:

    body{
      margin : 0;  /* for cross browsers */
      padding : 0; /* for cross browsers */
     }
    div{
      text-align : center; /* text alignment */
      width : 30%;
      margin : auto;       /* block alignment center */
    }
    <div>
      <label>Title</label>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 2021-05-07
      • 1970-01-01
      相关资源
      最近更新 更多