My goal is to make an element's position fixed and have it take up the full width of its parent div. Below is the HTML I am working with:
<div class="col-md-3">
<div class="player-avatar-card">
<div class="card-body">
<div class="avatar-image">
<img src="/imagecache/small/{{ $player->image_filename }}" alt="Profile Image" class="rounded-circle">
<img class="flag rounded-circle" src="/icons/flags-round/{{ $player->nationality }}.svg"></h3>
</div>
<h5>{{ $player->first_name }} {{ $player->last_name }}</h5>
<p>{{ $player->nationality }}, {{ $player->age }} years</p>
<div class="social-buttons">
<div class="col-12">
<a class="btn btn-info btn-lg" href="#" role="button">
<i class="ion-plus-round"></i> follow</a>
</div>
<div class="col-12">
<a class="btn btn-info btn-outline-info" href="#" role="button">
<i class="ion-android-share-alt"></i> share</a>
</div>
</div>
</div>
</div>
</div>
I am applying a fix position class, is-fixed-avatar
, when scrolling to the element:
const avatarCard = document.querySelector('.player-avatar-card');
const fixClassAvatar = 'is-fixed-avatar';
function stickyScroll() {
if( window.pageYOffset > 56 ) {
avatarCard.classList.add(fixClassAvatar);
}
if( window.pageYOffset < 56 ) {
avatarCard.classList.remove(fixClassAvatar);
}
}
$(window).scroll(stickyScroll);
Here is the CSS class being applied:
.is-fixed-avatar {
position: fixed;
max-width: inherit;
width: 100%;
}
However, the element extends outside of the col-md-3
div. How can I resolve this issue?
View the fiddle here. Be sure to expand it on the screen for better visibility.