Guide on centering a div within a div that spans the entire width of the page

There is a div on my page that spans the entire width. Nested inside is another div.

Is there a way to vertically center the child div within its parent, ensuring it stays centered even when the parent div expands or contracts as the page is resized?

Answer №1

To represent the value as a percentage, adjust the left and top to 50%. This will move the top left corner of the div to the center of the parent element. To ensure that the center of the div is also centered, subtract half of the height and width from the margin (for example, if both were 50px, subtract 25px). See the example below:

css:

#main{
    height: 100vh;
    width: 100%;
}

#secondary{
    width: 50px;
    height: 50px;
    top : 50%;
    left: 50%;
    margin-left: -25px;
    margin-top: -25px;
}

For more details, visit my JSFiddle page

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

After applying 'all: unset;' to remove all styles, the CSS hyphens property does not work as expected in Chrome

After applying this CSS code to remove all styles: * { all: unset; display: revert; } The issue arises in Chrome browser where CSS Hyphens are not taking effect. This can be seen with the following example: div { font-size: 3rem; -webkit-hy ...

What is the method for opening the image gallery in a Progressive Web App (PWA)?

I am struggling to figure out how to access the image gallery from a Progressive Web App (PWA). The PWA allows me to take pictures and upload them on a desktop, but when it comes to a mobile device, I can only access the camera to take photos. I have tried ...

How does the <link type=“”> affect your document?

I understand that this tag specifies the file type you are linking to (usually "text/css") for . But what is the reason behind including this information? ...

Tips for adjusting the horizontal spacing of a Bootstrap form

Take a look at the layout of my django filter form: https://i.sstatic.net/HAvcM.png I'm trying to add some horizontal spacing so that 'Name contains' section and the search bar, as well as 'result' are not too close together. He ...

CSS - Unchanging absolute unit across all devices

After reading through this particular inquiry, it became evident that a pixel does not qualify as an absolute unit—a fact that I have validated through practical experience. Despite experimenting with pt, cm, and mm, I have yet to discover a definitive ...

What is the best way to implement slug URLs in Django?

Hello there! I'm a beginner when it comes to working with Django and currently, I am in the process of creating a website. On the admin page located at , I have added two posts - each one having its own unique slug. The first post's slug is &apo ...

Looking to switch up the thumbs-up button design?

I am a beginner in using jquery and ajax, and I need some assistance. How can I incorporate this jquery code into my logic: $('.btn-likes').on('click', function() { $(this).toggleClass('liked'); }); Can so ...

Employing PHP for the purpose of updating the content within a <div> element solely when the submit button of a <form> is clicked

I have a unique issue with my website structure. I have an 'index' page that dynamically replaces the content of 'mainContents' when any button in the navigation bar is clicked. The purpose of this design is to reload only the necessar ...

A clever CSS hack for showing multiple UL lists with LI elements in different columns, based on the width of the webpage

On my HTML game website, I feature multiple UL lists at the bottom of each page: <h2>How to play?</h2> <ul> <li><a href="/en/ws/google">Via Google</a></li> <li><a href="/en/ws/apple ...

Updating the font style in MUI with React 18

I attempted to create a new theme in order to replace the default font family of MUI but unfortunately, it was not successful. This is what my theme component looks like: import { createTheme } from "@mui/material/styles"; const theme = createT ...

Steps to create a submit button that is linked to a URL and includes an image

I'm attempting to convert my submit button into an image that, when clicked, redirects to another page. Currently, the submit button is functional but lacks the desired image and href functionality. <input type="submit" name="submit" alt="add" o ...

Scrolling to the next wrap class in jQuery is not functioning properly with the chatbox and ScrollTop

I have created a chat box using jQuery. However, I am facing an issue where when I click the button multiple times, the scroll returns to the top. My objective is for the scroll to move to the next wrap class when the button is clicked. Here is a snippet ...

Looking to adjust the fill pattern dynamically

I previously implemented this code: Is there a way to modify the fill image on my 3 buttons to display in 3 distinct colors instead? ...

What is the best way to create a cornered button using CSS?

Is there a way to create a button using only CSS? https://i.stack.imgur.com/7mjVV.png This is the code I've come up with so far: .customButton { width: 100px; height: 100px; background: #6d1a3e; position: relative; transform: rotate(-90 ...

Make sure to validate a form when submitting from an external source rather than through an HTML

In my form, there are various input fields (some acting as inputs even if they're not). Each field is validated upon submission by angular validation and html attributes like required, ng-maxlength, minlength, etc. Now, we want to implement a keyboar ...

Is there anyone available to assist me with troubleshooting a login session that is not functioning properly

I'm a beginner in php and I'm facing an issue with logging in as a different user in a project. After logging out and returning to the browser, the previous user's window keeps opening. Below is the code for index.php: <?php if ( ...

Is there a way to use ajax to dynamically monitor the presence of a file in real-time?

Is there a way to automatically observe the status of a file using ajax without having to manually refresh it with a button? ...

Can content projection be utilized from a child component in Angular?

Keep in mind, this example could be achieved without using content projection. I am just showing a simplified version here. Imagine having a component that displays lists of names in two separate elements: import { Component } from '@angular/core&ap ...

When attempting to execute a function when a hidden div is not displayed, JQuery encounters an

I'm currently attempting to modify the text color of the h2 title with the id ticketSearchTitle when the ticketSearch div is not visible. However, the code I have been using to achieve this seems to cause issues (such as the absence of a pointer icon ...

Create a customized HTML popup featuring various packages

I am struggling to create an HTML popup within a React component due to some errors Here is the code snippet: <div> <button class="toggle">Button</button> <div id="link-box"> <ul> ...