How to center a “position: absolute” element

后端 未结 26 3253
-上瘾入骨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

    html, body, ul, li, img {
      box-sizing: border-box;
      margin: 0px;  
      padding: 0px;  
    }
    
    #slideshowWrapper {
      width: 25rem;
      height: auto;
      position: relative;
      
      margin-top: 50px;
      border: 3px solid black;
    }
    
    ul {
      list-style: none;
      border: 3px solid blue;  
    }
    
    li {
      /* center horizontal */
      position: relative;
      left: 0;
      top: 50%;
      width: 100%;
      text-align: center;
      font-size: 18px;
      /* center horizontal */
      
      border: 3px solid red; 
    }
    
    img {
      border: 1px solid #ccc;
      padding: 4px;
      //width: 200px;
      height: 100px;
    }
    
      
    • Dummy 1
    • Dummy 2
    • Dummy 3

提交回复
热议问题