html - 在 div 中放置按钮
问题描述
我试图将这 8 个按钮定位在正确的位置(所以左按钮在矩形的左侧,右按钮在右侧等等)。我是否需要为每个按钮设置一个 div,然后将其放置在“camButton”内,还是可以在没有这么多 div 的情况下完成?
.button {
align-items: center;
display: flex;
justify-content: center;
padding: 4px;
}
.button__arrow {
background-color: transparent;
height: 10px;
width: 10px;
}
.button__arrow--up {
border-left: 2px solid rgba(127, 0, 255);
border-top: 2px solid rgba(127, 0, 255);
transform: translateY(25%) rotate(45deg);
}
<div id="camButtonsParent">
<div id="camButtons">
<button class="button">
<div class="button__arrow button__arrow--up"></div>
</button>
</div>
</div>
解决方案
希望这能解决您的问题并帮助您按照需要对齐按钮。
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}
body {
height: 100vh;
display: grid;
place-items: center;
}
.container {
position: relative;
width: 300px;
height: 300px;
display: grid;
grid-template-rows: auto;
grid-template-columns: repeat(3, 1fr);
}
.container button {
color: white;
display: grid;
font-size: 1rem;
position: relative;
place-items: center;
border: 1px solid white;
}
.container button:nth-child(odd) {
background-color: red;
}
.container button:nth-child(even) {
background-color: green;
}
<div class="container">
<button>Top Left</button>
<button>Top</button>
<button>Top Right</button>
<button>Left</button>
<button>Center</button>
<button>Right</button>
<button>Bottom Left</button>
<button>Bottom</button>
<button>Bottom Right</button>
</div>
创建与SVG
图标相同。就像我在评论中提到的那样。
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}
body {
height: 100vh;
display: grid;
place-items: center;
}
.container {
position: relative;
width: 300px;
height: 300px;
display: grid;
grid-template-rows: auto;
grid-template-columns: repeat(3, 1fr);
}
.container button {
color: white;
display: grid;
position: relative;
place-items: center;
border: 1px solid white;
}
.container button i {
font-size: 4rem;
}
.container button:nth-child(1) i,
.container button:nth-child(9) i {
transform: rotate(315deg);
}
.container button:nth-child(3) i,
.container button:nth-child(7) i {
transform: rotate(45deg);
}
.container button:nth-child(odd) {
background-color: red;
}
.container button:nth-child(even) {
background-color: green;
}
<link href='https://unpkg.com/boxicons@2.0.9/css/boxicons.min.css' rel='stylesheet'>
<div class="container">
<button>
<i class='bx bx-chevron-up'></i>
</button>
<button>
<i class='bx bx-chevron-up'></i>
</button>
<button>
<i class='bx bx-chevron-up'></i>
</button>
<button>
<i class='bx bx-chevron-left'></i>
</button>
<button>
<i class='bx bxs-circle'></i>
</button>
<button>
<i class='bx bx-chevron-right'></i>
</button>
<button>
<i class='bx bx-chevron-down'></i>
</button>
<button>
<i class='bx bx-chevron-down'></i>
</button>
<button>
<i class='bx bx-chevron-down'></i>
</button>
</div>
推荐阅读
- python - 'float' 对象在我的游戏中不可调用
- apache-nifi - QueryRecord - 获取最大时间戳
- azure-devops - 定期发送包含查询结果的电子邮件(每天早上 9 点):
- cadence-workflow - Autoscale Cadence 客户端消耗数百万个活动或运行数百万个工作流实例
- google-cloud-platform - Google Cloud 平台中的 Devops 项目管理板
- java - 在 Android 中使用 base64 字符串创建 JSONObject 时遇到致命异常
- python - Python XPath 排除项目
- r - AWS 多因素身份验证和闪亮服务器
- javascript - 如何避免/删除 .row 中的空格
- github - 在 GitHub 上的 Markdown 文件中应用颜色