中心绝对UL的LI

时间:2018-05-11 20:16:39

标签: html css sass

我试图将点集中在我的绝对UL上。在网上尝试了很多不同的答案后,我遇到了很多麻烦。

这里是CSS(.new-dots是UL):

.photos {
    .new-dots {
      width: 100%;
      position: absolute;
      top: 0;
      text-align: center;

      li {
        display: inline;
        color: rgba(202, 202, 202, 0.78);
        margin-right: 5px;

        button {
          background: rgba(202, 202, 202, 0.75);
          border-width: 0;
          width: 16px;
          height: 16px;
          border-radius: 8px;
          color: transparent;
        }

        button:focus {
          outline: none;
        }
      }

      .slick-active {
        button {
          background: #FFF !important;
        }
      }
    }

    .photo {
      height: 100vw;
    }
  }

2 个答案:

答案 0 :(得分:0)

将其添加到elemet中,它将子元素置于父元素内。

代码示例



body {
position: relative;
height: 100vh;
}

.new-dots {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  
}

<div class="new-dots">Test Content</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

  • display: inline-block;更改为li中的display: inline;
  • 默认ul有自己的paddingmargin,所以你需要 删除它。在您的情况下,您必须设置padding-left: 0

  • 此外,您还可以从width: 100%中删除ul并进行设置 leftright0,以便它占据父母的全宽。

&#13;
&#13;
.photos .new-dots {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  text-align: center;
  padding-left: 0;
}
.photos .new-dots li {
  display: inline-block;
  color: rgba(202, 202, 202, 0.78);
  margin-right: 5px;
}
.photos .new-dots li button {
  background: rgba(202, 202, 202, 0.75);
  border-width: 0;
  width: 16px;
  height: 16px;
  border-radius: 8px;
  color: transparent;
}
.photos .new-dots li button:focus {
  outline: none;
}
.photos .new-dots .slick-active button {
  background: #fff !important;
}
.photos .photo {
  height: 100vw;
}
&#13;
<div class="photos">
  <ul class="new-dots">
    <li><button>123</button></li>
    <li><button>456</button></li>
    <li><button>789</button></li>
  </ul>
</div>
&#13;
&#13;
&#13;

工作笔here

相关问题