在固定div中水平居中文本

时间:2015-03-21 22:31:44

标签: html css twitter-bootstrap

我正在尝试将四个h4标记集中在各自的fixed div中(所有标记都在width: 100%以保持background-color: rgba(255, 255, 255, 0.5);跨越整个浏览器宽度。)< / p>

我尝试了很多CSS标签组合,但无法对其进行排序。

工作草案:http://parkerrichard.com/new/art.html

HTML:

<!-- row 1: body -->
<div class="container thumb-page">
    <div class="row thumb-col mb">
        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
            <div class="fix"><h4 class="hidden-xs">PAINTING</h4></div>
            <h4 class="centered hidden-lg hidden-md hidden-sm">PAINTING</h4>
        </div>
        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
            <div class="fix"><h4 class="hidden-xs">DRAWING</h4></div>
            <h4 class="centered hidden-lg hidden-md hidden-sm mt-sm">DRAWING</h4>
        </div>
        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
            <div class="fix"><h4 class="hidden-xs">DIGITAL</h4></div>
            <h4 class="centered hidden-lg hidden-md hidden-sm mt-sm">DIGITAL</h4>
        </div>
        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
            <div class="fix"><h4 class="hidden-xs">VIDEO</h4></div>
            <h4 class="centered hidden-lg hidden-md hidden-sm mt-sm">VIDEO</h4>
        </div>
    </div>
</div>

CSS:

/* Thumbnail section */

.thumb-col .fix {
    position: fixed;
    background-color: rgba(255, 255, 255, 0.5);
    display: inline-block;
    width: 100%;
}

.thumb-col h4 {
    letter-spacing: 2px;
}

.thumb-col .fix h4 {
    margin-top: 0px !important;
    margin-bottom: 15px !important;
}

.thumb-col div a img {
    margin: 0 auto !important;
    display: block;
    width: 200px;
}

2 个答案:

答案 0 :(得分:2)

我认为问题在于你为每个标题定位。尝试将所有标题移至.conatainer上方的新.thumb-page,并将position: relative添加到此容器行。同时保留与图像相同的网格结构,当然也要对每个标题文本使用text-align: center

EDID: 工作fiddle

答案 1 :(得分:0)

删除位置:固定;并添加text-align:center;对齐文本

.thumb-col .fix {
  position: fixed; - /* delete this */
  background-color: rgba(255, 255, 255, 0.5);
  display: inline-block;
  width: 100%;
  text-align: center; /* Add This */