Struggling with vertical and responsive image centering in Bootstrap 4?

I'm currently developing an Angular 6 app and facing an issue with centering an image on the home screen. Despite trying various solutions from this platform, the image always ends up positioned right next to the top navbar instead of being vertically centered.

Even after attempting all the recommendations provided in the post linked below, the result remains similar to what is depicted in the accompanying image:

Vertical Align Center in Bootstrap 4

I kindly request assistance in properly aligning the home image (or any other content) vertically while ensuring that the layout of other pages using the app.component.html template remains unaffected.

index.html:

<!doctype html>
<html lang="en">

<head>
    <title>WFRH</title>
    <base href="/">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <app>Loading...</app>
</body>

</html>

app.component.html:

<app-nav></app-nav>
<div class="jumbotron">
    <div class="container d-flex h-100" style="border: 1px solid red">
        <div class="row justify-content-center align-self-center text-center">
            <div class="col-sm-12">
                <!-- <router-outlet></router-outlet> -->
                I'm centered...
            </div>
        </div>
    </div>
</div>
<app-footer></app-footer>

home.component.html:

    <img src="../../../assets/cirsa_home.png" class="img-fluid" />

nav.component.html:

<nav class="navbar navbar-expand-sm bg-dark navbar-dark pb-0">
  <div class="container pb-1">
    <a class="navbar-brand" href="{{xxxApp.xxxWebsiteUrl}}" target="_blank">
      <img src="../../../assets/app_logo.png" />
    </a>

    <button style="margin-left: 10px;" class="navbar-toggler order-last order-sm-0" type="button" data-toggle="collapse"
      data-target="#collapsibleNavbar">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse order-last order-sm-0" id="collapsibleNavbar" *ngIf="(isLoggedIn | async)">
      <ul class="navbar-nav pull-left">

        <li class="nav-item text-left hover-inverse">
          <a class="nav-link text-light" href="#">
            <i class="fa fa-home"></i>
            <label [translate]="'wfrh_nav_home' | translate">Home</label>
          </a>
        </li>

        <li class="nav-item text-left dropdown hover-inverse">
          <a class="nav-link dropdown-toggle text-light" href="#" id="navbardrop" data-toggle="dropdown">
            <i class="fa fa-edit"></i>
            <label [translate]="'wfrh_nav_requests' | translate">Requests</label>
          </a>
          <div class="dropdown-menu bg-dark border-0 hover-inverse">
            <a [routerLink]="['/vacancy-form']" class="dropdown-item text-light" href="#">
              <label [translate]="'wfrh_nav_fillvacancy' | translate">Fill Vacancy</label>
            </a>
            <a class="dropdown-item text-light" href="#">
              <label [translate]="'wfrh_nav_leave' | translate">Leave</label>
            </a>
            // Remaining dropdown items...
          </div>
        </li>

        <li class="nav-item text-left hover-inverse">
          <a [routerLink]="['/login']" class="nav-link text-light">
            <i class="fa fa-sign-out"></i>
            <label [translate]="'wfrh_nav_logoff' | translate">Logoff</label>
          </a>
        </li>

      </ul>
    </div>

    // Remaining navbar elements...
  
  </div>
</nav>

<nav class="navbar navbar-expand-md bg-dark navbar-dark pt-0 pb-0">
  <div class="container pt-0 pb-1">
    <small class="username">
      {{userName | async}}
    </small>
  </div>
</nav>

styles.css:

@import "~bootstrap/dist/css/bootstrap.min.css";
@import "~font-awesome/css/font-awesome.css";

html, body {
    height: 100%;
}

Answer №1

It seems there may have been a misunderstanding in the explanation provided for vertical alignment. To align an item vertically based on its parent, the parent's height must be set (as seen in the example with body height:100%), but in cases where there are multiple different parents in your HTML, not only the parent needs to have the height property set to 100%, but also the children elements.

Here is a CSS example:

@import "~bootstrap/dist/css/bootstrap.min.css";
@import "~font-awesome/css/font-awesome.css";

html, body, div {
 height: 100%;
}

And here is an HTML example:

<body>
     <div>
        <image>the centered element</image>
     </div>
</body>

Answer №2

After extensive research and experimentation, I finally found a solution that effectively resolves the issue at hand. Hopefully, my discovery will prove beneficial to others facing the same challenge.

In my application's main template, the layout comprises a horizontal navigation bar in the header, followed by the content section, and a horizontal navbar in the footer. To achieve a centered alignment within the content div without spanning the entire screen, I opted to set a fixed height of 40px for the footer and applied a negative margin of 40px to the home image. While using negative margins may not be my preferred approach, it certainly gets the job done:

app.component.html (main structure):

<div class="main">
    <div class="row header m-0 p-0">
        <div class="col-12 m-0 p-0">
            <app-nav></app-nav>
        </div>
    </div>
    <div class="row content m-0 p-0">
        <router-outlet></router-outlet>
    </div>
    <div class="row footer">
        <app-footer></app-footer>
    </div>
</div>

home.component.html:

<div style="margin-top: -40px !important;" class="pl-5 pr-5">
    <img src="../../../assets/img_home.png" class="img-fluid" />
</div>

styles.css:

html, body {
  height: 100%;
}

.main {
  display: flex;
  flex-flow: column;
  height: 100%;
}

.main .row.header {
  flex: 0 1 auto;
}

.main .row.content {
  flex: 1 1 auto;
}

.main .row.footer {
  flex: 0 1 auto;
}

Here is a glimpse of how the home screen appears after implementing the adjustments:

https://i.sstatic.net/IXN5X.png

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

How to target an ID containing a dot using jQuery

My nodes sometimes have a dot in their ID attribute, as shown in this example: <div id="site_someSite.com"></div> When I try to select this element using jQuery with the following code: variable = $('#site_'+user); (where user is &a ...

Always ensure that only one div is visible at a time

I am currently working on a project where I cannot use ng-cloak due to the way the css files are loaded. I have been exploring alternative solutions and have tried a few different approaches. My main goal is to ensure that two icons are never shown at the ...

Manipulating layout with html5 VIDEO controls

I'm having an issue with the HTML5 video element. When I don't use "Controls," it displays with the border-radius as I specified. https://i.stack.imgur.com/KMEeJ.png However, when I add controls (which I need), an ugly border appears and overri ...

changing html numbered list to xml format

Looking to convert HTML ordered lists with different types into XML markup? <ol type=a> <li>This is list item a</li> <li>this is list item b</li> </ol> <ol type=i> <li>This is list item 1</ ...

The http url on an iPad in the src attribute of an HTML 5 video tag is malfunctioning

Having trouble loading a video on iPad using an http URL in the src attribute of an HTML5 video tag. Both static and dynamic approaches have been attempted, but it works fine on web browsers. Here is an example of the code: Static: <!DOCTYPE html ...

What is the best way to add rounded corners to tables in Bootstrap 3?

Is there a way to give the top and bottom corners of a table rounded edges? I am currently using Bootstrap 3 tables, but they have no corner radius by default. How can I achieve this effect? ...

Updating information without the need for a page refresh

My project involves text boxes and drop-down menus where users input data, then click "generate" to combine the text from the boxes and display the result on the page. I'm struggling with clearing these results if the user clicks generate again, for ...

Prevent the crawling of HTML segments by utilizing HTML-XSL parsing techniques

I am currently working with Watson Explorer FC 11.0.2 and I am facing a challenge in excluding certain HTML tags from the Watson crawlers. I am using an XSLT parser to extract metadata, title, and body from an HTML page located at the following path: "/h ...

Overriding Bootstrap's Sass variables

Struggling to customize Bootstrap variables in my main.scss file @import "node_modules/bootstrap/scss/functions"; @import "node_modules/bootstrap/scss/variables"; @import "node_modules/bootstrap/scss/mixins"; $primary: green; ...

Divide Footer Information into Two Sections: Left and Right

<footer> <div class="copyrights-left"> <p>&copy 2015. <a style="color: #fff;" href="index.html">Free Xbox Live Gold Membership and Free Xbox Live Gold Codes</a>. All rights reserved.</p></div> <div class="co ...

Sending data to another page by selecting a list item

Being new to web programming and JavaScript, I am facing a scenario where I have a list of requests displayed on one page and I need to pass the ID of the selected request to another page to display all the details of that particular request. I am strugg ...

What is the best way to sequentially process data column by column and incorporate them into a three-dimensional model using three.js?

Currently, I am in the process of creating a 3D model using three.js that will accurately display the position and orientation of a vehicle using accelerometer and gyroscope data. I have collected all the necessary information in an xlsx file, including th ...

The utilization of media within the meta in Next.js is not allowed

Recently, I came across an interesting article about PWAs on web.dev. The article discusses color schemes and at one point, they talk about it in detail, I'm currently working with Next.js and decided to try implementing the following code snippet: & ...

Guide on implementing gradient animation effects in a React component

How can I implement gradient animation effects like this example in a React component using inline CSS? I need to utilize the CSS-based gradient animation effects shown below directly within the React component. Are there any specific packages available ...

What are the steps to include media queries?

My website looks good on big monitors, but not on smaller ones or on mobile phones. I need help with adding media queries to my CSS Reset so it works across all devices. Here's the website link and the CSS Reset code: html, body, div, span, applet, o ...

Ways to unlock all the information windows in Google Maps

Is it possible to automatically display all info windows on the map when it is first opened, eliminating the need for users to click on markers? In other words, I would like all info windows for all markers to be shown by default when the map is opened. ...

Using javascript to locate and substitute a word divided among multiple tags - a step-by-step guide

I need to utilize JavaScript to locate and substitute a word that has been separated into multiple tags. For instance, consider the following HTML code: <html> <body> <div id="page-container"> This is an apple. ...

jQuery effects failing to run properly in Internet Explorer

<script type="text/javascript" src="css/jquery-1.7.1.js"></script> <script type="text/javascript"> function slidedown(id){ $('#rerooftext').hide(0); $('#inspectiontext').hide(0); $('#remodelingtext').hid ...

Using jQuery functions to disable adding or removing classes with a click event

Has anyone had success implementing a functionality where a div expands upon clicking, and reverts back to its original state when clicking on a cancel link within the same div? <div class="new-discussion small"> <a class="cancel">Cancel&l ...

Connecting an HTML box to a JavaScript function for the desired outcome: How to do it?

My goal is to connect the input box with id="b" (located inside the div with id="but1") with a JavaScript function that calculates values within an array. Although my junior logic review didn't detect any issues in the code, what mistakes could I have ...