Problem description:个 尽管按钮元素的上边距相等,但它们之间存在垂直偏移.

如果边距-顶部:属性更改,则整行按钮都会移动,而不是移动一个元素.

I have tried:

  • 不同的显示属性(display: inline;&display: inline-block;)
  • position: relative;

The next solution I would think of:

将属性position:absolute;分配给按钮,然后分别 for each 元素定义页边距.
但这不是一个优雅的解决方案(如果你有 idea ,我会寻找一个).

Picture with the described problem:

enter image description here

.VPcalc {
  width: 550px;
  height: 50px;
  border: 2px solid blue;
  margin-top: -5px;
  background-color: lightgrey;
}

div input {
  width: 50px;
  border: 2px solid grey;
}

button#btnR1 {
  display: inline-block;
  background-color: darkblue;
  opacity: 0.5;
  border-radius: 5px;
  border: 2px solid black;
  width: 100px;
  height: 20px;
  margin-top: 15px;
  margin-left: 15px;
}

button#btnR2 {
  display: inline-block;
  background-color: darkred;
  opacity: 0.5;
  border-radius: 5px;
  border: 2px solid black;
  width: 100px;
  height: 20px;
  margin-top: 15px;
  margin-left: 15px;
}

button#btnR1:hover,
button#btnR2:hover {
  background-color: white;
  color: black;
}

div input#tag {
  display: inline-block;
  background-color: white;
  opacity: 0.5;
  border-radius: 5px;
  border: 2px solid black;
  width: 100px;
  height: 20px;
  margin-top: 15px;
  margin-left: 15px;
}

div button#factor,
div button#calculation {
  display: inline-block;
  background-color: white;
  opacity: 0.5;
  border-radius: 5px;
  border: 2px solid black;
  width: 50px;
  height: 20px;
  margin-top: 15px;
  margin-left: 15px;
}

span#f1,
span#t1 {
  font-size: 12px;
}
<div class="VPcalc">
  <button id="btnR1" onClick="myFunction()">Neue Zahl</button>
  <button id="btnR2" onClick="auswerten()">Auswerten</button>

  <button id="factor"><span id="f1"></span></button>
  <button id="calculation"><span id="t1" ></span></button>
  <input type="number" id="tag"></input>
</div>

推荐答案

使用Flexbox实现这一点要容易得多:

.VPcalc {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  width: 550px;
  height: 50px;
  border: 2px solid blue;
  margin-top: -5px;
  background-color: lightgrey;
}

div input {
  width: 50px;
  border: 2px solid grey;
}

button#btnR1{
  
  background-color: darkblue;
  opacity: 0.5;
  border-radius: 5px;
  border: 2px solid black;
  width: 100px;
  height: 20px;
}

button#btnR2{
  background-color: darkred;
  opacity: 0.5;
  border-radius: 5px;
  border: 2px solid black;
  width: 100px;
  height: 20px;
}

button#btnR1:hover, button#btnR2:hover{
  background-color: white;
  color: black;
}

div input#tag{
  background-color: white;
  opacity: 0.5;
  border-radius: 5px;
  border: 2px solid black;
  width: 100px;
  height: 20px;
}

div button#factor, div button#calculation{
  background-color: white;
  opacity: 0.5;
  border-radius: 5px;
  border: 2px solid black;
  width: 50px;
  height: 20px;
}

span#f1, span#t1{
  font-size: 12px;    
}
<div class="VPcalc"> 
  <button id="btnR1" onClick="myFunction()">Neue Zahl</button> 
  <button id="btnR2" onClick="auswerten()">Auswerten</button> 
  <button id="factor" ><span id="f1"></span></button>
  <button id="calculation"><span id="t1" ></span></button>
  <input type="number" id="tag" ></input>
</div>

有一些选项可以将这些按钮隔开.我刚刚把它们放在一个空白处,但你可能更喜欢像justify-content: space-between这样的东西,而不是空白处.

Html相关问答推荐

HTML横幅未正确对齐页面顶部

html中背景色的全单元格R中的Rmarkdown表

restrict. form—text width to input s width'

如何在不影响子列表的情况下在HTML(OL Li)上同时加数字和列表?

为什么在CSS中字段是一个有效的 colored颜色 名称?

使用无限数量的元素创建特定的CSS网格

如何将文本环绕心形(而不仅仅是圆形)?

如何使用*ngFor将模板从父级传递到子级

如何制作';在第';页中搜索;是否发现多个元素中的单词?

按钮悬停效果不影响按钮内的图标

jquery向单词中的一组字母添加跨度

并排放置两个 div,同时 div2 环绕 div1

带圆角帽的 CSS 部分边框

使单行路径的 svg 填充父级的 100% 宽度

Tailwind:父母动态适应子元素的身高

如何使我的下拉菜单响应?

项目的水平列表:倒数第二个项目居中,而最后一个项目占据所有剩余空间

嵌套固定定位的 Div 滚动条渗透到前景中的 Div

在部分而不是正文中定义页面宽度

无法使用 Reactjs 多次更新本地存储