How to center a “position: absolute” element

后端 未结 26 3365
-上瘾入骨i
-上瘾入骨i 2020-11-21 05:35

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?

<
26条回答
  •  無奈伤痛
    2020-11-21 06:04

    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/

提交回复
热议问题