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

时间:2018-05-20 06:24:57

标签: html css frontend blogs paragraph

我正在为个人博客帖子构建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%放在我的内容部分的每个容器中,但没有任何效果。

非常感谢任何帮助!!!

3 个答案:

答案 0 :(得分:0)

您正在复制宽度声明。您只需要将内容宽度声明为100%,然后将其段落声明为60%:

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

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

或者,最好,您可以为段落标记提供一个类,并直接在CSS中定位它们。

答案 1 :(得分:0)

在代码中添加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>

答案 2 :(得分:0)

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

.blog_content.content相关。