I'm wondering why my element is aligning itself with its sibling. It seems that when the parent has overflow set to hidden, it causes the children's

Let me break down the diagram below for you:
The main box is marked in yellow and serves as the parent element.
Within this parent box, there are two child elements, one in black and one in cyan.
To hide any excess content of the cyan box that overflows its parent, the property overflow: hidden is applied.

Due to the nature of overflow: hidden, using margin: auto for centering causes issues. In an attempt to center the black box within its parent (the yellow box), left: 50% was utilized. However, it unexpectedly aligns with the width of the cyan box instead.

I am seeking guidance on alternative methods to correctly align the black box to the width of its parent. Any solution addressing the margin: auto issue would also be appreciated.

Below is the snippet of code in question:

.yellow-box {
    display:table-cell;
    height:498px;
    width:33.33333333%;
    overflow:hidden;
    position:relative;
}

.cyan-box {
    display:block;
    height:auto;
    position:absolute;
    z-index:1;
    top:0;
    left:0;
    width:654px;
    height:654px;
}

.black-box {
    width:144px;
    height:84px;
    position:absolute;
    z-index:2;
}

Answer №1

What an amazing optical illusion you've stumbled upon!

It may seem like left: 50% is centering the elements, but in reality, it's shifting the left side of .black-box to the middle of .yellow-box. Adding transform: translate(-50%); to .black-box will truly center it within its parent.

.black-box {
  width: 144px;
  height: 84px;
  position: absolute;
  z-index: 2;
  background: black;
  left: 50%;
  transform: translate(-50%);
}
.yellow-box {
  height: 498px;
  width: 33.33333333%;
  position: relative;
  background: yellow;
  margin-bottom: 20px;
}
.cyan-box {
  display: block;
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  width: 654px;
  height: 654px;
  background: cyan;
}
.half {
  width: 50%;
  border-right: 1px black solid;
  height: 100%;
}
<div class="yellow-box">
  <div class="black-box">
  </div>
  <div class="cyan-box">
  </div>
  <div class="half"></div>
</div>

The illusion dissipates when the page size changes. To see the true center of .yellow-box, I've added a line down the middle for reference.

Check out this example to compare the difference.

.yellow-box {
  height: 100px;
  width: 33.33333333%;
  position: relative;
  background: yellow;
  margin-bottom: 20px;
}
.cyan-box {
  display: block;
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  width: 654px;
  height: 100px;
  background: cyan;
}
.black-box {
  width: 144px;
  height: 84px;
  position: absolute;
  z-index: 2;
  background: black;
  left: 50%;
}
.black-box-two {
  width: 144px;
  height: 84px;
  position: absolute;
  z-index: 2;
  background: black;
  left: 50%;
  transform: translate(-50%);
}
.half {
  width: 50%;
  border-right: 1px black solid;
  height: 100%;
}
* {
  margin: 0;
  padding: 0;
}
<div class="yellow-box">
  <div class="black-box">
  </div>
  <div class="cyan-box">
  </div>
  <div class="half"></div>
</div>
<div class="yellow-box">
  <div class="black-box-two">
  </div>
  <div class="cyan-box">
  </div>
  <div class="half"></div>
</div>

Remember that .black-box isn't actually aligning with its sibling as it appears to be doing.

If you prefer using margin: 0 auto, ensure .black-box has position: relative, as margins don't affect absolutely positioned elements.

.yellow-box {
  height: 498px;
  width: 33.33333333%;
  position: relative;
  background: yellow;
  margin-bottom: 20px;
  overflow: hidden;
}
.cyan-box {
  display: block;
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  width: 654px;
  height: 654px;
  background: cyan;
}
.black-box {
  width: 144px;
  height: 84px;
  position: relative;
  z-index: 2;
  background: black;
  margin: 0 auto;
}
.half {
  width: 50%;
  border-right: 1px black solid;
  height: 100%;
}
<div class="yellow-box">
  <div class="black-box">
  </div>
  <div class="cyan-box">
  </div>
  <div class="half"></div>
</div>

By switching from absolute to relative positioning, margins come back into play. You can still use positioning properties like top, right, bottom, and left if needed.

Take a look at this example showcasing the two working solutions alongside your original code (left uses transform: translate(-50%), middle is the original, and right utilizes margin: 0 auto).

.yellow-box {
  height: 100px;
  width: 30%;
  position: relative;
  background: yellow;
  margin-bottom: 20px;
  overflow: hidden;
}
.cyan-box {
  display: block;
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  width: 654px;
  height: 100px;
  background: cyan;
}
.black-box {
  width: 144px;
  height: 84px;
  position: relative;
  z-index: 2;
  background: black;
  margin: 0 auto;
}
.black-box-two {
  width: 144px;
  height: 84px;
  position: absolute;
  z-index: 2;
  background: black;
  left: 50%;
  margin: 0 auto;
}
.black-box-three {
  width: 144px;
  height: 84px;
  position: absolute;
  z-index: 2;
  background: black;
  left: 50%;
  transform: translate(-50%);
}
.half {
  width: 50%;
  border-right: 1px black solid;
  height: 100%;
}
* {
  margin: 0;
  padding: 0;
}
body {
  display: flex;
  justify-content: space-between;
}
<div class="yellow-box">
  <div class="black-box">
  </div>
  <div class="cyan-box">
  </div>
  <div class="half"></div>
</div>
<div class="yellow-box">
  <div class="black-box-two">
  </div>
  <div class="cyan-box">
  </div>
  <div class="half"></div>
</div>
<div class="yellow-box">
  <div class="black-box-three">
  </div>
  <div class="cyan-box">
  </div>
  <div class="half"></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

Integrate my API with HTML using J.Query/Ajax technology

Finally, after much effort, I have successfully created a JSON java API. My API design is simple and based on Interstellar movie details: { "title": "Interstellar", "release": "2014-11-05", "vote": 8, "overview": "Interstellar chronicl ...

Ways to constrain checkbox choices to only one within an HTML file as the checklist with the checkboxes is being created by JavaScript

I am working on developing an HTML dialogue box to serve as a settings page for my program. Within this settings page, users can create a list of salespeople that can be later selected from a drop-down menu. My current objective is to incorporate a checkbo ...

I am encountering an issue where the characters "£" is displaying as "£" when my HTML is rendered. Can anyone explain why this is happening?

Here is the code I'm having trouble with: http://pastebin.com/QBLeLyNq. For some reason, the "code" part isn't working correctly. Any help would be appreciated. I used to run a small business and had a profit of £145 with an investment of only ...

The overflow hidden property in Tailwind CSS is not functioning properly during animations

I'm attempting to animate an image by moving it to different positions, but when it goes off the screen, the scrollbar appears. I tried adding overflow hidden to the parent element, but it didn't work as expected. Here is the relevant code snippe ...

Learn the process of updating Datatables' multiple column headers in real-time using ajax and jquery, all without the need to refresh

My goal is to dynamically change the column number and header of a table based on the return value of an AJAX call in conjunction with DataTables jQuery plugin. Below is the JavaScript and jQuery code I am currently using: $( document ).ready(function() { ...

Ways to align a button in the center using Material-UI

I'm struggling to find a way to center buttons in Material-UI. Here is the code snippet I currently have: function BigCard(props) { const { classes } = props; return ( <div> <Card className={classes.card}> <C ...

Tips for hiding website content on larger screens

Is there a way to hide website content on screen sizes over 1600px and display only a message image instead? ...

Order of stacked divs with absolute positioning

I'm struggling to make a photo expand when hovered over without it expanding under the existing content. My current HTML and CSS code is as follows: .userCard {width:360px;height:180px;border:1px solid black;float:left;margin:10px;position:relative ...

What are the steps to achieve such a design?

Can you show me how to create a single layout using HTML and CSS? I am struggling with splitting the screen properly. Could you provide a simple example, maybe on jsfiddle? https://i.stack.imgur.com/d2rbn.jpg Thank you in advance! EDIT: I have added a ...

Discovering specific keywords within HTML tags and performing replacements using jQuery

I am searching for specific strings within my HTML code, and if I find them, I want to replace them with nothing. For example: HTML: <img src=`javascript:alert("hello ,world!")`> My goal is to locate keywords like javascript, alert, etc, within H ...

Guide to updating Django model-based form records with choices option using an HTML form

I have a pre-existing model called ShiftChange that I am trying to update. In the model-based form, I have used CHOICES as follows: from django.db import models ====== ============= ) class ShiftChange(models.Model): ...

"Utilizing Primeng's P-Table Component for Enhanced Interactivity

Greetings to all! I am currently working with p-table, p-headercheckbox, and p-tableCheckbox along with additional filters for each column. However, I am encountering an issue where the filters are not aligning properly with the headers. ...

HTML File Path for C Drive

I am a beginner with HTML and I'm facing an issue regarding displaying an image located at "C:\Users\Jas mine\folder\landscape.jpg" in HTML. I have tried various solutions posted by others but none of them seem to work for me. Can ...

Populating options in <select> for Internet Explorer version 5

Let me address the first question here. The reason why I am using IE5 is because I am working on a Windows CE device which limits me to this browser. My application involves a webpage with various elements. In my HTML code, I have two text fields and a se ...

Efficiently utilizing CSS classes

My dilemma involves styling a textfield in two different locations on my website, each with its own parent element. The second location requires an additional margin-top that the first does not. What is a clever way to adjust the original margin-top withou ...

using percentage and float to enforce overflow-x

I have a small structure of list items inside a container and I am trying to display horizontal overflow instead of vertical overflow. However, I am having trouble achieving this, possibly due to the properties like float and percentage that I am using. I ...

Scrolling with Buttons in both Right and Left directions

I am working with three divs: left, middle, and right. The middle div will contain content, while the left and right divs are designated for buttons that will scroll the middle div. Specifically, I plan to display lists of pictures in the middle div. If a ...

In HTML, the size and position of an <a> element cannot be altered or adjusted

I am having trouble with an anchor element that is contained within a div. No matter what I try, I cannot resize or move it up or down without it covering its sibling element. I have attempted using transform-translate and margin properties, but to no avai ...

Looking for assistance with extracting only numerical values from a webpage using Selenium in Python

Website Elements <td style="font-size:20px;font-family:LucidaGrande,tahoma,verdana,arial,sans-serif;padding:10px;background-color:#f2f2f2;border-left:1px solid #ccc;border-right:1px solid #ccc;border-top:1px solid #ccc;border-bottom:1px solid #ccc; ...

Having trouble with images not showing up on React applications built with webpack?

Hey there! I decided not to use the create react app command and instead built everything from scratch. Below is my webpack configuration: const path = require("path"); module.exports = { mode: "development", entry: "./index.js", output: { pa ...