[html] How to center a "position: absolute" element

I'm having a problem centering an element that has the attribute position set to absolute. Does anyone know why the images are not centered?

_x000D_
_x000D_
body {
  text-align: center;
}

#slideshowWrapper {
  margin-top: 50px;
  text-align: center;
}

ul#slideshow {
  list-style: none;
  position: relative;
  margin: auto;
}

ul#slideshow li {
  position: absolute;
}

ul#slideshow li img {
  border: 1px solid #ccc;
  padding: 4px;
  height: 450px;
}
_x000D_
<body>
  <div id="slideshowWrapper">
    <ul id="slideshow">
      <li><img src="https://source.unsplash.com/random/300*300?technology" alt="Dummy 1" /></li>
      <li><img src="https://source.unsplash.com/random/301*301?technology" alt="Dummy 2" /></li>
    </ul>
  </div>
</body>
_x000D_
_x000D_
_x000D_

This question is related to html css css-position centering

The answer is


enter image description here

I'm not sure what you want to accomplish, but in this case just adding width: 100%; to your ul#slideshow li will do the trick.

Explanation

The img tags are inline-block elements. This means that they flow inline like text, but also have a width and height like block elements. In your css there are two text-align: center; rules applied to the <body> and to the #slideshowWrapper (which is redundant btw) this makes all inline and inline-block child elements to be centered in their closest block elements, in your code these are li tags. All block elements have width: 100% if they are the static flow (position: static;), which is default. The problem is that when you tell li tags to be position: absolute;, you take them out of normal static flow, and this causes them to shrink their size to just fit their inner content, in other words they kind of "lose" their width: 100% property.


Using left: calc(50% - Wpx/2); where W is the width of the element works for me.


The simpler, the best:

img {
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto auto;
            position: absolute;
}

Then you need to insert your img tag into a tag that sports position:relative property, as follows:

<div style="width:256px; height: 256px; position:relative;">
      <img src="photo.jpg"/>
</div>

If you have set a width you may use:

position: absolute;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
text-align: center;

To center a “position: absolute” element.

.your-element {
  position: absolute;
  left: 0;
  right: 0;
  text-align: center; // or this ->  margin: 0 auto;
}

What seems to be happening is there are two solutions; centered using margins and centered using position. Both work fine, but if you want to absolute position an element relative to this centered element, you need to use the absolute position method, because the absolute position of the second element defaults to the first parent that is positioned. Like so:

<!-- CENTERED USING MARGIN -->
<div style="width:300px; height:100px; border: 1px solid #000; margin:20px auto; text- align:center;">
    <p style="line-height:4;">width: 300 px; margin: 0 auto</p>
    <div style="position:absolute; width:100px; height:100px; background-color:#ff0000; top:-20px; left:0px;">
        <p style="line-height:4;">Absolute</p>
    </div>
</div>

<!-- CENTERED USING POSITION -->
<div style="position:absolute; left:50%; width:300px; height:100px; border: 1px solid #000; margin:20px 0 20px -150px; text-align:center;">
    <p style="line-height:2;">width:300px; position: absolute; left: 50%; margin-left:-150px;</p>
    <div style="position:absolute; width:100px; height:100px; background-color:#ff0000; top:0px; left:-105px;">
        <p style="line-height:4;">Absolute</p>
    </div>
</div>

Until I'd read this posting, using the margin:0 auto technique, to build a menu to the left of my content I had to build a same-width column to the right to balance it out. Not pretty. Thanks!


You can try this way :

_x000D_
_x000D_
* { margin: 0px; padding: 0px; }_x000D_
#body { height: 100vh; width: 100vw; position: relative; _x000D_
        text-align: center; _x000D_
        background-image: url('https://s-media-cache-ak0.pinimg.com/originals/96/2d/ff/962dff2247ad680c542622e20f44a645.jpg'); _x000D_
         background-size: cover; background-repeat: no-repeat; }_x000D_
.text { position: absolute; top: 0; bottom: 0; left: 0; right: 0; height:100px; _x000D_
        display: inline-block; margin: auto; z-index: 999999; }
_x000D_
<html>_x000D_
<body>_x000D_
 <div id="body" class="container-fluid">_x000D_
   <!--Background-->_x000D_
     <!--Text-->_x000D_
    <div class="text">_x000D_
      <p>Random</p>_x000D_
    </div>   _x000D_
 </div>_x000D_
</body>_x000D_
</html>
_x000D_
_x000D_
_x000D_


Your images are not centered because your list items are not centered; only their text is centered. You can achieve the positioning you want by either centering the entire list or centering the images within the list.

A revised version of your code can be found at the bottom. In my revision I center both the list and the images within it.

The truth is you cannot center an element that has a position set to absolute.

But this behavior can be imitated!

Note: These instructions will work with any DOM block element, not just img.

  1. Surround your image with a div or other tag (in your case a li).

    <div class="absolute-div">
      <img alt="my-image" src="#">
    </div>
    

    Note: The names given to these elements are not special.

  2. Alter your css or scss to give the div absolute positioning and your image centered.

    .absolute-div {
      position: absolute;
    
      width: 100%; 
      // Range to be centered over. 
    
      // If this element's parent is the body then 100% = the window's width
    
      // Note: You can apply additional top/bottom and left/right attributes
      // i.e. - top: 200px; left: 200px;
    
      // Test for desired positioning.
    }
    
    .absolute-div img {
      width: 500px;
      // Note: Setting a width is crucial for margin: auto to work.
    
      margin: 0 auto;
    }
    

And there you have it! Your img should be centered!

Your code:

Try this out:

_x000D_
_x000D_
body_x000D_
{_x000D_
  text-align : center;_x000D_
}_x000D_
_x000D_
#slideshow_x000D_
{_x000D_
  list-style : none;_x000D_
  width      : 800px;_x000D_
  // alter to taste_x000D_
_x000D_
  margin     : 50px auto 0;_x000D_
}_x000D_
_x000D_
#slideshow li_x000D_
{_x000D_
  position : absolute;_x000D_
}_x000D_
_x000D_
#slideshow img_x000D_
{_x000D_
  border  : 1px solid #CCC;_x000D_
  padding : 4px;_x000D_
  height  : 500px;_x000D_
  width   : auto;_x000D_
  // This sets the width relative to your set height._x000D_
_x000D_
  // Setting a width is required for the margin auto attribute below. _x000D_
_x000D_
  margin  : 0 auto;_x000D_
}
_x000D_
<ul id="slideshow">_x000D_
    <li><img src="http://lorempixel.com/500/500/nature/" alt="Dummy 1" /></li>_x000D_
    <li><img src="http://lorempixel.com/500/500/nature/" alt="Dummy 2" /></li>_x000D_
</ul>
_x000D_
_x000D_
_x000D_

I hope this was helpful. Good luck!


If you want to center an absolute element

#div {
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    width:300px; /* Assign a value */
    height:500px; /* Assign a value */
    margin:auto;
}

If you want a container to be centered left to right, but not with top to bottom

#div {
    position:absolute;
    left:0;
    right:0;
    width:300px; /* Assign a value */
    height:500px; /* Assign a value */
    margin:auto;
}

If you want a container to be centered top to bottom, regardless of being left to right

#div {
    position:absolute;
    top:0;
    bottom:0;
    width:300px; /* Assign a value */
    height:500px; /* Assign a value */
    margin:auto;
}

Update as of December 15, 2015

Well I learnt this another new trick few months ago. Assuming that you have a relative parent element.

Here goes your absolute element.

.absolute-element { 
    position:absolute; 
    top:50%; 
    left:50%; 
    transform:translate(-50%, -50%); 
    width:50%; /* You can specify ANY width values here */ 
}

With this, I think it's a better answer than my old solution. Since you don't have to specify width AND height. This one it adapts the content of the element itself.


Use margin-left: x%; where x is the half of the width of the element.


An absolute object inside a relative object is relative to its parent, the problem here is that you need a static width for the container #slideshowWrapper , and the rest of the solution is like the other users says

body {
    text-align: center;
}

#slideshowWrapper {
    margin-top: 50px;
    text-align:center;
    width: 500px;
}

ul#slideshow {
    list-style: none;
    position: relative;
    margin: auto;
}

ul#slideshow li {
    position: relative;
    left: 50%;
}

ul#slideshow li img {
    border: 1px solid #ccc;
    padding: 4px;
    height: 450px;
}

http://jsfiddle.net/ejRTU/10/


to center a a position:absolute attribute you need to set left:50% and margin-left: -50% of the width of the div.

<!-- for horizontal -->
<style>
div.center{
 width:200px;
 left:50%;
 margin-left:-100px;
 position:absolute;
}
</style>


<body>
 <div class='center'>
  should be centered horizontaly
 </div>
</body>

for vertical center absolute you need to do the same thing bud not with left just with top. ( NOTE: html and body must have min-height 100%; )

<!-- for vertical -->
<style>
 body,html{
  min-height:100%;
 }
 div.center{
  height:200px;
  top:50%;
  margin-top:-100px;
  position:absolute;
 }
</style>

<body>
 <div class='center'>
  should be centered verticaly
 </div>
</body>

and can be combined for both

   <!-- for both -->
 <style>
 body,html{
  min-height:100%;
 }
 div.center{
  width:200px;
  height:50px
  left:50%;
  top:50%;
  margin-left:-100px;
  margin-top:-25px;
  position:absolute;
 }
</style>


<body>
 <div class='center'>
  should be centered
 </div>
</body>

Here is easy and best solution for center element with “position: absolute”

_x000D_
_x000D_
 body,html{_x000D_
  min-height:100%;_x000D_
 }_x000D_
 _x000D_
 div.center{_x000D_
 width:200px;_x000D_
 left:50%;_x000D_
 margin-left:-100px;/*this is 50% value for width of the element*/_x000D_
 position:absolute;_x000D_
 background:#ddd;_x000D_
 border:1px solid #999;_x000D_
 height:100px;_x000D_
 text-align:center_x000D_
 }_x000D_
 _x000D_
 
_x000D_
<style>_x000D_
_x000D_
</style>_x000D_
_x000D_
<body>_x000D_
 <div class='center'>_x000D_
  should be centered verticaly_x000D_
 </div>_x000D_
</body>
_x000D_
_x000D_
_x000D_


_x000D_
_x000D_
html, body, ul, li, img {_x000D_
  box-sizing: border-box;_x000D_
  margin: 0px;  _x000D_
  padding: 0px;  _x000D_
}_x000D_
_x000D_
#slideshowWrapper {_x000D_
  width: 25rem;_x000D_
  height: auto;_x000D_
  position: relative;_x000D_
  _x000D_
  margin-top: 50px;_x000D_
  border: 3px solid black;_x000D_
}_x000D_
_x000D_
ul {_x000D_
  list-style: none;_x000D_
  border: 3px solid blue;  _x000D_
}_x000D_
_x000D_
li {_x000D_
  /* center horizontal */_x000D_
  position: relative;_x000D_
  left: 0;_x000D_
  top: 50%;_x000D_
  width: 100%;_x000D_
  text-align: center;_x000D_
  font-size: 18px;_x000D_
  /* center horizontal */_x000D_
  _x000D_
  border: 3px solid red; _x000D_
}_x000D_
_x000D_
img {_x000D_
  border: 1px solid #ccc;_x000D_
  padding: 4px;_x000D_
  //width: 200px;_x000D_
  height: 100px;_x000D_
}
_x000D_
<body>_x000D_
  <div id="slideshowWrapper">_x000D_
    <ul id="slideshow">_x000D_
      <li><img src="http://via.placeholder.com/350x150" alt="Dummy 1" /></li>_x000D_
      <li><img src="http://via.placeholder.com/140x100" alt="Dummy 2" /></li>_x000D_
      <li><img src="http://via.placeholder.com/200x100" alt="Dummy 3" /></li>      _x000D_
    </ul>_x000D_
  </div>_x000D_
</body>
_x000D_
_x000D_
_x000D_


Position absolute takes it out of the flow, and places it at 0x0 to the parent ( Last block element to have a position absolute or position relative ).

I'm not sure what exactly you what you are trying to accomplish, It might be best to set the li to a position:relative and that will center them. Given your current CSS

Check out http://jsfiddle.net/rtgibbons/ejRTU/ to play with it


    <div class="centered_content"> content </div>
    <style type="text/css">
    .centered_content {
       text-align: center;
       position: absolute;
       left: 0;
       right: 0;
    }
    </style>

see demo on: http://jsfiddle.net/MohammadDayeh/HrZLC/

text-align: center; works with a position: absolute element when adding left: 0; right: 0;


A simple CSS trick, just add:

width: 100%;
text-align: center;

This works on both images and text.


This worked for me:

position: absolute;
left: 50%;
transform: translateX(-50%);

If you don't know the width of the element you can use this code:

<body>
<div style="position: absolute; left: 50%;">
    <div style="position: relative; left: -50%; border: dotted red 1px;">
        I am some centered shrink-to-fit content! <br />
        tum te tum
    </div>
</div>

Demo at fiddle: http://jsfiddle.net/wrh7a21r/

Source: https://stackoverflow.com/a/1777282/1136132


Div vertically and horizontally aligned center

top: 0;
bottom: 0;
margin: auto;
position: absolute;
left: 0;
right: 0;

Note : Elements should have width and height to be set


probably the shortest

position:absolute;
left:0;right:0;top:0;bottom:0;
margin:0 auto;

Centering something absolutely positioned is rather convoluted in CSS.

ul#slideshow li {
    position: absolute;
    left:50%;
    margin-left:-20px;

}

Change margin-left to (negative) half the width of the element you are trying to center.


Just use display: flex and justify-content: center on the parent element

_x000D_
_x000D_
body {
    text-align: center;
}

#slideshowWrapper {
    margin-top: 50px;
    text-align: center;
}

ul#slideshow {
    list-style: none;
    position: relative;
    margin: auto;
    display: flex;
    justify-content: center;
}

ul#slideshow li {
    position: absolute;
}

ul#slideshow li img {
    border: 1px solid #ccc;
    padding: 4px;
    height: 100px;
}
_x000D_
<body>
   <div id="slideshowWrapper">
      <ul id="slideshow">
         <li><img src="https://source.unsplash.com/random/300*300?technology" alt="Dummy 1" /></li>
         <li><img src="https://source.unsplash.com/random/301*301?technology" alt="Dummy 2" /></li>
      </ul>
   </div>
</body>

<!-- Images from Unsplash-->
_x000D_
_x000D_
_x000D_

You can find this solution in JSFIDDLE


Without knowing the width/height of the positioned1 element, it is still possible to align it as follows:

EXAMPLE HERE

.child {
    position: absolute;
    top: 50%;  /* position the top  edge of the element at the middle of the parent */
    left: 50%; /* position the left edge of the element at the middle of the parent */

    transform: translate(-50%, -50%); /* This is a shorthand of
                                         translateX(-50%) and translateY(-50%) */
}

It's worth noting that CSS Transform is supported in IE9 and above. (Vendor prefixes omitted for brevity)


Explanation

Adding top/left of 50% moves the top/left margin edge of the element to the middle of the parent, and translate() function with the (negative) value of -50% moves the element by the half of its size. Hence the element will be positioned at the middle.

This is because a percentage value on top/left properties is relative to the height/width of the parent element (which is creating a containing block).

While a percentage value on translate() transform function is relative to width/height of the element itself (Actually it refers to the size of bounding box).

For unidirectional alignment, go with translateX(-50%) or translateY(-50%) instead.


1. An element with a position other than static. I.e. relative, absolute, fixed values.


Or you can now use flex box with postion absolute:

.parent {
    position: relative;
    display: flex;
    justify-content: center;
}

.child {
    position: absolute;
}

#parent
{
    position : relative;
    height: 0;
    overflow: hidden;
    padding-bottom: 56.25% /* images with aspect ratio: 16:9  */
}

img 
{
    height: auto!important;
    width: auto!important;
    min-height: 100%;
    min-width: 100%;
    position: absolute;
    display: block;
    /*  */
    top: -9999px;
    bottom: -9999px;
    left: -9999px;
    right: -9999px;
    margin: auto;
}

I don't remember where I saw the centering method listed above, using negative top, right, bottom, left values. For me, this tehnique is the best, in most situations.

When I use the combination from above, the image behaves like a background-image with the following settings:

background-position: 50% 50%;
background-repeat: no-repeat;
background-size: cover;

More details about the first example can be found here:
Maintain the aspect ratio of a div with CSS


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to css-position

How does the "position: sticky;" property work? How to stick table header(thead) on top while scrolling down the table rows with fixed header(navbar) in bootstrap 3? CSS z-index not working (position absolute) Relative div height How can I make the contents of a fixed element scrollable only when it exceeds the height of the viewport? How to position the Button exactly in CSS How to make fixed header table inside scrollable div? Absolute positioning ignoring padding of parent Have a fixed position div that needs to scroll if content overflows How to make div fixed after you scroll to that div?

Examples related to centering

Flutter: Trying to bottom-center an item in a Column, but it keeps left-aligning Centering in CSS Grid Bootstrap 4 Center Vertical and Horizontal Alignment Center a column using Twitter Bootstrap 3 How to horizontally align ul to center of div? How to center a <p> element inside a <div> container? Using margin:auto to vertically-align a div How to center body on a page? How to center a "position: absolute" element How to center a button within a div?