html - 如何使用css使内圆居中

标签 html css

div.container{
  border:1px solid red;
  position:relative;
}
span.parent{
  display:inline-block;
  width:30px;
  height:30px;
  border:1px solid gray;
  border-radius:50%;
  text-align: center;
  position:absolute;
  right:20px;
  top:10px;
}

span.child{
  background:green;
  width:80%;
  height:80%;
  display:inline-block;
  border-radius:50%;
  left:10%;
  top:10%;
  position:relative;
}
<div class="container">
  <p>some info goes here</p>
  <span class="parent"><span class="child"></span></span>
</div>

我正在尝试创建一个带边框的实心圆,但填充没有正确居中。如何解决这个问题?

最佳答案

另一种方法是使用 Flexbox。

如果您将以下内容添加到您的 parent :

display: flex;
align-items: center;
justify-content: center;

并且移除子元素的相对定位,子元素将在父元素内居中。

div.container{
  border:1px solid red;
  position:relative;
}
span.parent{
  display: flex;
  align-items: center;
  justify-content: center;
  width:30px;
  height:30px;
  border:1px solid gray;
  border-radius:50%;
  text-align: center;
  position:absolute;
  right:20px;
  top:10px;
}

span.child{
  background:green;
  width:80%;
  height:80%;
  display:inline-block;
  border-radius:50%;
}
<div class="container">
  <p>some info goes here</p>
  <span class="parent"><span class="child"></span></span>
</div>

编辑:如果您遇到 flexbox 圆圈在较小分辨率下被挤压的问题,请尝试使用 min-heightmin-width,并使用 margin: auto 代替 display: flex 居中。

关于html - 如何使用css使内圆居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46684565/

相关文章:

css - 如何消除渲染阻塞资源(app.css)?

javascript - Google Analytics 'Event Tracking' 对于我在其他网站上点击的下载链接

javascript - 从表单中读取数据,并使用它创建对象

css - 背景不在一页上覆盖网站

html - CSS:使 'list-style-image' 在图像本身上的位置在悬停时发生变化

html - 导航 li 样式在 Chrome 或 Safari 中不呈现,在 Firefox 中正常

CSS 不透明动画不起作用

php - 如果数量列的数值小于或等于10,如何更改表格行的背景颜色

javascript - 如何在页面加载时自动打开电子邮件

javascript - 折叠可见时,如何通过点击 body 隐藏我的折叠 Bootstrap 3 导航栏?