Android Chrome中Bootstrap 4.1模态框出现黑边问题求助
Hey there, I’ve dealt with this exact frustrating issue before—those random black bars around Bootstrap modals on Android Chrome are usually down to a few common pitfalls. Let’s walk through the fixes that should get your modal looking right:
1. Fix the tabindex Attribute
Your modal has tabindex="-100" which is incorrect. Bootstrap requires tabindex="-1" for modals to properly manage focus and render correctly across devices. This is a quick first fix:
<div class="modal fade" id="modalCombos" tabindex="-1" role="dialog" aria-hidden="true">
2. Ensure Proper Viewport Meta Tag
If your page doesn’t have a correctly configured viewport tag, Android Chrome can miscalculate the modal’s dimensions, leading to black bars. Add this to your <head> section:
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
This tells the browser to respect the device’s screen width and avoid unwanted scaling.
3. Targeted CSS for Android Chrome
Sometimes Chrome’s viewport handling still causes issues even with the above fixes. Add this custom CSS to force the modal backdrop and dialog to cover the entire screen:
@media (max-width: 767px) { /* Fix backdrop coverage */ .modal-backdrop.show { width: 100vw; height: 100vh; position: fixed; } /* Ensure modal dialog fits the screen */ .modal-dialog { margin: 0; width: 100%; max-width: none; height: 100%; display: flex; align-items: center; } .modal-content { border-radius: 0; height: auto; max-height: 100vh; overflow-y: auto; } }
This adjusts the modal to fit mobile screens perfectly, eliminating any black gaps.
4. Upgrade Bootstrap (If Possible)
Bootstrap 4.1 had known rendering bugs with modals on mobile Chrome. Upgrading to the latest 4.x version (like 4.6.2) will patch these issues without needing custom CSS. It’s always a good idea to use the most recent stable release of a framework for bug fixes and security updates.
Modified Modal Code (With Tabindex Fix)
Here’s your code with the corrected tabindex, ready to test:
<div class="modal fade" id="modalCombos" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Combos</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> @foreach (var item in combosList) { <div class="row" style="margin-bottom:5px"> <div class="btn btn-danger col-12" onclick="addItem('@item.Nome', '@item.Valor.ToString("F")')"> @item.Nome - R$ @item.Valor.ToString("F") </div> </div> } </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button> </div> </div> </div> </div>
Start with fixing the tabindex and checking the viewport tag—those two resolve the issue for most cases. If you still see black bars, add the custom CSS or upgrade Bootstrap.
内容的提问来源于stack exchange,提问作者Fabricyo Figueira

