将容器收缩到图像宽度

时间:2016-04-22 12:21:37

标签: html css

我目前正在开发一个网站,用户应该能够水平滚动,并在其上显示可点击的信息点。 网站需要完全响应,并确保我想将我的横向图像和信息点放在具有完全相同大小的图像的容器中。

HTML:

<div id="container-main">
    <div id="landscape">
        <img class="background" src="image.jpg" />

        <div class="point" style="top: 24%; left: 7.5%;"></div>
        <div class="point" style="top: 29%; left: 17.7%;"></div>
        <div class="point" style="top: 77%; left: 39%;"></div>
        <div class="point" style="top: 26%; left: 68%;"></div>
        <div class="point" style="top: 70%; left: 80%;"></div>
    </div>
</div>

CSS:

html {
    height: 100%;
}

body {
    height: 100%;
    min-width: 100%;
    margin: 0;
    padding: 0;
}

#container-main {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow-y: hidden;
}

#landscape {
    position: relative;
    display: inline-block;
    height: 100%;
}

#landscape > img.background {
    height: 100%;
    display: block;
}

.point {
    position: absolute;
    width: 50px;
    height: 50px;
    margin: -25px 0 0 -25px;
    background-color: white;
    border-radius: 5px;
    cursor: pointer;
}

我的CSS工作完全正常,直到您调整浏览器窗口的高度,然后信息点移动到错误的位置..但是当您刷新调整大小的网站时它再次正常工作。

试一试:Fiddle(调整输出大小,然后再点击“运行”。)

在我的真实项目中,我使用javascript将容器的宽度设置为图像的宽度,但我希望有一个干净的CSS解决方案。

我知道周围有类似的问题,但没有一个建议的解决方案适合我。

提前谢谢!

2 个答案:

答案 0 :(得分:2)

使用vw insted% 调整以下代码:

<div id="container-main">
    <div id="landscape">
        <img class="background" src="image.jpg" />

        <div class="point" style="top: 24vw; left: 7.5vw;"></div>
        <div class="point" style="top: 29vw; left: 17.7vw;"></div>
        <div class="point" style="top: 77vw; left: 39vw;"></div>
        <div class="point" style="top: 26vw; left: 68vw;"></div>
        <div class="point" style="top: 70vw; left: 80vw;"></div>
    </div>
</div>
<style>
html {
    height: 100%;
}

body {
    height: 100%;
    min-width: 100%;
    margin: 0;
    padding: 0;
}

#container-main {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow-y: hidden;
}

#landscape {
    position: relative;
    display: inline-block;
    height: 100%;
}

#landscape > img.background {
    height: 100%;
    display: block;
}

.point {
    position: absolute;
    width: 50px;
    height: 50px;
    margin: -25px 0 0 -25px;
    background-color: white;
    border-radius: 5px;
    cursor: pointer;
}
</style>

.....................另一个解决方案........................ .................................

更改css:

html {
    height: 100%;
}

body {
    height: 100%;
    min-width: 100%;
    margin: 0;
    padding: 0;
}

#container-main {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow-y: hidden;
}

#landscape {
    position: relative;
    display: inline-block;
    width: 100%;
}

#landscape > img.background {
    height: 100%;
    display: block;
}

.point {
    position: absolute;
    width: 50px;
    height: 50px;
    margin: -25px 0 0 -25px;
    background-color: white;
    border-radius: 5px;
    cursor: pointer;
}

注意:请参阅此site的响应式编码标准:

答案 1 :(得分:0)

首先,永远不要使用位置:固定;在CSS上除非你想让div或容器保持在同一个地方,即使你滚动。 其次,我建议使用jquery进行流畅的布局。流畅的布局是什么,即使您调整网站大小,它也会将页面内容保持在同一位置。如果您不想浪费时间编写长脚本文件,我个人建议您使用Dreamweaver。 Dreamweaver更新了他们的脚本,只需单击一些按钮,即可让用户轻松地制作流畅的布局。它会自动为您生成脚本文件和css。如果您是像我这样的手动编码器,那么只需使用Dreamweaver生成流畅的布局并手动开始编码。

希望这会有所帮助:)