I need the .word
and the .line
elements be fixed to the top of the viewport when the page is scrolled - in the position they initially are, i.e. the line is below the word.
I managed to fix the .word
with position: sticky
, but faced 2 problems:
sticky
to the .line
, it "jumps" above the word, and I don't know how to fix it to the top of the viewport, but force it to stay below the .word
elementsticky
is initially not the right value - it "shakes" the text when you start scrolling. The correct value is fixed
- it works just fine, I'd rather prefer to use it! However, it breaks the position of the .warning
class (red font): it should be on the opposite side (horizontally) from the .transcription
text, but the fixed
value collapses the text from those 2 classes together..word {
position: sticky;
top: 0;
z-index: 1;
background-color: white;
}
.word {
padding: 1vw 3vw 2% 3vw;
}
.word-details {
display: flex;
justify-content: space-between;
align-items: baseline;
}
.transcription {
font-weight: normal;
}
.warning {
color: red;
margin-left: auto;
}
.line {
border-top: 2px solid #fdb239;
}
.meaning {
list-style-type: none;
counter-reset: item;
hyphens: auto;
font-size: calc(0.7em + 1.5vw);
}
.meaning > li {
position: relative;
}
.meaning > li::before {
content: counter(item);
counter-increment: item;
position: absolute;
top: 0;
text-align: center;
margin-left: calc(-0.7em - 2.5vw);
}
.meaning-word {
margin-top: 50px;
}
.sentences {
list-style-type: none;
padding-left: 0;
font-size: calc(0.7em + 1.5vw);
margin-top: 30px;
}
.sentences > li.sentence-ru {
margin-top: 15px;
}
<!DOCTYPE html>
<html>
<body>
<div class="word">
<span>Word</span>
<div class="word-details">
<div class="transcription">/ transcription /</div>
<div class="warning">COMMENT</div>
</div>
</div>
<hr class="line">
<ol class="meaning">
<li class="meaning-word">Meaning1</li>
<ul class="sentences">
<li class="sentence-en">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</li>
<li class="sentence-ru">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</li>
</ul>
<li class="meaning-word">Meaning2</li>
<ul class="sentences">
<li class="sentence-en">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</li>
</ul>
</ol>
</body>
</html>
Hi,
keeping several elements separate at the top of the page while scrolling can be difficult, so I suggest a simpler solution. This "jumping" of Yours sticky bar is because the initial position of the sticky bar is different than the position setted to sticking it on the page top when scrolling.
To avoid your hr
line disappearing while scrolling, I would put all the components you want to keep at the top of the page in a container e. g. a div
and then make that container sticky.
Second problem is because the default body
margin (which is 10px) sets Your sticky bar a little lower at the start. Then we scrolling the page - the body
margin has already been overscrolled as well and You set the sticky bar to be absolutely on the top (top: 0;
), so it must jump to the top this additional 10px. Fast fix is setting body
top margin to 0 and then the position of Your sticky bar is always same on the top of the page.
Here below is an updated snippet of your code.
body {
margin-top: 0;
}
.sticky-container {
position: sticky;
top: 0;
z-index: 1;
background-color: white;
}
.word {
padding: 1vw 3vw 2% 3vw;
}
.word-details {
display: flex;
justify-content: space-between;
align-items: baseline;
}
.transcription {
font-weight: normal;
}
.warning {
color: red;
margin-left: auto;
}
.line {
border-top: 2px solid #fdb239;
}
.meaning {
list-style-type: none;
counter-reset: item;
hyphens: auto;
font-size: calc(0.7em + 1.5vw);
}
.meaning > li {
position: relative;
}
.meaning > li::before {
content: counter(item);
counter-increment: item;
position: absolute;
top: 0;
text-align: center;
margin-left: calc(-0.7em - 2.5vw);
}
.meaning-word {
margin-top: 50px;
}
.sentences {
list-style-type: none;
padding-left: 0;
font-size: calc(0.7em + 1.5vw);
margin-top: 30px;
}
.sentences > li.sentence-ru {
margin-top: 15px;
}
<html>
<body>
<div class="sticky-container">
<div class="word">
<span>Word</span>
<div class="word-details">
<div class="transcription">/ transcription /</div>
<div class="warning">COMMENT</div>
</div>
</div>
<hr class="line">
</div>
<ol class="meaning">
<li class="meaning-word">Meaning1</li>
<ul class="sentences">
<li class="sentence-en">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</li>
<li class="sentence-ru">Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</li>
</ul>
<li class="meaning-word">Meaning2</li>
<ul class="sentences">
<li class="sentence-en">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</li>
</ul>
</ol>
</body>
</html>
Cheers