【问题标题】:How to add increasing margin sizes to a row of buttons?如何为一行按钮添加增加的边距大小?
【发布时间】:2019-02-10 01:40:39
【问题描述】:

我正在尝试创建一排按钮,这些按钮的左侧边距尺寸不断增加。

例如:

[b1]--[b2]-----[b3]-------[b4]

'-' 成为空格。我无法弄清楚在stackoverflow上有连续空格的格式......如您所见,b1将有margin-left of 10%b2 20%b3 40%b4 80%

但是,看起来左边距正在累积,因此 b2 将总共有 margin-left of 30%b3 a total of 70% 等等...

我的目标是让每个按钮都位于距屏幕最左侧一定距离的位置。我正在尝试通过使用 margin-left 来实现这一点。但是,由于按钮在同一个 div 中,我相信这会使 margin-left 值累积,因此下一个按钮的 margin-left 值将始终与前一个按钮的 margin-left 值累积。

这只是我对正在发生的事情的猜想,但代码并没有按照我的想法做:每个按钮都应该离左边一定距离。例如左边的b1 should be 10%b2 should be 20% 从左边开始。从本质上讲,我希望所有按钮从一开始就从左边距开始。

所以这里是代码。请原谅我在这里输入的任何伪代码,因为我在另一台没有我的源代码的机器上。

html:

<div class='outer'>
    <div class='inner'>
        <div class='buttons'> 
            <button>b1<button/> 
            <button>b2<button/> 
            <button>b3<button/> 
            <button>b4<button/>  
        </div>
    </div>
</div>

css:

outer { width: 100% }
inner { width: 100% }
buttons { width: 100% } 
b1 { margin-left: 10%}
b2 { margin-left: 20%}
b3 { margin-left: 40%}
b4 { margin-left: 80%}

【问题讨论】:

    标签: html css button alignment margins


    【解决方案1】:

    您正在寻找的是“位置:绝对”(在某些情况下或“位置:固定”)。

    您可以将“绝对”属性应用于每个按钮,如下所示,这将使每个按钮的位置都是绝对的(即不基于前一个按钮的位置,如您的示例所示)。你可以在这里阅读更多关于绝对定位的信息:https://www.w3schools.com/css/css_positioning.asp

    正如你所提到的,你的代码在这个例子中有点偏离,所以我会指出你的 div 上缺少结束标签,你的结束按钮标签不正确并且你的 CSS 语法是关闭的。

    以下代码应该可以解决您的问题:

    .buttons button {position: absolute}
    .b1 { margin-left: 10%}
    .b2 { margin-left: 20%}
    .b3 { margin-left: 40%}
    .b4 { margin-left: 80%}
    <div class='buttons'> 
        <button class='b1'>b1</button> 
        <button class='b2'>b2</button> 
        <button class='b3'>b3</button> 
        <button class='b4'>b4</button>  
    </div>

    请注意:您也可以将上面示例中的“absolute”替换为“fixed”以获得相同的效果。 “absolute”属性会将其定位到最近的定位祖先(即,如果您要重新定位外部/内部 div),而“Fixed”将始终相对于视口定位。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      首先您的 div 没有正确关闭,下面是您的代码与关闭 div 的示例,还尝试更正确地组织您的代码以供其他人阅读,不确定这是否回答了您的问题,因为我真的不能得到关于你想要完成什么的问题。

      <div class='outer'>
      <div class='inner'> 
      </div>
      <div class='buttons'> 
      </div>
      </div>
      
      outer {
         width: 100%;
      }
      
      inner {
         width: 100%;
      }
      
      buttons {
         width: 100%;
      }
      
      .one {
         width: 10%;
      }
      
      .two {
         width: 20%;
       }
      
      .three {
         width: 40%;
      }
      
      .four {
         width: 80%;
       }
      

      【讨论】:

      • 奥斯卡,我很抱歉。我应该校对我的帖子。请参阅更新的代码。内部 div 包装了按钮 div。我的目标是让每个按钮都位于距屏幕最左侧一定距离的位置。我正在尝试通过使用 margin-left 来实现这一点。但是,由于按钮位于同一个 div 中,我相信这会使 margin-left 值累积,因此下一个按钮的 margin-left 值将始终与前一个按钮的 margin-left 值累积。
      • 这只是我对正在发生的事情的猜想,但代码并没有按照我的想法做:每个按钮都应该离左边一定距离。例如 b1 应该是左边的 10%。 b2 应该是左边的 20%。
      猜你喜欢
      • 2011-10-28
      • 1970-01-01
      • 2021-11-24
      • 2015-06-12
      • 2022-01-25
      • 1970-01-01
      • 2022-01-24
      • 2023-03-25
      • 2019-05-13
      相关资源
      最近更新 更多