使文本在响应图像中保持相同位置

时间:2014-05-31 19:45:40

标签: html css image text-alignment

我试图让我的图像上的文字叠加保持对图像的响应,因此它们位于图像框的中央,但我不能让它保持在图像宽度内。

我做了一个小提琴,http://jsfiddle.net/m9tEd/

我想要实现的目标的图片:

Image of what I'm trying to achieve

<div id="gallery">
<ul>
    <li>
        <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         
        </div>
    </li>
        <li>
            <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         

            </div> 
        </li>
        <li>
            <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         

            </div> 
        </li>
        <li>
            <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         

            </div>
        </li>
        <li>
            <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         

        </li>
        <li>
            <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         

        </li>
        <li>
            <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         

        </li>
        <li>
            <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         

        </li>
        <li>
            <div class="gallery-image"> 
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"/>
        <p class="gallery-desc">Upper line</p><br>
        <p class="line-separator-four"></p>
        <p class="gallery-desc-bottom">Bottom</p></a>         

        </li>
     </ul>
    </div>


enter code here

2 个答案:

答案 0 :(得分:1)

我认为这是你正在寻找的东西:

<强> CSS

#gallery {
    width: 100%;
    margin-right: auto;
    margin-left: auto;
    margin-top: 45px;
    position: relative;
    -webkit-transform: translateZ(0);
}

#gallery ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

#gallery ul li {
    overflow: hidden;
    width: 30%;
    margin: 1.66%;
    float: left;
}

#gallery li a {
    position: relative;
    display: block;
}

#gallery ul p {
    z-index: 2;
    font-size: 1.5em;
    font-family: 'avenir';
    margin: 0;
}

#gallery ul a {
    color: #fff;
}

#gallery li a:hover {
    color: #FF8000;
}

#gallery ul img {
    position: relative;
    z-index: 1;
    width: 100%;
}

.gallery-desc {
    left: 8%;
}

.line-separator-four {
    width: 50%;
    margin: 0 auto;
    height: 1px;
    background: #F5F5F5;
    border-bottom: 1px solid #F5F5F5;
    display: inline-block;
}

.caption {
    z-index: 2;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    text-align: center;
}

.caption:before {
    content: ' ';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

.caption_content {
    display: inline-block;
    vertical-align: middle;
}

<强> HTML

<div id="gallery">
    <ul>
        <li>
            <div class="gallery-image">
                <a href="#"><img src=
                "http://lorempixel.com/output/fashion-q-c-320-210-9.jpg"></a>

                <div class="caption">
                    <div class="caption_content">
                        <p class="gallery-desc">Upper line</p>

                        <p class="line-separator-four"></p>

                        <p class="gallery-desc-bottom">Bottom</p>
                    </div>
                </div>
            </div>
        </li>
    </ul>
</div>

<强> jsFiddle demo.

答案 1 :(得分:1)

呃......猜我太慢了。我和daniel有点不同,虽然有3行3 .gallery-image div并且文本列表

jsFiddle

HTML:

<div class="gallery">
    <div class="gallery-image">
        <a href=""><img src="http://lorempixel.com/output/fashion-q-c-320-210-9.jpg" />
            <ul>
                <li class="gallery-desc">Upper line</li>
                <li class="line-separator-four"></li>
                <li class="gallery-desc-bottom">Bottom</li>
            </ul>
        </a> 
    </div>

CSS:

.gallery {
    width:100%;
    margin-top:45px;
    -webkit-transform: translateZ(0);
}
.gallery-image {
    position: relative;
    float: left;
    display:inline-block;
    width: 32%;
    margin-left: 2%;
    font-size:1.5em;
    font-family:'avenir';
    text-align:center;
}
.gallery-image:first-child {
    margin-left:0;
}
.gallery-image a {
    color:#fff;
}
.gallery-image a:hover {
    color:#FF8000;
}
.gallery-image a:hover ul li.line-separator-four {
    background: #FF8000;
    border-bottom: 1px solid #FF8000;
}
.gallery-image img {
    position: relative;
    z-index:1;
    width: 100%;
}
.gallery-image ul {
    position: absolute;
    top:15%;
    width:100%;
    margin:0 auto;
    z-index:10;
    list-style:none;
    padding:0;
}
.gallery-image li {
    margin-bottom:10%;
}
.line-separator-four {
    display:block;
    width: 50%;
    margin-left: 25%;
    height: 1px;
    background: #F5F5F5;
    border-bottom: 1px solid #F5F5F5;
}