Problem with centering the content both horizontally and vertically

I'm struggling to get my content centered both vertically and horizontally. Also, my container-fluid isn't covering the footer when I inspect it in developer tools. I've searched through various resources but still can't seem to solve it. You can view my codepen here: https://codepen.io/anon/pen/yqdbWd

My goal is to remove the sidebar since it's a single-page layout with minimal content. I've attached screenshots depicting my issues: https://i.sstatic.net/1l5oR.png. The container-fluid also doesn't occupy the entire page as desired: https://i.sstatic.net/a0bEb.png. Lastly, I'm seeking a way to shorten the length of my horizontal line like this example: https://i.sstatic.net/jhp5f.png

Below is the code I've worked on so far:

HTML

<body>
<div class="container-fluid h-100">
        <nav class="navbar navbar-light rounded">
    <a class="navbar-brand" href="#">
      <svg class="d-block" width="36" height="36" viewbox="0 0 612 612" xmlns="http://www.w3.org/2000/svg" focusable="false"><title>Bootstrap</title><path fill="currentColor" d="M510 8a94.3 94.3 0 0 1 94 94v408a94.3 94.3 0 0 1-94 94H102a94.3 94.3 0 0 1-94-94V102a94.3 94.3 0 0 1 94-94h408m0-8H102C45.9 0 0 45.9 0 102v408c0 56.1 45.9 102 102 102h408c56.1 0 102-45.9 102-102V102C612 45.9 566.1 0 510 0z"/><path fill="currentColor" d="M196.77 471.5V154.43h124.15c54.27 0 91 31.64 91 79.1 0 33-24.17 63.72-54.71 69.21v1.76c43.07 5.49 70.75 35.82 70.75 78 0 55.81-40 89-107.45 89zm39.55-180.4h63.28c46..."

Below is the accompanying CSS:

   body,html {
      height: 100%;
      background:#333A4D;
   }
   .navbar{
      margin: 10px;
      background: #333A4D;
      padding: 20px;
    }
   .nav-link{
      font-size: 25px;
      line-height: 32px;
      color: #F3B0B6;
   }
  .list-inline-item{ margin-left: 1rem; }
  .jumbotron{ background: none; }
  .text-border{
     display: block;
     height: 1px;
     border: 0;
     border-top: 4px solid #F3B0B6;
    }
...

Answer №1

To avoid using height:100% on the jumbotron .row, you can set its container-fluid as a flexbox column (d-flex flex-column) and then apply flex-grow-1 to the jumbotron .row to fill the remaining height between the navigation bar and footer. This adjustment will help eliminate the scrollbar.

   <div class="container-fluid h-100 d-flex flex-column">
      <nav class="navbar navbar-light fixed-top">
        ..
      </nav>
      <div class="row align-items-center flex-grow-1">
          <div class="col-6 mx-auto">
              <div class="jumbotron text-center mb-0">
                  <hr class="text-border" />
                  <p class="lead">Stay tuned for updates.</p>
                  <p class="lead">Stay Alert For Updates</p>
                  <hr class="text-border" />
              </div>
          </div>
      </div>
      <footer class="mt-auto">
        ..
      </footer>
    </div>

To center the jumbotron on the page, ensure that the navbar is set to fixed-top so it doesn't shift the jumbotron content down and cause it to be off-center. Additionally, remove the bottom margin on the jumbotron (mb-0). You can refer to this generic Bootstrap 4 example


Related: Bootstrap 4 Navbar and content fill height flexbox

Flex fill height nav row footer jumbotron

Answer №2

Your main section had height: 100%; added to it, causing it to expand to the full height of the screen. However, the header was pushing the section down, resulting in the appearance of a scrollbar.

To address this issue, I applied position: absolute; to the footer so that it would stick to the bottom of the page. Additionally, I used display: flex; with some clever techniques to replace the outdated float: left & float: right.

In order to further optimize your code and reduce line length, I substituted

<div class="col-4 mx-auto">
with
<div class="col-6 mx-auto">
. For even more concise code, you can use
<div class="col-3 mx-auto">
or a lower value if desired.

body,
html {
  height: 100%;
  background: #333A4D;
}

.navbar {
  margin: 10px;
  background: #333A4D;
  padding: 20px;
  margin-top: 0px;
}

.nav-link {
  font-size: 25px;
  line-height: 32px;
  color: #F3B0B6;
}

.nav-item {
  margin-left: 1rem;
}

.list-inline-item {
  margin-left: 1rem;
}

.jumbotron {
  background: none;
}

.text-border {
  display: block;
  height: 1px;
  border: 0;
  border-top: 4px solid #F3B0B6;
}

.lead {
  text-transform: uppercase;
  color: #F3B0B6;
  font-size: 25px;
  line-height: 31px;
}

@media (min-width: 48em) {
  .navbar-brand {
    float: left;
  }
  .nav {
    float: right;
  }
}

.footer {
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  padding: 0 40px;
  display: flex;
  justify-content: space-between;
}

.centered-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<body>
  <div class="container-fluid h-100">
    <nav class="navbar navbar-light rounded">
      <a class="navbar-brand" href="#">
        <svg class="d-block" width="36" height="36" viewbox="0 0 612 612" xmlns="http://www.w3.org/2000/svg" focusable="false"><title>Bootstrap</title><path fill="currentColor" d="M510 8a94.3 94.3 0 0 1 94 94v408a94.3 94.3 0 0 1-94 94H102a94.3 94.3 0 0 1-94-94V102a94.3 94.3 0 0 1 94-94h408m0-8H102C45.9 0 0 45.9 0 102v408c0 56.1 45.9 102 102 102h408c56.1 0 102-45.9 102-102V102C612 45.9 566.1 0 510 0z"/><path fill="currentColor" d="M196.77 471.5V154.43h124.15c54.27 0 91 31.64 91 79.1 0 33-24.17 63.72-54.71 69.21v1.76c43.07 5.49 70.75 35.82 70.75 78 0 55.81-40 89-107.45 89zm39.55-180.4h63.28c46.8 0 72.29-18.68 72.29-53 0-31.42-21.53-48.78-60-48.78h-75.57zm78.22 145.46c47.68 0 72.73-19.34 72.73-56s-25.93-55.37-76.46-55.37h-74.49v111.4z"/></svg>
      </a>
      <ul class="nav justify-content-end">
        <li class="nav-item">
          <a class="nav-link active" href="#">Updates</a>
        </li>
      </ul>
    </nav>
    <div class="row align-items-center centered-content">
      <div class="col-4 mx-auto">
        <div class="jumbotron text-center">
          <hr class="text-border" />
          <p class="lead">Perth says tuned on it.</p>
          <p class="lead">Stay Alert For Updates</p>
          <hr class="text-border" />
        </div>
      </div>
    </div>

    <footer class="footer mt-auto">
      <p>2018 © Palungo</p>
      <div>
        <ul class="list-inline">
          <li class="list-inline-item"><a href="#">Terms</a></li>
          <li class="list-inline-item"><a href="#">Privacy</a></li>
        </ul>
      </div>
    </footer>

  </div>
</body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

MySQL automatically inserts a null record if no value is provided

Currently, I am working on a project that involves inserting, viewing, and deleting records. Although everything functions smoothly, an issue persists where a null record is automatically added for some reason. The deletion feature works perfectly for othe ...

trouble with the layout of the table

Could you assist me in improving the design to enhance data clarity? Your help would be greatly appreciated. Thank you for your anticipated response. CSS File: table-layout: fixed; width: 100%; border-collapse: collapse; table-layout: fixed; ove ...

Is there a way to modify existing code in bootstrap4 using CSS?

Is there a way to customize the CSS in Bootstrap, specifically when it comes to changing the fonts and colors in the carousel component? If you have any insights on this, please share! ...

Eliminating the issue of double scroll bars that overlap each other

Currently, I am developing a website that utilizes javascript, html, and css. One of the pages on the site displays all users as list items within an unordered list, which is nested inside two separate div elements. When accessing the page on my phone, bot ...

Using FEWER loops to create column classes on Twitter - What is their functionality?

Bootstrap utilizes various LESS mixins to create its column classes, along with other classes; .make-grid-columns() { // Common styles for all sizes of grid columns, widths 1-12 .col(@index) when (@index = 1) { // initial @item: ~".col-xs-@{index} ...

Launching a program through a web browser - a step-by-step guide

I am looking to create a specific sequence of events: A web browser opens, and a user logs in (the exact action is not crucial) The user is then redirected to a new page where another program should automatically open This process is similar to what happ ...

When I attempt to utilize the API, the JavaScript implementation of a <script src=...> element seems to interfere

Within one of my HTML files, I encountered the following line near the top: <script src="//maps.google.com/maps/api/js?key=apikey"></script> The API key is currently hardcoded in this file, but I would like to use a configuration option store ...

Not implementing auto-scroll feature because of the presence of `position: sticky` or `position: fixed` CSS properties

I'm encountering a console warning while working on my Nextjs project. Here is the snippet of my code: <aside className={`site-off desktop-hide ${showMenu ? "show-menu" : ""}`}> .... </aside> And here is the relevant ...

What is the best way to refresh information following its removal?

In my app, I have implemented a feature where posts are displayed as HTML cards using a component called PostList. Each card has a delete button to remove it from the list. The issue I am facing is that when I delete a card, it remains visible in the post ...

In order to eliminate the underline in Bootstrap links, I am seeking a solution

I have encountered a similar issue before, and the solution proposed involved making changes via CSS. Despite my attempts, I have been unsuccessful in replicating the solution with my own code. Currently, the link appears as follows: I have also referred t ...

Developing a user-friendly widget for a website that is not optimized for mobile devices

Here's the backstory: I'm currently in the process of creating a mobile-friendly widget for my clients. Unfortunately, they have web pages that are not optimized for mobile devices, and it doesn't seem like that will change anytime soon. Th ...

Is there a way to ensure that the text stays positioned behind the initial image even as the window size is adjusted

<html> <head> <title> Example </title> <style> img:hover {opacity : 0.3;} img {z-index : 1; height : 33%; width : 33%; } .first { position : absolute; top: 0%; left: 0%;} .second {position : absolute; top: 0%; left: 33%; ...

Creating a line break in HTML using Bootstrap

Reference the source provided, specifically the red alert option. Here is an example: <div class="alert alert-dismissible alert-danger"> <button type="button" class="close" data-dismiss="alert">&times;</button> <strong> ...

What is the process for adjusting the code to manage the labels of specific buttons?

There is a code $("button").click(function() { var btn = this; if (btn.loaded) { $(btn).prev("div").html(btn.originalContent); btn.loaded = false; $(btn).text('Get Dynamic chart'); } else { btn.originalConte ...

Setting the height of a div using CSS and navigating between views using Angular UI

Issue One: When moving one square, the border should appear and the menu should cover the entire height. I've already spent 2 hours trying to fix this, but it still doesn't fill the whole height or center vertically. Problem Two: If you make the ...

When attempting to incorporate the "active" class into a Bootstrap 4 carousel using jQuery, issues arise specifically when retrieving data from a MongoDB database

I'm working on a carousel feature where I need to load images from my MongoDB database. However, the issue I'm facing is that even though I've tried adding the active class to the first item, the carousel doesn't transition to display t ...

Looking to resolve the line formatting issue in my Bootstrap navigation bar

I'm having an issue with my responsive navigation in Bootstrap. When I resize the window, the navigation transforms to allow content expansion. However, I've noticed that when I expand the content, the lines, background color, and logo start to l ...

I want to know how to use PHP to count the number of occurrences of a specific value in a MySQL table and then echo the count. Can you help me with

I am working with a MySQL table that contains several varchars in one column. My goal is to determine the number of times a specific varchar appears in this column and then display that count using PHP. Can anyone provide me with the necessary SQL code to ...

Why is it that my threshold for frustration in Bootstrap has been reached due to the ineffective grid system

It’s puzzling to me why my breakpoint is fixed at 800 px when I’ve specified it to be 991 (lg and lower) in Bootstrap 4. Here's the code snippet that I’m using: <div class="container-fluid"> <div class="row"> <div c ...

An Ajax GET request will not be able to locate the JSON file

I am having issues retrieving Key and Values from a JSON file using the function provided. Despite placing the 'datafile.json' in the same directory, the code fails to execute the alert(weblink) function, while the alert('test 1') works ...