Guide on aligning a non-fixed child within a fixed parent container

I am working on a project with a fixed container (div#popup) that has some CSS styling. The container is set to position:fixed, with specific dimensions, border radius, and background color.

Within this fixed container, there is a form element that I am having trouble centering. Despite trying to use margin: auto in the CSS, the form still sticks to the top of the container rather than being centered.

I'm sure there is a straightforward solution to this issue, but I can't seem to figure it out. Any advice or suggestions would be greatly appreciated. Thank you!

Answer №1

To center the content vertically in a container, you can set the container to have display: flex, flex-direction: column, and use justify-content: center;

#popup {
        height: 60vh;
        width: 60vw;

        position: fixed;
        left: 20vw;
        top: 20vh;
        border-radius: 8%;
        background-color: rgb(241, 237, 231);
        display: flex;
        flex-direction: column;
        justify-content: center;
    }
    
    #popup form {
        position: static;
        margin: auto;
    }
<div id='popup'>
                <form action='emailform.php' method="POST">
                    <input type="text" name='name' placeholder="Name: "/>
                    <input type="text" name='mail' placeholder="Your Email: "/>
                    <input type="text" name='subject' placeholder="Subject: "/>
                    <textarea name='message' placeholder="Message: "></textarea>
                    <button type='submit' name="submit">Submit</button>
                </form>
     </div>

If you also want to horizontally center the form within the container, you can set the container's direction to row:

#popup {
        height: 60vh;
        width: 60vw;

        position: fixed;
        left: 20vw;
        top: 20vh;
        border-radius: 8%;
        background-color: rgb(241, 237, 231);
        display: flex;
        flex-direction: row;
        justify-content: center;

        
    }
    
    #popup form {
        position: static;
        margin: auto;
        display: flex;
        flex-direction: column;
        justify-content: center;
        
    }
<div id='popup'>
                <form action='emailform.php' method="POST">
                    <input type="text" name='name' placeholder="Name: "/>
                    <input type="text" name='mail' placeholder="Your Email: "/>
                    <input type="text" name='subject' placeholder="Subject: "/>
                    <textarea name='message' placeholder="Message: "></textarea>
                    <button type='submit' name="submit">Submit</button>
                </form>
                <form action='emailform.php' method="POST">
                    <input type="text" name='name' placeholder="Name: "/>
                    <input type="text" name='mail' placeholder="Your Email: "/>
                    <input type="text" name='subject' placeholder="Subject: "/>
                    <textarea name='message' placeholder="Message: "></textarea>
                    <button type='submit' name="submit">Submit</button>
                </form>
     </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

The HTML input element does not comply with the specified pattern

My current struggle lies in validating the input field for a "Phone Number". Despite creating a regex pattern that demands 10 numbers, I still encounter the error message: "Please match the requested format". I am puzzled as to why this is happening, even ...

React Image Slider not loading pictures

Can someone help me troubleshoot why my images are not displaying in the slider on my homepage? I have set up the slider using React Slideshow Image, but the images are not showing up for some reason. I am also wondering if anyone knows of any full-screen ...

HTML border width is set to 100%, but there seems to be an issue with the responsive menu border and icon display

One issue I'm encountering with my website involves the border at the bottom of my navigation bar. Despite trying to adjust the box-sizing property to border-box, I still can't get it to display correctly. My goal is to have the border span 100% ...

The intervals in hooks seem to be malfunctioning and not updating the date and time as expected

I am trying to continuously update the date and time every minute using React hooks. However, I am facing difficulty in updating the time properly. const Link = (props) => { let date = new Date(); const [dateTime, setDateTime] = useState({ cu ...

Juggling PHP scripts within javascript

Can you help me with a question I have? I am working on multiple JS scripts, such as this one: <script> $('#mapveto1').click(function() { $('#mapveto1').addClass('banned'); $('#map1').text('cobble ...

Stylish Material Design Icons

Our website utilizes CSS classes to display icons across various pages. .alert { /* used for "alert" type messages */ background: url(images/alerts.png) no-repeat left top; padding: 5px 0 15px 35px; margin: 0; } Use: <p id="ctl00_ctl00_ContentMessagePa ...

Responsive Video Embed Using Bootstrap-5

I'm facing some challenges with Bootstrap responsive video embedding. It seems like the parent container is not responding to changes, only the content within the <iframe> tag is being responsive. Any idea what might be going wrong? .embed- ...

Having trouble displaying API JSON data in a Vue.js table component

In my HTML file, I have implemented fetching data from an API and displaying it in a table successfully. Now, I am trying to convert the HTML file to use Vue.js, but encountering some issues. Despite being able to fetch data from the API with Vue, the tab ...

What is the technique for creating a repeating image rather than stretching it?

Is there a way to repeat a background image to fit its content? I have a background image that needs to be repeated in this manner: https://i.sstatic.net/qVKkp.png However, when using the following code: .imgbord { background: url('https://i.imgur ...

Tips for displaying just the image name without the entire image file path

When I try to upload an image, the path displayed is "C:\fakepath\promotion1.JPG". This fake path is causing the image not to upload to my project media folder. How can I solve this issue? Instead of the complete path, I only want to capture the ...

Superceding Meta Character Encoding Statements

Have you ever wondered what happens when two older-style character encoding declarations are used in a webpage? Will the first declaration override the second, or vice versa? Or will they both be ignored and excluded from the encoding algorithm? For Ex ...

Is it possible to incorporate Excel files into a web-based system that only supports HTML?

Currently, I am working on an HTML-based system where my supervisor has specified that only HTML can be used to keep things easy and simple. My question is: Can Excel files be added to the HTML-based system, and if so, is it possible to work with, save, a ...

Leveraging jQuery for Adding Text to a Span While Hovering and Animating it to Slide from Left to

<p class="site-description">Eating cookies is <span class="description-addition"></span>a delight</p> <script> var phrases = new Array('a sweet experience', 'so delicious', 'the best treat', ...

What is the method to obtain the coordinates based on a city name, and subsequently store those coordinates in a variable?

I'm currently exploring ways to extract the geographical coordinates based on user-provided city names. I am aware that Google offers this functionality through their API, but I am struggling with how to actually capture and store these results in PHP ...

New and improved Bootstrap 5 menu bar

I am seeking to customize my navigation bar by adding a black background that spans its entire length. I do not want to rearrange the links or logo; just apply a background color. Another issue arises during SASS compilation: I am trying to change the col ...

I'm seeking answers on selenium and the proper use of CSS selectors, as well as troubleshooting the error message 'selenium.common.exceptions.ElementClickInterceptedException'

After some research, I think I have a grasp on CSS selectors and their appearance. However, when using selenium, how can I locate a CSS selector on a website and then click on it? The correct syntax eludes me. I encountered this error as well: selenium.co ...

Creating and deleting HTML elements in a dynamic array format

My current approach involves utilizing jQuery and JavaScript for the purpose of dynamically adding and removing HTML elements. Specifically, I am focusing on the removal of particular HTML elements. The code snippet is as follows: $(document).ready(fun ...

Moving Angularjs table rows to another tableMoving table row from one Angular

I need a solution for transferring rows from one table to another. Here are the two tables involved: First Table: <table> <tr ng-repeat="row in items"> <td>{{row.PRODUCTDESCRIPTION}}</td> <td><inpu ...

Unlock the power of nested dynamic content creation with JavaScript

Every time I attempt to use getelementbyid on a dynamically loaded div, I receive null as the result. This occurs even after trying both window.onload = function () { and $(window).load(function() { index.html: <main > <div id="main-div"> ...

How come I can't see this on my display?

I'm facing an issue with this particular HTML snippet not displaying when I view the file in Chrome or any other browser. Below is the code: <!DOCTYPE html> <html> <head> <title> # <title& ...