html - 根据按钮位置动态标注位置
问题描述
我正在开发一个界面,该界面允许用户更改按钮位置并在按钮下编写标签文本。我的问题是,标签并不总是在按钮下方居中,这取决于文本标签的大小和字母数量,
这是我的 CSS 课程
.button-wrapper {
display: inline-block;
max-width: 80px;
text-align: center;}
.button-wrapper > button {
display: block;
height: 75px;
width: 75px;
border-radius: 50%;
position: relative;
border: 1px solid gray;
text-align: center;
:active {border: 3px solid yellow;}
:hover{border: 2px solid red;}}
.button-wrapper > label {
display: block;
position: relative;
font-size: 14px;
margin:0 auto; left:0; right:0;
padding: 3px;}
这是我的 html 代码
<div class="button-wrapper">
<button
[class.btn-primary]="true"
[style.color]="buttonStyle.labeLFontColor"
[ngStyle]="{'margin-left': bar.accessPosition+'%','font-style': buttonStyle.labeLFontFamily, 'font-size.px':buttonStyle.labeLFontSize, 'font-weight':buttonStyle.labeLFontWeight,
'background-color': buttonStyle.backgroundColor, 'font-family': buttonStyle.labeLFontFamily, 'border-color': buttonStyle.borderColor,
'border': buttonStyle.borderSize+'px solid', 'background':'url(assets/showcase/images/accessButtons/' + bar.accessBackImage+') white center center no-repeat',
'background-size': '100% 75px'}">
</button>
<label *ngIf="useLabel" [ngStyle]="{'margin-left': bar.accessPosition+'%','font-style': buttonStyle.labeLFontFamily, 'font-size.px':buttonStyle.labeLFontSize, 'font-weight':buttonStyle.labeLFontWeight,'font-family': buttonStyle.labeLFontFamily }">{{accessLabel}}</label>
</div>
如您所见,标签大小是动态的,标签文本也有输入来编写文本...那么如何将标签固定为始终居中并低于用户所写的任何内容。谢谢你。
解决方案
根据您提供的stacblitz 链接,以下 CSS 可能是您想要实现的(只需将您的 CSS 替换为此):
.button-wrapper {
display: grid;
justify-items: center;
}
.button-wrapper > button {
display: block;
height: 75px;
width: 75px;
border-radius: 50%;
position: relative;
border: 1px solid gray;
}
.button-wrapper > button:hover {
border: 2px solid red;
}
.button-wrapper > button:focus {
border: 3px solid yellow;
}
.button-wrapper > label {
border: 1px solid red;
display: block;
position: relative;
font-size: 14px;
margin: 0 auto;
left: 0;
right: 0;
padding: 3px;
}
笔记:
- 你的一些 CSS 结构很糟糕
- 对于任何布局和定位,请考虑CSS Grid
推荐阅读
- php - Symfony 4:创建添加实体表单时出错(@ParamConverter 注释找不到对象)
- bash - `cat` 命令的正确语法和用法?
- powerbi - 从表中返回查找值的函数
- node.js - Node.Js 应用无法成功部署到 Heroku
- java - 外部化 Grails 应用程序的运行配置
- ruby-on-rails - 如何使用控制器将对象传递给部分?
- docker - 项目中具有不同上下文的多个 Dockerfile
- node.js - 如何使 404 路由在 React-Router 中工作?
- powershell - 使用子字符串使用powershell在另一个变量上查找变量
- symfony - 如何使用 Twig 在变量上添加动态属性?