Bootstrap 4 container fluid unwanted margins [dupl

2020-05-03 12:25发布

I'm writing a page in html using Bootstrap 4.2. I would like to have a horizontal navigation bar at the top of the page, that occupies the whole width of the page, and remains static during navigation.

I tried to use a few divs with the class "container-fluid". The problem is that a margin appears on the left and right side of the bar

Here is an example of what I get in jsfiddle

<div class="container-fluid">
      <div class="sticky-top shadow" id="containerMain">
        <div class="container-fluid bg-secondary text-light p-1">
          <div class="row align-items-center ">
            Line number one content here
          </div>
        </div>
        <div class="container-fluid filters bg-light align-items-center">
          <div class="row p-1">
            Line number two content here
          </div>
        </div>
      </div>
    </div>

Does anyone know how to make these unwanted margins disappear? I tried without the "container-fluid" class, but then the margin appears on the right side, with a horizontal scroll that I don't want either.

Thank you very much in advance,

3条回答
戒情不戒烟
2楼-- · 2020-05-03 12:46

I think the bootstrap "row" class adds some margin (15px). Maybe you can add an own class to make margin: 0. Or delete the row class

I wish that helped!

Thank you.

查看更多
仙女界的扛把子
3楼-- · 2020-05-03 12:51

Not sure if this is what you're looking for:

Here's the jFiddle

  <nav class="fixed-top navbar-light bg-light m-0 p-0">
  <div class="container-fluid">
    <div class="row text-center text-light bg-secondary p-1">
      <div class="col">
        Line number 1 content here
      </div>
    </div>
     <div class="row text-center text-secondary bg-light p-1">
      <div class="col">
        Line number 2 content here
      </div>
    </div>
  </div>
 </nav>
查看更多
看我几分像从前
4楼-- · 2020-05-03 12:53

container-fluid has padding-left and padding-right of 15px which is the gap you're seeing. You could overwrite it by adding the class px-0 which is padding of 0 for left and right. And you would then have to overwrite the 15px margins of the row with a mx-0 class.

But if it's a nav that you want, then what you should be using is the nav component of Bootstrap: https://getbootstrap.com/docs/4.1/components/navbar/

查看更多
登录 后发表回答