Here’s my web page (I’m sorry, but this post is going to contain quite a few screenshots, I don’t see any other way)
Do you see that scroll bar below the table? I don’t like it. So I decided to reduce lateral margins a little bit. Here’s a list of what I tried
- Specify margins manually. Result: no centering
<div class="container m-2">
- Using
conainer-sm
. Result: no margins at all
<div class="container-sm">
- Combine both methods. Result: I can’t properly describe it, but it’s weird
<div class="container-sm m-2">
- Using
mx
. Result: no centering
<div class = "container mx-4">
I searched on StackOverflow too (and googled)
How do I reduce lateral margins of my container without overriding Bootstrap’s CSS with my custom stylesheet?
It seems over the top to create your CSS for such a trivial thing (even if it’s as little as <style> .container {margin-left: 4 rem; margin-right: 4 rem} </style>
)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<!-- and so forth -->
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
UPD: My latest findings
It has something to do with container
‘s max-width
(if I increase it from 1140 to, say, 2000, the margins shrink) and also with the outer-most row
. See this dashed area? It’s what de-facto increases the margin on the right
I guess mw-100
and justify-content-center
are my friends in this situation? It appears so
2
Answers
You can use the built-in container-fluid class instead of container to make the container span the full width of the viewport. This can help eliminate any unwanted lateral margins.
You can’t reduce the sizing of Bootstrap containers without overriding the breakpoints that control them, or without overriding the specific container class CSS.
However, you can switch to the fluid container and contain that with a custom container having widths of your choosing.
Also,
container-sm
isn’t a valid Bootstrap 4 class.