html - 为什么我不能让按钮的高度等于它们的宽度?

标签 html css css-float height

<分区>

在我的网页上应该有一个带有方形按钮的面板(在左侧,屏幕宽度的 25%)。 它应该是这样的: My UI's design
但目前这些按钮是水平拉伸(stretch)的。我需要以某种方式垂直拉伸(stretch)它们。
我尝试设置 min-width: 100%; height: auto 使 height=width,但它什么也没做。还尝试使用 jquery 来均衡它们 - 仍然没有。

这是我的代码:

#left_panel {
  float: left;
  width: 25%;
  text-align: center;
}

.btn {
  border-radius: 25px;
  padding: 1%;
  margin: 1%;
  background-color: green;
}

.item {
  border-radius: 10px;
  float: right;
  width: 71%;
  padding: 1%;
  margin: 1%;
  background-color: grey;
}

checkbox {
  display: inline-block;
  width: auto;
}

.name {
  display: inline-block;
  background-color: white;
  width: auto;
}

.del {
  border-radius: 25px;
  display: inline-block;
  float: right;
  background-color: red;
  width: auto;
}
<div id="left_panel">
  <div class="btn" id="open">Open</div>
  <div class="btn" id="add">Add</div>
  <div class="btn" id="info">Info</div>
</div>
<div id="list">
  <!--space for list-->
</div>

最佳答案

您可以使用 padding-bottom: 100% 方法使它们成为正方形。

#left_panel {
  float: left;
  width: 25%;
  text-align: center;
}

.btn {
  border-radius: 25px;
  padding: 1%;
  margin: 1%;
  background-color: green;
  position: relative;
}
.btn span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.btn::after {
  content: '';
  display: block;
  padding-bottom: 100%;
}

.item {
  border-radius: 10px;
  float: right;
  width: 71%;
  padding: 1%;
  margin: 1%;
  background-color: grey;
}

checkbox {
  display: inline-block;
  width: auto;
}

.name {
  display: inline-block;
  background-color: white;
  width: auto;
}

.del {
  border-radius: 25px;
  display: inline-block;
  float: right;
  background-color: red;
  width: auto;
}
<div id="left_panel">
  <div class="btn" id="open"><span>Open</span></div>
  <div class="btn" id="add"><span>Add</span></div>
  <div class="btn" id="info"><span>Info</span></div>
</div>
<div id="list">
  <!--space for list-->
</div>

关于html - 为什么我不能让按钮的高度等于它们的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52312419/

上一篇:css - 不能动画兄弟元素

下一篇:css - Jekyll 元素不一致

相关文章:

html - 左 Div 固定,多个右 Div 可滚动

jquery - 使用 jquery 查找隐藏的选项

css - Jquery UI 日期选择器显示不正确

css - 如何使溢出元素不增加 flexbox 的高度?

CSS清除:both and width 100%?

css - 将 Bootstrap 网格划分为子网格

javascript - 如何以原始大小显示此图像,但仅在特定元素内显示?

css - 将多个 float div 并排居中

html - 向左/向右浮动 div,而不在大窗口上将它们分开

javascript - 如何使滚动条居中?