Tips on adjusting sticky behavior for responsiveness using bootstrap-4

In my project, I am utilizing angular with bootstrap. The header of the project consists of two parts and a content area. However, when the resolution is small, the header wraps.

Check out the header and content on large screens

View the header and content on small screens, wrapped

<div class="sticky row">
  <div class="col-md-7" style="height: 50px; background-color: seagreen;">
    Header A
  </div>
  <div class="col-md-5" style="height: 50px; background-color: skyblue;">
    Header B
  </div>
</div>
<div style="height: 1500px; width: 100%; background-color: slategray;">
  any content<br />
  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
  eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
  voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
  kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</div>

The header has been made sticky so that the content flows beneath it.

.sticky {
  position: -webkit-sticky; /* Safari */
  position: sticky;
  top: 0px;
}

Everything seems to be working well, as evidenced by this Stackblitz demo: Check it out here

However, for smaller screens, I want Header B to scroll beneath Header A without being sticky. Since both headers are within one div, I am unsure if there is a way to achieve this while maintaining the wrapping of the header.

Any insights or ideas would be greatly appreciated. Thank you!

Answer №1

In my design, I've set the header ratio to be 6:4 for larger screens. The first division will have a sticky position by default, and the second division will only be sticky on larger screens.

HTML :

<div class="custom-col-6 sticky" style="height: 50px; background-color: seagreen;">
  Header A
</div>
<div class="custom-col-4" style="height: 50px; background-color: skyblue;">
  Header B
</div>

CSS :

.custom-col-4,
.custom-col-6 {
  width: 100%;
  display: inline-block;
}

@media (min-width: 768px) {
  .custom-col-6 {
    width: 60%;
  }
  .custom-col-4 {
    width: 40%;
    position: sticky;
    top: 0;
  }
}

Demo : https://stackblitz.com/edit/angular-bootstrap-responsive-sticky-2aprb3?file=src/app/app.component.css

Answer №2

To show or hide a div element, you can utilize the bootstrap 4 grid property. Take a look at the code snippet below:

<div class="sticky row">
  <div class="col-md-7" style="height: 50px; background-color: seagreen;">
    Header A
  </div>
  <div class="d-none d-md-block col-md-5" style="height: 50px; background-color: skyblue;">
    Header B
  </div>
</div>
 <div class="d-block d-md-none" style="height: 50px; background-color: skyblue;">
    Header B
  </div>
<div style="height: 1500px; width: 100%; background-color: slategray;">
  any content<br />
  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
  eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
  voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
  kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Develop an exclusive "click-once" button for a webpage

Can anyone assist me in creating a "one-time clickable" button to launch my website? I've searched through numerous threads on Stack Overflow but haven't found a solution yet. ...

Positioning elements precisely on a webpage through absolute positioning and floating them

This seems to present a conflicting situation. Ideally, I am looking to position two child elements on the left and right edges of the parent element, vertically centered, and overlaying all other sibling elements. ...

I need to fix the width of my background image in the URL - what's the best way to do

<div style="background-image:url('19.jpg');height:40px;position:fixed;width:1274px">hello </div> I'm trying to keep this div tag in a fixed position, but I also need to set a width for it. It seems like the background image is c ...

Is it possible to initially design a login page using HTML/CSS and JavaScript, and then integrate VUE into it?

As part of a school project, I am tasked with developing a web-based application for a library system. The goal is to create a platform where librarians can login and manage books by adding, removing, or editing them. My responsibility lies in handling the ...

Ways to obtain a tab and designate it as the default in angular when using angular material Tabs

I am facing an issue with accessing tabs within a nested component. The parent component contains the tab feature and to reach the tabs inside the child component, I am using the following code: document.querySelectorAll('.mat-tab-group'); The a ...

Update the color of a span in JQuery when its value equals 0

Currently, my goal is to update the color of a span tag only if its value is 0. I attempted to achieve this with the following code: $('span.number:contains("0")').css('color', '#C1C1C1'); However, this code also changes the ...

PHP dropdown menu is a dynamic feature that provides users

Can anyone help me identify the issue with this code? I am struggling to display the sub menu... I suspect the problem lies in the section where I retrieve the sub menu from the database. However, upon inspection, everything seems correct... It's po ...

When using Angular 10 or later, the mouseclick event will consistently trigger Angular's change detection mechanism

When I bind an event to elements on a component's HTML page, the component continues to detect changes even when the element event is fired. Despite setting the change detection mode of the component to OnPush, this behavior persists. To test this, I ...

Issues with Bootstrap Navigation Collapse Utilizing Data Attributes

I'm having trouble getting a Twitter Bootstrap 3 navbar to collapse using data attributes, as it is not expanding properly. Just to give some context, the project I am working on is an ASP.NET C# MVC project that runs on DNN (DotNetNuke) CMS. When I ...

Enabling or disabling cell editing dynamically in Ag-grid based on another field's value

I'm currently working with ag-grid in Angular and implementing full row editing. One requirement I have is to dynamically disable editing for a specific field based on the value of another field. However, I need this field to be disabled or enabled im ...

Applying CSS styles to a shadow DOM element will not produce the desired visual

I'm encountering an issue while attempting to apply CSS to an element within a shadow-root by adding a class to it. In my Angular component, I have the following code: CSS .test { border: 1px solid red; } TS document.getElementById('my-div&a ...

Safari fails to set a div to 100% of its parent's height

Currently, I am developing a website where the layout of a section is structured as follows: <div class="col-md-6"> <section class="right" data-type="background" data-speed="30" style="background: url(<?php echo get_field('book_image& ...

What is the trick to having the ball change colors every time it hits the wall?

Recently, I stumbled upon this interesting ball bouncing question while searching for some JavaScript code. While the basic bounce animation was simple to achieve, I started thinking about how we could make it more dynamic by changing the color of the ball ...

Fading colored images and backgrounds using Javascript

Although js and html are not my strong points, I am attempting to create two simple effects on a single page. As the user scrolls down the page, I want the background image or color to change as different divs come into view and then move off the screen. ...

Determination of an arc trajectory using JavaScript

I have been trying to figure out how to replicate this effect. The Red arrowhead remains stationary while the background features a curved path. Currently, I am adjusting the background position to give the illusion of the arrowhead moving along the curved ...

Is there a way for me to manually designate certain domains with the rel="follow" attribute while assigning all other external links with the rel="nofollow" attribute?

I'm working on developing a community platform similar to a social network. I've implemented a code that automatically makes all external links nofollow. However, I would like to create a feature that allows me to remove the nofollow attribute f ...

Exploring PHP Queries with Div Classes

I'm in desperate need of assistance. I'm currently working on putting together a website and pulling data from a mysql database. The specific div that I'm trying to populate is provided below. The issue I'm facing is how do I integrate ...

Problem with CSS: The fixed footer is too wide when set at a width of 100%

I'm experimenting with using percentages instead of pixels and noticing some unusual behavior. Why is my footer extending to the right (beyond the "pagePlaceHolderInner" element)? It works when I assign a specific pixel value to both elements ("pagePl ...

Is it possible for users to customize the window size in an Angular 8 application?

Hello everyone, I'm new to Angular and this is my first time posting on stackoverflow. So please be kind! ...

Implementing a builder pattern with @Ngrx/Store

Feeling a bit uncertain... I'm wondering if it's suitable, awesome or not advisable to employ a builder pattern to retrieve API response and then apply the builder pattern on that response before storing it in @ngrx/store? And later on when acces ...