Achieving vertical centering in Bootstrap using the "table-cell" method

I'm trying to center a div both vertically and horizontally using the "table-cell" method without specifying a specific height. However, it's not working as expected.

Why isn't this method working for me?

Here is the code I am using:

<section class="content-01 v-center">
<div>
  <div class="container">
    <div class="slogan text-center">
      <h1>VERTICAL CENTERING</h1>
    </div>
  </div>
</div>

And here is the CSS associated with it:

.content-01.v-center {
display: table;
width: 100%;

}

.content-01.v-center>div {
display: table-cell;
vertical-align: middle;
margin-top: 0;
margin-bottom: 0;
float: none;
}  

You can view the example on jsfiddle

Any suggestions on what I might be doing wrong? Thank you!

Answer №1

The issue at hand is that the html and body tags are closing before reaching the bottom of the viewport:

To address this, you will need to specify to the browser that the height of both the html and body elements should be set to at least 100%:

html, body {
    height: 100%;
}

You also need to define a height for the container section:

.content-01.v-center {
    /* ... */
    height: 100%;  
}

Here is an updated link to the fiddle: http://jsfiddle.net/5Gh7t/8/

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

Changing the order of element names depending on their location within the parent element using jQuery

<div class="content"> <div> <input type="text" name="newname[name]0"/> <div> <input type="text" name="newname[utility]0"/> <div> <textarea name="newname[text]0 ...

Locating content inside a span element with tags and spacing in jquery

Within the span class of teamName, there are various tags including white-spacing, a sup tag, and a p tag: <span class="teamName"> Dr.<sup>J</sup> W Smith ...

An issue occurred while trying to retrieve the js file, as an error with code net::ERR_ABORTED 404 (Not

Although this question has been asked before, I am unsure where to find the solution. My express backend server needs to render an HTML page with main.js in it upon launch. app.js code: var createError = require('http-errors'); var express = req ...

Extract a CSS property and store it in a variable using JQuery

Is there a way to extract a CSS value from a specific class on a webpage and then use it for styling other elements in different ways? I created an example on jsfiddle where I want to retrieve the background color from .box and save it to a variable nam ...

What is the best way to ensure consistent code placement at the bottom of my Django Template using inheritance?

Lately, I've been working on incorporating a footer into my layout.html file that is inherited by all other pages within my Django project. I am aiming to create a footer that remains fixed at the bottom of every webpage just like how Stack Overflow ...

Creating a custom layout for displaying blog post previews with Django and CKEditor

I am currently using Python + Django for my blog. Previously, I manually added a custom HTML tag <post_cut/> to create a blog post preview and used Python slice to display only the preview. This method helped me avoid issues with fixed length preview ...

Having Trouble with $.ajax Function in my Program

After spending three days experimenting with various methods, I'm still unable to successfully use the Javascript ajax command to send form values to a php script. Despite no errors being displayed and the scripts running smoothly, nothing is getting ...

HTML5 coding can be enhanced by applying unique CSS rules for alignment purposes

html { font-family: "Open Sans", sans-serif; font-weight: 100; background-color: #fbfbfb; } body { font-family: "Open Sans", sans-serif; font-weight: 100; } body h1 { font-weight: 100; } body h3 { font-family: "Open Sans", sans-serif; fo ...

Troubleshooting challenges arise with Bootstrap's button group spacing issue

I am encountering spacing issues with the markup provided below. <h2>Profitability Report</h2> <form method="post"> <div class="row"> <div class="col-md-12"> <div class="btn-group btn-group-toggle ...

Does .NET MVC provide the necessary separation of HTML/CSS/JS for my project?

Currently, I am collaborating with my ASP.NET development team in an effort to improve the quality of our HTML output. We are facing challenges due to ASP.NET's tendency to insert JavaScript directly into the page, creating dependencies on JS for form ...

Is using transform scale in CSS to scale SVG sprites a valid method of scaling?

I have been using an SVG sprite as a CSS background image in this manner: .icon-arrow-down-dims { background: url("../sprite/css-svg-sprite.svg") no-repeat; background-position: 16.666666666666668% 0; width: 32px; height: 32px; display: inline-b ...

Guide to centering the navigation bar within a container using Bootstrap 4

I'm looking to center the navbar on my webpage. How can I style it so that it appears in the middle of the page? Currently, it's aligned with the left side of the page. section class="navbarSection"> <div class="contain ...

Executing a webservice method in an html page using javascript without the need to refresh the page

Is it possible to call a webservice from an index.html page using JavaScript? My webservice is located at "localhost/ws/service.asmx" and the specific web method I want to call is called HelloWorld. The index.html page contains an HTML submit button whic ...

The hyperlink tag within the overlay div is unresponsive

I'm facing an issue with my overlay div (rb-overlay) that pops up when users click on a page option. The overlay takes up the entire page and includes a close button in the top right corner. However, I've noticed that the link at the end of the t ...

Is it possible to inject code into HTML using JQuery?

Despite having looked at numerous examples and being aware of the answer to my question, I can't help but feel like I might be missing something. I suspect that the reason this isn't working is because I am running this code locally, rather than ...

What could be causing the hover effect to not work on other elements within the div?

I am working on creating a card that displays only the image when not hovered, but expands and reveals additional information in a div to the right of the image when hovered. Unfortunately, when I hover over the image and then move towards the adjacent div ...

Tips for creating a form that adapts to different devices, ensuring it looks great no matter the screen size

<div class="modal" id="myModal" style="width:50%; top:15%;" role="dialog" > <div class=""> <!-- Modal content--> <div class="modal-content"> ...

Integrating Contact Form with PhoneGap API using JavaScript and HTML5

I am currently working on building a contact form using the PhoneGap API integrated with JavaScript and HTML5 for the form design. The contact form I am creating should allow me to add new contacts and search for existing ones. To achieve this, I have been ...

Struggling to align the Bootstrap list-group container in the middle

Good day to you! I'm currently working on a sidebar layout using Bootstrap's list-group and I need to set a specific width to ensure it aligns properly with the other elements above and below it. However, when I try setting the width to 300px, t ...

Are we utilizing this JavaScript function properly for recycling it?

Two functions have been implemented successfully. One function adds the autoplay attribute to a video DOM element if the user is at a specific section on the page. The other function smoothly slides in elements with a transition effect. The only limitatio ...