I'm new in css. I confused in position. There is a simple example.
https://jsfiddle.net/4t8zn9yo/1/
$(document).ready(function() {
$('.content').scroll(function() {
if ($(this).scrollTop() > 0) {
if (!$('.topline').hasClass('long'))
$('.topline').addClass('long')
} else {
if ($('.topline').hasClass('long'))
$('.topline').removeClass('long')
}
});
});
.header {
position: relative;
height: 65px;
background: brown;
overflow-y: hidden;
overflow-x: hidden;
}
.topline {
position: absolute;
top: 0px;
width: 100vw;
height: 5px;
background: blue;
transition: 0.2s;
}
.topline.long {
height: 65px;
transition: 0.2s;
}
.topbar {
display: flex;
justify-content: center;
position: relative;
margin-top: 10px;
}
.topbar div {
flex: 1;
color: white;
}
.content {
height: calc(100vh - 65px);
overflow-y: auto;
overflow-x: hidden;
}
.content-parta {
height: 800px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class='header'>
<div class='topline'>
</div>
<div class='topbar'>
<div>test1</div>
<div>test2</div>
<div>test3</div>
<div>test4</div>
</div>
</div>
<div class='content'>
<div class='content-parta'>
</div>
</div>
When scroll event trigger, topline class is higher than topbar class. I have no idea why the son of topbar class is higher than topline class.
When I move the topline class after the topbar class, it is the highest.
I want to know why.