Align element vertically below Navbar

Asked

Viewed 90 times

1

I have a page that I’m going to split into two columns. The right column should be aligned both vertically and horizontally and cannot create a scroll bar. I managed to do this, but as there is a navbar, the second column is getting bigger than the screen size, thus creating the scroll bar. How do I center this column vertically without creating the scroll bar, taking into account the size of my navbar?

html, body{
	height: 100%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
    </ul>
  </div>
</nav>


<div class="container h-100">
  <div class="row h-100">
    <div class="col">
      <p class="bg-white">Coluna 1</p>
    </div>
    <div class="col d-flex bg-info justify-content-center align-items-center">
      <p class="bg-white">Coluna 2</p>
    </div>
  </div>
</div>

1 answer

1


I haven’t found a "default" way to do this... I mean without needing extra CSS...

But as you have a standard height for the Navbar, just take that time out of height total... thus making a calc of (100% - Height da Navbar) vc "remove" the scroll

See Example, notice that I put the height of .container with class .altura the height stays height: calc(100% - 56px); where 56px is the time of navbar.

html, body{
    height: 100%;
}
.container.altura {
    height: calc(100% - 56px);
}
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" />

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.bundle.min.js"></script>


<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
        aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
            </li>
        </ul>
    </div>
</nav>


<div class="container altura">
    <div class="row h-100">
        <div class="col">
            <p class="bg-white">Coluna 1</p>
        </div>
        <div class="col d-flex bg-info justify-content-center align-items-center">
            <p class="bg-white">Coluna 2</p>
        </div>
    </div>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.