【问题标题】:hr vertical centered in a divhr 在 div 中垂直居中
【发布时间】:2022-11-04 20:35:16
【问题描述】:

如何在 div 中垂直居中 HR 标签,我试过了,但它没有完美居中

.container {
    position: relative;
    width: 80vw;
    height: 40vh;
    border: solid 4px #333;
}

hr {
    height: 5px;
    background-color: cadetblue;
    position: absolute;
    right: 0;
    top: 50%;
    left: 0;
    transform: translateY(-100%);
}
<div class="container">
  <hr>
</div>

【问题讨论】:

    标签: html css


    【解决方案1】:

    将transform 更改为translateY(-50%); 并将margin: 0; 添加到hr:

    .container {
        position: relative;
        width: 80vw;
        height: 40vh;
        border: solid 4px #333;
    }
    
    hr {
        height: 5px;
        background-color: cadetblue;
        position: absolute;
        right: 0;
        top: 50%;
        bottom: 50%;
        left: 0;
        transform: translateY(-50%);
        margin: 0;
    }
    <div class="container">
      <hr>
    </div>

    【讨论】:

      猜你喜欢
      • 2014-04-05
      • 2011-07-07
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 2013-04-11
      • 2014-09-16
      • 1970-01-01
      • 2011-03-22
      相关资源
      最近更新 更多