html - 在 div 中将三角形居中
问题描述
我试图在我的 div 中将一个黑色的右指向三角形居中,但没有运气。三角形应该用作按钮。我希望三角形在盒子内看起来像这样:
目前,我的三角形在盒子的最左边。如何解决此问题以使其垂直和水平居中?
我的代码:
.arrow-right {
width: 0;
height: 0;
border-style: solid;
border-width: 10px 0 10px 17.3px;
border-color: transparent transparent transparent black;
background-color: #ffcd11;
}
<div class="arrow">
<button class = "arrow-right"></button>
</div>
解决方案
一种解决方案是(如果容器内已有箭头)。使用绝对/相对位置。并将其定位在父元素的中间。
.arrow-right {
width: 0;
height: 0;
border-style: solid;
border-width: 10px 0 10px 17.3px;
border-color: transparent transparent transparent black;
background: yellow;
position:absolute;
top: calc(50% - 10px);
left: calc(50% - 8.65px);
}
.arrow {
width:200px;
height: 100px;
background: yellow;
position:relative;
}
<div class="arrow">
<button class = "arrow-right"></button>
</div>
推荐阅读
- spring-mvc - 如何在 Spring 控制器方法参数中添加自定义参数(如:@RequestParam、Model)?
- git - 为什么拉取请求会在变基后显示额外的提交?
- java - 可变列表的 java 到 kotlin 语言翻译问题
- http - 为什么使用 HTTP 协议很难实现实时连接?
- grads - 在 Grads 中打开 Shapefile
- javascript - 带有经典模块的 TypeScriptResolution 工作不正确
- python - 将字典中的值转换为矩阵
- javascript - 从 regex () 中获取字符串的行为不符合预期
- xml - xslt 根据标准和 groupBy 删除记录
- php - Wordpress 出现问题,在主目录中生成随机文件和代码