Collapsing headers can be considered an excellent solution that shows important information to users, like special offers and key notifications. Most developers rely on JavaScript for creating such effects, however, it’s possible to create a simpler collapsing header effect using pure CSS also. Know-how, by contacting our web application development company in San Jose.
Collapsing headers work the same as parallax effects. The technique is that the background of the collapsing header will stay fixed so that the content below can flow on top of it whenever the user scrolls down the page.
How to create a collapsing header effect?
Collapsing headers create a great user experience. Users can scroll the page back any time when they want to see special information and it doesn’t distract their attention while they are reading the rest of the content.
1. Create the HTML
HTML includes three main sections: <header> for the fixed top menu bar, .banner for the collapsing header, and .article for the rest of the content. Here is what the code looks like:
2. Add Basic Styles with CSS
The CSS starts by defining some resets and including some basic styles.
Were you in search of the ideal company that does website & mobile application development company in California, USA? Our agency is only a call away.
3. Position the Top Menu Bar
To be able to position the fixed menu bar on the top of the page, it is required to set the <header> element’s position to fixed and the z-index to a value higher than zero. As the z-index defaults to auto, it only has to be higher than the z-index value of the element’s parent(s). The CSS below uses 99, as it’s likely to stay higher than any parent of the <header> element:
The z-index: 99; the rule will allow the top menu bar to stay on top of all elements, even when the collapsing header completely collapses and the rest of the content reaches the top of the page.
Our UI/UX design company in California has been successfully providing web development services to various clients across the world. Do get in touch with us for your web and app development needs.
4. Style the Menu
Though these CSS rules are not necessary for creating the collapsing header, this is how you can style the top menu:
The .nav and .menu items inherit the width of the <header> element (100%) that allows them to span across the entire screen. Besides, .menu also makes use of flexbox, so that the menu items can line up horizontally in a row and the align-items property will center them vertically.
The flex: 1; CSS rule can be added to the .logo element. The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis. When it stands with only one value, it refers to flex-grow. And then the other two properties stay at their default value.
When flex-grow is set to 1, the given element gets all the extra space in the flex container. Hence, the logo is pushed to the left of the container while the menu items stay on the right.
Were you looking for the best UI/UX design services in California? You should be talking to our expert team then.
5. Position the Collapsing Header
The collapsing header also has a fixed position similar to the top menu bar. However, it doesn’t get a z-index value that allows it to “collapse” as the user scrolls down the page and the rest of the content gradually covers the banner.
The flexbox can be used to align the content inside the collapsing header. It then gives a column-based flex layout that allows you to easily align the elements both vertically and horizontally using the justify-content and align-items properties.
Fulfill your custom web development services San Jose CA with the help of our agency. Do call to know more.
6. Style the Collapsing Header
This is not part of the collapsing header effect but the descendants of the .banner element (title, description, and button) can be styled effectively.
When both the top menu bar and the collapsing header have a fixed position, they stay on the top of the page and cover the upper part of the content. The header is collapsible in the next step by styling the rest of the content.
7. Style the Rest of the Content
To make the header collapse on scroll, four things have to be done:
- Most important is to set a background for the rest of the content so that it can flow on top of the collapsible header. This effect works similar to parallax effects; the different backgrounds need to cover each other.
- Next it is required to position the content relative to the two fixed elements. The top: 370px; rule below is the sum of the height of <header> (70px) and .banner (300px).
- Following that one will have to set the width to 100% so that the content will cover the entire header.
- Finally, the height has to be set to 100%, too, so that the background will cover the entire height of the page (this is important on mobile or in the case of longer pages).
The best solution for your web development and performance management can be attained through our web application maintenance company in California
Conclusion
Collapsing headers extend a user-friendly means to show additional content on top of the page. They work similarly to parallax effects. What is required to be done is to make the different backgrounds move at different speeds and position the flowing content relative to the fixed element(s).
Many interesting CSS effects can give an extra touch to your design. If you want to know how to do it, get in touch with our web application testing agency in San Jose.