【问题标题】:Vertical align sentence in div Bootstrap 3div Bootstrap 3中的垂直对齐句子
【发布时间】:2015-06-15 14:49:31
【问题描述】:

所以我使用的是最新版本的 Bootstrap,我有以下 div

<div class="col-sm-6 col-md-2 col-lg-8 memory textContainer">
    <p>{{ $memory->description }}</p>
</div>

最大句子长度为 200 个字符,因此不能放在一行中。对于我的垂直对齐,我有以下 CSS。

.textContainer {
    height: 200px;
    line-height: 200px;
}

.textContainer p {
    height: 200px;
}

这是有效的,但当句子长于一行时不起作用,因为两行之间的间距也是 200 像素,如下图所示。

有没有办法解决这个问题?非常感谢!

【问题讨论】:

    标签: css twitter-bootstrap alignment vertical-alignment


    【解决方案1】:

    您追求什么样的跨浏览器兼容性?接受的答案在不支持 css3 转换的浏览器中效果不佳。如果您需要支持旧版浏览器,请尝试以下操作:

    <div class="col-sm-6 col-md-2 col-lg-8 memory textContainer">
    
      <div class="centered">
        <p>{{ $memory->description }}</p>
      </div>
    
    </div>
    
    .textContainer {
    height: 200px;
    }
    /* The ghost, nudged to maintain perfect centering */
    .textContainer:before {
      content: '';
      display: inline-block;
      height: 100%;
      vertical-align: middle;
      margin-right: -0.25em; /* Adjusts for spacing */
    }
    
    /* The element to be centered, can also be of any width and height */ 
    .centered {
      display: inline-block;
      vertical-align: middle;
    }
    

    参考:https://css-tricks.com/centering-in-the-unknown/

    【讨论】:

    • 这个更好,谢谢分享,因为你也可以为 ul 做。谢谢。
    【解决方案2】:

    这对你有用吗?

    .textContainer {
        height: 200px;
        background: silver;
        position: relative;
    }
    .textContainer p {
        position: absolute;
        padding: 0;
        margin: 0;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
    }
    <div class="textContainer">
        <p>content content content content content content content content content content content content content content content</p>
    </div>

    【讨论】:

    • 是的!非常感谢,现在需要知道我为什么工作!
    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 2018-10-13
    • 2016-03-07
    • 2017-10-21
    相关资源
    最近更新 更多