【问题标题】:How to make paragraph fit different browser width如何使段落适合不同的浏览器宽度
【发布时间】:2018-05-20 06:24:57
【问题描述】:
我正在为我的个人博客帖子构建html 和CSS 模板。我的部分博客内容如下:
.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% 放在我的内容部分的每个容器中,但没有任何效果。
非常感谢任何帮助!!!
【问题讨论】:
标签:
html
css
frontend
blogs
paragraph
【解决方案1】:
您正在复制您的宽度声明。您只需将内容宽度声明为 100%,然后将其段落声明为 60%:
.content {
text-align: center;
width: 100%;
}
.content > p {
background-color: white;
width: 60%;
margin: 0 auto;
display:block;
}
另外,最好是,你可以给你的段落标签一个类,并直接在你的 CSS 中定位它们。
【解决方案2】:
在您的代码中,通过将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>
【解决方案3】:
.blog_content {
background-color: white;
width: 100%; // 60 -> 100
}
.blog_content 相对于.content。