如何在 div 的中间对齐 svg 圆圈



我需要做一个圆圈来模拟Mac OS中关闭窗口的按钮。但是当我尝试将其移低时,svg 的边框将其切断。如何移动边框以将圆圈直接放置在面板的中间(垂直(?

JSFiddle: https://jsfiddle.net/sm6r0kvn/2/

<div class="panel">
  <svg viewbox="0 0 20 17" width="20" heigth="50"><circle r="7" cx="14" cy="9.5" fill="#fc615e"/></svg>
</div>

您可以像<svg viewbox="0 0 20 20" width="20" heigth="20">一样设置视图框,然后将cx设置为 50% 并cy 50%。如果您想将其垂直居中panel,只需将其设为弹性框并添加align-items: center - 请参阅下面的演示:

.block {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: 550px;
  min-width: 480px;
  width: 80vw;
  height: 200px;
  background: black;
  border-radius: 10px 10px 5px 5px;
}
.panel {
  background-color: #e0e0e0;
  border-radius: 5px 5px 0 0;
  display: flex; /* added */
  align-items: center; /* added */
}
.text {
  color: white;
  padding: 5px;
}
<div class="block">
  <div class="panel">
    <svg viewbox="0 0 20 20" width="20" heigth="20">
      <circle r="7" cx="50%" cy="50%" fill="#fc615e"/>
    </svg>
  </div>
  <div class="text">
    Text here
  </div>
</div>

这是因为您在 svg 视图框之外绘制了 cicrcle。您可以使用CSS移动整个svg框或使其变大

svg {
  border: 1px blue solid;
}
.panel.moved {
  margin-left: 100px;
}
<div class="panel">
  <svg viewbox="0 0 20 20" width="200" heigth="200">
    <circle r="10" cx="10" cy="10" fill="#fc615e"/>
  </svg>
</div>
<div class="panel">
  <svg viewbox="0 0 20 20" width="200" heigth="200">
    <circle r="10" cx="20" cy="10" fill="#fc615e"/>
  </svg>
</div>
<div class="panel">
  <svg viewbox="0 0 30 20" width="300" heigth="200">
    <circle r="10" cx="20" cy="10" fill="#fc615e"/>
  </svg>
</div>
<div class="panel moved">
  <svg viewbox="0 0 20 20" width="200" heigth="200">
    <circle r="10" cx="10" cy="10" fill="#fc615e"/>
  </svg>
</div>