How to center a “position: absolute” element

后端 未结 26 3261
-上瘾入骨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 05:58

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

    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;
    }
    
       
    • Dummy 1
    • Dummy 2

    You can find this solution in JSFIDDLE

提交回复
热议问题