【问题标题】:Positioning a bootstrap button inside a bootstrap div在引导 div 中定位引导按钮
【发布时间】:2017-04-02 03:44:49
【问题描述】:

我正在尝试解决引导程序的样式问题。我是前端开发的新手,希望您能帮助我解决这个具体问题。我正在迭代淘汰员工名单。我为每个员工创建了一张包含 3 个部分的卡片:带有名称和按钮的部分, 图片,一般信息。它应该是这样的:

蓝色区域的白条就是我画出来的员工的名字。

<section id="picturesSection" class="background-image" data-bind="foreach: { data: people, as: 'person'}">
    <div class="cardPositioning panel panel-primary">
        <div class="panel-heading my-panel-heading">
            <h3 class="panel-title cardTitle" data-bind="text: person.Name"></h3>
            <button type="button" class="btn btn-primary btn-xs removeButton fa fa-minus" ></button>
        </div>
        <header>
            <!--<img width="256" height="256" src="app/Images/horse.jpg">-->
            <img width="256" height="256" data-bind="attr:{src: 'app/' + person.srcImage}">
        </header>
        <footer>
            <!-- $data.firstName-->
            <p class="nameEmployeePos" data-bind="text: person.DateOfBirth"></p>
            <p class="nameEmployeePos" data-bind="text: person.Role"></p>
            <p class="nameEmployeePos" data-bind="text: person.Email"></p>
        </footer>
    </div>
</section>

还有CSS

#picturesSection{
width: 100%; overflow: hidden;
}

.cardPositioning{
float: left;
margin:20px;
}

.background-image {
background-image: url("../Images/back.png");
width: 100%;
z-index: 10;
background-position-x: 1400px;
background-position-y: 400px;
background-repeat: no-repeat;
background-attachment: fixed;
}

.removeButton{
margin-bottom: 2px;
margin-left: 0px;
margin-right: 0px;
}

问题是如何将按钮放置在代表“减号”图标的卡片中。我需要该按钮来从数据库中删除员工。我想在蓝色空间的右上角有按钮。 Bootstrap 在 panel panel-primary 类中有一些填充,由于员工姓名的位置(我想保留),我不想删除这些填充。想知道如果可能的话我该如何解决这些问题。提前致谢!

【问题讨论】:

    标签: jquery html css twitter-bootstrap knockout.js


    【解决方案1】:

    在按钮上使用 Bootstrap "pull-right" 类并将面板标题设置为 "inline-block"

    HTML

    <section id="picturesSection" class="background-image" data-bind="foreach: { data: people, as: 'person'}">
        <div class="cardPositioning panel panel-primary">
            <div class="panel-heading my-panel-heading">
                <h3 class="panel-title cardTitle panel-title-mod" data-bind="text: person.Name"></h3>
                <button type="button" class="btn btn-primary btn-xs removeButton fa fa-minus pull-right" ></button>
            </div>
            <header>
                <!--<img width="256" height="256" src="app/Images/horse.jpg">-->
                <img width="256" height="256" data-bind="attr:{src: 'app/' + person.srcImage}">
            </header>
            <footer>
                <!-- $data.firstName-->
                <p class="nameEmployeePos" data-bind="text: person.DateOfBirth"></p>
                <p class="nameEmployeePos" data-bind="text: person.Role"></p>
                <p class="nameEmployeePos" data-bind="text: person.Email"></p>
            </footer>
        </div>
    </section>
    

    CSS

     #picturesSection{
    width: 100%; overflow: hidden;
    
    }
    
    .cardPositioning{
    float: left;
    margin:20px;
        position:relative;
    }
    
    .background-image {
    background-image: url("../Images/back.png");
    width: 100%;
    z-index: 10;
    background-position-x: 1400px;
    background-position-y: 400px;
    background-repeat: no-repeat;
    background-attachment: fixed;
    }
    
    .removeButton{
    margin-bottom: 2px;
    margin-left: 0px;
    margin-right: 0px;
      position:absolute;
      right:0;
      top:0;
    }
    
    .panel-title-mod {
      display: inline-block;
    }
    

    查看此代码笔

    http://codepen.io/Arshmeet/pen/oYYRLM

    【讨论】:

    • 好吧,我明白了!按钮到达右侧.. 但它还没有在右上角.. 可能是由于填充.. 如果可能的话想把它放在那里
    • 你现在可以看到我认为这是你需要的:)
    • 您的解决方案和@Mark one 都工作正常!选择你的只是因为答案的广泛性!但它解决了我的问题!谢谢:)
    【解决方案2】:

    有多种方法可以做到这一点--

    1- 在按钮上使用 pull-right 类。它扭曲了包装器 div。所以将.panel-heading的高度增加到40px;

    2- 在删除按钮类上使用 position: relative 和 left : 90% 以获得所需的结果

    https://jsfiddle.net/1t13t988/

    【讨论】:

    • 它确实扭曲了包装!增加高度解决了这个问题。现在按钮在右边,但我水平居中。我想将它移到右上角,但我认为卡片面板的填充避免了这样做。我怎样才能实现它?
    • @Tarta :使用 my-panel-heading 类并删除填充。例如 - jsfiddle.net/1t13t988/1
    【解决方案3】:

    你可以试试绝对位置:

        #picturesSection .panel-heading {
            position: relative;
        }
    
        .removeButton{
            position: absolute;
            top: 0;
            right: 0;
        }
    

    这是code pen

    【讨论】:

      【解决方案4】:

      您是否尝试将“向右拉”类添加到按钮?

      【讨论】:

      • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
      • @gkb 将做笔记以供将来参考。感谢您的建议!
      【解决方案5】:

      我建议尝试其他问题中给出的答案:How can I get my Twitter Bootstrap buttons to right align?

      这应该可以,但我还没有测试过:

      <section id="picturesSection" class="background-image" data-bind="foreach: { data: people, as: 'person'}">
          <div class="cardPositioning panel panel-primary">
              <div class="panel-heading my-panel-heading">
                  <h3 class="panel-title cardTitle" data-bind="text: person.Name"></h3>
                  <button type="button" class="btn btn-primary btn-xs removeButton fa fa-minus pull-right" ></button>
              </div>
              <header>
                  <!--<img width="256" height="256" src="app/Images/horse.jpg">-->
                  <img width="256" height="256" data-bind="attr:{src: 'app/' + person.srcImage}">
              </header>
              <footer>
                  <!-- $data.firstName-->
                  <p class="nameEmployeePos" data-bind="text: person.DateOfBirth"></p>
                  <p class="nameEmployeePos" data-bind="text: person.Role"></p>
                  <p class="nameEmployeePos" data-bind="text: person.Email"></p>
              </footer>
          </div>
      </section>
      

      如果失败,您可以随时制定自定义 CSS 规则,让您的按钮向右浮动:

      .cardPositioning > .panel-heading > button {
          float: right;
      }
      

      【讨论】:

      • 不幸的是,这在包装 div 中造成了失真
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 2019-01-21
      相关资源
      最近更新 更多