css 如何使span正确地环绕图标

ckocjqey  于 2024-01-09  发布在  其他
关注(0)|答案(3)|浏览(115)

我有下面的HTML代码,它显示了一个带有柜台徽章的购物车图标。SVG图标被 Package 在容器SPAN中。但是,正如你在附件中看到的,由于某种原因,这个容器(红色边框)和图标(蓝色边框)之间有一些不对齐,图标溢出了容器。
有什么方法可以使容器和图标的边界合适吗?

HTML:

<ul class="site-header-cart">
    <li>
        <a class="cart-contents" href="https://www.example.com/cart/">
            <span class="cart-button">
                <svg class="svg-icon" width="20" height="20" aria-hidden="true" role="img" focusable="false" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" style="border: 1px solid blue;"><path d="M 7.499889,21 Q 6.7574042,21 6.2286651,20.47125 5.6999261,19.9425 5.6999261,19.2 5.6999261,18.4575 6.2286651,17.92875 6.7574042,17.4 7.499889,17.4 8.2423738,17.4 8.771113,17.92875 9.2998519,18.4575 9.2998519,19.2 9.2998519,19.9425 8.771113,20.47125 8.2423738,21 7.499889,21 Z M 16.499704,21 Q 15.757219,21 15.22848,20.47125 14.699741,19.9425 14.699741,19.2 14.699741,18.4575 15.22848,17.92875 15.757219,17.4 16.499704,17.4 17.242189,17.4 17.770928,17.92875 18.299667,18.4575 18.299667,19.2 18.299667,19.9425 17.770928,20.47125 17.242189,21 16.499704,21 Z M 6.7349048,6.6 8.8948603,11.1 H 15.194731 L 17.66968,6.6 Z M 5.8799223,4.8 H 19.15465 Q 19.672138,4.8 19.942133,5.2612499 20.212128,5.7225 19.964633,6.1949999 L 16.769698,11.955 Q 16.522203,12.405 16.105962,12.6525 15.689721,12.9 15.194731,12.9 H 8.4898688 L 7.499889,14.7 H 18.299667 V 16.5 H 7.499889 Q 6.4874098,16.5 5.9699205,15.611249 5.4524311,14.7225 5.9249213,13.845 L 7.1398964,11.64 3.899963,4.8 H 2.1 V 3 H 5.0249398 Z M 8.8948603,11.1 H 15.194731 Z"></path>
                </svg>
                <span class="cart-badge">1</span>
            </span>
        </a>
    </li>
</ul>

字符串

CSS:

.site-header-cart {
  position: relative;
  margin: 0;
  padding: 0;
  list-style: none;
}
.site-header-cart .cart-contents {
  display: inline-block;
  background-color: transparent;
  height: auto;
  width: auto;
  padding: 10px 0;
  color: #222;
  font-size: 0.875rem;
  text-decoration: none;
  border: 1px solid lightgray;
}
.site-header-cart .cart-contents .cart-button {
  position: relative;
  border: 1px solid red;
}
.site-header-cart .cart-contents .cart-button svg {
  display: inline-block;
  max-width: 100%;
  fill: currentColor;
  border: 1px solid blue;
}
.site-header-cart .cart-contents .cart-button .cart-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 15px;
  color: #fff;
  background-color: #999;
  border-radius: 100%;
  font-size: 0.563rem;
  line-height: 15px;
  text-align: center;
}

结果:


的数据

tvmytwxo

tvmytwxo1#

display: flex沿着与align-items: centerjustify-content: center一起添加到.site-header-cart .cart-contents .cart-button选择器中,将使SVG图标在其容器中正确对齐。

.site-header-cart {
  position: relative;
  margin: 0;
  padding: 0;
  list-style: none;
}
.site-header-cart .cart-contents {
  display: inline-block;
  background-color: transparent;
  height: auto;
  width: auto;
  padding: 10px 0;
  color: #222;
  font-size: 0.875rem;
  text-decoration: none;
  border: 1px solid lightgray;
}
.site-header-cart .cart-contents .cart-button {
  position: relative;
  display: flex; /* Added this */
  align-items: center; /* Added this */
  justify-content: center; /* Added this */
  border: 1px solid red;
}
.site-header-cart .cart-contents .cart-button svg {
  display: inline-block;
  max-width: 100%;
  fill: currentColor;
  border: 1px solid blue;
}
.site-header-cart .cart-contents .cart-button .cart-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 15px;
  color: #fff;
  background-color: #999;
  border-radius: 100%;
  font-size: 0.563rem;
  line-height: 15px;
  text-align: center;
}

个字符

cfh9epnr

cfh9epnr2#

你好,你可以添加dispaly:inline-table ou inline-block像这样:

.site-header-cart .cart-contents .cart-button {
     position: relative;
     display: inline-table;
     border: 1px solid red;
     }

字符串
see the result by clicking here

chy5wohz

chy5wohz3#

在span wrapper中使用display: inline-blockline-height: 0

.site-header-cart .cart-contents .cart-button {
  position: relative;
  border: 1px solid red;
  display: inline-block;
  line-height: 0;
}

字符串

相关问题