代码之家  ›  专栏  ›  技术社区  ›  Forivin

警报有多条线时引导中心警报关闭按钮

  •  1
  • Forivin  · 技术社区  · 8 年前

    非常简单的问题:

    此代码: http://www.bootply.com/DiP1WtXyho

    <div class="alert alert-info" role="alert">
      Line 1
      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    </div>
    
    <div class="alert alert-info" role="alert">
      Line 1
      <br>
      Line 2
      <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    </div>
    

    导致此问题:
    enter image description here

    如您所见,在第一个警报中,关闭按钮居中。在第二个警报中没有。我怎样才能解决这个问题?(如果可能,不向我的所有警报中添加完整的表结构。)

    2 回复  |  直到 8 年前
        1
  •  6
  •   crazymatt    8 年前

    另一种方法是将关闭框设置为 absolute 但一个不幸的副作用是,你必须硬编码顶部 margin .

    .alert-info {
      position: relative;
    }
    
    .close {
      float: none;
      position: absolute;
      right: 10px;
      top: 50%;
      margin-top: -10px; /* Needs to be half the height of the element (21/2) */
    }
    

    recreated this in a js.fiddle if you want to see it in action 希望能有所帮助。

    编辑 看起来 height 需要基于关闭框(X)而不是 top-padding 。元件高度为 21px 所以 -10px 效果很好。 Updated the Fiddle to showcase this update .

        2
  •  2
  •   Community CDub    4 年前

    对齐的步骤 close 按钮垂直居中添加下面的css将使其成为 vertically aligned centered .

    注释

    仅给予 line-height:0 将使其居中,但会影响这些 alerts 有单行文本的。 谢谢

    .alert{
       position:relative;
    }
    .close{
      position:absolute;
      top:48%;
      right:35px;
      line-height:0;
    }
    

    就是这样