Tips for positioning a container at the center of the screen

I am having trouble centering text in the middle of my screen. Despite my efforts, it seems like it's not working as expected. Would someone be able to review the following index.html code and point out where I might be going wrong?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .header {
            display: flex;
            background-color: black;
            padding: 2rem;
        }

        .outer-container {
            display: flex;
            flex-direction: column;
            justify-content: center;
            height: 100vh;
        }
  
        .inner-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            cursor: default;
            justify-content: center;
        }
  
        span {
            padding: .5rem;
        }
    </style>
</head>
<body>
    <div class="parent-container">
        <div class="header">Header</div>
        <div class="outer-container">
            <div class="inner-container">
              <h1>HEADING</h1>
              <span>Some text here.</span>
            </div>
        </div>
    </div>
</body>
</html>

The issue appears to be with my understanding that the flex-layout fills all available space. With a "column" direction, I anticipated the outer container to take up the entire height of the screen, but this doesn't seem to be the case.

Update:

To illustrate the problem when setting the outer-container height to 100vh, I have now included a parent-container. The height of 100vh is causing an overflow as the correct height should be 100vh minus the header's height.

Answer №1

insert justify-content: center; within the styles for both divs

Answer №2

Your container is not utilizing full screen height and lacks vertical alignment. For a solution with both vertical and horizontal alignment, you can refer to this codepen https://codepen.io/ignasb/pen/KKBQzjQ. In the code snippet provided, height and alignment css properties have been added:

.outer-container {
  height: 100vh;
  justify-content: center;

  display: flex;
  flex-direction: column;
}

You may also need to include additional styles if a scrollbar appears:

body {
  margin: 0;
}

Answer №3

To quickly achieve the desired result, consider setting the parent container, such as body, .outer-container, or a .wrapper, to fill the entire viewport using height: 100% or 100vh. Then, adjust for the heights of other elements within the same container and utilize the following CSS to center the content:

   display: grid; place-items: center;

snippet

/* Ensure padding/border size are included in element size */
*    { box-sizing: border-box }

body { margin: 0 } /* Remove default spacing that causes overflow */

.parent-container {
    display: flex;
    flex-direction: column;

    height: 100vh; /* May cause overflow if body margin exists */
}

/* Utilizes space from .parent-container */
.header {
    display: flex;
    background-color: black;
    padding: 2rem;
}

.outer-container {
    flex: 1; /* Expands to fill available space */
    display: grid;
    place-items: center; /* Demonstrates easy centering */
}

.inner-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: default;
    justify-content: center;
}

span {
    padding: .5rem;
}
<div class="parent-container">
    <div class="header">Header</div>
    <div class="outer-container">
        <div class="inner-container">
            <h1>HEADING</h1>
            <span>Some text here.</span>
        </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

How can I align text to the right in a navbar using Bootstrap 5?

My current navbar setup is as follows: <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container-fluid"> <div class="collapse navbar-collapse" id="navbarText"> ...

Tips for aligning hyperlinks in the navigation bar using CSS3 and HTML5

Struggling to center the links in the navigation bar has been a challenge. I really want those buttons smack dab in the middle of the header bar, but despite trying everything, they stubbornly remain on the left-hand side. .header { overflow: ...

implementing a time picker feature with jQuery

As I am not familiar with jQuery, I came across this script while searching for a time picker. If anyone could guide me on how to incorporate this code into my HTML page to enable the time picker functionality, I would greatly appreciate it. /* jQuery t ...

Guide on implementing iterative data path in v-for loop using Vue

I'm just starting out with Vue and I want to use image file names like "room1.jpg", "room2.jpg", "room3.jpg" in a loop Below is my code, where the second line seems to be causing an issue <div v-for="(p,i) in products" :key="i"> <img src ...

Having trouble with named anchor link not functioning in Internet Explorer. I have attempted the recommended fixes from Stack Overflow with no

My issue involves using a named anchor tag: <table id="search_results" name="search_results"> When I try to link to it like this: pagename.php#search_results It works in Firefox and Chrome, but not in IE (8). I have explored using an <a> t ...

I am experiencing difficulty getting Bootstrap Columns to appear next to each other despite trying numerous solutions

Check out this code snippet: <div class="mainptext"> <h3><strong>Master the following:</strong></h3> <div class="container"> <div class="row"> <div class ...

Ways to make two blocks of text appear side by side and adapt to different screen sizes

Check out our website imageI'm facing a challenge in making these two text elements appear next to each other rather than stacking on top of each other. Additionally, I need them to adapt responsively to changes in screen size. When 'Icontext&ap ...

Tips for transforming a menu into a dropdown menu

Is there a way to convert the current menu into a Dropdown menu? Currently, the menu is not collapsing and it keeps opening. Any suggestions on how to achieve this? Here is an example for reference: https://i.stack.imgur.com/2X8jT.png ar ...

retrieve the webpage hosted on the server

Seeking to develop a website where users can input a website URL and have it loaded from the server hosting the HTML page, rather than from the user's computer as iframe does by default. I've experimented with various techniques but all seem to l ...

Discovering elements with multiple classes using watir-webdriver

In this scenario, let's consider an element like the one below: <div class="first_class second_class"></div> Now, we can locate it by its classes using the following methods: browser.div(class: 'first_class') browser.div(class ...

What is the best way to iterate through my array and display each value within my button element?

I have a challenge where I'm trying to iterate over an array named topics. This array contains the names of various people. Within my loop, my goal is to extract each name and insert it into a button as text. When I use console.log within my loop, I ...

What steps can I take to ensure my header appears perfectly aligned? (HTML/CSS)

My header on my website is all messed up with the links appearing incorrectly. I suspect it's an issue with style.css, so I attempted to modify the style.css file but had no luck. I have very limited experience with .css and cannot seem to figure out ...

Converting this HTML/PHP code into JavaScript: A step-by-step guide

I have been struggling to convert this code into JavaScript in order to set the document.getElementById().innerHTML and display it in HTML. Can anyone assist with writing this code in JavaScript? <form action='create_new_invoice.php?name="<?php ...

After my data rows are filled, I add jQuery and CSS striping animations to enhance their appearance

Before any click, they have this appearance: Clicking on Sales in the business section reveals a drop-down select box. If I click on "Sales" and modify the text area, it will transition to grey. While the grey rows change accordingly, the black rows shoul ...

Remove an entire anchor tag from a contenteditable div with just one key press

I need help with my HTML code. Here is what I have: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Document</title> </head> <body> <div id="edi ...

Adjust the initial scroll position to - apply overflow-x: scroll - on the specified element

I have an image that can be scrolled to the right on screens that do not fit the full width, but I want the center of the image to be displayed first instead of the left side. Below is my React code: import React, { useEffect, useRef, useState } from &qu ...

Troubleshooting: Sticky navigation menu shifts unexpectedly to the right upon sticking

Whenever the scroll bar on my website at travismorenz.com reaches the sticky menu, it slightly shifts the menu over a few pixels. This issue can be observed by scrolling down slowly and watching the menu. The sticky menu functionality is achieved using th ...

Import JavaScript from a PDF using Java's iTextPDF library

I have successfully created a PDF from Java using the iText PDF library. However, I am facing an issue where I cannot add JavaScript code either from HTML when onload is called or from Java code. I am not receiving any exceptions. What am I doing wrong? Is ...

Having trouble retrieving the image from the /var/www/efs/image/ directory using the html <img src> tag

Currently, I am facing a challenge in my project where I am trying to retrieve an image based on the users ID. The image is stored in the /var/www/efs/image folder. I attempted to specify the directory using the following code: <img src={"/var/www/ ...

Tips for ensuring that the click event function properly for numerous elements sharing the same class

I'm currently working on adding a flip effect to multiple tiles whenever a user clicks on them as part of building a dashboard-style webpage. I am having trouble making the click event work for all tiles with the same class name. Even though all the ...