CSS - Is it possible to center (horizontaly) an absolute element that is bigger than it's parent without using negative values?

Viewed 70

So I know there are multiple ways to center a child element. This is a purely theoretical question. I am just wondering if there are any other ways to center an element.
I created an overview of the multiple techniques for centering elements at the bottom of this post. The problem is based on the first example.

gif-reference of the question

overview of the solutions already provided:

figure {
  position: relative;
  width: 23vw;
  height: 23vw;
  overflow: hidden;
  margin: 0 auto;
}

.optie0 img {
  width: 150%;
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0; 
}

.optie1 img {
  position: absolute;
  width: 150%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.optie2 img {
  width: 120%;
  position: absolute;
  left: -50%;
  right: -50%;
  top: -50%;
  bottom: -50%;
  margin: auto;
}

.optie3 {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-content: center;
}

.optie3 img {
  width: 150%;
}

.optie4 img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}

.optie5 {
  display: grid;
  grid-template-rows: 23vw;
  grid-template-columns: 23vw;
}

.optie5 img {
  width: 120%;
  justify-self: center;
  align-self: center;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <p>problem:</p>
    <figure class="optie0">
      <img
        src="https://images.unsplash.com/photo-1606225472150-0b700e620ef5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
        alt="flower"
      />
    </figure>
    <p>solution1:</p>
    <figure class="optie1">
      <img
        src="https://images.unsplash.com/photo-1606225472150-0b700e620ef5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
        alt="flower"
      />
    </figure>
    <p>solution2:</p>
    <figure class="optie2">
      <img
        src="https://images.unsplash.com/photo-1606273789302-5ca427ba2253?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=700&q=80"
        alt="cart"
      />
    </figure>
    <p>solution3:</p>
    <figure class="optie3">
      <img
        src="https://images.unsplash.com/photo-1606156114499-f44bbb400363?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=701&q=80"
        alt="cart"
      />
    </figure>
    <p>solution4:</p>
    <figure class="optie4">
      <img
        src="https://images.unsplash.com/photo-1606038188414-ab55f710a8b0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80"
        alt="cart"
      />
    </figure>
    <p>solution5:</p>
    <figure class="optie5">
      <img
        src="https://images.unsplash.com/photo-1606252255805-c4fab737286b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80"
        alt="cart"
      />
    </figure>
  </body>
</html>

EDIT: Where it is possible to determine the width of the child element. As you can see in the video reference. The code of the first example works in all direction but only when you scale the child bigger as it's parent. It is not able to center horizontal.

PLEASE: IF there is a solution to the question, don't provide one of the other solution already in my example. I understand how it works. I like the first solution and it works great in all directions except for going left if the child is bigger than it's parent.

4 Answers

using 0px wide absolute parent element and display: flex;

.parent{
width: 100px;
height:100px;
margin: auto;
border: 1px solid;
position: relative;
}
.container{
  position:absolute;
  top:10px;
  left: 50%;
  width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.content{
  border: 1px solid;
  padding:20px;
  white-space: nowrap;
  background: rgba(100,100,100,0.4);
}
<div class="parent">
  <div class="container">
    <div class="content">
      Your content of text or image.
    </div>
  </div>
</div>

Old-fashioned way wit transform -50%.

<div class="a">
  Wrapper
  <div class="b">
    Centered
  </div>  
</div>
.a { 
  position: relative;
  height: 200px;
  width: 200px;
  border: 1px solid red;
  margin: 300px auto;
}

.b { 
  position: absolute;
  height: 300px;
  width: 300px;
  border: 1px solid blue;
  color: blue;
  left: 50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);
}

enter image description here

Example: https://codepen.io/vovchisko/pen/BaLBGEZ

Is it possible to center (horizontally) an absolute element that is bigger than its parent without using negative values?

Yes. But you don't need to use position: absolute.

You can straightforwardly achieve this effect by adding:

  • display: flex
  • justify-content: center

to the smaller parent container.


Working Example:

body {
  display: flex;
  justify-content: center;
}

.smaller {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 180px;
  height:180px;
  background-color: rgb(213, 35, 30);
  border-radius: 50%;
  box-shadow: 0 0 12px rgb(255, 0, 0);
}

.smaller::before {
  content: '';
  position: absolute;
  top: 30px;
  left: 30px;
  display: block;
  width: 120px;
  height: 120px;
  background-color: rgb(255, 255, 255);
  border-radius: 50%;
}

.longer {
  z-index: 6;
  height: 36px;
  padding: 8px;
  color: rgb(255, 255, 255);
  line-height: 36px;
  text-align: center;
  font-size: 32px;
  font-weight: 700;
  font-family: sans-serif;
  text-transform: uppercase;
  background-color: rgb(0, 24, 163);
}
<div class="smaller">
  <div class="longer">Underground</div>
</div>

Just update the container css class with this.

You'll get the required component working efficiently.

.container {
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
Related