如何解决css问题才能显示图片标题?

时间:2014-03-11 17:04:19

标签: html css

我想显示一些带有标题的图像。默认只显示图像,但我想默认显示带标题的图像。我在下面给出了我的css和html代码:

   <style type="text/css">

    .caption-style-1{
        list-style-type: none;
        margin: 0px;
        padding: 0px;

    }

    .caption-style-1 li{
        float: left;
        padding: 0px;
        position: relative;
        overflow: hidden;
        margin-right:5px;
    }

    .caption-style-1 li:hover .caption{
        opacity: 1;

    }

    .caption-style-1 .hover-effect{
        margin: 0px;
        padding: 0px;
        float: left;
        width:220px;
        height:150px;
        //background-color:#999;
        margin-bottom:5px;
    }

    .hover-effect img{
        margin: 0px;
        padding: 0px;
        float: left;
        z-index: 4;
    }
    .hover-effect h1
    {
        font-size:20px;
        border:solid 1px red;
    }

    .hover-effect p
    {
        font-size:13px;
        margin:0;
        padding:0;
    }


    .caption-style-1 .caption{
        cursor: pointer;
        position: absolute;
        opacity: 0;
        -webkit-transition:all 0.45s ease-in-out;
        -moz-transition:all 0.45s ease-in-out;
        -o-transition:all 0.45s ease-in-out;
        -ms-transition:all 0.45s ease-in-out;
        transition:all 0.45s ease-in-out;

    }
    .caption-style-1 .blur{
        //background-color: rgba(0,0,0,0.65);
        height: 150px;
        width: 220px;
        z-index: 5;
        position: absolute;
    }

    .caption-style-1 .caption-text h1{
        text-transform: uppercase;
        font-size: 10px;
    }
    .caption-style-1 .caption-text .blur{
        z-index: 10;
        color: #fff;
        position: absolute;
        width: 220px;
        height: 150px;
        text-align: center;
        background-color: rgba(0,0,0,0.65);
    }

    /** Nav Menu */
    ul.nav-menu{
        padding: 0px;
        margin: 0px;
        list-style-type: none;
        width: 490px;
        margin: 60px auto;
    }

    ul.nav-menu li{
        display: inline;
        margin-right: 10px;
        padding:10px;
        border: 1px solid #ddd;
    }

    ul.nav-menu li a{
        color: #eee;
        text-decoration: none;
        text-transform: uppercase;
    }

    ul.nav-menu li a:hover, ul.nav-menu li a.active{
        color: #2c3e50;
    } 

    .opacity
    {
        opacity:0.2;
        filter:alpha(opacity=20); /* For IE8 and earlier */
    }

    </style>

我的html代码如下:

<ul class="caption-style-1">
   <li>
     <div class="hover-effect">
         <div class="opacity"><img src="img/chaps_1x.jpg" width="220px" height="150px" alt=""></div>
          <h1>Amazing Caption</h1>
      <p>Whatever It Is - Always Awesome</p>
     </div>

     <div class="caption">
    <div class="blur"></div>
      <div class="caption-text">
        <img src="img/chaps_1x.jpg" width="220px" height="150px" alt="">
      </div>
    </div>
    </li>
</ul>

任何想法?

1 个答案:

答案 0 :(得分:0)

您的HTML代码编写得不好。如果您使用HTML5,那么您可以使用figcaptionfigure等HTML5代码。 Caption标签未在您的样式中显示,因为它们位于图像后面。

所以这是使用z-index属性移动到前面的方法。

 .hover-effect h1
    {
        font-size:20px;
        border:solid 1px red;
      position:fixed;
        z-index:999;
    }

    .hover-effect p
    {
        font-size:13px;
        margin:0;
        padding:0;
       position:fixed;
        z-index:999;
    }

这是Demo。 http://jsbin.com/yegiyico/1/