html - 在悬停时更改图像和文本并单击

标签 html css image hover

我有一张图片和旁边的文字,我想在悬停或点击图片时同时更改这两者。

我可以设法让图像和文本分别交换,但不能同时交换。

HTML

<body>
<div id="page-container">
    <div class="box-container">
        <div class="image-container">
            <a href="#"><img src=".jpg" height="100" width="200"/></a>
        </div>
        <div class="second-image-container">
            <a href="#"><img src=".jpg" height="100" width="200"/></a>
        </div>
        <div id="text">
            <div id="text-box">
                <p>This is some text</p>

                <p id="more-text">This is some more text</p>
            </div>
        </div>
        <div id="text-two">
            <div id="text-box-two">
                <p>This is some text</p>

                <p id="even-more-text">This is some more text</p>
            </div>
        </div>
    </div>
</div>
</div>
</div>
</body>
</html>

CSS

div#text {
    position: absolute;
    background-color: black;
    width: 350px;
    height: 300px;
    top: 10px;
    right: -65px;
}

div#text p {
    position: absolute;
    color: white;
    top: 100px;
    left: 125px;
}

div#text-box p {
    background-color: blue;
    width:
}

div#text-box p#more-text {
    background-color: red;
    top: 65px;
    left: 90px;
}

div.image-container {
    opacity: 1;
    position: absolute;
    z-index: 500
}

div.second-image-container {
    position: absolute;
    top:;
    opacity: 1;
    z-index: 119
}

div#text-two {
    position: absolute;
    background-color: black;
    width: 350px;
    height: 300px;
    right: -65px;
    top: 10px;
}

div#text-box-two p {
    position: absolute;
    background-color: blue;
    z-index: 10
}

div#text-box-two p#more-text {
    background-color: red;
}

    /*and the CSS that makes each individual image and text to swap:*/
div.image-container:hover, div#text:hover {
    opacity: 0
}

最佳答案

与其尝试在悬停每个单独的 div(图像容器和文本)时执行此操作,不如在悬停包含的 div(框容器)时更改不透明度。 CSS 看起来像这样:

.box-container:hover .image-container,
.box-container:hover #text {
    opacity: 0;
}

关于html - 在悬停时更改图像和文本并单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11548856/

相关文章:

html - 定位在页眉和分区内

html - css 技巧在复制粘贴时禁用文本选择

javascript - 从下方顺利滑入新的 div

jquery - 如何使用 jQuery 更改多个元素的字体大小

html - 将背景颜色与图像重叠,但不与内容重叠

python - 如何在 Python 3 中使用 https URL 下载图像?

javascript - 在 FireFox 中关闭可编辑的 div 中的语法更正

css - Firefox 中的 SVG CSS 内容

python - 如何使用数据 URI 方案显示来自 ndb 数据存储的图像(通过将数据传递到模板而不必发出另一个请求)

javascript - 如何在javascript中叠加图像?