What is the best way to vertically center align the content inside a div with a height of 100vh?

https://i.sstatic.net/RABhH.jpg

.components{
    background-color: #1DA1F2;
    text-align: left;
     height: 100vh;
     padding: 20px;
 }

With the current CSS code, I've set the outermost div (.components) to have a height of 100vh but I'm having trouble aligning the inner content in the middle of the screen. The inner div is called .row. How can I achieve vertical alignment for the content within the outer div?

Answer №1

Below is a simple example I created (I hope it meets your requirements):

To achieve the desired layout, simply add display:flex;align-items:center to the .components class. Bootstrap was utilized only for ensuring the columns stay side by side, but whether you use bootstrap or not, applying display:flex will yield the same result.

.components {
  background-color: #1DA1F2;
  text-align: left;
  height: 100vh;
  padding: 20px;
  display:flex;
  align-items:center;
}

.row {
  width:100%;
}
.col-sm-4 {
  background:red;
  height:50px;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="components">
  <div class="row">
    <div class="col-xs-4">
      col
    </div>
    <div class="col-xs-4">
      col
    </div>
    <div class="col-xs-4">
      col
    </div>
    <div class="col-xs-4">
      col
    </div>
    <div class="col-xs-4">
      col
    </div>
    <div class="col-xs-4">
      col
    </div>
  </div>
</div>

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

Utilizing jQuery to establish various AJAX requests through functions on page load and button click

Utilizing a basic ajax call both on page load and click events, where I have defined separate functions for each. Despite using different URLs and parameters in the function, the JSON data from the previous call is still being displayed when clicked. Bel ...

.cshtml fusion turns sour

My attempt to create a loop that displays small boxes led me to the following code snippet with unexpected results: (code) : <div class="row"> <div class="col-lg-3 col-xs-6"> <!-- small box --> <!-- please change the addr ...

The issue with Bootstrap Vue scrollspy arises when trying to apply it to dynamic data. Upon closer inspection, it becomes evident that the elements are activating and highlighting one by

In my application built with Vue, content is displayed based on the component type (Header, Text, Image) in a JSON file. I am looking to implement scroll spy functionality specifically for the Header component containing headings. I have attempted to use ...

It is impossible for me to utilize inline SVG as a custom cursor in CSS

Below is the code I have written. I am attempting to change the cursor using an inline SVG, but it doesn't seem to be working as expected. However, when I use the same code as a background, it works perfectly: .container { width: 50vw; height: ...

Choosing changeable object label in CSS

Looking for a way to style multiple customized HTML tags (Angular.js directives) that share the same suffix? Imagine you have three modal tags that you want to style similarly: 1. <a-modal> 2. <b-modal> 3. <c-modal> Is there a selector ...

Selenium cannot find iFrames

I've come across various suggestions about transitioning to iframes such as driver.switchTo().frame("test frame"); and driver.switchTo().defaultContent(); Yet, I'm having trouble navigating to MenuFrame 4 in the following code. I am utilizing Ja ...

Angular application's HTML Canvas unexpectedly changes to a black color when I begin drawing

Currently, I am developing an Angular application in which users can draw text fields on PDF pages. To achieve this functionality, I have integrated the ng2-pdf-viewer library and created a PDF page component that incorporates an HTML canvas element. Howe ...

Encountering the error message "AngularJS error: $(...).modal is not a function" after updating from Bootstrap 3.3.6 to version 4.0

Since updating the bootstrap version in my application from 3.3.6 to 4, I've been encountering an error stating that $(...).modal is not a function. This was previously working fine before the update. angular.js:14700 TypeError: $(...).modal is n ...

Turn off automatic spelling correction and predictive text in a content-editable section

I'm currently working on a cross-browser application using Script#. I've incorporated a contenteditable div where users can input text. However, I am facing an issue with the auto correct/auto completion feature altering the user's text. Co ...

Tips for ensuring an image fits perfectly within a MUI grid

I am currently working on an application that involves a collection of cards. My tech stack includes React and MUI v5. One issue I've been facing is with the grid layout, specifically in trying to keep the image size consistent within the grid item. ...

Decode JSON data retrieved from a remote URL address

I am in need of assistance decoding this JSON code from a URL page. The JSON code on the URL is in the following format: {"current":{"artists_id":"55","albums_id":null,"albums_tracks_id":null},"html_current":"<li><p>Pr\u00e1v\u011b h ...

Using AngularJS to dynamically bind HTML content based on a variable’s value

I am trying to dynamically display an image of a man or a woman on a scene, depending on the gender of the person logged into my website. The ng-bind-html directive is working fine when I define "imageToLoad" statically. However, it fails when I try to gen ...

A large responsive bootstrap website filling only half the screen

I am currently in the process of developing a simple version of a Content Management System (CMS). I have created a page that offers an HTML template, allowing users to insert their own text and image uploads to display on another screen. The template fun ...

Position the typography component to the right side

Is there a way to align two typography components on the same line, with one aligned to the left and the other to the right? I'm currently using this code but the components are aligned next to each other on the left side. const customStyles = makeSt ...

What is the best way to trigger the Save as ..dialog by clicking on an image?

Below is the code snippet provided. When the link is clicked, a save as dialog should pop up. <a href="http://www.experts-exchange.com/xp/images/newNavLogo.png" target="_new"> <img src="http://www.experts-exchange.com/xp/images/newNavLogo.png" a ...

Looking for subsequence in dropdown choices with no assigned values

I need assistance with searching for a specific substring within text that is fetched from options generated by MySQL. How can I retrieve the selected option's text value in order to search for my desired substring? $(document).ready(function() { ...

"Why are all the rows from my query being returned in my HTML/PHP/AJAX code

I've been working on a webpage that allows users to input a minimum GPA in a textbox to search a database and display records of students who meet that specific criteria. The HTML code calls a separate PHP file and utilizes AJAX to call a function. Ho ...

What is the best way to ensure that form inputs and labels stay aligned?

Let's consider a scenario where there is a web page containing a form: <form> <label for="FirstName">First:</label> <input name="FirstName" type="text"> <label for="MiddleName">Middle:</label> <input n ...

What's the reason behind the data not being retrieved by $.ajax?

After attempting to validate a form and send the values using $.ajax, I encountered an error message stating "Undefined index: is_ajax". Why is the form_data not being received? What could be causing this issue and how can it be resolved? function validat ...

Having trouble accessing the 'checked' property of the checkbox

I've been developing a web application using Vite, JavaScript, jQuery, and Bootstrap. I'm facing an issue where the jQuery .is(':checked') function is not working as expected on one specific page. Despite the checkboxes being checked, t ...