html - 具有预先确定宽度的 Div 内具有背景颜色的 Div
问题描述
我觉得我应该能够解决这个问题,但我真的不能......
我基本上有一个包含另一组 div/元素的 div。我希望此容器中的第一个 div 具有背景颜色,以有效地为父 div 提供彩色顶栏/部分。我能得到的最接近的是使用 display: flex; 给它全高度着色,但我无法按照我想要的方式得到它。任何帮助表示赞赏。
.container {
border: 1px solid grey;
border-radius: 4px;
padding: 0px 10px 10px 10px;
}
.content {
height: 400px;
}
.sp-h3 {
display: flex;
background-color: #008ed0;
color: white;
}
* {
box-sizing: border-box;
}
.container::before,
.container::after {
content: "";
clear: both;
}
<div class="container">
<div class="sp-h3">
<h3>Bob McBob</h3>
</div>
<div class="content">
<p>Just for something</P>
<ul>
<li>The number 1</li>
<li>The number 2</li>
<li>The number 3</li>
<li>The number 4</li>
</ul>
</div>
</div>
解决方案
您可以设置padding
为子元素,而不是将其设置为整个.container
.
.container {
border: 1px solid grey;
border-radius: 4px;
}
.content {
height: 400px;
padding: 0px 10px 10px 10px;
}
.sp-h3 {
display: flex;
background-color: #008ed0;
color: white;
padding: 0px 10px 10px 10px;
}
* {
box-sizing: border-box;
}
.container::before,
.container::after {
content: "";
clear: both;
}
<div class="container">
<div class="sp-h3">
<h3>Bob McBob</h3>
</div>
<div class="content">
<p>Just for something</P>
<ul>
<li>The number 1</li>
<li>The number 2</li>
<li>The number 3</li>
<li>The number 4</li>
</ul>
</div>
</div>
推荐阅读
- php - 点击 PHP 和 MySql 上的计数器
- php - LinkedIn api 调用使用 .bat 文件
- python - Pandas 基于所有现有列的值创建新列,第一列除外
- c# - 如何对预定的逻辑进行单元测试?
- ios - 如何从 UICollectionViewCell 中的 UITableViewCell 内部推送视图?
- c++ - 对 apache::thrift::server::TThreadedServer::TThreadedServer 的未定义引用
- java - REST - 应用层还是服务层?
- javascript - 如何检查html表单输入是否为整数
- python - 使用 SQLAlchemy 对 postgresql 数据库进行更改跟踪
- java - 获取 java.net.UnknownHostException: