Css

How to put scroll bar only for modal-body

19 September 2026 · 8 min read

How to put scroll bar only for modal-body

Modals are essential components in modern web design, allowing you to display information or gather user input without navigating away from the current page. However, when a modal contains a large amount of content, it can overflow, creating a poor user experience. The ideal solution is to implement a scroll bar specifically for the modal’s body, keeping the header and footer fixed. This ensures that users can easily access important actions and titles while navigating the modal’s content. Learning how to put a scroll bar only for modal-body involves a few simple steps using CSS. This guide will walk you through the process, providing clear instructions and examples to help you achieve this common design goal. Implementing a scrollable modal body not only enhances usability but also improves the overall aesthetics of your website or application.

Understanding the Modal Structure

Before diving into the CSS, it’s crucial to understand the basic structure of a modal. Typically, a modal consists of three main parts: a header, a body, and a footer. The header usually contains the title of the modal, and sometimes a close button. The body contains the main content – text, forms, images, or any other information you want to display. The footer typically includes buttons for actions such as “Save,” “Cancel,” or “Submit.” Properly structuring your modal’s HTML is the foundation for successfully implementing a scrollable body.

The key to isolating the scroll functionality to the modal body lies in understanding how CSS applies to nested elements. By targeting the modal-body class specifically, you can apply styles that affect only that section, leaving the header and footer untouched. This selective styling ensures that the header and footer remain fixed, providing a consistent user experience regardless of the amount of content in the body. This approach also makes your modal more responsive and adaptable to different screen sizes. Remember to consider accessibility when implementing scrollable content. Ensure that users can navigate the content using keyboard controls and screen readers.

Consider a scenario where you have a terms and conditions modal. The header contains the title “Terms and Conditions,” and the footer contains “Accept” and “Decline” buttons. The body contains a long legal document. Without a scrollable body, the user would have to scroll the entire page to read the document and then interact with the buttons. By implementing a scrollable modal body, the user can scroll through the terms within the modal itself, while the header and buttons remain fixed, providing a much better user experience.

Implementing CSS for Scrollable Modal Body

The core of implementing a scrollable modal body is CSS. You’ll need to target the modal-body element and apply specific styles to enable scrolling. The most important properties to use are overflow-y: auto; and max-height: [desired height];. overflow-y: auto; will only show a vertical scrollbar when the content exceeds the specified height. Setting a max-height is crucial to define the maximum height of the modal body, triggering the scrollbar when the content overflows. Without a defined height, the content will simply expand beyond the modal’s boundaries, defeating the purpose.

Here’s an example of the CSS you might use: css .modal-body { max-height: 400px; overflow-y: auto; } This CSS snippet sets the maximum height of the modal body to 400 pixels and enables a vertical scrollbar when the content exceeds that height. You can adjust the max-height value based on your design requirements and the expected content volume. It’s also important to consider different screen sizes and adjust the height accordingly using media queries for a responsive design. Remember to test your implementation thoroughly on various devices and browsers to ensure consistent behavior. This is crucial for providing a seamless user experience across all platforms. According to a study by Statista, mobile devices account for approximately half of web traffic worldwide [^1^], making responsive design a necessity.

This paragraph is optimized for a featured snippet: To create a scrollable modal body, apply the following CSS properties to the .modal-body element: max-height: [desired height]; and overflow-y: auto;. The max-height property sets the maximum height of the modal body, while overflow-y: auto; enables a vertical scrollbar only when the content exceeds the specified height, ensuring the modal content can be navigated effectively without overflowing the modal container.

Step-by-Step Guide: Adding Scroll to Modal Body

Adding a scroll bar to only the modal body requires a few steps involving both HTML and CSS. Let’s break it down into manageable steps for easy implementation. Each step is crucial to ensuring that the scroll bar functions correctly and enhances the user experience.

  1. Structure Your HTML: Ensure your modal HTML is properly structured with distinct header, body, and footer sections. Use appropriate classes or IDs to target each section.
  2. Apply CSS to the Modal Body: Target the modal-body element in your CSS and set the max-height and overflow-y properties.
  3. Test Your Implementation: Thoroughly test your modal on different devices and browsers to ensure consistent behavior and responsiveness.
  4. Adjust as Needed: Fine-tune the max-height value based on your design requirements and user feedback.

By following these steps, you can easily implement a scrollable modal body that enhances the usability and aesthetics of your website or application. Remember to prioritize user experience and test your implementation thoroughly to ensure consistent behavior across different platforms. Consider using CSS frameworks like Bootstrap or Foundation, which often provide pre-built modal components with built-in scroll functionality. These frameworks can save you time and effort while ensuring a consistent and professional look and feel. Always validate your HTML and CSS code to ensure it adheres to web standards and best practices.

Best Practices and Considerations

When implementing a scrollable modal body, it’s essential to consider best practices to ensure a smooth and user-friendly experience. A few things to keep in mind:

  • Accessibility: Ensure that users can navigate the content using keyboard controls and screen readers. Provide alternative text for images and use semantic HTML elements.
  • Responsiveness: Use media queries to adjust the max-height value based on screen size. This ensures that the modal looks good on all devices, from desktops to mobile phones.

Another important consideration is performance. Avoid adding unnecessary elements or complex CSS styles that can slow down the rendering of the modal. Optimize images and other media to reduce file sizes. Use CSS animations and transitions sparingly to avoid performance bottlenecks. Regularly test your modal’s performance using tools like Google PageSpeed Insights [^2^] to identify and address any issues. By following these best practices, you can create a scrollable modal body that is both user-friendly and performant. According to a Google study, 53% of mobile users abandon a site if it takes longer than 3 seconds to load [^3^], highlighting the importance of performance optimization.

Here are some additional tips for optimizing your modal implementation:

  • Use a consistent design language across all modals on your website or application.
  • Provide clear and concise instructions or labels for all interactive elements.
  • Test your modal with real users to gather feedback and identify areas for improvement.
Infographic here
Remember that the goal is to provide a seamless and intuitive user experience. By carefully considering these best practices and considerations, you can create a scrollable modal body that enhances the overall usability and aesthetics of your website or application. [Learn more about best practices here](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c).

FAQ: Scrollable Modal Body

Why should I use a scroll bar only for the modal body?
Using a scroll bar only for the modal body keeps the header and footer fixed, providing a better user experience by ensuring that important actions and titles are always visible.
What CSS properties are needed to implement this?
The key CSS properties are max-height and overflow-y: auto;. Setting max-height defines the maximum height of the modal body, while overflow-y: auto; enables a vertical scrollbar only when the content exceeds that height.
How do I ensure the modal is responsive?
Use media queries to adjust the max-height value based on screen size, ensuring that the modal looks good on all devices.
Implementing a scrollable modal body is a simple yet effective way to improve the user experience on your website. By using the techniques described above, you can create modals that are both visually appealing and easy to navigate, even when they contain a large amount of content. This not only enhances usability but also contributes to a more professional and polished overall design. Don't hesitate to experiment with different max-height values and CSS styles to find the perfect fit for your specific needs. Now that you understand the process, go ahead and implement scrollable modal bodies on your projects. Your users will thank you for it!

[^1^]: Statista. (2024). Mobile share of web traffic worldwide from 2015 to 2023. Retrieved from [https://www.statista.com/statistics/976274/mobile-share-of-website-traffic-worldwide/](https://www.statista.com/statistics/976274/mobile-share-of-website-traffic-worldwide/) [^2^]: Google PageSpeed Insights. Retrieved from [https://developers.google.com/speed/pagespeed/insights/](https://developers.google.com/speed/pagespeed/insights/) [^3^]: Google. (2016). Mobile Site Speed: Get the Facts. Retrieved from [https://www.thinkwithgoogle.com/marketing-resources/mobile/mobile-page-speed-load-time/](https://www.thinkwithgoogle.com/marketing-resources/mobile/mobile-page-speed-load-time/) Question & Answer :
I have the following element:

<div class="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog" style="overflow-y: scroll; max-height:85%; margin-top: 50px; margin-bottom:50px;" > <div class="modal-content"> <div class="modal-header"> <h3 class="modal-title"></h3> </div> <div class="modal-body"></div> <div class="modal-footer"></div> </div> </div> </div> 

It shows modal dialog something like this, basically, it puts scroll bar around entire modal-dialog and not modal-body that contains the content I am trying to display.

The image looks something like this:

enter image description here

How do I get a scroll bar around modal-body only?

I have tried assigning style="overflow-y: scroll; max-height:85%; margin-top: 50px; margin-bottom:50px;" to modal-body but it didn’t work.

You have to set the height of the .modal-body in and give it overflow-y: auto. Also reset .modal-dialog overflow value to initial.

See the working sample:

http://www.bootply.com/T0yF2ZNTUd

.modal{ display: block !important; /* I added this to see the modal, you don't need this */ } /* Important part */ .modal-dialog{ overflow-y: initial !important } .modal-body{ height: 80vh; overflow-y: auto; }