a div sandwiched between two others

I am trying to nest a <div> within another <div> and center it within the parent. Here is my current approach:

<div id="redthing">
    <div id="whitebox" >
    </div>
</div>

The CSS code I am using is as follows:

#redthing {
    margin-top: 2px;
    background-color: #f00d28;
    height: 250px;
}

#whitebox {
    margin: 0 auto;
    margin-top: 10px;
    padding-top: 20px;
    height: 10px;
    width: 400px;
    background-color: white;
    border:5px solid #000;
}

As you can see, I am facing difficulty in centering the whitebox vertically within the redbox. The current setup leaves equal space between the top and bottom of the red box and the white box. Can anyone provide guidance on how to achieve vertical centering? Thank you.

Answer №1

Let's analyze the elements in your design. Each line depicted represents a height of 10px:

┏━━━━━━━━━━━━━━━━━━━━━┓
┃10px margin top      ┃
┃┌───────────────────┐┃
┃│20px padding top   │┃
┃│padding continues  │┃
┃│10px height        │┃
└─────────────────────┘
┊   lots of empty space  
┗━━━━━━━━━━━━━━━━━━━━━┛

Can you clarify how this layout is intended to function?

You must ensure that the total of your padding and margin is accurate or implement the following solution:

/* add this to the container */
position: relative;

/* add this to the inner box */
position: absolute;
top: 50%;
margin-top: -Xpx;
/* where X represents half the offsetHeight of the box - for example, (height+padding+border)/2 */

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

Exploring the differences between React state and CSS :hover in the context of a dropdown menu that is accessible to both desktop users (via mouse) and

I have come across a dilemma regarding a dropdown menu that needs to cater to both Desktop/PC users (with mouse) and Mobile devices (with touch). After considering my options, here are the proposed solutions: OPTION 1 One approach is to implement it usi ...

Python: Fails to reach the second iteration in the For loop

I need Python to iterate through multiple pages by incrementing the page number in the URL. # Using get request on the site from bs4 import BeautifulSoup import requests from warnings import warn response1 = requests.get('https://lasvegas.craigslist ...

Unable to display imported image in React application

I have a component where I want to display an image: import React from 'react' import background from '../media/content-background.jpg' function ContentPage(){ return( <div id="content-page"> < ...

` `Spinning graphics and written content``

I am looking to create a dynamic element on my website where an image and corresponding text block rotate every few seconds. An example of what I am envisioning can be seen on this website: While I know how to implement a javascript for rotating images, I ...

CSS descendant selector add style excluding child elements

My issue involves two divs each containing a table. When I apply the CSS class "table" to the first div, the second table in the div is impacted as well. .tbl-generic th:nth-child(-n+2),td:nth-child(-n+2) { background-color:red; width:25px; } ...

Control your thumbnails with the powerful iDangerous Swiper feature

Are you new to coding? I'm looking for help on linking thumbnail images to a swiper so that when a thumbnail is clicked, it moves the swiper-container to the corresponding slide. Any ideas or suggestions would be greatly appreciated! Here's an e ...

Is there a method or API available for interacting with an <object> that is currently displaying a video?

Yay, I figured it out! I've been using video.js to play videos on a website that needs to work offline too. Unfortunately, using flash is not an option due to compatibility issues. So, I decided to write my own fallback solution. For Internet Explor ...

The :before and :after pseudo classes are not displaying properly on the video element

I'm trying to display a title over a video element while it's playing, but I've run into an issue. When using the <video> tag, it doesn't work as expected, however, when I switch it to a <div>, everything works perfectly fin ...

jQuery fails to modify HTML according to its intended purpose

I've been struggling to update a price using JQuery. Even though the code seems fine when I check the console, the HTML doesn't reflect the changes. Additionally, when I try to log console.log(newPrc), it gives an error saying "newPrc" is not def ...

Button on aspx page not functioning properly when clicked

I seem to be experiencing an issue with buttons. To check if the function is being triggered, I used alert("") and found that it does enter the function when the button is clicked. However, after the alert, any other code inside the function seems to not w ...

The JQuery duplication process did not function as anticipated

This question builds on a previous one related to jQuery cloning and incrementing IDs. The issue is that when adding new elements, the IDs are not being generated in the correct sequence. For example, if the initial ID is expy-1, clicking "add more" should ...

Refresh a different Angular Controller after submitting a POST request

Just to clarify, I am looking to dynamically reload another controller with new data after a POST request without refreshing the page. Here is my code: No issues with saving data to the database. Script var app = angular.module('userBase', []) ...

Automatically selecting the country phone code based on the country dropdown selection

When the country dropdown is changed, I want the corresponding phone code dropdown to be dynamically loaded. <div class="row"> <div class="col-sm-8 col-md-7 col-lg-6 col-xl-5 pr-0"> <div class="form-group py-2"> <l ...

What is the best strategy to prevent reflow and flickering when loading images on a website?

My website displays an image on the left with text on the right, but when the server is busy or the connection is slow, the page flickers. It initially shows the text on the left and then suddenly moves it to the right once the image loads. This issue see ...

Adjust the top spacing in relation to the paragraph tag using jQuery

I wish to create multiple div elements with nested p tags that are initially hidden. Upon hovering over each div, I want the corresponding p tag to move up by the negative value of its own height. For instance: The first p tag has a height of 60px and a m ...

Finding an element that lacks both a class and an id, and is not consistently present - what's the trick?

Currently, I am faced with a predicament in my code where a <li> element only appears under specific conditions, making it difficult to apply positioning. This element lacks an id and class attribute, which prevents me from targeting it accurately us ...

What steps can I take to make my animation work in the opposite direction as well?

I'm currently working with an angular slider that is set to TRUE/OPEN by default. The issue I am facing is that while I am able to slide it using angular animations in one direction, I am unable to see the transition when sliding it back. Any assistan ...

Leverage jquery to adjust the height or width of an element depending on which dimension is larger

I'm a beginner in jquery and html and am currently developing a gallery webpage. The functionality involves setting the selected image as the page background and then scrolling vertically based on the mouse pointer's position. This works well for ...

Interactive jQuery feature for dynamic search suggestions across multiple entries

Here is the HTML code snippet I am working with: <input name="data[Content][0][name]" type="text" class="content_name_1" id="content_name"> <input name="data[Content][1][name]" type="text" class="content_name_2" id="content_name"> ...

Lower the transparency of a container without affecting the transparency of its elements

Is there a way to decrease the transparency of the page contents container background without affecting the opacity of the actual content? <div id="container"> <div id="page contents"> Insert your amazing articles and more here. </ ...