Utilizing the vh unit to vertically center a div

My approach to setting the height of my wrapper to 100vh in order to fill up the entire screen and then aligning a div vertically in the middle by setting it to 50% using position: relative, top, and transform (translateY) seems to be working perfectly on my desktop computer. However, when I view it on my MacBook (both running Chrome), the alignment is off and the div appears "pushed" to only about 30% of the screen height. Can anyone spot what might be wrong with my code? Here is the snippet:

#main-1 {
    width: 100%;
    height: 100vh;  
    background-image: linear-gradient(to right, #e1484f 50%, #f9f9f9 50%);
}
#info-left {
    width: 20%;
    height: 300px;
    padding-left: 10%;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    float: left;    
    text-align: center;
    color: #FFF;
}

Answer №1

To center your inner container, follow these steps:

position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);

This code will ensure your div is centered both horizontally and vertically.

Remember to include the -webkit prefix for better cross-browser support.

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 find information in a multi-page table?

I've implemented a table with pagination and search functionality to look up data within the table. However, currently the search only works within the current page of the table rather than searching the entire dataset. Is there a way to modify the se ...

Guide on ensuring Bootstrap tooltip remains visible using just JavaScript

Greetings, I recently found myself searching for ways to ensure that the tooltips on my webpage remain visible at all times. During my quest for a solution, I stumbled upon this enlightening discussion on Stack Overflow. Interestingly, all the proposed sol ...

I am looking to develop a customizable table where the user can input their desired information

Looking to create an HTML page featuring a 10x10 table with alternating red and green squares. After loading the page, a pop-up window will prompt the user to input a word, which will then appear only in the red squares of the table. While I've succes ...

Refine information by using checkboxes with the help of jQuery, MySQL,

How do I use PHP and jQuery to filter content with checkboxes? I'm getting a warning about an undefined index when I try to post the form via jQuery. Check out my JavaScript file: function filterContent(){ var all = $(".all").val(); $('input.al ...

Step-by-step tutorial for making a mesmerizing fade out grunge background

How can I achieve a fading grid background effect? I have experimented with using a conic-gradient for the background, but I am unsure how to make it fade out. background: conic-gradient(from 90deg at 1px 1px,#0000 90deg,grey 0) 0 0/50px 50px; ...

What is the best way to find an element using either 'className1' or 'className2'?

Within my project, all locators are defined as elements of an enum and follow this format: For example, if it is a CSS locator - DIV_LOCATION("css=div.location-text.text-overflow"). This method parses the string and identifies it as a CSS locator if it be ...

Showing a PDF file within a Bootstrap modal

I'm encountering difficulties with a simple task. My goal is to display a PDF using the HTML <object> tag within a Bootstrap modal. I've put together a Plunkr to illustrate the issue. Essentially, there is a straightforward <object> ...

Ensure that the div is set to start on a new line without causing any sudden jumps to the

I have 3 divs that I want to display side by side. If the text inside each div gets too long, I would like it to break to a new line and have the next div stay beside it. <div style="float: left;">This is some text</div> <div style="flo ...

I am finding it difficult to navigate relative paths when using Master Pages

I utilized Visual Studio 2010 to start a fresh project with the Web Application template. Without making any modifications, please note that Login.aspx and Default.aspx both point to the same Site.Master master page in the website root directory. Addition ...

Find what you're looking for by exploring the search results inside the text box marked

update1: Appreciate your response! However, I am facing an issue where typing 'h' displays a set of values. When selecting a value, it should appear in the text box with a cross symbol, similar to how tags are edited on StackOverflow. I am work ...

JS script for clicking various icons on a webpage that trigger the opening of new tabs

I am working on a webpage where I have 9 icons with the same class. I am trying to write JavaScript code that will automatically open all 9 icons when the webpage loads. I tried using the code below, but it only clicks on the first icon and stops. let ...

How to properly center and align the width of a figcaption element within an image placed in a figure element

I've been struggling for two days now trying to fix an issue with the alignment of images and captions on my Django application. In my app, users can upload images, and I'm using figure and figcaption tags to display the image with a caption. Ho ...

Customizing Autocomplete: Enhancing the selected and set item

My autocomplete function looks like this: $('#' + id).autocomplete({ minLength: minLength, source: function (d, cb) { ... } // select event of OCR number autocompl ...

Javascript file containing prohibited characters disrupting form functionality when linked with Jquery

Having some trouble with these files - HTML form, PHP processor, and JavaScript. The browser is showing errors like U+12AF for illegal characters in the JavaScript file. Spent 1.5 hours on this already and feeling frustrated. --submit.js-- $(docume ...

Troubleshooting problems with CSS when zooming in and out

My CSS is designed for screen sizes larger than 1100 pixels. However, when I zoom in using ctrl and the plus sign, the center container on the page fails to wrap properly and suddenly breaks. HTML: <div id="outer_footer_bottom"> <div class=" ...

Creating a chic look for your conditional statement: If Else Styling

While it may not be possible to directly style PHP code, you can definitely style the HTML output that PHP generates. I'm curious if there's a way for me to apply styles to the output of an IF ELSE statement. if ($result != false) { print "Y ...

The HTML date input is displaying the date incorrectly, appearing one month off when set via the value attribute

i have tried this in two different browsers 1.Chromium Version 76.0.3809.87 2.Mozilla firefox 68.0.1 every time i enter a date in the input field in the format y-m-d, the month seems to be reduced by one without any explanation this is my code <i ...

Set a consistent pixel measurement for all images

I am working on a project where I have an image of a card that needs to be repeated 30 times. Each time the card is repeated, I want it to overlap with the previous card in a specific position, resembling a deck of cards. The issue I am facing is that whe ...

Safari: Fixed-positioned DIVs staying put after DOM updates

Hey there! I've been experimenting with animating absolutely positioned DIVs on my webpage using some JavaScript to update the top and left CSS properties. It's been working smoothly in Chrome, Firefox, and even Internet Explorer 8. However, I e ...

Looking to enhance the style and appearance of your REACT App? Consider incorporating tools like Boostrap CDN, React-bootstrap, MDBoostrap, or any other styling options available. These can

I am relatively new to Frontend development and I am eager to create a visually appealing APP using the MERN Stack. I have recently started working on developing with REACT JS. In order to enhance the styling of my project, I considered importing the CDN ...