html - css block 链接和图像

标签 html css hyperlink

给定这个 html:

<div class="nation">
    <a href="library.php?type=nation&amp;id=America">
        <div class="nation-image">
            <img src="nations/America.png" alt="snip" />
        </div>
        <h3>America</h3>
    </a>
</div>

以下 CSS 生成整个 <div class="nation">成为可点击的 block 链接:

.nation {float: left; text-align: center; width: 189px;}

.nation img {width: 160px; margin: 10px 0 0 0; border: 1px solid #16160C;}

但如果我进行以下单个添加:

.nation {float: left; text-align: center; width: 189px;}

    .nation-image {height: 138px;}

.nation img {width: 160px; margin: 10px 0 0 0; border: 1px solid #16160C;}

指定<div class="nation-image">的高度然后图像(并且只有图像)不再是链接,但 div 的其余部分(它周围的边距,h3 等)仍然是 block 链接。

我很困惑。

最佳答案

演示: http://jsbin.com/uyupu/2

您的 HTML 结构必须是这样的:

<div class="nation">
<a href="#">
    <img src="your.jpg" alt="Daim Graffiti" id="usa" />
    <span class="nation-flag">America</span>
</a>
</div>

然后是你的 CSS:

    .nation {
      margin: 0;
      overflow: hidden;
      float: left;
      position: relative;
    }
    .nation a {
      text-decoration: none;
      float: left;
    }
    .nation a:hover {
      cursor: pointer;
    }

    .nation a img {
      float: left;
      margin: 0;
      border: none;
      padding: 10px;
      background: #fff;
      border: 1px solid #ddd;

/* note that you don't really need to set the Height 
but the width so the image auto scaling fine!*/

      width:200px
    }

    .nation a .nation-flag {
      position: absolute;
      right: 20px;
      bottom: 20px;
      font-size: 1.2em;
      color: #fff;
      background: #000;
      padding: 5px 10px;
      filter:alpha(opacity=65);
      opacity:.65;
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=65)"; 
    /*--IE 8 Transparency--*/
    }

关于html - css block 链接和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2454863/

相关文章:

php - 选择不与表单一起提交

php - 如何在 PHP 中复制 SQL 结果变量?

html - 允许水平移动的固定物体

css - 视差在移动设备的背景上出现奇怪的填充

jquery - 如何使用 jQuery 确认点击链接

html - 如何在android中准确地使用链接标签中的href属性?

javascript - 用 <div> 填充 2 点之间的间隙

html - 将 li 放在特定高度后的下一列

javascript - jQuery 窗口调整大小触发器仅触发一次

jquery - 使用 jquery 添加链接的 href 值