【问题标题】:Why do my images become unaligned when using an Anchor tag?为什么我的图像在使用 Anchor 标签时会变得不对齐?
【发布时间】:2017-04-22 08:27:14
【问题描述】:

我是 HTML 和 CSS 的新手,我只是想不通。我已经设法水平对齐三个图像,以便它们在网页上舒适地彼此相邻。它们的大小也都根据我的需要而定。但是,我正在尝试将 Lightbox 脚本与它们一起使用(允许您在弹出窗口中查看图像),并且需要将图像标记包装在 <a> 标记中以链接到 Lightbox 脚本。但是,当我添加 <a> 标签时,图像会完全错位且不成比例。有什么建议吗?

<div class="imagecont">
    <a href="images/gallery/tube1.jpg" data-lightbox="gallery0" data-title="24-Hour Tube concept art.">
        <img class="imagecenter" src="images/gallery/tube1.jpg" alt="Tube 1" style="width: 50%; height: 50%;"/>
    </a>
    <a href="images/gallery/tube2.jpg" data-lightbox="gallery0" data-title="24-Hour Tube concept art.">
        <img class="imagecenter" src="images/gallery/tube2.jpg" alt="Tube 2" style="width: 100%; height: 100%;"/>
    </a>
    <a href="images/gallery/tube3.jpg" data-lightbox="gallery0" data-title="24-Hour Tube concept art.">
        <img class="imagecenter" src="images/gallery/tube3.jpg" alt="Tube 3" style="width: 50%; height: 50%;"/>
    </a>
</div>

.imagecenter {
    justify-content: center;
    margin-right: 10px;
    margin-left: 10px;
    margin-top: auto;
    margin-bottom: auto;
    vertical-align: middle;
    display: inline;
}

.imagecont {
    width: 50%;
    height: auto;
    display: flex;
    justify-content: center;
    margin-right: auto;
    margin-left: auto;
    margin-top: 40px;
    margin-bottom: 40px;
}

【问题讨论】:

    标签: javascript html css flexbox


    【解决方案1】:

    您也可以简单地在锚标记上使用 CSS。

    img a { adjust spacing as needed }
    

    或将其设为这些元素的特殊类。

    img a.anchorimage { adjust spacing as needed }
    

    或者更好的是,在进行任何自己的样式设置之前,先使用重置样式表。

    http://cssreset.com/what-is-a-css-reset/

    【讨论】:

      【解决方案2】:

      当您将图像包装在 a 标签中时,a 标签现在成为图像之前的弹性项目(即 .imagecont div 的子元素,它是弹性容器),并且图像成为a 标签的子代。因此,您必须将所需的大小、对齐方式等应用于a 标签,并使图像相对于a 标签,例如heigth: 100%

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-05
        • 2016-09-01
        • 2016-02-05
        • 2023-04-08
        相关资源
        最近更新 更多