首页 > 解决方案 > 为什么 CSS text-align:center; 不行?

问题描述

这是代码

html {
  font-family: 'Noto Sans KR', sans-serif;
}

h1 {
  text-align: center;
}

img {
  display: block;
  margin: auto;
}

p {
  font-size: 16px;
  line-height: 2;
  letter-spacing: 1px;
}
<h1>웹 프로그래밍 기초</h1>
<img src="images/CB9A982F-A812-486E-A9E6-30E6ACFA916D.jpeg" alt="HTML5">
<h2>2.2 CSS </h2>
<h3>글꼴과 텍스트 </h3>
<P> 글꼴과 텍스트 스타일을 바꿔봅시다&lt;/P>

我将其保存在 Visual Studio 中。但预览不应用 css 文本(如文本对齐、高度、字母间距 ..)

请帮帮我

标签: css

解决方案


text-align: center样式在您的代码片段中的 h1 上正常工作,如果您想text-align: center在所有元素上应用 ,您可以为所有元素使用父容器元素并将text-align: center样式添加到其中。您可以在下面看到结果。
希望这可以帮助你:)

 <html>
 <body>
 <style>
 html {
      font-family: 'Noto Sans KR', sans-serif;
    }

    h1 {
      text-align: center;
      width: 100%;
    }

    img {
      display: block;
      margin: auto;
    }

    p {
      font-size: 16px;
      line-height: 2;
      letter-spacing: 1px;
    }

    #container {
      text-align: center;
    }
    </style>

    <div id="container">
    <h1>웹 프로그래밍 기초</h1>
    <img src="images/CB9A982F-A812-486E-A9E6-30E6ACFA916D.jpeg" alt="HTML5">
    <h2>2.2 CSS </h2>
    <h3>글꼴과 텍스트 </h3>
    <P> 글꼴과 텍스트 스타일을 바꿔봅시다&lt;/P>
    </div>
    </body>
    </html>


推荐阅读