What is the best way to vertically center these two boxes within the confines of this red box?

Is there a way to vertically align the white and green boxes inside the red box? I'm aiming for a layout similar to the one shown in the image below. Your help is greatly appreciated! Check out this demo on jsfiddle.

<style>
    *{
        padding:0;
        margin:0;
    }
    .box{
        width:600px;
        height:600px;
        position: absolute;
        margin:auto;
        left:0;
        right:0;
        bottom:0;
        top:0;
        background: red;
    }

    .box2{
        margin:0 auto;
        background: green;
        width:100px;
        height:100px;
    }

    .box3{
        margin:0 auto;
        background: white;
        width:100px;
        height:100px;
    }
    .whole-background{
        position: absolute;
        width:100%;
        height:100%;
        background: rgba(0,0,0,.5);
    }
</style>
    <div class="whole-background"></div>

    <div class="box">
        <div class="box2"></div>
        <div class="box3"></div>
    </div>  

Answer №1

To achieve the desired layout, utilize the red box as a table-cell. Both smaller boxes should be shown using inline-block.

For horizontal alignment, set text-align: center; and for vertical alignment, use vertical-align: middle;

View the end result here: http://jsfiddle.net/JxDn5/2/

*{
    padding:0;
    margin:0;
}
.box{
    width:100px;
    height:600px;
    margin:auto;
    background: red;
display: table-cell;
vertical-align: middle;
text-align: center;
padding: 0 250px;
}

.box2{
    margin:0 auto;
    background: green;
    width:100px;
    height:100px;
display: inline-block;
vertical-align: middle;
}

.box3{
    margin:0 auto;
    background: white;
    width:100px;
    height:100px;
display: inline-block;
vertical-align: middle;
}
.whole-background{
    position: absolute;
    width:100%;
    height:100%;
    background: rgba(0,0,0,.5);
}

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

Every file downloaded through the iframe is automatically stored in a designated server folder

Is it possible for my website to have an iframe that enables users to browse the web, and when they click on a download button on any external website, the file can be saved directly to a specific folder on my server instead of their own computer? I'm ...

Each time I attempt to alter the color based on the count, an error is thrown: "TypeError: Cannot read property 'style' of null"

If the count is equal to zero, I want to change the color to black in the code below: if(Count === 0){ document.getElementById('value').style.color = 'black'; } return( <div className='container_new'> < ...

The jQuery scroll function is not working for the span class I specified. What could be causing this issue?

I attempted to implement a scroll-to plugin to move between elements on a page when clicking previous and next buttons. The source demo works differently from my modified version, and I am struggling to figure out where I went wrong. Source demo: My Curr ...

What are the best ways to create a responsive login form?

After recently beginning to learn HTML and CSS, I have encountered some confusion. Take a look at this table design for screens under 500px, and then compare it to how it should appear on screens 500px or larger. I believe utilizing a media query may be t ...

Explanation of URL in jQuery Data Template

Below is the HTML code found in a jQuery Data Template: <a class="comment" style = "text-decoration:underline" href="ItemTitle/${Application.ApplicationId}/${Application.ApplicationName}/${DomainObjectId}/${DomainObjectPropertyNameFormatted}">${Doma ...

Override the default Bootstrap 5 styling for nav-links

I recently encountered a problem while trying to customize the appearance of the nav-link elements in my Bootstrap 5 navbar. I initially thought I could simply override the existing styles by targeting the class in my CSS. However, my attempts were unsucce ...

Is there a way for me to adjust the image dimensions so that it doesn't surpass the width of its parent container?

When working with images, it can be tricky to set the original width while also ensuring it fits within a parent container. For example, if the parent container has a width of 1000px, you may want the image to have a max-width of 100%, but not exceed 1000p ...

Issue with JavaScript HTML Section Changer not functioning properly

I need to implement a button that switches between two pages when pressed. Although the code seems simple, I am struggling to make it work. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"& ...

Showing various pictures using numerous buttons in JavaScript

Looking for some assistance with my JavaScript project. I am new to coding and trying to create a program that displays different images when buttons are clicked. Can anyone lend a hand? Here's the code I have so far: <html> <body> < ...

What is the best way to halt a CSS transition using JavaScript when dealing with an image?

I am facing an issue while attempting to create a basic CSS transition using JavaScript. The goal is for the start button to trigger the movement of an element, based on the duration of the keyframe animation. Then, clicking the end button should make the ...

Scroll the div back to the top when the form is submitted

I have set up a form in a pop-up using Bootstrap modal. The form is quite long, so after submission, the message appears at the top of the form. However, I want it to scroll to the top when the user submits the form so they can easily see the message. Is ...

What's causing my button to line up horizontally within a flexbox that is positioned vertically?

My button is aligned within the flex-box, shouldn't the elements be stacked in a column? I am working on a timer that features a row with two columns. One column contains an image while the other consists of a heading, a flexbox with four rectangles ...

What is the best way to darken the background when an alert is displayed and disable the dimming effect when the alert is closed?

Here's the JavaScript snippet I'm using: reset.addEventListener("click", function(){ document.querySelector("#body").classList.add("darkenPage"); myReset(); alert("Reset Successful!!"); document.querySelector("#body").classList.re ...

Parent component in Material-ui theme distinguishing itself from child component of the same type

I'm working on customizing the styling of a Material UI Expansion Panel, specifically to apply my theme only to the main parent panel when it is expanded, and not to any nested panels within it. For instance: https://i.sstatic.net/nBhcN.png Here&ap ...

Utilize overlib text in your jQuery projects

As I develop my personal Grease Monkey userscript, I find myself in need of modifying the overlib text through the use of jQuery. Here's a representation of it in HTML code: <div class="expbar" onmouseover="return overlib('Some text',HA ...

Siblings mousedown event propagation

In my HTML code, I have a division that contains multiple image objects with the position set to absolute. Here is an example: <div> <img> <img> <img> <img> </div> The problem arises when the ...

What causes the size of text to change when I zoom in my browser?

As I work on my website, I am facing a challenge with maintaining consistent text size as the page scales. For example: p { width: 10%; height: 10%; background-color: rgb(0,0,0); top: 50%; left: 50%; position: fixed; color: rgb(255,255,25 ...

Tips for customizing a dropdown menu where the first item is not the label and may include an input field

How can I create a dropdown box that includes a label of "select so and so..." and when the user clicks on it, a list of options is displayed with the first option being blank instead of showing the label name? Is it possible to include an input box insi ...

Creating a table with multiple rows condensed into a single row (Using Material-UI/CSS)

Here is the array structure I am working with: const sample = [ { name: 'apple', detail: [{'a', 'b'}]}, { name: 'banana', detail: [{'a', 'b'}]}, ]; In order to create a table similar to the ...

To submit the form, users must check off multiple checkboxes using JavaScript conditional statements

Currently, I am working on creating a form that requires multiple fields like name and email. While I have been successful in sorting out those fields, I am facing a challenge with the checkboxes. I am trying to figure out how to make it mandatory for two ...