Aligning Content in the Middle of a Bootstrap Column

Can anyone help me with centering the content of a bootstrap column vertically? I'm facing an issue when setting width: 100% and height: 100% for the overlay div. Any solutions?

Here is an example image of what I am trying to achieve:

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


Below is the code snippet in question:

var coverSection = $(".cover-table");
$(coverSection).height($(window).height());

.cover-table {
    display: table;
    width: 100%;
    text-align: center;
    background-image: url("http://mlmconsultantsblog.com/wp-content/uploads/2010/02/man-working-on-laptop-dreamstime_7728015.jpg");
    background-size: cover;
    background-attachment: fixed;
    color: white;
    
    .cover-cell {
        display: table-cell;
        vertical-align: middle;
        
        .overlay {
            width: 100%;
            height: 100%;
            background-color: rgba(22, 22, 22, 0.80);
        }
    }
}

Note: Please run the code snippet in full page.

Answer №1

To achieve a responsive layout, you can utilize the flex property in CSS. Simply update the following code within your cover-table:

.cover-table {
   display: flex;
   align-items: center;
   justify-content: center;
}

Below is a functional example:

var coverSection = $(".cover-table");
    $(coverSection).height($(window).height());
.cover-table{
    display:flex;
    align-items:center;
  justify-content:center;
    width:100%;
    text-align:center;
    background-image:url("https://example.com/image.jpg");
    background-size:cover;
    background-attachment:fixed;
    color:white;
    .cover-cell{
        display:table-cell;
        vertical-align:middle;
        .overlay{
            width:100%;
            height:100%;
            background-color:rgba(22, 22, 22, 0.80);
        }
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<section class="cover-table">
    <div class="cover-cell">
        <div class="overlay">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="cover-nested">
                            <h4>Hello Universe</h4>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

Answer №2

If you utilize display:table for the container with the class .cover-table, you can then apply display:table-row to elements with the class .cover-cell and use display:table-cell along with vertical-align:middle for elements with the class .overlay.

var coverSection = $(".cover-table");
    $(coverSection).height($(window).height());
.cover-table{
    display:table;
    width:100%;
    text-align:center;
    background-image:url("http://mlmconsultantsblog.com/wp-content/uploads/2010/02/man-working-on-laptop-dreamstime_7728015.jpg");
    background-size:cover;
    background-attachment:fixed;
    color:white;
}
.cover-cell{
  display:table-row;
}
.overlay{
  width:100%;
  height:100%;
  background-color:rgba(22, 22, 22, 0.80);
  display:table-cell;
  vertical-align:middle;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<section class="cover-table">
    <div class="cover-cell">
        <div class="overlay">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <div class="cover-nested">
                            <h4>Hello World</h4>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

Answer №3

The secret code:

.overlay {
  height: 100vh;
  width: 100vw;
  position: relative;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
}

.overlay .container{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

// Note: The following JS code is not needed due to CSS implementation
//var coverSection = $(".cover-table");
//    $(coverSection).height($(window).height());
.cover-table {
  display: table;
  height: 100vh;
  /* Achieves same effect as the removed JS code */
  width: 100%;
  text-align: center;
  background-image: url("http://mlmconsultantsblog.com/wp-content/uploads/2010/02/man-working-on-laptop-dreamstime_7728015.jpg");
  background-size: cover;
  background-attachment: fixed;
  color: white;
  .cover-cell {
    display: table-cell;
    vertical-align: middle;
    .overlay {
      width: 100%;
      height: 100%;
      background-color: rgba(22, 22, 22, 0.80);
    }
  }
}
.overlay {
  height: 100vh;
  width: 100vw;
  position: relative;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
}


.overlay  .container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<section class="cover-table">
  <div class="cover-cell">
    <div class="overlay">
      <div class="container">
        <div class="row">
          <div class="col-sm-12">
            <div class="cover-nested">
              <h4>Hello World</h4>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

Answer №4

All you need to do is include this CSS code and your text will be perfectly centered.

Here's the CSS:

.centered-text {
    display: flex;
    align-items: center;
    justify-content: center;
}

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

Is there a way to navigate to the next or previous image in a lightbox by using ev.target.nextElementSibling or ev.target.prevElementSibling?

I am new to the world of web development Currently, I'm facing an issue with my lightbox feature. I want to navigate between images by using ev.target.nextElementSibling and ev.target.prevElementSibling when clicking on the next/previous arrow.png ic ...

Is it possible to configure Lambda NodeJS 12.x flags (like --experimental-modules) when using AWS SAM local start-api?

When setting up my nodejs server, I chose to use ES6 module syntax: package.json "type": "module" So far, running my server locally as a nodejs process has been successful. For example: "scripts": { "dev": "npm outdated ; nodemon --experimental-modul ...

Is it possible for my Java Applets to be compatible with Chrome 45?

Our web application utilizes three Java Applets for various functions. We are aware that Chrome 45 will no longer support NPAPI. Upon visiting Oracle's page, it is stated that Java Plugin depends on NPAPI. I have tested my Applets on Chrome 43 and 4 ...

Is there a Unicode character substitution happening?

There are certain mobile browsers that appear to have trouble supporting all unicode characters, such as the down arrow icon like this: span.icon:after { content:"\25be"; } Currently, no symbol is shown. Is there a way for me to identify this i ...

Creating a dynamic dropdown menu: a step-by-step guide

<ul class="nav navbar-nav friend-label"> <li class="dropdown" ng-repeat="module in modules"> <a href="" class="dropdown-toggle" data-toggle="dropdown"> {{module.ModuleName}} < ...

"Encountering a HTTP 500 Error while using jQuery Ajax with a Web

Exploring the functionality of a jQuery ajax request, I have developed two simple projects within a Visual Studio solution. One project serves as a web service while the other consumes it. If you are interested in checking out this small project, feel fre ...

"Steps to retrieve the button's ID when using the onClick event in Reactjs

Currently, I am working with Reactjs and Nextjs. I am utilizing functional components instead of classes. Within a loop, I have buttons and I am attempting to retrieve the id of the button when clicked using "onClick". Unfortunately, I am encountering th ...

Should private members be kept confidential during program execution?

While Typescript's "private" members may not be truly private at runtime, traditional closures maintain the privacy of their members. Is there value in ensuring that private members remain private during runtime? ...

Accordion jQuery failing to display tabs

I have a question about using 2 jQuery accordions with tabs inside them. Everything seems to be working, but when I try to expand the second accordion, the two tabs inside don't render properly. I'm not sure what the issue is. Any help on this wo ...

Error: Attempting to assign a value to the non-existent property 'user' <br>    at [file path] on sqlite3

I encounter an unspecified error when I try to establish a session for the user after validating their credentials during login. I'm utilizing express-session to create the session, but it's not directly imported into the file as instructed by my ...

Preserve file sequence with jquery file upload

I recently came across an interesting upload script at the following link: This script utilizes jquery file upload to allow for uploading multiple files simultaneously. I'm curious about how to transmit the order in which the files were selected to t ...

Guide on automatically filling input text fields with database values when a checkbox is clicked

I need to automate the process of filling in multiple input text fields with values from variables retrieved through a SELECT query when a checkbox is clicked. For instance, if the SELECT query returns the Warehouse Street Address ($WarehouseStreetAddres ...

Is Flex still wrapping elements even when set to nowrap?

My goal is to create a layout with 4 blocks, where the first section contains 2 blocks each occupying 50% of the width, followed by 2 other blocks in the next section. I am utilizing the flex property for this layout. I want the flex-container to take up ...

Troubles with submitting jQuery validation plugin via AJAX

Whenever I try to use the jQuery validation plugin for validating a form within a Bootstrap modal, the validation does not work and the form cannot be submitted. This is the code for the Bootstrap modal form: <div class="modal fade" id="form-content" ...

Unexpected strings added to files using jQuery Upload File Plugin, caused by WebKitFormBoundary

I'm currently utilizing a plug-in found at My purpose for using this plug-in is to send files to a WCF Rest Service and save them onto the hard drive. The upload process seems to work fine, however, there is an issue with images, executables, etc. a ...

What could be causing my Vue component to not refresh?

Can anyone help me figure out why this component isn't re-rendering after changing the value? I'm attempting to create a dynamic filter similar to Amazon using only checkboxes. Here are the 4 components I have: App.vue, test-filter.vue, filtersIn ...

Uploading Files and Content using AJAX

I'm currently developing a client database system for our organization. It may not have all the bells and whistles, but it definitely gets the job done. Now that I've got the basics covered, I'd like to incorporate some file management funct ...

Having trouble getting two divs to align on the same line using the 'inline' method

I am struggling to align multiple buttons within divs on the same line. Two of these buttons should only be displayed when a specific value is greater than 0. I attempted using display:inline-block in a container div, along with style="float:right", but it ...

Save user information to initialize rootScope upon refreshing the page

I'm looking for a way to retain a User Json Object in such a manner that I can use it to initialize my $rootScope.user Json Object with the saved information even after refreshing the page. The storage should persist as long as the browser window rem ...

How can I make this div appear on the screen?

I have recently encountered an issue with a navbar on my webpage. Despite adding all the necessary links, one of them mysteriously disappears from view. It seems to be present when inspected in the console, but it just won't display on the front end. ...