【发布时间】:2015-05-16 21:06:41
【问题描述】:
我正在尝试为下图中显示的数字制作一个 css 块。我的想法/目标是制作一个响应式块,所以如果有一个数字,它将是圆形的,如果有两个,那么就像第二个。我已经尝试过让border-radius: 50% 所以我第二个成功做的第一个块不像border-radius: 50% 的图像中那样
所以我的问题是可以用一个类块或每个按钮(左|右)产生这样的结果,我需要为每个块编写特殊类吗?
【问题讨论】:
标签: css css-shapes
我正在尝试为下图中显示的数字制作一个 css 块。我的想法/目标是制作一个响应式块,所以如果有一个数字,它将是圆形的,如果有两个,那么就像第二个。我已经尝试过让border-radius: 50% 所以我第二个成功做的第一个块不像border-radius: 50% 的图像中那样
所以我的问题是可以用一个类块或每个按钮(左|右)产生这样的结果,我需要为每个块编写特殊类吗?
【问题讨论】:
标签: css css-shapes
为此,您将需要一个“固定”高度(否则,您需要使用 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>
注意:边框半径应设置为整体高度的一半。
我还添加了一个最小宽度以确保它始终至少是一个圆圈。
$(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>
【讨论】:
.height() 方法 — 比如:.height() / 2 — as$(this).css("height") 不会返回高度的计算值(在这种特殊情况下,它会返回 60px,因为两行具有line-height 和30px 的文本)。
对于椭圆使用 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>
【讨论】: