What is the best way to horizontally center an image in Bootstrap 4 while having a fixed-top navbar and preventing vertical scrollbars?

I am struggling to achieve the desired outcome, which is depicted in this image: https://i.sstatic.net/i6X0j.jpg

Specific requirements for this project include:

  1. The image must be responsive
  2. A fixed-top navbar should remain visible
  3. No vertical scrolling should occur
  4. The footer also needs to stay at the bottom of the page

I have spent an entire day attempting to solve this issue, but so far, I have not had any success. A previous attempt that I made can be found here:

https://codepen.io/Codewife_101/pen/rpvdPq

However, using 'align-self-center' in my code caused unwanted vertical scrolling on the website, especially when viewing it on mobile devices.

This is my first version:

  <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Red Logo Website</title>
      <link rel="stylesheet" href="css/font-awesome.min.css">
      <link rel="stylesheet" href="css/bootstrap.css">
      <link rel="stylesheet" href="css/custom.css">
      <!--Material Design Icons-->
      <link href="https://fonts.googleapis.com/icon?family=Material+Icons"
          rel="stylesheet">
      <!--adding Roboto different styles-->
      <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900" rel="stylesheet">
    </head>
    ...

I also attempted another approach documented here:

https://codepen.io/Codewife_101/pen/eyVxre

Here is a snippet from my second version:

  <nav class="navbar navbar-expand-sm navbar-light bg-light fixed-top">
    <div class="container">
      <img src="img/navbar_red_logo.32x32.png" class="img-fluid" alt="red-logo">
      ...
  <footer id="main-footer" class="text-center">
    <div class="container">
      <p class="text-muted">Copyright &copy; 2018 <img src="img/navbar_red_logo.32x32.png" class="img-fluid" alt="red-logo"> RedDesign LLC</p> 
...

Unfortunately, neither solution resulted in the vertical centering of the elements as intended. Any suggestions or solutions would be greatly appreciated.

While my question may appear similar to others at first glance, my specific situation and limitations prevent me from commenting on existing discussions. Thank you in advance for any advice provided.

Answer №1

To achieve a fixed header and footer on your website, you can utilize CSS.

Apply the CSS property position:fixed to your header element:

<div id="NAVBAR">

Do the same for your footer:

<div id="FOOTER">

In your CSS file, define the styles for these elements:

#NAVBAR {
    position: fixed;
    bottom: 0;
}

#FOOTER {
    position: fixed;
    top: 0;
}

Make sure to set the height of the body in accordance with your screen resolution to avoid any scrolling effects.

Answer №2

Give this a try, it involves using calc and vh (viewport height) for dynamic height adjustment:

nav, footer{
  max-height: 50px;
}

body,html, section{
  height: 100%;
}
section{
  display: flex !important;
  justify-content: center;
  align-items: center;
}


section img{
  object-fit: contain;
  height: calc(100vh - 150px) !important; 
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">

<nav class="navbar navbar-default navbar-fixed-top">

  <a class="navbar-brand" href="#">Navbar</a>

 <ul class="navbar-nav nav">
   <li class="nav-item"><a href="" class="nav-link">Link</a></li>
   <li class="nav-item"><a href="" class="nav-link">Link</a></li>
   <li class="nav-item"><a href="" class="nav-link">Link</a></li>
 </ul>
</nav>

<section>
  <img src="http://www.tandemconstruction.com/sites/default/files/styles/project_slider_main/public/images/project-images/IMG-Residence-1_0.jpg" alt="" class="img-fluid">
</section>

<footer class="navbar navbar-default navbar-fixed-bottom text-center">
   <p class="text-muted navbar-brand">Copyright &copy; 2018 RedDesign LLC</p> 
</footer>

Answer №3

I have resolved the issue in your code and provided the corrected version below.

The mistake was a basic one made by beginners: You placed the image inside a row without placing it within a column. The Bootstrap rows and columns are meant to be used together, so whenever you define a .row, make sure to include at least one .col inside that row. This usually resolves all issues.

Here is the updated version of your code:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">

<nav class="navbar navbar-expand-sm navbar-light bg-light fixed-top">
    <div class="container">
        <img src="img/navbar_red_logo.32x32.png" class="img-fluid" alt="red-logo">
        <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarNav2"><span class="navbar-toggler-icon"></span></button>
        <div class="collapse navbar-collapse" id="navbarNav2">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item">
                    <a class="nav-link font-weight-bold" href="">Brand Name</a>
                </li>
            </ul>
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#about">About</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#about">Contact</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!-- 1. JUMBOTRON -->
<section id="showcase">
    <div class="container" style="height: 100vh;">
        <div class="row h-100">
            <div class="col align-self-center">
                <img src="https://picsum.photos/440/" class="img-fluid mx-auto d-block">
           </div>
        </div>
    </div>
</section>

<!--FOOTER-->
<footer id="main-footer" class="text-center fixed-bottom">
    <div class="container">
        <div class="row">
            <div class="col">
                <p class="text-muted">Copyright &copy; 2018 <img src="img/navbar_red_logo.32x32.png" class="img-fluid" alt="logo"> RedDesign LLC</p>
            </div>
        </div>
    </div>
</footer>


<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>

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

Ways to style specific dates on a datepicker?

Is there a way to customize the appearance of the first and last selected days in my ui datepicker for booking, similar to the design shown in the image linked below? https://i.sstatic.net/bKnRS.jpg var dateFormat = "DD/MM/YY", ...

Reactively responsive table view

I am new to CSS and recently discovered a responsive table on this website that I would like to incorporate into my application. My challenge is figuring out how to switch the table layout from left to right (headers on the right and values on the left) w ...

The importance of CSS z-index in optimizing for mobile responsiveness

In my jquery modal dialog box, I wanted to create a shadow effect only between the dialog box and the screen. To achieve this, I added an overlay using a <div>: <div id="overlay" class="overlay btn-hidden"></div> <d ...

Displaying a division when a button is pressed

Despite my best efforts, I can't seem to get the chosen div to show and hide when the button is pressed. <button id="showButton" type="button">Show More</button> <div id="container"> <div id="fourthArticle"> <i ...

JavaScript can sometimes present peculiar challenges when it comes to setting style attributes, especially when a DOCTYPE is

It seems like I am encountering an issue when trying to dynamically create and modify a <div> element using JavaScript. The problem arises when I use the XHTML 1 Transitional doctype. This is how I am generating the <div>: var newDiv = docume ...

Upon selecting multiple checkboxes, corresponding form fields will be displayed based on shared attributes

When multiple checkboxes are selected by the user, corresponding form fields should appear based on the checkboxes. For example, if both flight and hotel checkboxes are checked, then the full name and last name fields should be displayed while other fields ...

Achieving Div Alignment in a Fluid Bootstrap Grid

I am currently utilizing the bootstrap grid system, as depicted below, and I am attempting to center some div elements (pic). Despite my attempts with various CSS properties like {margin:0 auto; width:50%}, I am unable to achieve the desired result. I fi ...

Why doesn't "align-self: flex-end" relocate the game board to the bottom of the screen?

Hey there, I am currently working on developing a Connect 4 game and have decided to use a table for the board. To position the board properly, I am utilizing flexbox. Although I have specified align-items as 'flex-end' in order to bring it to th ...

prettyPhoto popup exceeds maximum width and height limitations

I am currently using the most up-to-date version from No Margin for Errors and I have set allow_resize to true. However, the size of the display is still too large. Is there a way to set a maximum width/height? I have already configured the viewport as fo ...

Issues with implementing @font-face on a singular font

I've encountered an issue while setting up a website for a client concerning the fonts. Most fonts are displaying correctly, but there is one font, "chunkfive", that is not working as expected. The problem becomes more severe in Internet Explorer wher ...

Ways to integrate a CSS file into XSLT

I have a CSS file that looks like this: .table_class1DeffCell { border-top-width : 1; border-left-width : 1; border-right-width : 1; border-bottom-width : 1; } .table_class11DeffCell { border-bottom-color : 000000; border-top-color : 000000; border-right- ...

Issue with the overall layout in Bootstrap 4 involving the main content area and sidebar

I am encountering challenges with the overall design of my website, particularly with how the main content area and sidebar are positioned. The layout I have in mind is: https://i.sstatic.net/PYphE.jpg Below is the code snippet I am using to implement th ...

Arranging div blocks in columns that are seamlessly aligned

I am dealing with a situation where I have several boxes or blocks arranged in a 3-column layout. These boxes are styled using properties like float: left, width: 33%, but they have varying heights. Is there a way to make sure that the boxes always fill i ...

How to vertically center content within a container-fluid in Bootstrap 4

I'm currently working on vertically aligning the two rows within the container-fluid. They are already aligned horizontally, but I want to center them on the screen. <!DOCTYPE html> <html lang="en"> <head> <! ...

What is the best way to continuously loop an image from left to right and right to left indefinitely using JavaScript?

As part of the challenge, I am tasked with creating a Pacman game using HTML, CSS, and JavaScript. One key aspect is to control the movement of the ghosts throughout the game. However, I am facing an issue with the current function that controls the ghost& ...

Navigate between links by using the Tab key, while excluding certain links from the sequence

On my webpage, I have a main menu, various links in the main content area, and a left menu. The challenge is to make the website accessible for people who are blind... When using the tab button to navigate between links, the order currently goes as follow ...

Creating parallel lines utilizing pseudo elements

Can someone assist me in drawing double lines under my paragraph using pseudo elements only without using display block? Here is my code snippet with display block: p:after { content: ""; display: block; width: 40px; margin: 20px auto 0; bord ...

Prevent manual scrolling with CSS

I've incorporated CSS smooth scrolling into my project, which is activated by clicking a specific div element. My current goal is to prevent manual scrolling on the page so that users can only navigate by clicking the designated div and not by conven ...

Dealing with React Parsing Issue: Functions or Classes

Currently, I am in the process of transferring a small page that fetches JSON data from an API and displays it as a bootstrap list into React. As I set up the components, I find myself struggling with the usage of functions and classes. The main issue see ...

Personalizing the fileTemplate selection in FineUploader

My English is not the best, so apologies in advance. I'm struggling to customize the FineUploader FileTemplate option. I don't want to use fineUploaderBasic; I want total customization. Initially, I managed to hide the file name and size after a ...