Align a Bootstrap 4 container column both vertically and horizontally on top of a background image

After searching the internet for 5 hours, all I found was that each child of the column would line up horizontally.

I included the nav bar as it sometimes interferes with the layout.

<header>

  <nav class="navbar navbar-expand-lg navbar-dark bg-dark sticky-top">
    <div class="container">
      <a class="navbar-brand" href="/">Name</a>
      <!-- Collapse button -->
      <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>
      <!-- Collapsible content -->
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <!-- Links -->
        <div class="navbar-nav">
          <form class="form-inline" action="/search">
            <input class="form-control bg-dark text-light mr-sm-2 py-1" placeholder="Search" aria-label="Search" name="q" required type="search">
          </form>
          <a class="nav-item nav-link" href="#">One</a>
          <a class="nav-item nav-link" href="#">Two</a>
          <a class="nav-item nav-link" href="#">Three</a>
          <!-- Right alignment needed -->
          <a class="nav-item nav-link" href="/sign-in/"><i class="fa fa-user-circle-o" aria-hidden="true"></i> Sign in</a>
        </div>
      </div>
    </div>
  </nav>

  <!-- HERE - Hero image -->

  <div id="intro">
    <div class="container">
      <div class="row">
        <div class="col-md-10">
          <h2 class="display-4 mb-2">My website</h2>
          <hr>
          <h4 class="my-4">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</h4>
          <a href="#">
            <button class="btn btn-outline-light px-4 py-2">Action <i class="fa fa-rocket" aria-hidden="true"></i></button>
          </a>
        <div>
      </div>
    </div>
  </div>

</header>

CSS

html,
body,
header,
#intro {
    height: 100%;
}

#intro {
    background: url("via.placeholder.com/2000x1000") no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
   color: white;
}

I have tried various solutions and cleanup my code, but still facing issues. I attempted different methods like flexbox and justify content...

Now I suspect it might be related to Bootstrap, but I'm not sure.

Edit: Forgot to mention, horizontal alignment can be achieved with:

margin: auto;
text-align: center;
align-items: center;

on the column.

Answer №1

Are you looking for something like this?

Check out this link

To achieve the desired layout, make sure to include the following classes in your row:

Additionally, ensure that your header or any parent element has a height of 100%.

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

#bg-container {
  height: 100%;
  background-image: url("http://i350.photobucket.com/albums/q409/spawn_065/galaxy.jpg");
  background-position: center;
  background-size: cover;
}

#image-container {
  position: relative;
  height: 300px;
  width:  300px;
  background-color: green;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<header>

  <nav class="navbar navbar-expand-lg navbar-dark bg-dark sticky-top">
    <div class="container">
      <a class="navbar-brand" href="/">Name</a>
      <!-- Collapse button -->
      <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>
      <!-- Collapsible content -->
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <!-- Links -->
        <div class="navbar-nav">
          <form class="form-inline" action="/search">
            <input class="form-control bg-dark text-light mr-sm-2 py-1" placeholder="Search" aria-label="Search" name="q" required type="search">
          </form>
          <a class="nav-item nav-link" href="#">One</a>
          <a class="nav-item nav-link" href="#">Two</a>
          <a class="nav-item nav-link" href="#">Three</a>
          <!-- Right alignment needed -->
          <a class="nav-item nav-link" href="/sign-in/"><i class="fa fa-user-circle-o" aria-hidden="true"></i> Sign in</a>
        </div>
      </div>
    </div>
  </nav>

<div id="bg-container">

  <div class="row align-items-center justify-content-center h-100">
    <div class="col-4">
      <div id="image-container">
        Image
      </div>

    </div>
  </div>

</div>

</header>

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

What is the best way to include a scrollbar in a modal window?

Does anyone know how to add a scroll function to my modal in CSS? I have too many elements and would like to implement a scrollbar. You can see what it looks like here: https://i.sstatic.net/4Txrn.png Any suggestions on how to add the right scrollbar? If ...

Having trouble getting the Boostrap 4 modal to function properly in asp.net?

Struggling to incorporate a Bootstrap 4 modal popup into my ASP.NET page without triggering a postback. I've tried various suggestions like placing the modal code outside of the form tag or using an update panel, but nothing seems to work. Interesting ...

Utilizing CSS animation triggered by a timer to produce a pulsating heartbeat effect

Here's a unique way to achieve a heartbeat effect on a spinning circle. The goal is to have the circle double in size every second and then shrink back to its original size, resembling a heartbeat. To achieve this, a JavaScript timer is set up to remo ...

Customize the Emotion Styled Material UI Tab Indicator Overwrite

Looking for a way to customize the appearance of tab indicators using Emotion styled components. I'm struggling to target nested classes in my code. Here's what I have so far: const StyledTabs = styled(Tabs)( { classes: { indicator: ...

The boundary for the multipart/form-data in the $http post request is missing

I noticed that the boundary I set for the post call appears different in Chrome. How can I make my custom boundary "--test" display correctly on the request payload? var url = '/site/apkUpload'; var deferred = $q.defer(); console.log ...

Utilizing PHP and WordPress to categorize and showcase custom posts through looping

In the process of creating a dynamic webpage, I am faced with the task of presenting a list of categories in an accordion-style dropdown for a custom post type. Each category name should serve as the accordion title, while the associated posts are displaye ...

Revealing child elements by expanding the absolute div from the center

I am trying to create a rectangular mask that expands on hover to display its children with varying heights. Currently, I have achieved this using an absolutely positioned div that adjusts its left, width, and max-height properties. However, I would like i ...

``KnockoutJS offers a variety of options for handling options, including optionsText

I am curious about the functionality of data-bind, options, optionsText, and optionsValue in this code snippet: <div class="header dropdown"> <select data-bind=" value: locale.selected_locale, options: [{ value: 'en-CA&ap ...

Using Bootstrap 4 beta for Form Validation

I'm struggling to grasp the concept of building validation forms. The bootstrap documentation includes a JS code snippet (referred to as starter code) like the one below: <script> // Example starter JavaScript for disabling form submissions if ...

Position the second line of text to align in MUI

I am facing an issue with aligning the text on the second line. It should match up with the text on the first line. For reference, you can check out the Codesandbox by CLICKING HERE <Card sx={{ maxWidth: 200 }}> <CardMedia component="i ...

Struggling with setting up a search bar for infinite scrolling content

After dedicating a significant amount of time to solving the puzzle of integrating infinite scroll with a search bar in Angular, I encountered an issue. I am currently using Angular 9 and ngx-infinite-scroll for achieving infinity scrolling functionality. ...

Positioning Bootstrap 4 elements within Angular application views

As an Angular 7 developer, I'm currently working on creating a blog template using Bootstrap 4 within my application. The layout consists of two cards placed in a row - one for displaying the blog posts and the other for showcasing different categorie ...

Internet Explorer displaying incorrect formatting for HTML CSS tables

Having an issue with table display on IE 9/10/11. Tested code on different browsers and platforms, everything looks good except when viewed on my HTTP server (lighttpd and python SimpleHTTPServer), the table is showing up incorrectly: var cell = $(&apos ...

Is the default animation duration of Compass being ignored?

After recently updating to Compass version 1.0.16, I started setting up basic usage of the new animation features. However, I encountered an issue where setting default values for different animation settings did not take effect. This led me to hard-code t ...

Structuring PHP, HTML, and jQuery files on a website

I'm a beginner in web development and currently working on a school project to create a basic educational video platform. My next task involves turning my site into a single page application, which I plan to accomplish with the help of jQuery and AJAX ...

What might be causing the overflow of my page width by my navbar?

I am currently working on creating a product landing page for freecodecamp, and I'm facing some issues with my navbar - the first element I need to get right. Could you help me identify what's wrong with my code? Additionally, I would like to ad ...

What is the best way to send messages between different sections on the same page?

Basically, in the following code: <?php $hostname = ''; $username = ''; $password = ''; $dbn = ''; try { $dbh = mysqli_connect($hostname , $username, $password ,$dbn); //echo 'Connected to database'; } ...

Which is better for decorating logo text - CSS3 or Jquery?

I've been attempting to achieve this design using CSS without much success. The goal is for the entire logo to scale using fittext.js while looking visually appealing. However, I keep encountering obstacles that prevent me from achieving my desired re ...

A JointJS element with an HTML button that reveals a form when clicked

How do I bind data to a cell and send it to the server using the toJSon() method when displaying a form on the addDetail button inside this element? // Custom view created for displaying an HTML div above the element. // ---------------------------------- ...

What is the most efficient way to create a single CSS class that will style two text lines, such as a title and subtitle

Here's the code snippet I'm currently working with: .data-title { font-weight: bold; } .data-content { color: gray; } <p class="data-title"> Title </p> <p class="data-content"> Content </p> This is how I want ...