How to position a fixed width div in the center of a Bootstrap 4 layout with fixed header and footer

My fixed-width div needs to be precisely 816px wide.

I want to center it on the page.

Using the Bootstrap 4 Fixed Header/Footer layout with a body height of 100%, I tried using mx-auto for centering, which works well on desktop but causes a horizontal scroll bar on mobile devices.

To reproduce this issue, run the snippet below in full screen and switch to mobile: Pixel 2XL view.

main > .container-fluid {
    padding: 60px 15px 0;
  }
  
  .footer {
    background-color: #f5f5f5;
  }
  
  .footer > .container-fluid {
    padding-right: 15px;
    padding-left: 15px;
  }
  
  code {
    font-size: 80%;
  }
<html lang="en" class="h-100">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    <meta name="generator" content="Jekyll v3.8.5">
    <title>Viewer</title>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
    <!-- Custom styles for this template -->
    <link href="styles.css" rel="stylesheet">
  </head>
  <body class="d-flex flex-column h-100">
    <header>
  <!-- Fixed navbar -->
  <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
    <a class="navbar-brand" href="#">Fixed navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarCollapse">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
      </ul>
      <form class="form-inline mt-2 mt-md-0">
        <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>
    </div>
  </nav>
</header>

<!-- Begin page content -->
<main role="main" class="flex-shrink-0">
  <div class="container-fluid">
    <h1 class="mt-5">Page title</h1>
    
    <div class="mx-auto" style="width: 816px; background-color: red;">
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
        <p>Test conent</p>
    </div>
  </div>
</main>

<footer class="footer mt-auto py-3">
  <div class="container-fluid">
    <span class="text-muted">Place sticky footer content here.</span>
  </div>
</footer>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>

</body>
</html>

Answer №1

To easily center your content within a div, try incorporating the following CSS code into your style sheet:

.centeredContent {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

If you want to avoid having a horizontal scrollbar, create an additional class like contentList next to mx-auto and include this CSS rule:

.contentList {
max-width: 100%;
}

For more information on using flexbox, check out: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Here's a demo link for reference: https://jsfiddle.net/oLrmwzq0/

Answer №2

If the width is fixed at 816px, an overflow will occur when the viewport width is less than that value, leading to the display of a horizontal scrollbar. A simple solution is to change width:816px to max-width:816px, but this will result in the width of the div decreasing on smaller screens.

Example 1:

.container-fluid { padding: 60px 15px 0; } .footer { background-color: #f5f5f5; } .footer .container-fluid { padding-right: 15px; padding-left: 15px; } code { font-size: 80%; }

On the other hand, if you want the div to maintain its fixed width even on smaller screens, you can use overflow-x:auto on its container-fluid and make only that element scrollable without affecting the entire page.

Example 2:

.container-fluid { padding: 60px 15px 0; } .footer { background-color: #f5f5f5; } .footer .container-fluid { padding-right: 15px; padding-left: 15px; } code { font-size: 80%; }

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

Experiencing a problem with XAMPP? Changes made to the code are not reflected after saving

Recently, I've encountered a strange issue with my XAMPP test server while working on a game project. Everything was running smoothly until I noticed that when I make changes to certain files in Notepad++ and save them, the updates are not being refle ...

Issues with navigation functionality in Internet Explorer 8 have been identified in Twitter Bootstrap 3 RC2

I am currently using Twitter Bootstrap 3 RC2 to create a navigation bar at the top of my page, which is working perfectly fine except on IE8. In IE8, it seems like the browser is rendering the page as if it were on a smaller screen, causing the menu to co ...

Rails Application experiencing issues with Bootstrap 4.1.1 integration

Despite running bundle install and bundle update, my app is not reflecting any changes with the bootstrap code. I have included the following in my Gemfile: gem 'bootstrap', '~> 4.1', '>= 4.1.1' Here is my layout file ...

Is there a way for me to prioritize the sidebar over the main content on the page

My theme is efficient and visually appealing, but there's one issue with the mobile layout - it displays the sidebar after the content. How can I rearrange this in my theme so that the sidebar appears before the content? Thanks for your help! ...

I need assistance in utilizing my PHP variable UserNameID to remove the specific user's row from the database

I want to give users the option to delete their account from the database with a simple button click on my php forum. See the code snippet below: <div class="btn-group"> <button style="width:200px" type="button" class="btn btn-primary"> ...

Adaptive grids in Bootstrap

I've been using bootstrap and I'm trying to get the items to align next to each other, but using the bootstrap columns doesn't seem to be working for me. I may be coding it incorrectly. <link rel="stylesheet" href="https://cdn.jsdelivr ...

Issues arise when the sub-menu does not function properly and overlaps with other elements of

I've been working with a client on their website and encountered an issue. On the mobile version of our menu, when I hover over a menu item that has several sub-menu items, those items display properly, but there is an element below them showing thro ...

Is the Ngx-bootstrap popover automatic positioning malfunctioning when placed on the right side of the screen?

I am currently facing an issue while implementing popovers on our application using Angular 6.0.1, Ngx-bootstrap 2.0.5, and Bootstrap 4.1.1. The problem arises when I use 'auto' positioning for the popovers, specifically on the right side of the ...

PHP link malfunctioning

Recently, I have been working on adding links to new pages in PHP using Panique for authorization. You can find more information about Panique here. In the views directory, I created a new page called product-des.php. However, when I tried to add a link f ...

`Balance in structure`

Having trouble with CSS. I would like to make this form symmetrical. Buttons should be in one column, textfields in another, and the question mark should also have its own column. Apologies if the ticket structure is not perfect, this is only my second on ...

Issue with extra spacing within a div element in Bootstrap 5

Why is there extra spacing on the right side of my div? The image and text are supposed to take up 50% of the screen each. However, for some reason, there is additional spacing on the right that prevents this from happening. I am using Bootstrap 5. Click ...

Turn off a feature on older buttons

For my project, I am looking to dynamically add specific elements like buttons. However, every time a new button is added, the function call for the old button gets duplicated. var btnElem ='<button type="button"class="doSomething">Button< ...

How to access HTML content in Python web scraping with a delay?

Currently, I am using Python to scrape the HTML from this site: Upon accessing the webpage, you'll notice that the content changes within a few seconds. This page displays the latest posts on a forum, and my goal is to create a Discord bot that can s ...

Exploring the World of GiantBomb APIs

I have successfully created an account and obtained my API key. I am looking to implement a basic search functionality on my webpage, where users can enter a search query and click a button to display the game title and image. You can find more informatio ...

Details about Spree Customers

In my website development project, I am using Spree for eCommerce functionality. I need to customize the layout to match the rest of my application's design. While I have successfully transferred most of the infrastructure to my new template, I have e ...

What is the best way to make a CSS change triggered by a onScroll() event stay in place permanently?

I need a div element to be displayed as a "scroll back to top" button on my webpage when the user has scrolled past a certain point. To achieve this, I am using the JavaScript code provided below: $(document).ready(function() { $(window).scroll(functio ...

Struggling with certain CSS design elements

I'm encountering some CSS styling issues. The button in my form remains perfectly positioned when I resize the window, but the inputs are moving around. Why is this happening? Also, when a button is pushed, a h2 element with a little arrow needs to b ...

The title element is persistently appearing on the same line

As a beginner, I've created a document with a 3x3 checkerboard using HTML and CSS. However, I'm facing an issue where the heading element is not displaying on a separate line as expected. I believe the problem lies in the CSS styling, specifical ...

Utilize CSS to eliminate gaps between spans

I can't figure out how to remove the vertical spacing between these two spans. Any help would be greatly appreciated. The issue I'm facing is the large gap between these two spans: <span class="twentyeight nomargin threshold_green">6</ ...

Unlocking secrets: Displaying PIN/ Password using Bootstrap pincode-input.js

I am using Bootstrap's pincode-input.js to mask the PIN on my web app. However, I would like the PIN to be initially hidden, and only revealed when the user clicks the show button. Can anyone help me with this? Thank you in advance. <input type="t ...