html - 不使用 div 包裹的圆形按钮
问题描述
我想做圆形按钮。在这个片段中,当屏幕很窄以至于两个按钮都不完全适合时,按钮 A 开始挤压,而按钮 B 仍然是一个圆圈(我想要的)。按钮 B 用 div 包裹,按钮 A 没有。
两个问题:
a) 为什么简单地用 div 包裹按钮 B 会使其行为不同?
b)如果可能的话,我怎样才能在没有额外 div 的情况下获得所需的行为(按钮 B)?
.counter {
display: flex;
margin-top: 10pt;
background-color: #444444;
justify-content: space-around;
align-items: center;
border-radius: 60pt;
width: 100%;
padding: 5pt;
}
button {
outline: none;
}
.btn {
background-color: #222222;
border-radius: 50%;
border: 1pt solid white;
width: 50pt;
height: 50pt;
display: inline-block;
}
.btn-text {
color: white;
font-size: 14pt;
text-align: center;
}
.btn:active {
background-color: #444444;
}
<div class="counter">
<button class="btn"><span class="btn-text">A</span></button>
<div class="btn-div">
<button class="btn"><span class="btn-text">B</span></button>
</div>
</div>
解决方案
这是因为宽度随着容器调整,使其也压缩。
您必须设置min-width和min-height以确保宽度不会低于您所需的宽度并防止其缩小
.counter {
display: flex;
margin-top: 10pt;
background-color: #444444;
justify-content: space-around;
align-items: center;
border-radius: 60pt;
width: 100%;
padding: 5pt;
}
button {
outline: none;
}
.btn {
background-color: #222222;
border-radius: 50%;
border: 1pt solid white;
min-width: 50pt;
min-height: 50pt;
display: inline-block;
}
.btn-text {
color: white;
font-size: 14pt;
text-align: center;
}
.btn:active {
background-color: #444444;
}
<div class="counter">
<button class="btn"><span class="btn-text">A</span></button>
<div class="btn-div">
<button class="btn"><span class="btn-text">B</span></button>
</div>
</div>
检查这个小提琴
来源:最小/最大宽度与宽度
推荐阅读
- ruby-on-rails - Ruby missing psych for YAML ouput
- doxygen - 如何真正关闭 Doxygen 中的 LaTex 输出
- html - 防止文本比 flex 容器中的图像宽
- ruby-on-rails - Chrome doesn't start in WSL (Ubuntu 18.04) with "DevToolsActivePort file doesn't exist" under Chromedriver
- php - Remote mail() function
- swiftui - 如何在 SwiftUI 中将字体粗细更改为中等?
- python - 无效的凭据引发身份验证突变异常
- vue.js - 类型错误:e 不是函数
- javascript - 将 javascript 中的 Protobuf 模式转换为单独的 proto 定义文件
- laravel - 更改值状态时的 ID 问题