我有一个用于保存表的容器,并且我试图应用溢出-x属性,但是它有问题。我有一个工作中的Codepen,我会在稍后发布,但是问题是它没有响应。当视口关闭时,它只是“粘住了”(我无法对此进行更好的解释,但Codepen会显示出来)。这是代码:
<section class='main-container'>
<h6>Users</h6>
<table>
<thead>
<tr>
<th class='name'>Name</th>
<th class='email'>Email</th>
<th class='role'>Role</th>
<th class='status'>Status</th>
<th class='alerts'>Alerts/Actions</th>
<th class='delete'></th>
</tr>
</thead>
<tbody>
<tr>
<td class='name'>Joe Bob</td>
<td class='email'>bobjoe@me.com</td>
<td class='role'>This is the role</td>
<td class='status'>Approved</td>
<td class='alerts'>There's an alert</td>
<td class='delete'></td>
</tr>
<tr>
<td class='name'>Joe Bob</td>
<td class='email'>bobjoe@me.com</td>
<td class='role'>This is the role</td>
<td class='status'>Approved</td>
<td class='alerts'>There's an alert</td>
<td class='delete'></td>
</tr>
<tr>
<td class='name'>Joe Bob</td>
<td class='email'>bobjoe@me.com</td>
<td class='role'>This is the role</td>
<td class='status'>Approved</td>
<td class='alerts'>There's an alert</td>
<td class='delete'></td>
</tr>
<tr>
<td class='name'>Joe Bob</td>
<td class='email'>bobjoe@me.com</td>
<td class='role'>This is the role</td>
<td class='status'>Approved</td>
<td class='alerts'>There's an alert</td>
<td class='delete'></td>
</tr>
<tr>
<td class='name'>Joe Bob</td>
<td class='email'>bobjoe@me.com</td>
<td class='role'>This is the role</td>
<td class='status'>Approved</td>
<td class='alerts'>There's an alert</td>
<td class='delete'></td>
</tr>
</tbody>
</table>
</section>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
scroll-behavior: smooth;
border: none;
color: #425563;
-webkit-tap-highlight-color: transparent;
}
.main-container {
--away-left: 20px;
--border-radius: 2px;
--shine-brand-blue: #425563;
--shine-gray-two: #eceeef;
--shine-gray-one: #f1f2f3;
display: flex;
width: calc(100% - 80px);
max-width: 600px;
margin-right: 40px;
margin-left: 40px;
margin-top: 40px;
background-color: #fff;
border-radius: var(--border-radius);
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.2);
flex-flow: column nowrap;
justify-content: flex-start;
overflow-x: scroll;
h6 {
font-size: 16px;
height: 50px;
font-family: var(--main-font);
color: var(--shine-brand-blue);
line-height: 40px;
width: 100%;
border-bottom: 1px solid var(--shine-gray-two);
padding-left: var(--away-left);
display: flex;
align-items: center;
justify-content: flex-start;
}
table {
margin: 25px 0;
width: calc(100% - 40px);
margin-left: 20px;
margin-right: 20px;
border-collapse: collapse;
th, td {
text-align: left;
border-bottom: 1px solid #e0e3e5;
&.name {min-width: 149px;}
&.email {min-width: 230px;}
&.role {min-width: 148px;}
&.status {min-width: 82px;}
&.alerts {min-width: 195px;}
&.delete {min-width: 15px;}
}
th {
font-size: 10px;
font-weight: 500;
color: #7d8d9a;
text-transform: uppercase;
}
td {
font-size: 12px;
font-weight: 500;
line-height: 50px;
color: var(--shine-brand-blue);
select {
height: 40px;
width: 90%;
min-width: 140px;
border-radius: var(--border-radius);
background-color: var(--shine-gray-one);
cursor: pointer;
}
}
tbody tr {
transition: all .3s;
&:hover { background-color: #f5f6f7; }
}
}
}
main.next-to-aside {
position: absolute;
top: 60px;
right: 0px;
}
如何使表格具有响应性,而不仅仅是像本Codepen那样“粘住”呢?: https://codepen.io/adammcgurk/pen/aXyZxm
答案 0 :(得分:0)
所以我实际上不得不将表包装在div
中。仅使用section
是行不通的,因为它包含的孩子多于table
。因此,样板代码如下所示:
<style>
.responsive-table {
overflow-x: scroll;
}
</style>
<section class='main-container'>
<!-- other content -->
<div class='responsive-table'>
<table>
<!-- table content -->
</table>
</div>
</section>