Only Google Chrome supports vertical centering using position:absolute

I am trying to create a div with multiple elements in the center of a section that has been set to 100% of screen height. However, I am facing an issue as the solution I implemented only seems to work on Google Chrome and not on other browsers. I have used properties like position:absolute, display:table, margins and top:0, bottom:0;

var section = document.querySelectorAll('section');
  var winh = window.innerHeight;
  header.style.height = winh+'px';
  for (var i = 0; i <= section.length; i++) {
    section[i].style.height = winh+'px';
  }
section {
    padding:100px 0;
    position:relative;
    background:#222;
}                                             
section div {
    width:90%;
    max-width:1200px;
    margin:0 auto;
    text-align:center;
    position:absolute;
    left:0;right:0;top:0;bottom:0;
    display:table; 
}
<section class="users">
    <div>
        <div class="icon"><i class="fa fa-user" aria-hidden="true"></i></div>
        <div class="count">15 000</div>
        <div class="title">użytkowników</div>
    </div>
</section>

The design looks perfect on Chrome but is not rendering correctly on other browsers. The height of the section is being declared using JavaScript.

DEMO (view in more than one browser):

Answer №1

I solved the issue by adding a specified height, eliminating the display table property, and adjusting the margin to auto 0. The height is crucial for the centering technique that involves top: 0, bottom: 0, margin-top: auto, and margin-bottom: auto.

section {
    padding:100px 0;
    position:relative;
}                                             
section > div {
    width:90%;
    max-width:1200px;
    margin: auto 0;
    height: 40px;
    text-align:center;
    position:absolute;
    left:0;right:0;top:0;bottom:0;
}
<section class="users>
    <div>
        <div class="icon"><i class="fa fa-user" aria-hidden="true"></i></div>
        <div class="count">15 000</div>
        <div class="title">użytkowników</div>
    </div>
</section>

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

A step-by-step guide on how to use the Google Closure Compiler: a

Is there anyone who could assist me in adding a snippet to the basic process of Google Closure Compiler? I have been trying unsuccessfully to do this via JavaScript code. I am using an example snippet from the official npm page, but when I run it, someth ...

Guide to snagging a cookie with a Chrome extension

I'm attempting to retrieve a cookie from a specific website using a Chrome extension, but as someone who has never created one before, I'm facing some challenges. Despite reading numerous tutorials, I haven't been successful in capturing the ...

Refresh a single Object Key in React.js

Hey there, I'm currently working on updating book information via a PUT request to my API. My goal is to send only one property at a time, while keeping the rest unchanged. The issue I'm facing is that if I send just one property, the others are ...

I'm looking for an easy way to generate a special effect when my mouse interacts with a div using HTML, CSS, or JavaScript

I'm attempting to replicate this interesting effect where a div is surrounded by a container when the mouse hovers over it. It looks pretty cool, like in this image here: https://i.stack.imgur.com/p0epq.png Does anyone have any suggestions on how I ...

"Enhancing JSON elements on the fly with WSO2's enrich mediator: A step-by-step guide

I attempted to utilize the following configuration: <property name="messageType" scope="axis2" type="STRING" value="application/xml"/> <property expression="get-property('orderSourceSF')& ...

Turn on the text field when the enter key is pressed

I've searched online for solutions, but none of them have resolved my issue. Upon loading my page, I am able to successfully have my JS file select the first textfield. However, I am struggling with getting it to proceed to the next textfield when th ...

Angular Toggle Button on multiple lines

I am looking to incorporate several angular toggle buttons in my project. The number of buttons I need will depend on the data from my database. These buttons are displayed on a sidebar, which means that only a maximum of 3 buttons can be shown in one lin ...

How should dates be formatted in Rails?

Is it possible to customize the default date input in a Rails form? Currently, when I use an HTML form, the date input appears as a drop-down menu with the year range limited from 2008 to 2018. How can I adjust this so that I can enter any year I want whil ...

Encountering a 415 Error while making an ajax call using formData

I am attempting to make an ajax call with formData that includes a list of image files and some strings. However, the call is not successful and I am receiving error 415. Here is the code: var file = picChooser.files[0]; var jobExecutionImagesContext = n ...

Issue: Unable to access GET request with Express and handlebars

Hello everyone, I'm just getting started with JS/Handlebars and I'm facing an issue trying to display an image from my home.hbs file in VS Code. When I start the server, this is the message I receive: https://i.sstatic.net/wUxB7.jpg Below is th ...

I'm attempting to integrate a 3D model into my React website using Three.js, but I'm encountering errors like 'Failed to parse source map' and other Three.js errors in the console

My goal is to display a 3D model on my website using Threejs and react, but I encountered an error. Upon further investigation, it appears to be an issue with the <Model position={[0.025, -0.9, 0]} /> line in the Model3D.js file. The error occurs wh ...

What is the best way to resize the mat-form-field mat-chip?

This is the result of my HTML coding https://i.sstatic.net/2KOvk.png I am trying to achieve uniform size for the mat-chip form field similar to the one above. I referenced this location for using chips. I am looking to reduce the size of the chip in the ...

Using Multiple Submit Buttons in an HTML form with Express.js and Node.js

I'm currently implementing a like/dislike feature with two buttons on the HTML side. <form method="post" name="ratings"> <input type="submit" name="vote" value="like"> <input type="submit" name="vote" value="dislike"> </f ...

Get the Torrent File by Downloading it from the Given URL

Currently, I am diving into the world of node.js and I have a desire to create a simple command-line program that is capable of downloading .torrent files from a given URL. For example, let's consider the following torrent URL: I have managed to wri ...

What is the best way to retrieve an object value within an if statement?

What can I do to ensure that the line within the if statement functions properly? const player1 = { name: "Ashley", color: "purple", isTurn: true, play: function() { if (this.isTrue) { return `this["name"] is current ...

Provide a boolean value of true or false to indicate whether all delete operations were successfully completed

Currently, I am using Sequelize, GraphQL, and Typescript for my coding. Within my database, I have two tables named RecordInformation and OtherDescription. The RecordInformation table contains a foreign key called "OtherID" which references the OtherDescri ...

Rotating Tetris pieces around an axis using HTML5 Canvas

I am currently working on a project and I have encountered a problem. When you press the UP key and hold it down, you will see what I mean: My goal is to make the object rotate around its axis instead of its current behavior. Please take a look at the co ...

Creating a Dynamic Soundtrack: How to Embed an Audio

Success! I finally figured it out, with a little help from everyone. I've been diving into the world of Audio Playlists in HTML and attempted to follow a tutorial on the topic found here: https://www.youtube.com/watch?v=vtZCMTtP-0Y. However, I encoun ...

Broadcast and listen to events in AngularJS using `$rootScope.$emit`

I am currently working on a large Angular app and I have encountered the need to trigger an event using $rootscope.$emit, and then listen to this event inside a factory. Doing this inside a controller is not feasible because the controller hasn't load ...

What is the best way to adjust the background color of Material UI's theme based on different screen sizes?

Having trouble replacing hard-coded CSS breakpoints with Material UI theme settings. @media screen and (min-width: 0px) and (max-width: 400px) { body { background-color: red; } } @media screen and (min-width: 401px) and (max-width: 599px) { body { ...