Tips for aligning all positioned elements in a div

I am trying to center absolute items inside my .child_div element with a green background. How can I do this properly?

.parent_div {
  position: relative;
  background: lightgreen;
  width: 1200px;
  margin: auto;
}

.child_div {
  position: relative;
  height: 200px;
  margin: auto;
  display: flex;
  justify-content: center;
}

.item {
  width: 80px;
  height: 80px;
  position: absolute;
}

.item1 {
  background: orange;
  left: 20%;
}

.item2 {
  background: yellow;
  left: 25%;
}

.item3 {
  background: blue;
  left: 30%;
}

.item4 {
  background: red;
  left: 35%;
}
<div class="parent_div">
  <div class="child_div">
    <div class="item item1"></div>
    <div class="item item2"></div>
    <div class="item item3"></div>
    <div class="item item4"></div>
  </div>
</div>

Answer №1

By eliminating the position: absolute and unnecessary margin and left values, you are able to achieve the desired outcome.

Update - For overlapping elements, consider utilizing position: relative along with adjusting the left values.

.parent_div {
  background: lightgreen;
  width: 1200px;
}

.child_div {
  height: 200px;
  display: flex;
  justify-content: center;
}

.item {
  width: 80px;
  height: 80px;
}

.item1 {
  background: orange;
}

.item2 {
  background: yellow;
}

.item3 {
  background: blue;
}

.item4 {
  background: red;
  position: relative;
  left: -20px;
}
<div class="parent_div">
  <div class="child_div">
    <div class="item item1"></div>
    <div class="item item2"></div>
    <div class="item item3"></div>
    <div class="item item4"></div>
  </div>
</div>

Answer №2

If you want to achieve this effect, use display:inline-block and text-align: center.

.parent_div {
  background: lightgreen;
  width: 1200px;
  text-align:center;
}

.child_div {
  height: 200px;
  display: inline-block;
}

.item {
  width: 80px;
  height: 80px;
  float: left;
}

.item1 {
  background: orange;
  left: 20%;
}

.item2 {
  background: yellow;
  left: 25%;
}

.item3 {
  background: blue;
  left: 30%;
}

.item4 {
  background: red;
  left: 35%;
}
<div class="parent_div">
  <div class="child_div">
    <div class="item item1"></div>
    <div class="item item2"></div>
    <div class="item item3"></div>
    <div class="item item4"></div>
  </div>
</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

Problem with responsive design on iPhone

I'm currently working on developing a responsive chatbot using CSS Bootstrap. However, I've encountered an issue where the header and footer are not fixed when I open the app on an iPhone. The keyboard header is also moving up the screen, which s ...

Offspring element overrides styling of its parent

#popBox{ width:100%; height:100%; position:fixed; left:0; top:0; border-collapse:collapse; background:black; opacity:0.8; display:none; } <table id="popBox"> <tr> <td></td> <td></td> ...

What is the solution for resolving the delay between a variable in HTML and the controller in Angular 4?

I am encountering a common issue in my Angular 4 or 2 project. Whenever I initialize a variable inside a subscribe function, there is a delay before it gets assigned a value. The problem arises when I try to use this variable in the HTML template using *ng ...

Converting JSON data retrieved from a URL into HTML format

I have a JSON output from a URL that I need to format into html. The structure of the JSON data is as follows: prtg-version "xxxxxxx" treesize 0 channels 0 name "Downtime" name_raw "Downtime" lastvalue "" lastvalue_raw "" 1 name ...

Is there a way to exclusively show the browser name and version while excluding any supplementary details with 'HTTP_USER_AGENT'?

I am trying to modify my script to only display the browser version and name using 'HTTP_USER_AGENT'. I want to ensure that my script still covers all modern browsers in case someone helps me and specifies the code only for the Safari browser. C ...

What causes the variation in results when I input CSS directly into the head section versus linking it from a separate .css file?

As a beginner, I am facing an issue with my CSS. When I directly insert the CSS into the head of my .html file, everything looks perfect. However, when I link my .css file into the head, the very first word ("Holy") does not display properly. I have double ...

Redirecting after executing JavaScript code

Is it possible to implement a redirect after the subscription script for push notifications has been executed successfully? <script> var OneSignal = window.OneSignal || []; OneSignal.push(function() { OneSignal.init({ ...

Text that is curving around a D3.js pie chart

I am currently working on creating a 3D-Js chart and I would like the pie text to wrap around the pie itself. This is the exact effect that I am trying to achieve: I am facing two main issues: I am currently printing two separate charts (with labels prin ...

I encountered an issue while attempting to utilize a JavaScript script - I received an error message stating "Uncaught ReferenceError: jQuery is not defined."

Currently, I am utilizing a template with multiple pages and I need to use one of them. I followed all the necessary steps correctly, but I encountered an error in the console that says Uncaught ReferenceError: jQuery is not defined. Below is my HTML scrip ...

Mapping variables to attributes in polymers

I'm working with a polymer element that looks like this: <polymer-element name="foo" attributes="bar"> <template> <core-ajax url="url.php" params="{{ {last: bar} }}"></core-ajax> {{bar}} </template> </polymer-element& ...

Utilizing graphics within the Atom text editor

Hey, I have a bit of a silly question. I can't seem to figure out why these images aren't showing up on my Atom editor. Can anyone help me with this? <body> <div class="top-container"> <img src="images/clou ...

Try incorporating the <code> tags or a similar method in ReactJS when using JSX

I've been experimenting with ReactJS and JSX to develop a prototype for a styleguide. One issue I encountered is that JSX seems to be ignoring my <code> tags, instead of displaying the raw HTML code needed to call components. Here's what ...

Avoiding leaps through the use of dynamic pictures?

Currently, I am implementing the picture element along with srcset to download the same image but in varying resolutions depending on the screen size of the device. The image has a style of max-width: 100%, causing it to shift the content below when downl ...

Burger menu floating above container block element

I have set up a bootstrap container with the following styling: .container { margin-top: 12em; font-family: 'Amatic SC', cursive; border-bottom: 1px solid; border-color: #800000; } The purpose behind this is to have my navigatio ...

Mobile view div failing to fill remaining space

When viewing my website on a mobile device, the heading in the div tag is not occupying the remaining space like it does on desktop. The heading is an h4 tag inside a div tag for mobile view. If you want to check out my website in inspect mode, here is the ...

Display an image with only a portion visible, no canvas, no frame, and no need

My dilemma involves an img with a surrounding box div. http://jsfiddle.net/6d4yC/7/ 1) I am seeking to display only a portion of the image (250x150) without generating a white overlay when it is in its large size. Placing a #box1 div around the image has ...

Stretch the div to the right and maintain its horizontal width

I am in the process of coding a two-column layout, with a total width of 980px. Each column will have a background that expands all the way to the left and right sides as shown in the screenshot here. On the left side, there is a blue div with a width of ...

"I'm experiencing an issue where my JSON data is not displaying in the browser when I run the code

I am having trouble displaying my json data in the browser. This is my first time working with json and I can't seem to identify the issue. I tried researching online and found that it could be related to mime types, but I still can't solve it. B ...

Navigating through each element of an array individually by using the onClick function in React

I'm currently working on a project that involves creating a button to cycle through different themes when pressed. The goal is for the button to display each theme in sequence and loop back to the beginning after reaching the last one. I've imple ...

Creating a checklist using HTML and CSS can be achieved by utilizing the <span> element and

I'm having trouble changing the color of a span box to red when I focus on a link. The span successfully changes color when activated, but nothing happens when I try to use the 'focus' pseudo-class. On click, different categories will displa ...