【问题标题】:Display flex horizontally centering issue显示 flex 水平居中问题
【发布时间】:2022-02-04 19:49:42
【问题描述】:

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

我正在使用 display flex、align-self 和 justify-self 使其居中。

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

你可以在右边看到,有一个我不知道它来自哪里的条纹元素。

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

您可以从这里的小提琴中看到问题

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>

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

【问题讨论】:

  • 问题?什么问题?您在父级中定义了 gap。
  • 这不是一个元素,它是浏览器突出显示的间隙/空间
  • 不清楚您的问题是什么-如果是徽标圈中的间隙-那是因为您的圈子是50px,而您的图标是32px-如果要使图标居中,只需添加align-items:center; justify-content:center;到你的标志

标签: css flexbox center display


【解决方案1】:

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

gap: 0px 10px;

要使图标居中,请将基于宽度/高度的方法替换为使用padding。这样,您就不必担心图标的大小会导致错位:

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

试试下面修改后的sn-p:

.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>

【讨论】:

    【解决方案2】:
    <!-- 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 -->
    

    【讨论】:

    • 欢迎来到 SO,在回答问题时,请不要只输入大量代码 - 请尝试解释您所做的更改以及它如何解决问题
    【解决方案3】:

    在带有显示网格的 div 中使用内联元素 span 会产生这种差距。通过将宽度 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/

    【讨论】:

      【解决方案4】:

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

      justify-content: center;
      

      在 customer_card_logo 上保存了问题。 谢谢!

      【讨论】:

        猜你喜欢
        • 2014-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        • 2022-11-18
        • 2021-11-09
        • 1970-01-01
        • 2019-09-03
        相关资源
        最近更新 更多