Tips for Aligning a Collection of Relative Positioned Divs within a Parent Relative Positioned Div

Struggling to dynamically center a group of relative positioned divs within a parent div that is also relative positioned.

The parent div has a width of 620px, while the child divs are each 200px wide. There could be 1 to 3 child divs per line, which is why I am attempting to center the group of child divs within the parent div dynamically. For instance, if there is only one child div, it should be centered in the parent div, or if there are two child divs, they should both be centered.

I initially considered using inline-block for the child divs, but complications arose from the absolute positioning of other divs inside them, rendering inline-block ineffective.

This is my current css implementation, and a working example can be seen here: https://jsfiddle.net/y3ghpkvs/

.parentClass {
    position: relative;
    width: 620px;
    height: 500px;
    background-color: gray;
}

.childClass {
    position: relative;
    width: 200px;
    height: 400px;
    float: left;
    margin-left: 5px;
    background-color: white;
}

.insideChildDiv1 {
    position: absolute;
    width: 100%;
    height: 95px;
    top: 0;
    left: 0;
    background-color: black;
    color: white;
    text-align: center;
    line-height: 100px;
}

.insideChildDiv2 {
    position: absolute;
    width: 100%;
    height: 200px;
    top: 100px;
    left: 0;
    background-color: green;
}

.insideChildDiv3 {
    position: absolute;
    width: 100%;
    height: 95px;
    bottom: 0;
    left: 0;
    color: white;
    text-align: center;
    line-height: 100px;
    background-color: black;
}

If anyone has tips on how to properly center the 2 childClass divs inside the parentClass div, please share! Thanks!

Answer №1

Method 1: Implementing Flexbox

Consider utilizing flex CSS. By using flex, you can easily align items vertically or horizontally to the parent container.

Utilize justify-content: center; to centrally align the divs.

Revised Fiddle Link

.parentContainer {
  width: 620px;
  background-color: gray;
  display: flex;
  justify-content: center;
  margin: auto;
  flex-wrap: wrap;
}

.childElement {
  width: 200px;
  height: 400px;
  background-color: white;
  position: relative;
  margin: 3px;
}

.innerChildDiv1 {
  position: absolute;
  width: 100%;
  height: 95px;
  top: 0;
  left: 0;
  background-color: black;
  color: white;
  text-align: center;
  line-height: 100px;
}

.innerChildDiv2 {
  position: absolute;
  width: 100%;
  height: 200px;
  top: 100px;
  left: 0;
  background-color: green;
}

.innerChildDiv3 {
  position: absolute;
  width: 100%;
  height: 95px;
  bottom: 0;
  left: 0;
  color: white;
  text-align: center;
  line-height: 100px;
  background-color: black;
}
<div class="parentContainer">
  <div class="childElement">
    <div class="innerChildDiv1">George</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">CEO</div>
  </div>
  <div class="childElement">
    <div class="innerChildDiv1">John</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">Vice President</div>
  </div>
  <div class="childElement">
    <div class="innerChildDiv1">John</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">Vice President</div>
  </div>
  <div class="childElement">
    <div class="innerChildDiv1">John</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">Vice President</div>
  </div>
</div>

Method 2: Utilizing Inline-Block Display

.parentContainer {
  width: 620px;
  background-color: gray;
  margin: auto;
  text-align: center;
}

.childElement {
  width: 200px;
  height: 400px;
  background-color: white;
  position: relative;
  margin: 4px 0;
  display: inline-block;
  margin-left: 2px;
}

.innerChildDiv1 {
  position: absolute;
  width: 100%;
  height: 95px;
  top: 0;
  left: 0;
  background-color: black;
  color: white;
  text-align: center;
  line-height: 100px;
}

.innerChildDiv2 {
  position: absolute;
  width: 100%;
  height: 200px;
  top: 100px;
  left: 0;
  background-color: green;
}

.innerChildDiv3 {
  position: absolute;
  width: 100%;
  height: 95px;
  bottom: 0;
  left: 0;
  color: white;
  text-align: center;
  line-height: 100px;
  background-color: black;
}
<div class="parentContainer">
  <div class="childElement">
    <div class="innerChildDiv1">George</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">CEO</div>
  </div>
  <div class="childElement">
    <div class="innerChildDiv1">John</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">Vice President</div>
  </div>
  <div class="childElement">
    <div class="innerChildDiv1">John</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">Vice President</div>
  </div>
  <div class="childElement">
    <div class="innerChildDiv1">John</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">Vice President</div>
  </div>
  <div class="childElement">
    <div class="innerChildDiv1">John</div>
    <div class="innerChildDiv2"></div>
    <div class="innerChildDiv3">Vice President</div>
  </div>
</div>

I trust this information proves useful for your project!

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 and Ajax to fade in content after all images and other assets have finished loading

I've encountered an issue with loading pages via ajax for users with custom URLs. For example, a profile is usually found at http://example.com/users/Dan, but if a user has a custom URL like http://example.com/DansCustomURL, I need to fetch their desi ...

Obtaining the weight of a webpage using Selenium Automation_Framework

Currently, I am in the process of creating performance-related tools for web pages within a Selenium framework designed for .NET systems. With a focus on page performance, I am specifically analyzing page load time and page weight. In order to measure th ...

Execute operations on element itself rather than the output of document.getElementbyId

I encountered an issue involving an HTML element with the ID of BaseGridView. When I call a function directly on this element, everything works perfectly. However, if I use document.getElementById() to retrieve the element and then call the function, it do ...

Tips for navigating a dynamic viewport using scroll movement

Attempting to create a responsive page with two distinct sections at this example link including: Map View Table View Both of these views (table and map divs) need to be responsive without a hard-coded height, so the size of the map div adjusts automatic ...

Using Bootstrap's card component, design a grid layout for your website

I am attempting to construct a grid using the Bootstrap 4 card component. After reviewing the documentation and utilizing the card-deck feature, I aim to have each row consist of two columns with the behavior similar to col-12 col-md-6. Additionally, the s ...

When a container is styled with 'overflow-y: auto', it will display additional horizontal and vertical scrolling bars

I have two files, one HTML and one CSS. The HTML file contains the following code: <div class="app"> <div class="header"></div> <div class="main"> <div class="container_1"> ...

The HTML function transforms blank spaces into the symbol "+"

Just starting out with a question: I created a basic submission form, but I noticed that if there are any spaces in the inputs, the values get changed to a plus sign (+). Here's my form: <form name="input" action="search" method="get"> Web Ad ...

The swf file doesn't stretch to fit the window when the height is set to 100%

Struggling with creating a flash recorder controlled by JavaScript? Trying to get the flash to fill the browser window but disappearing when setting height or width to 100%? Where am I going wrong? <div id="flashrecorder"> <object wmode="trans ...

Angular child component displaying of table data is not looping properly

Currently, I am using Angular 13 along with Bootstrap 3 to develop an application that consists of two main components: form-component (dedicated to inputting user details) and list-component (designed to showcase all data in a table format). Within the HT ...

"Need help solving the issue of generating a random number for a Firebase DB column after adding a new user

Whenever I add a new user using JavaScript, it generates a random number below the Admins column like this. However, I want to adjust this so that it appears as shown in these tables, displaying the username value. If anyone can help me modify the code acc ...

Iterating through elements within a Div will retrieve the initial element exclusively

Is there a way to loop through all elements within the MainDiv Div in order to retrieve their values? Currently, I am only able to retrieve the value of the first element. <div id="MainDiv"> <input type="text" id="MyText"value="Text1" /> ...

Unable to choose multiple sentences within an HTML Page displayed in a UIWebView

I am dealing with an HTML page structured as follows: <html> <body> <div id='0> <span id='0'>Hi </span> <span id='1'>How </span> <span id='2'>Are </span> <sp ...

The webpage lacked the functionality of smooth scrolling

I created a website which you can view here. Check out the code below: <div id="mainDiv" class="container"> <div id="header"> <div class="plc"> <h1><a href="#"></a></h1> ...

Changing the content of a form with a personalized message

I am currently working on a Feedback modal that includes a simple form with fields for Name, rating, and comment. After the user submits the form, I intend to display a custom message such as "Your feedback has been submitted." However, I am facing an issu ...

What is the trick to showing text underneath a font awesome icon?

My HTML code contains font awesome icons, and I'm looking to have text appear below each icon instead of next to it. Currently, the text is displayed midway to the right of each icon. <div class="icons"> <span class="fa-stack f ...

Showcasing certain elements as the user scrolls

Looking for a way to display an element (such as a div) when the user scrolls without using JQuery? Here's an example that tries to achieve this: var scroll = document.body.scrollTop; var divLis = document.querySelectorAll("div"); for(let i = 0; i ...

What causes the Object expected error in jQuery?

Looking for a way to demo horizontal collapse pane with a simple web page that includes html, css, and jquery. <html> <head> <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> <title>Sa ...

Unraveling the intricacies of Wordpress, PHP, and HTML

What is the purpose of the post, ID, and other elements within the article tag? I expected the post_class to be defined in my CSS, but I cannot locate it. If I remove the entire code block from the <article>, my website layout breaks. However, remov ...

Retrieving Table Cell Values Using Jquery: A Step-by-Step Guide

<table class="table" align="center" id="tblMain"> <tr> <td>Status</td> <td><b><span name="current" value="Dispatch">Dispatch</span></b></td> </tr> </table> JS $(&apos ...

Rendering High-quality Images in Internet Explorer Browser

Currently, I am working on optimizing my website for high resolution monitors, particularly the new iPad. The site is already formatted to my liking, with images having increased resolutions while still fitting into specific DIVs. For instance, an image wi ...