如何使段落适应不同的浏览器宽度

How to make paragraph fit different browser width

我正在为我的个人博文构建 htmlCSS 模板。我的部分博客内容如下:

.content {
  text-align: center;
  width: 60%;
}

.blog_content {
  background-color: white;
  width: 60%;
}
<section class="content">
  <div class="row">
    <div class="blog_content">
      <h2>This is the post title!</h2>
      <h3>Title description!</h3>
      <div class="photo">
        <img src="#" alt="NULL">
      </div>
      <p>
        This is the first paragraph!
      </p>
    </div>
  </div>
</section>

我想让段落始终占浏览器宽度的 60% 并始终居中。但是,我的 CSS 代码不起作用 - 我尝试将 text-align: center 和 width: 60% 放在内容部分的每个容器中,但没有任何效果。

非常感谢任何帮助!!!

您重复了宽度声明。你只需要声明内容宽度为100%,然后声明它的段落为60%:

.content {
    text-align: center;
    width: 100%;
}

.content > p {
  background-color: white;
  width: 60%;
  margin: 0 auto;
  display:block;
}

或者,最好是,您可以给您的段落标签一个 class 并直接在您的 CSS 中定位它们。

在您的代码中,将 margin: auto; 添加到 .blog-content 并删除给定 .content 的宽度将解决您的问题。

.content {
  text-align: center;
}

.blog_content {
  background-color: white;
  width: 60%;
  margin: 0 auto;
}
<section class="content">
  <div class="row">
    <div class="blog_content">
      <h2>This is the post title!</h2>
      <h3>Title description!</h3>
      <div class="photo">
        <img src="http://via.placeholder.com/150x30" alt="NULL">
      </div>
      <p>
        This is the first paragraph!
      </p>
    </div>
  </div>
</section>

.blog_content {
  background-color: white;
  width: 100%;  // 60 -> 100
}

.blog_content 相对于 .content.