This may be possible with CSS, but in my opinion, you will need to use some JavaScript to fix this problem:
Here is the trick:
var sidebar = $('.sidebar');
sidebar.scroll(function(e) {
sidebar.find('.title').css({
marginLeft: sidebar.scrollLeft()
});
});
We will add some margin-leftto the elements .titleequal to the number of horizontal scroll pixels. This will always show the title bar to the right of the scrollable item.
$(function() {
var sidebar = $('.sidebar');
sidebar.scroll(function(e) {
sidebar.find('.title').css({
marginLeft: sidebar.scrollLeft()
});
});
});
.sidebar {
overflow-x: auto;
width: 160px;
height: 195px;
background-color: lightgrey;
padding-top: 20px;
}
.title {
width: 100%;
text-align: right;
background-color: grey;
}
.list {
margin-top: 20px;
white-space: nowrap;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sidebar">
<div class="title">
Title
</div>
<ul class="list">
<li>
This is a very long long line
</li>
</ul>
<div class="title">
Title2
</div>
<ul class="list">
<li>
This is a very long long line
</li>
</ul>
</div>
Run code source
share