旋转的文本覆盖粘性导航栏
Rotated text overlays sticky navigation bar
我正在构建一个具有粘性导航栏和 table 的网站。
table 的第一列包含垂直文本。 table 足够长,因此可以滚动。然而,当滚动时,旋转的文本以一种有趣的方式表现:它出现在导航栏上。所有其他文本都按预期运行。
这是我的 example.html:
<!DOCTYPE html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Funny overlay example</title>
</head>
<body>
<nav class="navbar">
<div>
Sticky Navbar
</div>
</nav>
<section>
<div>
<table>
<tr><td rowspan="8" class="cell_vert_text"><div class="rotwrap"><div class="textcon">Problematic Text</div></div></td><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
</table>
</div>
</section>
</body>
</html>
和 style.css:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.navbar {
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
}
table td.cell_vert_text {
text-align: center;
white-space: nowrap;
vertical-align: middle;
width: 1.5em;
}
table td.cell_vert_text div.rotwrap div.textcon {
transform: rotate(270deg);
margin-left: -10em;
margin-right: -10em;
}
我怀疑旋转对 DOM 结构有一些副作用,但我不太明白发生了什么。我试图通过引入 z-index 属性来解决问题,但没有帮助。
我不仅在寻找将旋转文本推到导航栏后面的解决方案,而且还在寻找对实际情况的清晰解释。谢谢!
元素上的转换会创建一个 stacking context,因为它的父级 none 是一个堆叠上下文,它与另一个堆叠上下文,导航栏(位置粘性)处于同一级别。
当我们在同一层上有 2 个堆叠上下文,并且都没有 z-index
时,最后一个显示在它之前的那些之上。
解决方案:在导航栏上设置 z-index:
.navbar {
z-index: 1;
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
}
演示
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
height: 150px;
overflow: auto;
}
.navbar {
z-index: 1;
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
}
table td.cell_vert_text {
text-align: center;
white-space: nowrap;
vertical-align: middle;
width: 1.5em;
}
table td.cell_vert_text div.rotwrap div.textcon {
transform: rotate(270deg);
margin-left: -10em;
margin-right: -10em;
}
<div class="container">
<nav class="navbar">
<div>
Sticky Navbar
</div>
</nav>
<section>
<div>
<table>
<tr>
<td rowspan="8" class="cell_vert_text">
<div class="rotwrap">
<div class="textcon">Problematic Text</div>
</div>
</td>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
</table>
</div>
</section>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
height: 5000px;
}
.navbar {
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
z-index: 1;
}
table td.cell_vert_text {
text-align: center;
white-space: nowrap;
vertical-align: middle;
width: 1.5em;
}
table td.cell_vert_text div.rotwrap div.textcon {
transform: rotate(270deg);
margin-left: -10em;
margin-right: -10em;
}
<body>
<nav class="navbar">
<div>
Sticky Navbar
</div>
</nav>
<section>
<div>
<table>
<tr><td rowspan="8" class="cell_vert_text"><div class="rotwrap"><div class="textcon">Problematic Text</div></div></td><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
</table>
</div>
</section>
</body>
给导航栏规则一个正的z-index
值
.navbar {
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
z-index: 1
}
我正在构建一个具有粘性导航栏和 table 的网站。 table 的第一列包含垂直文本。 table 足够长,因此可以滚动。然而,当滚动时,旋转的文本以一种有趣的方式表现:它出现在导航栏上。所有其他文本都按预期运行。
这是我的 example.html:
<!DOCTYPE html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Funny overlay example</title>
</head>
<body>
<nav class="navbar">
<div>
Sticky Navbar
</div>
</nav>
<section>
<div>
<table>
<tr><td rowspan="8" class="cell_vert_text"><div class="rotwrap"><div class="textcon">Problematic Text</div></div></td><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
</table>
</div>
</section>
</body>
</html>
和 style.css:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.navbar {
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
}
table td.cell_vert_text {
text-align: center;
white-space: nowrap;
vertical-align: middle;
width: 1.5em;
}
table td.cell_vert_text div.rotwrap div.textcon {
transform: rotate(270deg);
margin-left: -10em;
margin-right: -10em;
}
我怀疑旋转对 DOM 结构有一些副作用,但我不太明白发生了什么。我试图通过引入 z-index 属性来解决问题,但没有帮助。 我不仅在寻找将旋转文本推到导航栏后面的解决方案,而且还在寻找对实际情况的清晰解释。谢谢!
元素上的转换会创建一个 stacking context,因为它的父级 none 是一个堆叠上下文,它与另一个堆叠上下文,导航栏(位置粘性)处于同一级别。
当我们在同一层上有 2 个堆叠上下文,并且都没有 z-index
时,最后一个显示在它之前的那些之上。
解决方案:在导航栏上设置 z-index:
.navbar {
z-index: 1;
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
}
演示
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
height: 150px;
overflow: auto;
}
.navbar {
z-index: 1;
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
}
table td.cell_vert_text {
text-align: center;
white-space: nowrap;
vertical-align: middle;
width: 1.5em;
}
table td.cell_vert_text div.rotwrap div.textcon {
transform: rotate(270deg);
margin-left: -10em;
margin-right: -10em;
}
<div class="container">
<nav class="navbar">
<div>
Sticky Navbar
</div>
</nav>
<section>
<div>
<table>
<tr>
<td rowspan="8" class="cell_vert_text">
<div class="rotwrap">
<div class="textcon">Problematic Text</div>
</div>
</td>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
<tr>
<td class="cell_horiz_text">Ok Text</td>
</tr>
</table>
</div>
</section>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
height: 5000px;
}
.navbar {
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
z-index: 1;
}
table td.cell_vert_text {
text-align: center;
white-space: nowrap;
vertical-align: middle;
width: 1.5em;
}
table td.cell_vert_text div.rotwrap div.textcon {
transform: rotate(270deg);
margin-left: -10em;
margin-right: -10em;
}
<body>
<nav class="navbar">
<div>
Sticky Navbar
</div>
</nav>
<section>
<div>
<table>
<tr><td rowspan="8" class="cell_vert_text"><div class="rotwrap"><div class="textcon">Problematic Text</div></div></td><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
<tr><td class="cell_horiz_text">Ok Text</td></tr>
</table>
</div>
</section>
</body>
给导航栏规则一个正的z-index
值
.navbar {
background: #ff0000;
position: sticky;
top: 0;
width: 100%;
z-index: 1
}