Html 如何将两个元素设置为正确的位置:粘滞,以避免相互覆盖?

Html 如何将两个元素设置为正确的位置:粘滞,以避免相互覆盖?,html,responsive-design,position,sticky,Html,Responsive Design,Position,Sticky,当Property position设置为sticky时,两个块div堆叠在彼此的顶部,当页面呈现且用户向下滚动时,两个div都将粘在视图的顶部。底部的div最后遮住了顶部的div。我想做的是,或者说我想弄明白的是,我怎样才能使粘性的盒子在粘性的盒子下面粘住(或堆叠)?这样,底部的div(粘滞的div_b_)就不会弄脏顶部的div(粘滞的盒子a) StackOverFlow.com示例作者:AFT3RL1F3 .粘盒子 { 显示:块; 位置:粘性; 职位:-网络工具包粘性; 背景颜色:蓝色;

当Property position设置为sticky时,两个块div堆叠在彼此的顶部,当页面呈现且用户向下滚动时,两个div都将粘在视图的顶部。底部的div最后遮住了顶部的div。我想做的是,或者说我想弄明白的是,我怎样才能使粘性的盒子在粘性的盒子下面粘住(或堆叠)?这样,底部的div(粘滞的div_b_)就不会弄脏顶部的div(粘滞的盒子a)


StackOverFlow.com示例作者:AFT3RL1F3
.粘盒子
{
显示:块;
位置:粘性;
职位:-网络工具包粘性;
背景颜色:蓝色;
宽度:100%;
高度:200px;
保证金:0;
填充:0;
排名:0;
}
.粘盒
{
显示:块;
位置:粘性;
职位:-网络工具包粘性;
背景色:红色;
宽度:50%;
高度:400px;
边距:0自动0自动;
填充:0;
排名:0;
}
这是一个带有CSS属性“position:sticky
这是另一个具有CSS3属性的框:sticky;'










































以下是一个示例

div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  background-color: yellow;
  padding: 50px;
  font-size: 20px;
}

<div class="sticky">
<p> This is your sticky box </p>
</div>
<div>
<p>This is your other divs and properties </p>
</div>
div.sticky{
职位:-网络工具包粘性;
位置:粘性;
排名:0;
背景颜色:黄色;
填充:50px;
字体大小:20px;
}
这是你的粘盒子

这是您的其他分区和属性


所以我想出来了,其实很简单。如果你想让两个div粘在一起,比如一个下拉菜单中的一个反应灵敏的粘性顶部导航,你必须把它们放在一个容器里。希望这有助于任何人谁搜索这个!AFT3RxL1F3;-)


StackOverFlow.com示例作者:AFT3RL1F3
.盒子a
{
位置:粘性;
职位:-网络工具包粘性;
排名:0;
}
.框-b
{
高度:200px;
背景色:红色;
宽度:50%;
左边距:50%;
保证金权利:50%;
}
.box-c
{
宽度:100%;
高度:75px;
背景颜色:蓝色;
}
你好,世界!



a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z



你好,世界!
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z



你好,世界! ```
这就是我制作的导航栏,它有一个功能灵敏的移动下拉菜单。听起来你已经明白了,但我想我会给你一些反馈。然而,在表面上,这个范例是把所有应该粘在一个粘性容器中的物体;实现它比听起来要困难得多。祝你好运


<!DOCTYPE HTML>
<html lang='us-en'>
<head>
<style type='text/css'>
.nav {
      position: sticky;
      position: -webkit-sticky;
      top: 0;
      left: 0;
      display: grid;
      grid-template-columns: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
}

.nav-bar {
      background-color: #000;
      display: block;
      width: 100%;
}

.nav-bar a {
      display: inline-block;
      font-size: 26px);
      text-decoration: none;
      margin: 16px 4px 0 12px;
}



/*!!! ~~~ ICONS ~~~ */
#home {
      display: block;
      float: left;
      padding: 12px;
      font-size: 38px !important;

}

#bars {
      display: none;
      float: right;
      padding: 4px;
      font-size: 38px !important;
}



/*! ~~~ Drop & Drop-Items  ~~~ */
.nav-drop {
      background-color: #000;
      display: none;
      width: 100%;
}

.nav-drop button {
      display: block;
      width: 54%;
      margin: 12px 23%;
      border: 1px solid #0FF;
      padding: 1px;
}

</style>
</head>


<!-- BODY'S MARKUP -->
<body>

<div class="nav">
      <div class="nav-bar">
            <i id="home" class="fa fa-home" aria-hidden="true" onclick="go2('home')">&nbsp; </i>
            <a href="home">HOME &nbsp;| </a>
            <a href="about">ABOUT &nbsp;| </a>
            <a href="contact">CONTACT &nbsp;| </a>
            <a href="forum">FORUM</a>
            <i id="bars" class="fa fa-bars" aria-hidden="true" onclick="dropMenu()"></i>
      </div>
      <div id="nav-drop" class="nav-drop">
            <button onclick="go2('about')">ABOUT</button>
            <button onclick="go2('contact')">CONTACT</button>
            <button onclick="go2('forum')">FORUM</button>
      </div>
</div>
</body>
</html>


.导航{
位置:粘性;
职位:-网络工具包粘性;
排名:0;
左:0;
显示:网格;
网格模板列:100%;
宽度:100%;
保证金:0;
填充:0;
}
.导航栏{
背景色:#000;
显示:块;
宽度:100%;
}
.导航栏a{
显示:内联块;
字体大小:26px);
文字装饰:无;
利润率:16像素4像素0 12像素;
}
/*!!! ~~~ 图标~~~~*/
#家{
显示:块;
浮动:左;
填充:12px;
字体大小:38px!重要;
}
#栅栏{
显示:无;
浮动:对;
填充:4px;
字体大小:38px!重要;
}
/*! ~~~ 放下&放下物品~~~*/
.导航下降{
背景色:#000;
显示:无;
宽度:100%;
}
.导航下降按钮{
显示:块;
宽度:54%;
利润率:12px23%;
边框:1px实心#0FF;
填充:1px;
}
关于
接触
论坛

是,但这不会粘在顶部元素的底部。我希望它们都能粘住,但当它们到达页面顶部时会堆叠起来。换句话说,一个接一个地粘在一起。
<html lang="en">

<head>
    <meta name="author"
          content="AFTERxL1F3">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <meta charset="utf-8">
    <title>StackOverFlow.com Example By: AFT3RL1F3</title>

    <style type="text/css">
        .box-a
        {
            position: sticky;
            position: -webkit-sticky;
            top: 0;
        }

        .box-b
        {
            height: 200px;
            background-color: red;
            width: 50%;
            margin-left: 50%;
            margin-right: 50%;
        }

        .box-c
        {
            width: 100%;
            height: 75px;
            background-color: blue;
        }

    </style>
</head>

<body>
    <h1>HELLO WORLD!</h1>
    <br><br>
    <div class="box-a">
        <div class="box-b"></div>
        <div class="box-c"></div>
    </div>


    <br>a<br>b<br>c<br>d<br>e<br>f<br>g<br>h<br>i<br>j<br>k<br>l<br>m<br>n<br>o<br>p<br>q
    <br>r<br>s<br>t<br>u<br>v<br>w<br>x<br>y<br>z
    <br><br><br><br>HELLO WORLD!
    <br>a<br>b<br>c<br>d<br>e<br>f<br>g<br>h<br>i<br>j<br>k<br>l<br>m<br>n<br>o<br>p<br>q
    <br>r<br>s<br>t<br>u<br>v<br>w<br>x<br>y<br>z
    <br><br><br><br>HELLO WORLD!

</body>

</html>```

<!DOCTYPE HTML>
<html lang='us-en'>
<head>
<style type='text/css'>
.nav {
      position: sticky;
      position: -webkit-sticky;
      top: 0;
      left: 0;
      display: grid;
      grid-template-columns: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
}

.nav-bar {
      background-color: #000;
      display: block;
      width: 100%;
}

.nav-bar a {
      display: inline-block;
      font-size: 26px);
      text-decoration: none;
      margin: 16px 4px 0 12px;
}



/*!!! ~~~ ICONS ~~~ */
#home {
      display: block;
      float: left;
      padding: 12px;
      font-size: 38px !important;

}

#bars {
      display: none;
      float: right;
      padding: 4px;
      font-size: 38px !important;
}



/*! ~~~ Drop & Drop-Items  ~~~ */
.nav-drop {
      background-color: #000;
      display: none;
      width: 100%;
}

.nav-drop button {
      display: block;
      width: 54%;
      margin: 12px 23%;
      border: 1px solid #0FF;
      padding: 1px;
}

</style>
</head>


<!-- BODY'S MARKUP -->
<body>

<div class="nav">
      <div class="nav-bar">
            <i id="home" class="fa fa-home" aria-hidden="true" onclick="go2('home')">&nbsp; </i>
            <a href="home">HOME &nbsp;| </a>
            <a href="about">ABOUT &nbsp;| </a>
            <a href="contact">CONTACT &nbsp;| </a>
            <a href="forum">FORUM</a>
            <i id="bars" class="fa fa-bars" aria-hidden="true" onclick="dropMenu()"></i>
      </div>
      <div id="nav-drop" class="nav-drop">
            <button onclick="go2('about')">ABOUT</button>
            <button onclick="go2('contact')">CONTACT</button>
            <button onclick="go2('forum')">FORUM</button>
      </div>
</div>
</body>
</html>