有没有办法让以下内容:
#id {
border-radius: 100px;
width: 100px;
height: 100px;
}
<div id="circle">
<h3>Hello</h3>
</div>
一个圆形的 div
,文字在垂直和水平方向上居中。同时保持圆圈的响应式。
我的意思是让它有一个例如包含元素宽度的50%
的width
,同时保持height
以相同的百分比使其成为圆形。
将静态的100px
更改为百分比会使圆变成椭圆。
有没有办法让以下内容:
#id {
border-radius: 100px;
width: 100px;
height: 100px;
}
<div id="circle">
<h3>Hello</h3>
</div>
一个圆形的 div
,文字在垂直和水平方向上居中。同时保持圆圈的响应式。
我的意思是让它有一个例如包含元素宽度的50%
的width
,同时保持height
以相同的百分比使其成为圆形。
将静态的100px
更改为百分比会使圆变成椭圆。
padding-bottom
属性来保持圆形的宽高比(更多信息请参见此处)transform:translate(-50%,-50%);
将内容在圆形中垂直和水平居中(请参见此答案中的方法1).circle{
position:relative;
width:50%;
padding-bottom:50%;
background:gold;
border-radius:50%;
}
.circle h3{
position:absolute;
top:50%; left:50%;
transform: translate(-50%, -50%);
margin:0;
}
<div class="circle">
<h3>Hello</h3>
</div>
注意,您需要添加供应商前缀到 transform 属性以最大化浏览器支持(请参见 canIUse 了解更多信息)。
如果您使用相同的视口单位(vw
或vh
),那么您应该获得一个响应式的圆形。
100视口单位将是宽度或高度的100%。因此,这非常类似于使用百分比。
div {
width: 10vw;
height: 10vw;
border-radius: 50%;
background: blue;
}
<div></div>
.circle {
width: 100%;
border-radius: 50%;
color: white;
background-color: blue;
text-align: center;
aspect-ratio: 1 / 1;
}
aspect-ratio
的属性,它适用于元素的宽度和高度。@media
规则用于媒体查询,以应用不同的样式/大小/宽度/高度,适用于不同的媒体类型/设备。
媒体查询可用于检查许多内容,例如:
视口的宽度和高度 设备的宽度和高度 方向(平板电脑/手机是横向还是纵向模式?) 分辨率
请参见以下示例:
/* Mobile & small screen size devices*/
@media only screen and (max-width: 600px) {
.circle {
width: 210px!important;
height: 210px!important;
}
}
/* Desktop, Laptop, Tablet */
@media only screen and (min-width: 650px) {
.circle {
width: 356px;
height: 356px;
}
}
.circle {
margin: auto;
width: 356px;
height: 356px;
opacity: 1;
border-radius: 50%;
background-color: gray;
}
<div class="circle"></div>
vw
或vh
单位。示例 - insertusernamehere