显示 flex 水平居中问题

Display flex horizontally centering issue

我在 div 中将元素居中时遇到问题。

我正在使用 display flexalign-selfjustify-self 来居中。

我认为这样就可以了,但是当我从网络浏览器检查时,我有一个未知元素。

你可以看到右边有一个不知道从哪里来的条纹元素

这是用网格制作的父元素。

您可以从 fiddle 此处

查看问题

https://jsfiddle.net/Martin40/qtjphvga/3/

这是我的 CSS

.customer_card{
    background: green;
    padding: 10px;
    margin-bottom: 20px;
    border-radius: 10px;
    display: grid;
    grid-template-columns: 20% 80%;
    gap: 0px 10px;
    justify-content: center;
    justify-items: stretch;
    align-items: center;
}

.customer_card_logo {
    display: flex;
    position: relative;
    justify-self: center;
    text-align: center;
    background: grey;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    color: white;
    font-weight: bold;
    cursor: pointer;
}

.customer_card_logo > .material-icons-outlined {
    align-self: center;
    justify-self: center;
    font-size: 2rem;
    transition: all 0.4s;
}
.customer_card_logo:hover > .material-icons-outlined {
    transform: rotate(90deg);
}

.customer_card_name {
    display: block;
    font-weight: 600;
    font-size: 1.5rem;
}

.customer_card_addresse {
    font-size: 1.2rem;
}

这是我的 HTML

<head><link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined|Material+Icons+Two+Tone|Material+Icons+Round|Material+Icons+Sharp" rel="stylesheet"></head>

<div class="customer_card">
<div class="customer_card_logo"><span class="material-icons-outlined"> autorenew </span></div>
<div class="customer_card_informations">
    <span class="customer_card_name">Company Name</span>
    <span class="customer_card_addresse">Adresse <br> ZIP CITY NAME</span>
</div>

有人知道问题出在哪里吗? 谢谢!

“条纹元素”只是您在此处 .customer_card 定义的网格间隙(不会导致居中问题):

gap: 0px 10px;

要使图标居中,请将 width/height-based 方法替换为使用 padding 方法。这样,您就不必担心可能导致错位的图标大小:

.customer_card_logo {
    /* width: 50px; */
    /* height: 50px; */
    padding: 0.5rem;
}

试试下面修改后的代码段:

.customer_card {
  background: green;
  padding: 10px;
  margin-bottom: 20px;
  border-radius: 10px;
  display: grid;
  grid-template-columns: 20% 80%;
  gap: 0px 10px;
  justify-content: center;
  justify-items: stretch;
  align-items: center;
}

.customer_card_logo {
  display: flex;
  position: relative;
  justify-self: center;
  text-align: center;
  background: grey;
  border-radius: 50%;
  #width: 50px;
  #height: 50px;
  padding: 0.5rem;
  color: white;
  font-weight: bold;
  cursor: pointer;
}

.customer_card_logo>.material-icons-outlined {
  align-self: center;
  justify-self: center;
  font-size: 2rem;
  transition: all 0.4s;
}

.customer_card_logo:hover>.material-icons-outlined {
  transform: rotate(90deg);
}

.customer_card_name {
  display: block;
  font-weight: 600;
  font-size: 1.5rem;
}

.customer_card_addresse {
  font-size: 1.2rem;
}
<head>
  <link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined|Material+Icons+Two+Tone|Material+Icons+Round|Material+Icons+Sharp" rel="stylesheet">

</head>

<div class="customer_card">
  <div class="customer_card_logo" title="Changer de client"><span class="material-icons-outlined"> autorenew </span></div>
  <div class="customer_card_informations">
    <span class="customer_card_name">Company Name</span>
    <span class="customer_card_addresse">Adresse <br> ZIP CITY NAME</span>
  </div>
</div>

<!-- language: lang-css -->

.customer_card {
    background: green;
    padding: 10px;
    margin-bottom: 20px;
    border-radius: 10px;
    display: grid;
    grid-template-columns: 20% 80%;
    gap: 0px 10px;
    justify-content: center;
    justify-items: stretch;
    align-items: center;
}

.customer_card_logo {
    display: flex;
    position: relative;
    justify-content: center;
    text-align: center;
    background: grey;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    color: white;
    font-weight: bold;
    cursor: pointer;
}

.customer_card_logo > .material-icons-outlined {
    align-self: center;
    justify-self: center;
    font-size: 2rem;
    transition: all 0.4s;
}

.customer_card_logo:hover > .material-icons-outlined {
    transform: rotate(90deg);
}

.customer_card_name {
    display: block;
    font-weight: 600;
    font-size: 1.5rem;
}

.customer_card_addresse {
    font-size: 1.2rem;
}

<!-- language: lang-html -->

    <head>
        <link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined|Material+Icons+Two+Tone|Material+Icons+Round|Material+Icons+Sharp" rel="stylesheet">

    </head>
    <body>
    <div class="customer_card">
        <div class="customer_card_logo" title="Changer de client"><span class="material-icons-outlined"> autorenew </span></div>
        <div class="customer_card_informations">
            <span class="customer_card_name">Company Name</span>
            <span class="customer_card_addresse">Adresse <br> ZIP CITY NAME</span>
        </div>
    </div>

</body>

<!-- end snippet -->

谢谢大家。 我没有谈论网格的间隙,而是 flex 中的剥离元素 ;)

justify-content: center;

关于customer_card_logo保存的问题。 谢谢!

在带有显示网格的 div 中使用内联元素跨度会产生该间隙。通过将宽度 100 百分比添加到您的跨度将修复它。

您可以在下面找到代码。

    .customer_card{
        background: green;
        padding: 10px;
        margin-bottom: 20px;
        border-radius: 10px;
        display: grid;
        grid-template-columns: 20% 80%;
        gap: 0px 10px;
        justify-content: center;
        justify-items: stretch;
        align-items: center;
    }

    .customer_card_logo {
        display: flex;
        position: relative;
        justify-self: center;
        text-align: center;
        background: grey;
        border-radius: 50%;
        width: 50px;
        height: 50px;
        color: white;
        font-weight: bold;
        cursor: pointer;
    }

    .customer_card_logo > .material-icons-outlined {
        width: 100%;
        align-self: center;
        justify-self: center;
        font-size: 2rem;
        transition: all 0.4s;
    }
    .customer_card_logo:hover > .material-icons-outlined {
        transform: rotate(90deg);
    }

    .customer_card_name {
        display: block;
        font-weight: 600;
        font-size: 1.5rem;
    }

    .customer_card_addresse {
        font-size: 1.2rem;
    }
        <head>
            <link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined|Material+Icons+Two+Tone|Material+Icons+Round|Material+Icons+Sharp" rel="stylesheet">

        </head>
        
        <div class="customer_card">
            <div class="customer_card_logo" title="Changer de client"><span class="material-icons-outlined"> autorenew </span></div>
            <div class="customer_card_informations">
                <span class="customer_card_name">Company Name</span>
                <span class="customer_card_addresse">Adresse <br> ZIP CITY NAME</span>
            </div>
        </div>

https://jsfiddle.net/3b1kLzv2/5/