Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

CSS怎样做出自适应圆形边框?

image
想做这样一个未处理订单提醒的数字提示 想让它始终居中 边框是圆形的
现在这样写的话宽度可以随着数字长度自适应 但是怎样才能做出自适应圆形边框呢?
image

<view class="yuncang2">
    2222
</view>
.yuncang2{
  position: absolute;
  display:flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: 1px solid red;
  top: 20rpx;
  color: red;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

image.png
这个是不是你要的效果?根据文字长度自动变化圆的大小?
css代码

.box{
            display: inline-flex;
            justify-content: center;
            position: absolute;
        }
        .box1{
            padding-top: calc(100% + 20px);
            visibility: hidden;
            min-width: 12px;
            min-height: 12px;

        }
        .box2{
            position: absolute;
            padding-top: calc(100% + 20px);
            width: calc(100% + 20px);
            display: flex;
            justify-content: center;
            align-items: center;
            border: 1px solid red;
            border-radius: 50%;
        }
        .box3{
            position: absolute;
            text-align: center;
            top: 50%;
            transform: translateY(-50%);
        }

html代码

<div class="box">
    <div class="box1">2</div>
    <div class="box2">
        <div class="box3">2</div>
    </div>
</div>

这里 保持 box1 和 box3中的 文本内容一致即可。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...