Почему не работает position sticky
main {
display: block;
}
.section {
padding: 50px 0;
background: white;
position: relative;
overflow: hidden;
}
.tabs {
position: relative;
overflow: hidden;
margin: 0 auto;
width: 100%;
font-weight: 300;
font-size: 1.25em;
}
.sticky {
position: -webkit-sticky;
position: sticky;
top: 0em;
}
nav {
display: block;
}
.tabs nav ul {
position: relative;
display: -ms-flexbox;
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex;
margin: 0 auto;
padding: 0;
max-width: 1200px;
list-style: none;
-ms-box-orient: horizontal;
-ms-box-pack: center;
-webkit-flex-flow: row wrap;
-moz-flex-flow: row wrap;
-ms-flex-flow: row wrap;
flex-flow: row wrap;
-webkit-justify-content: center;
-moz-justify-content: center;
-ms-justify-content: center;
justify-content: center;
}
.tabs nav ul li {
position: relative;
z-index: 1;
display: block;
margin: 0;
text-align: center;
-webkit-flex: 1;
-moz-flex: 1;
-ms-flex: 1;
flex: 1;
}
.content-wrap {
position: relative;
}
.content-wrap section.content-current {
display: block;
}
.content-wrap section {
margin: 0 auto;
max-width: 1200px;
text-align: center;
}
<main>
<section class="section">
<div class="container tabs tabs-style-underline">
<p class="text-justify pb20">Текст</p>
<nav class="sticky">
<ul>
<li>Вкладка 1</li>
<li>Вкладка 2</li>
<li>Вкладка 3</li>
<li>Вкладка 4</li>
<li>Вкладка 5</li>
</ul>
</nav>
<div class="content-wrap">
<section id="1">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="4">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="5">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
</div>
</div>
</section>
</main>
Всем привет, не пойму почему не срабатывает position sticky для элемента nav. Уже и высоту для родителя (class="section") задавал, никак.
Ответы (2 шт):
Проблема в родительских элементах, которым установлено свойство overflow:hidden.
Если его убрать - все работает как и ожидалось.
main {
display: block;
}
.section {
padding: 50px 0;
background: white;
position: relative;
}
.tabs {
position: relative;
margin: 0 auto;
width: 100%;
font-weight: 300;
font-size: 1.25em;
}
.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
border: 1px solid green;
background: white;
z-index: 1;
}
nav {
display: block;
}
.tabs nav ul {
position: relative;
display: -ms-flexbox;
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: flex;
margin: 0 auto;
padding: 0;
max-width: 1200px;
list-style: none;
-ms-box-orient: horizontal;
-ms-box-pack: center;
-webkit-flex-flow: row wrap;
-moz-flex-flow: row wrap;
-ms-flex-flow: row wrap;
flex-flow: row wrap;
-webkit-justify-content: center;
-moz-justify-content: center;
-ms-justify-content: center;
justify-content: center;
}
.tabs nav ul li {
position: relative;
z-index: 1;
display: block;
margin: 0;
text-align: center;
-webkit-flex: 1;
-moz-flex: 1;
-ms-flex: 1;
flex: 1;
}
.content-wrap {
position: relative;
}
.content-wrap section.content-current {
display: block;
}
.content-wrap section {
margin: 0 auto;
max-width: 1200px;
text-align: center;
}
<main>
<section class="section">
<div class="container tabs tabs-style-underline">
<p class="text-justify pb20">Текст</p>
<nav class="sticky">
<ul>
<li>Вкладка 1</li>
<li>Вкладка 2</li>
<li>Вкладка 3</li>
<li>Вкладка 4</li>
<li>Вкладка 5</li>
</ul>
</nav>
<div class="content-wrap">
<section id="1">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="2">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="3">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="4">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
<section id="5">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</section>
</div>
</div>
</section>
</main>
Была похожая проблема: спозиционированная position:sticky шапка прокручивалась при скролле, не удавалось зафиксироать ее.
Как оказалось (неочевидно), ниже в разметке был элемент с overflow:scroll. Добавил родительскому этого элемента свойство overflow-x:hidden и проблему удалось решить.
Теперь шапка спозиционирована, как нужно, не прокручивается при скролле страницы и верстка не дергается при скролле.