如何集中动态元素

How to centralize dynamic elements

我正在尝试将颜色 (.attr-image-container) 集中在 div(.swat-container) 上,但没有任何效果,所以有人可以帮助我吗?

我试过 margin:0px auto;在很多地方

我的html:

<dd class="last">
<div class="input-box">
    <div class="settings-swatch-container">
        <div class="swat-container">
            <div class="attr-image-container">
                <div class="attr-image" alt="Amarelo" title="Amarelo" style="background-color:#FFF700;"></div>
            </div>
            <div class="attr-image-container">
                <div class="attr-image" alt="Azul Escuro" title="Azul Escuro" style="background-color:#005EFF;"></div>
            </div>
            <div class="attr-image-container">
                <div class="attr-image" alt="Verde" title="Verde" style="background-color:#4DFF29;"></div>
            </div>
        </div>
    </div>
</div>

Mycss

.swat-container{
    width:100%;
    height:100%;
    margin:auto;
}

.attr-image
{
    width: 30px;
    height: 30px;
    padding: 1px;
    margin-right: 5px; 
    cursor:pointer;
    border: 2px solid #00BFFF;
}

.main-swatch-container
{
    width: 100%;
    clear: both;
    margin:auto;
}

.attr-image-container
{
    float:left;
    position:relative;
    text-align: center;
}

使用 flex...

dd {
  padding: 0;
  margin: 0;
}
.swat-container {
  width: 100%;
  height: 100%;
  margin: auto;
  display: flex;
  justify-content: center;
}
.attr-image {
  width: 30px;
  height: 30px;
  padding: 1px;
  margin-right: 5px;
  cursor: pointer;
  border: 2px solid #00BFFF;
}
.main-swatch-container {
  width: 100%;
  margin: auto;
}
.attr-image-container {
  position: relative;
  text-align: center;
}
<dd class="last">
  <div class="input-box">
    <div class="settings-swatch-container">
      <div class="swat-container">
        <div class="attr-image-container">
          <div class="attr-image" alt="Amarelo" title="Amarelo" style="background-color:#FFF700;"></div>
        </div>
        <div class="attr-image-container">
          <div class="attr-image" alt="Azul Escuro" title="Azul Escuro" style="background-color:#005EFF;"></div>
        </div>
        <div class="attr-image-container">
          <div class="attr-image" alt="Verde" title="Verde" style="background-color:#4DFF29;"></div>
        </div>
      </div>
    </div>
  </div>
</dd>