html - 为所有具有相同属性的元素添加通用 CSS
问题描述
我有一个表格部分,如下所示。我希望无论我有“col”属性,都应该在左侧添加 20px 的边距。我只需要语法方面的帮助。
我已经编辑了问题并在此处包含了我的完整代码。请注意,在移动视图中,每一行都堆叠在另一行之上。
<div class="tableComponent">
<table class="table table-striped" id="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Designation</th>
<th scope="col">Contact Details</th>
</tr>
</thead>
<tbody>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
</tbody>
</table>
</div>
CSS
.tableComponent table{
font-family: 'Poppins';
font-size:12px;
width: 50%;
border-collapse: separate;
}
.tableComponent table thead th{
border-bottom: 0px;
}
.tableComponent table td,th{
border-top: 0px;
}
.tableComponent .table-striped tr:nth-child(even){
background-color:#F8F8F8
}
.tableComponent tr{
border: hidden;
}
@media all and (max-width: 500px){
.tableComponent table{
width: 90%;
margin-left: 5vw
}
.tableComponent thead {
display: none;
}
.tableComponent tr {
display: flex;
flex-direction: column;
margin-bottom: 5px;
}
.tableComponent td::before {
content: attr(col);
font-weight: bold;
}
.tableComponent td[col]{
padding-right: 5rem;
}
}
我不知道怎么写这个。有什么建议么?
解决方案
如果要选择td
withcol
属性,可以这样做:
td[col]{ }
但请注意,这margin-right
不适用于 td - 您必须使用填充。
当您处理表格时,您不能以与其他元素相同的方式应用相同的间距,例如:
- 您不能向表格单元格添加边距
- 你不能在单元格之间添加不同大小的边框
使用填充的工作片段:
td[col]{
padding-right: 20px;
}
<table>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
</table>
更新:您的编辑完全改变了问题,因此上述答案不再有帮助。
现在的问题是您在错误的位置添加了填充。您正在使用 将标题添加到表格结构之外:before
,因此当您尝试向单元格本身添加填充时,它不会被添加到您想要的位置。
这是您的 CSS,它是正确的,只是您的标题不在单元格中,因此它没有向它们添加填充:
.tableComponent td[col]{
padding-right: 5px;
}
相反,您需要在样式标题的 CSS 规则中添加填充,例如
.tableComponent td::before {
content: attr(col);
font-weight: bold;
padding-right: 10px;
}
工作片段(注意我已将媒体查询增加到 5000 像素,因此我们可以在这里看到):
.tableComponent table{
font-family: 'Poppins';
font-size:12px;
width: 50%;
border-collapse: separate;
}
.tableComponent table thead th{
border-bottom: 0px;
}
.tableComponent table td,th{
border-top: 0px;
}
.tableComponent .table-striped tr:nth-child(even){
background-color:#F8F8F8
}
.tableComponent tr{
border: hidden;
}
@media all and (max-width: 5000px){
.tableComponent table{
width: 90%;
margin-left: 5vw
}
.tableComponent thead {
display: none;
}
.tableComponent tr {
display: flex;
flex-direction: column;
margin-bottom: 5px;
}
.tableComponent td::before {
content: attr(col);
font-weight: bold;
padding-right: 10px;
}
}
<div class="tableComponent">
<table class="table table-striped" id="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Designation</th>
<th scope="col">Contact Details</th>
</tr>
</thead>
<tbody>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
<tr>
<td col="Name">Mark</td>
<td col="Designation">Otto</td>
<td col="Contact Details">@mdo</td>
</tr>
</tbody>
</table>
</div>
更新 2
要选择具有特定col
值的单元格,您可以执行以下操作:
.tableComponent td[col="Name"]::before {
padding-right: 20px;
}
.tableComponent td[col="Designation"]::before {
padding-right: 5px;
}
要使所有标题的宽度相同,您可以这样做:
.tableComponent td::before {
width: 100px;
display:inline-block;
}
推荐阅读
- while-loop - 如何在 R 中使用 while 循环生成具有特定数字的矩阵?(for->while)
- python - 多处理 - 内存消耗
- python - Windows 10 找到 pip 但没有找到 python
- java - 需要一个找不到的“com.ganesh.utils.JwtUtils2”类型的 bean
- reactjs - React 父布局取决于子级(必须重新渲染)
- mysql - 使用 Web 服务和 myPHPAdmin 将迁移的 .net 站点连接到 Synology NAS 上的 MySQL 数据库的问题
- c++ - 未定义的符号 str::string 和字符串文字
- javascript - GEOJSON 为什么给出 3 个坐标系,谁能更简单地解释一下它的属性
- python - 导入 keras 和 tensorflow 时出错
- python - PyQT5 QSound 声音之间的延迟