Align the inner div vertically within the outer div by a percentage of the height

I am facing a challenge with aligning a div of unknown height within a container that has a height set to 100%. My goal is to vertically align the inner div at 30% from the bottom of the outer div.

In a working example I tried, using middle successfully aligns the inner div in the vertical middle of the outer div. However, when I used 30%, it caused the inner div to extend outside the browser window!

Observations on "vertical-align: middle":

Issues with "vertical-align: 30%":

Why did this happen? And more importantly, how can I achieve the desired positioning for the inner div as indicated by the arrow?

Note: It's essential that the solution does not involve explicitly defining a height for the inner div.

Sample code:

<!doctype html>
<html>
<head>
    <style type="text/css">
        /* reset */
        html, body {
            margin:0;
            padding:0;
        }
        html, body, #wrapper {
            height: 100%;
        }
        #wrapper {
            left: 0;
            position: fixed;
            text-align: center;
            top: 0;
            width: 100%;
        }
        #wrapper:before {
            content: "";
            display: inline-block;
            height: 100%;
            margin-right: 0;
            vertical-align: middle; // or 30%  <= change position here
            width: 1px;
        }
        #header {
            display: inline-block;
            position: relative;
            vertical-align: middle;
        }
    </style>
</head>
<body>
    <div id="wrapper">
        <header id="header">
            <h1>Text</h1>
            <p>More text</p>
        </header>
    </div>
</body>
</html>

Answer №1

    #container::before {
        content: "";
        display: block;
        height: 50%;
        margin-right: 10px;
        width: 2px;
    }

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

Enhance the HTML5 Canvas arc by redrawing it upon hover

I have a challenge with my arcs: the first one loads on page-load, the second one loads on mouse-over, and the third one loads on mouse-out. However, I want the mouse-over-out effect to happen repeatedly rather than just once as it currently does. For ref ...

Class for making elements draggable using jQuery UI

Is it possible to use jQueryui's draggable/droppable combo and add a class to the dragged item when dropped into a specific container, rather than adding a class to the container itself? I've tried adding a class to the container, but that is not ...

Mozilla displays the background color even when the visibility is set to hidden

When working with CSS, I've noticed that when I use visibility: hidden on a <td> tag in Mozilla, the background color still shows. To see an example, visit this jsfiddle. It displays fine in Chrome but not in Mozilla. ...

Struggling to stack two forms on top of each other within a Bootstrap row class

I am trying to incorporate 2 forms on a web page, one below the other. Here is what I have: <body id="Signup"> <form class="row col-lg-6"> <legend>Légende</legend> <div class="form-group"> ...

Avoid Refreshing the Page When Pressing the Like Button

I've been working on code for liking a post and saving the value to a database using server-side code. However, I'm running into some issues when the page refreshes after clicking the like button. I tried using event.preventDefault() in my JavaSc ...

Is Toggleclass malfunctioning?

I have been working on making the 'timelineTile' div open and close, as well as making it close if another one is opened or if the background is clicked. I have successfully implemented the functionality to close the div when another one is click ...

What is causing my email form field to be excessively tall with centered text?

I have noticed that the layout of my form field seems to be different than expected. The webpage in question is located at Specifically, the email input field appears to be taller than usual and centered on the page. However, upon reviewing my code, I am ...

Select two out of the three links from a repeating HTML structure using JQuery

Having difficulty creating an efficient jQuery selector to choose 2 out of 3 existing a elements within a repeating HTML structure. Below is the code snippet. The goal is to select Link 1, Link 2, excluding Link 3. Keep in mind that the entire HTML struct ...

What is the method to merge elements of unique and predefined dimensions in HTML?

The code below showcases a 300*300 px box with a footer that adjusts its height based on the contents of the green box. The content in the green box is editable, allowing users to input text and automatically increase the height of the footer (marked in re ...

Choosing a parent element with SASS/SCSS

I'm facing a slight issue with SCSS syntax while creating a small component using the BEM methodology. Here's the HTML: <div class="message message--success"> <div class="message__title"> BEM Example </di ...

Is there a way to potentially utilize window.open() to open a specific section of a webpage?

My HTML page consists of 2 div blocks and 2 links. I want to open the content of the first div in a new window when the first link is clicked, and the content of the second div in another new window when the second link is clicked. You can find the code h ...

Enable the option to customize the icon of the recently activated sidebar menu item

I need help with changing the arrow icon of a menu-item only when it is clicked, without affecting all other menu-items. In the image below, you can see that currently, clicking on any menu-item changes all the arrows. Attached Image //sidebarMenu jQu ...

What is the process for closing the side menu by clicking on the dark area?

I created a basic side navigation menu. When you resize the window to a smaller size, a red square will appear. If you click on this red square, the menu will open. The menu opens correctly, but I want it to close when I click on the dark area instead of ...

Attempting to keep the button centered along the top border of the div

I am struggling to center a button on the top border of a div, even after trying various methods like floats. I need the button to always remain centered and responsive. Here is an example of what I am attempting to achieve. .ThankYouReview { dis ...

Understanding the positioning of HTML elements using CSS

If you have the HTML snippet below: ... <fieldset> <div>above or below</div> <div>content</div> </fieldset> ... Is there a way to use CSS to position the <div>above or below</div> above or below the < ...

Ways to adjust the brightness of any color when hovered over

Is it possible to create a universal effect where an element becomes darker or lighter when hovered over, regardless of the initial color? Here is an example: .change-color{ Background:green; width:100px; height:100px } .change-color:hover{ Background ...

Centering a div and span horizontally with an input child

I am looking for a way to automatically add a # before a hex color code in an input field and center both the div and span containers horizontally: <div id="color_wrapper"> <span>#<input type="text" value="ffffff"></span> & ...

Personalized design for Sweet alert 2

I am trying to customize a sweetAlert2 by defining a className, but it seems that the styling is not applying to the sweet alert. I have tried calling the class name "styleTitle" and various other names, but nothing seems to work. Could the issue be with t ...

Aligning Cards using Bulma Styles

Currently in the process of developing a Pomodoro Timer application and utilizing Bulma as my CSS Framework. I am really enjoying working with it thus far, although still getting the hang of Flexbox. I am seeking advice on the best approach for this projec ...

When a height is specified in percentage, the Tbody element does not display correctly on the entire page

Hey there! Currently, I am working on a fixed header table. My goal is to create a table where the tbody section expands with the screen size and I would like to set the height in percentage (%) but unfortunately it doesn't seem to be functioning as e ...