html - 使用绝对位置在左侧有一个偏移量

标签 html css

我有一个图像列表,悬停时在中心显示一个文本。但是,文本在左侧有一个偏移量。我不知道它究竟是什么造成了偏移量。

我可以使用 leftright 将其居中并将其设置为 0,尽管 width 将设置为最大值 100%这不是我需要的。

为什么会偏移?还有像 calc 这样的属性,我可以用它来获取宽度并将其减去 left:50%

CSS:

ul {width:100%; float:left; margin:0; padding:0; list-style:none;}
.Arrivals {margin-bottom:60px;}
.Arrivals .products .product-category {float:left !important; padding:0 !important; margin:1% !important; position:relative !important; width:23% !important; box-sizing:border-box; display:list-item; clear:none !important;}
.Arrivals .products .product-category:nth-child(3) {float:right !important; width:48% !important;}
    .Arrivals .products .product-category a {width:100%; display:block; text-align:center; margin:auto;}
        .Arrivals .products .product-category img {margin:0 !important; width:100%; height:auto; display:block; margin:0 0 1em; box-shadow:none;}

        .Arrivals .products .product-category h2 {    display: none !important;    opacity: 0;    transition: .3s ease all;    -webkit-transition: .3s ease all;    -moz-transition: .3s ease all;    -o-transition: .3s ease all;    float: left;    text-align: left !important;    position: absolute;    top: calc(50% - 22px);    padding: 1em 1.75em !important;    margin: 0 auto !important;    /* opacity: 1; */    text-align: center;    display: inline-block !important;    background: #fff;    color: #000;    z-index: 999;    font-size: .9em !important;    letter-spacing: 1px;    font-weight: 500;    border-radius: 3px;}
        .Arrivals .products .product-category h2 .count {display:none;}

    .Arrivals .products .product-category a:after {content:''; background:#000; opacity:.6; width:100%; display:none; height:100%; position:absolute; top:0; border-radius:3px;} 
    .Arrivals .products .product-category a:hover:after, .Arrivals .products .product-category:hover a:after {display:block; border:1px solid rgba(0, 0, 0, .2); box-shadow: 0 3px 9px rgba(0, 0, 0, .5);}
    .Arrivals .products .product-category:hover h2, .Arrivals .products .product-category a:hover h2 {opacity:1; display:inline-block !important;}

HTML:

<div class="Arrivals"><ul class="products">
<li class="product-category product first">
    <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg"/> <h2 class="woocommerce-loop-category__title">
            Watches <mark class="count">(1)</mark>      </h2>
        </a></li>
<li class="product-category product first">
    <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg"/> <h2 class="woocommerce-loop-category__title">
            Watches <mark class="count">(1)</mark>      </h2>
        </a></li>
<li class="product-category product first">
    <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg"/> <h2 class="woocommerce-loop-category__title">
            Watches <mark class="count">(1)</mark>      </h2>
        </a></li>
<li class="product-category product first">
    <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg"/> <h2 class="woocommerce-loop-category__title">
            Watches <mark class="count">(1)</mark>      </h2>
        </a></li>
<li class="product-category product first">
    <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg"/> <h2 class="woocommerce-loop-category__title">
            Watches <mark class="count">(1)</mark>      </h2>
        </a></li>                                   
</ul>       </div>

Fiddle here

最佳答案

将以下代码添加到代码中将其水平居中

left: 50%;
transform: translateX(-50%);

ul {
  width: 100%;
  float: left;
  margin: 0;
  padding: 0;
  list-style: none;
}

.Arrivals {
  margin-bottom: 60px;
}

.Arrivals .products .product-category {
  float: left !important;
  padding: 0 !important;
  margin: 1% !important;
  position: relative !important;
  width: 23% !important;
  box-sizing: border-box;
  display: list-item;
  clear: none !important;
}

.Arrivals .products .product-category:nth-child(3) {
  float: right !important;
  width: 48% !important;
}

.Arrivals .products .product-category a {
  width: 100%;
  display: block;
  text-align: center;
  margin: auto;
}

.Arrivals .products .product-category img {
  margin: 0 !important;
  width: 100%;
  height: auto;
  display: block;
  margin: 0 0 1em;
  box-shadow: none;
}

.Arrivals .products .product-category h2 {
  display: none !important;
  opacity: 0;
  transition: .3s ease all;
  -webkit-transition: .3s ease all;
  -moz-transition: .3s ease all;
  -o-transition: .3s ease all;
  float: left;
  text-align: left !important;
  position: absolute;
  top: calc(50% - 22px);
  padding: 1em 1.75em !important;
  margin: 0 auto !important;
  /* opacity: 1; */
  text-align: center;
  display: inline-block !important;
  background: #fff;
  color: #000;
  z-index: 999;
  font-size: .9em !important;
  letter-spacing: 1px;
  font-weight: 500;
  border-radius: 3px;
}

.Arrivals .products .product-category h2 .count {
  display: none;
}

.Arrivals .products .product-category a:after {
  content: '';
  background: #000;
  opacity: .6;
  width: 100%;
  display: none;
  height: 100%;
  position: absolute;
  top: 0;
  border-radius: 3px;
}

.Arrivals .products .product-category a:hover:after,
.Arrivals .products .product-category:hover a:after {
  display: block;
  border: 1px solid rgba(0, 0, 0, .2);
  box-shadow: 0 3px 9px rgba(0, 0, 0, .5);
}

.Arrivals .products .product-category:hover h2,
.Arrivals .products .product-category a:hover h2 {
  opacity: 1;
  display: inline-block !important;
}

/*  additional part */
.woocommerce-loop-category__title {
  left: 50%;
  transform: translateX(-50%);
}
<div class="Arrivals">
  <ul class="products">
    <li class="product-category product first">
      <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg" />
        <h2 class="woocommerce-loop-category__title">
          Watches <mark class="count">(1)</mark> </h2>
      </a>
    </li>
    <li class="product-category product first">
      <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg" />
        <h2 class="woocommerce-loop-category__title">
          Watches <mark class="count">(1)</mark> </h2>
      </a>
    </li>
    <li class="product-category product first">
      <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg" />
        <h2 class="woocommerce-loop-category__title">
          Watches <mark class="count">(1)</mark> </h2>
      </a>
    </li>
    <li class="product-category product first">
      <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg" />
        <h2 class="woocommerce-loop-category__title">
          Watches <mark class="count">(1)</mark> </h2>
      </a>
    </li>
    <li class="product-category product first">
      <a href="#"><img src="https://www.presentationpro.com/images/product/medium/slide/PPP_IFlat_LT3_Flat_Image_Placeholder_01_Circle.jpg" />
        <h2 class="woocommerce-loop-category__title">
          Watches <mark class="count">(1)</mark> </h2>
      </a>
    </li>
  </ul>
</div>

关于html - 使用绝对位置在左侧有一个偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728451/

相关文章:

html - 输入框和按钮与 IE 和 Firefox 不匹配

html - 使用 flex 扩展到父元素外部的子元素

javascript - 如何将预先生成的数组传递给 Angular 模板中的组件?

javascript - 遍历两个数组的内容并一起显示

html - 使用 :before and :after pseudo-elements with images

css - 为什么 backdropFilter 在 CSSStyleDeclaration 中不可用?

javascript - 在 Fraction Slider 中调整浏览器大小的字体自行调整大小

php - 我怎样才能做出更好的设计来了解选中哪个复选框以便将其值赋给mysql?

css - 以编程方式从网页创建PDF

html - 如何使我的导航栏在两个不均匀的元素之间居中?