css 制作响应式椭圆块

css make responsive oval block

我正在尝试为下图中显示的数字制作 css 块。我的想法/目标是制作一个响应式块,这样如果有一个数字,它将是圆形的,如果有两个,则像第二个一样。我曾尝试制作 border-radius: 50% 所以我成功完成的第一个块与 border-radius: 50%

图像中的不一样

所以我的问题是可以用一个 class 块或每个按钮(左 | 右)做出这样的结果我需要为每个块写特殊的 class 吗?

椭圆使用 100%:

border-radius: 100%;

对于体育场,使用较大的 px 值:

border-radius: 9999px;

例子

.round{
  display: inline-block;
  width:50px;
  height:50px;
  background: red;
  border-radius: 100%;
  margin: 10px;
}

.ellipse,.stadium{
  width: 80px;
}

.stadium{
  border-radius: 9999px;
}
<div class="round circle"></div>
<div class="round ellipse"></div>
<div class="round stadium"></div>

div{
 height:50px;
 width:50px;
 border-radius:9999px;
 background:red;
 text-align:center;
 display:inline-block;
 line-height:3em;
 font-weight:bold;
 font-size:16px;
  }
<div>2</div>
 <div>28</div>

固定高度解决方案

为此,您需要 "fixed" 高度(否则,您需要使用 jquery 来计算)。

你需要做的是这样的事情;

html,body{background:#222;}
div {
  margin:10px;
  display: inline-block;
  height: 30px;
  border-radius: 25px;
  background: lightblue;
  font-size: 30px;
  min-width: 30px;
  text-align: center;
  line-height: 30px;
  padding: 10px;
  position:relative;
  color:blue;
}
div:before{
  content:"";
  position:absolute;
  left:0;
  top:-10px;
  width:100%;
  border-top:3px solid tomato;
  }
<div>1</div>
<div>123</div>

注意:为此 border-radius 应设置为整体高度的一半。

我还包括了一个最小宽度以确保它始终至少是一个圆。


JQuery非固定高度的解决方案

$(document).ready(function() {
  $('div').each(function(index) {
    var height = $(this).height();
    $(this).css("border-radius", height + "px");
  });
});
html,
body {
  background: #222;
}
div {
  margin: 10px;
  display: inline-block;
  border-radius: 25px;
  background: lightblue;
  font-size: 30px;
  min-width: 30px;
  text-align: center;
  line-height: 30px;
  padding: 10px;
  position: relative;
  vertical-align:top;
  color: blue;
}
div:before {
  content: "";
  position: absolute;
  left: 0;
  top: -10px;
  width: 100%;
  border-top: 3px solid tomato;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>1</div>
<div>123</div>

<div>Not a set height,
  <br/>either :)</div>