Achieving Vertical Center Alignment in CSS for Dynamic Div Elements

I am currently working on vertically centering a div called container. It was successfully centered, but now I am using jQuery to add more divs to it like so:

$("#add").on("click", function () {
    $(".elements").append("<div class='element'>World</div>");
});

If you want to see the issue in action, check out this JSFiddle: http://jsfiddle.net/CDk4E/

Here's the problem I am facing:

Initially, when no divs are added, everything looks fine (the page remains vertically centered). However, as soon as new divs are added, the .element div containing "Hello" moves to the top. Ideally, I would like it to stay in its original position and just have new .element divs added to the bottom.

EDIT: Additionally, the size of the first element is constant.

Answer №1

Consider adding padding:50%; to the CSS rule for your .container element

<style>
    html, body {
    height: 100%;
}
.wrapper {
    height: 100%;
    width: 500px;
    margin:0 auto;
    display: table;
}
.container {
    display: table-cell;
    height: 100%;
    padding-top:50%;
    border:1px red dashed;
    position:relative;
}

.element{
    background:purple;
    color:#FFF;
    width:100%;
    padding:10px;
}

</style>

Check out the result here: http://jsfiddle.net/CDk4E/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

Using jQuery to Extract Content from Frames (excluding iframes)

My website originally had jQuery functioning fine with a header, sidebar, and main content. However, my manager made the decision to convert these elements into separate frames using the <frame> tag instead of iframes, causing numerous issues. One i ...

We could not locate the requested resource with a DELETE request using the fetch JSON method

Currently, I am in the process of developing a webpage that utilizes JSON API REST alongside XAMPP with an Apache server. Up until now, everything has been working smoothly as I have been utilizing the DELETE method successfully. However, I seem to have hi ...

Elevated structuring: Encompassing ngRepeated elements within Angular.js

You are working with an array of objects and need to apply a specific structure to every set of elements during iteration If you want: Each (element) to wrap around every 4 consecutive ng-repeated elements wrap ng-repeated-element ng-repeated-elem ...

Using a Get request may not retrieve the most recent data that was added through a Post request

I've added some data using a Post request and now I'm trying to retrieve all the data from the table using a Get request. However, it seems that the last data I added is not being included in the response. Is there a specific approach I should ta ...

MongoDB failing to display Mongoose model fields

Currently, I am immersed in a personal project where I am constructing a web application and diving into the world of node.js. The aspect I am currently focused on involves the uploading of a CSV file, converting the CSV data into a JSON object, and storin ...

Switching from PHP to JavaScript before returning to PHP to establish and manage sessions

Currently, I am in the process of resolving an issue I am facing. The problem arises when utilizing the following code for someone trying to sign into the admin panel: <script> function myFunction() { //alert('you can type now, end ...

Tips for creating a mobile-friendly responsive table

I need help with making my table responsive in mobile view. Can someone provide guidance on how to achieve this? Feel free to ask if you have any questions related to my issue. tabel.html I am currently using the Bootstrap framework to create the table ...

The regular expression should consist of just letters and a single dot

Currently, I am working on creating a regular expression that allows only letters and one dot. However, there is a specific rule to follow: the dot cannot be located at the beginning or end of the string. For example: abc.difference This is what I attem ...

"Error occurs when attempting to insert indexes into a JSON array, causing the Dat

I am facing an issue with my datatable setup. I have a php array that is converted to a JSON array and populates the table successfully. However, when I try to add calculated fields to the table using PHP, it seems like Datatables is unable to read them pr ...

The Jquery Datepicker U.I is malfunctioning and does not register selected values when the user only picks

I've successfully implemented the Jquery Datepicker to collect dates from users. However, I'm facing an issue where I need to double-click in order for the date value to be captured and displayed in the text box. I've been attempting to uti ...

"Learn the trick to easily switch between showing and hiding passwords for multiple fields in a React application

I am looking to implement a feature that toggles the visibility of passwords in input fields. It currently works for a single input field, but I am unsure how to extend this functionality to multiple input fields. My goal is to enable the show/hide passwo ...

What is the best way to align text above a wrapper box?

After watching a tutorial on YouTube about creating a simple login form, I decided to customize it with some personal touches. I wanted to include a "Welcome" text above the login form, but when using h1, the text appeared next to the box rather than abov ...

Guide on displaying an element depending on the chosen value from a dropdown list using AngularJS

Below is the HTML code I am working on: <select ng-model='selectedtype' ng-options='item.tagname for item in types.name'> <option value="">select type</option> </select> <p ng-show = 'selectedtype.t ...

What are some ways to improve the performance of my CSS and HTML webpage?

I have been diligently working on an HTML/CSS website to enhance my skills, but I am feeling uncertain about the excessive use of floats and sizes in my design. I also find myself in need of assistance with some CSS elements. Here's what I have: Wha ...

Using PHP, jQuery, and Ajax to submit a form and display results on the same page

I am in need of the following functionality: When a user submits a form by clicking on index.php, the input from the form should be processed by an external PHP file (search.php) and then the results should be displayed on the original page (index.php) wi ...

Save the XML file and input the value (XMLHttpRequest is unable to load the file) - On the Client Side

Important Note: All operations are performed client-side I am attempting to upload an XML file and display the values in a text input field. I have been using AJAX to perform this post operation. While it works smoothly on Firefox, there seems to be a pro ...

Combining multiple API requests using NodeJS

I'm currently experimenting with SteamAPI to enhance my understanding of NodeJS. My aim is to retrieve game information after making an initial request to obtain the player's profile and storing the game IDs in an array. However, I am facing a ch ...

Sending data with AJAX to an ASMX web service

Struggling with uploading files to an existing ASMX service in C#. Our Silverlight app works fine with the service, but our new AJAX-powered app is unable to leverage it. Taking advice from various sources: .NET MVC deserialize byte array from JSON Ui ...

Trouble with jsFiddle: jQuery not functioning properly despite identical code

I am currently facing an issue with my jQuery code, Here is the script that generates a list through PHP and then makes each item clickable to display sub contents of a folder. You can view a working example on jsfiddle: here However, when I try to run i ...

Passing a variable from a jQuery function to a submitted form: the ultimate guide

I stumbled upon this code snippet online and it's exactly what I needed to learn from as a beginner using jQuery. However, I have a question about transferring or passing the value of counter that was used in the jQuery script within the HTML head tag ...