【问题标题】:try to center text whilst keeping style尝试在保持样式的同时将文本居中
【发布时间】:2017-01-09 21:05:51
【问题描述】:

好的,我很有趣引导程序并且有一些带有文本的缩略图框(见下面的代码)。

我还希望垂直和水平对齐文本,我使用flexbox 完成了此操作,这似乎可行,但是现在spanh4p 标记都在同一行而不是比stacked

什么是解决这个问题的最佳方法,所以我得到标签stacked,但同时水平和垂直居中。

HTML

<div class="col-md-3 overlord-thumbnail">
    <div class="thumbnail">
        <a href="#tab4" data-toggle="tab">
            <div class="caption">
                <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                <h4>Latest News</h4>
                <p>text here</p>
            </div>
        </a>
        <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
        <h4> Latest News</h4>
        <p>text here</p>
    </div>
</div>

CSS

.overlord-thumbnail {
    height: 188px;
    overflow-y: hidden;
    overflow-x: hidden;
}

.thumbnail {
    background: whitesmoke;
    border-radius: 0;
    height: 100%;
    display: -moz-box;
    display: -ms-flexbox;
    display: inline-flex;
    -moz-box-pack: center;
    -ms-flex-pack: center;
    -moz-box-align: center;
    -ms-flex-align: center;
    align-items: center;
}

.caption {
    position:absolute;
    top:-100%;
    right:0;
    background: rgba(66, 139, 202, 0.64);
    width:100%;
    height:100%;
    padding:2%;
    text-align:center;
    color:#fff !important;
    z-index:2;
    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    -ms-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
}
.thumbnail:hover .caption {
    top:0;
}

目前

应该是

【问题讨论】:

  • 您想要最新消息、此处的文字和一行中的图标吗?
  • 不,我希望它们像没有显示的那样堆叠:inline-flex;
  • @BenjaminOats .. 看看我的回答,这就是你想要的吗?

标签: html css twitter-bootstrap flexbox


【解决方案1】:

问题在于默认样式会覆盖您正在应用的样式。使用.overlord-thumbnail&gt;.thumbnail 给予优先权。

您还需要使用flex-direction:column; 将它们堆叠在一起,而不是彼此相邻排列。

.overlord-thumbnail {
    height: 188px;
    overflow-y: hidden;
    overflow-x: hidden;
}

.overlord-thumbnail>.thumbnail{
    background: whitesmoke;
    border-radius:0;
    width:100%;
    height: 100%;
    display:flex;
    flex-direction:column;
    align-items:center;
    justify-content:center;
    text-align:center;
}

.caption {
    position:absolute;
    top:-100%;
    right:0;
    background: rgba(66, 139, 202, 0.64);
    width:100%;
    height:100%;
    padding:2%;
    text-align:center;
    color:#fff !important;
    z-index:2;
    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    -ms-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
    
}
.thumbnail:hover .caption {
    top:0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="col-md-3 overlord-thumbnail">
        <div class="thumbnail">
            <a href="#tab4" data-toggle="tab">
                <div class="caption">
                    <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
                    <h4>Latest News</h4>
                    <p>text here</p>
                </div>
            </a>
            <span class="glyphicon glyphicon-search" aria-hidden="true"></span>
            <h4> Latest News</h4>
            <p>text here</p>
        </div>
    </div>

【讨论】:

  • @BenjaminOats ...哦,我想我错了,我以为你试图将悬停时出现的文本居中。
  • 好的,现在居中。我希望这是你想要达到的目标。也请不要忘记接受并支持答案
猜你喜欢
  • 2020-08-15
  • 2016-10-11
  • 1970-01-01
  • 2014-12-30
  • 2016-10-06
  • 2020-01-21
  • 2012-11-18
  • 1970-01-01
  • 2013-03-27
相关资源
最近更新 更多