【问题标题】:positioning navigation buttons on an image slider在图像滑块上定位导航按钮
【发布时间】:2017-01-22 20:58:32
【问题描述】:

我想将导航按钮放置在图像滑块上,并且我想做出响应。我试过这样做,但只有当我以像素而不是百分比设置顶部位置时它才有效。以像素为单位设置没有响应。

<div id="container">
<header>
    header is here
</header>
<div id="carousel">
<div class="sliderbuttons">
    <input type="button" name="next" id="next" value=" > ">
    <input type="button" name="next" id="prev" value=" < ">
    </div>
    <div class="slides">
        <img src="http://www.planwallpaper.com/static/images/4-Nature-Wallpapers-2014-1_ukaavUI.jpg" alt="image1" class="slide active">
        <img src="http://www.mrwallpaper.com/wallpapers/green-Rice-1600x900.jpg" alt="image2" class="slide">
        <img src="http://cdn.wonderfulengineering.com/wp-content/uploads/2016/01/nature-wallpapers-10.jpg" alt="image3" class="slide">
    </div>


</div>

</div>

CSS

*{
    box-sizing: border-box;
}

#container {
    width: 90%;
    margin: 0 auto;
}

header {
    background: black;
    height: 20px;
    padding: 1.5em;
    color: white;
}

#carousel {
    position: relative;
    margin: 0 auto;
    width: 45%;
    background-color: #f4f4f4;
    margin-top: 15px;
    min-height: 100%;

}

.slide {
    position: absolute;
    max-width: 100%;

}
.sliderbuttons {
    }

#prev,#next {
    position: absolute;
    background-color: rgba(255, 148, 41, 0.68);
    box-shadow: 2px white;
    border:none;
    font-size: 2em;
    color: white;
    font-weight: bold;
/*  font-family: 'Baloo Tamma', cursive;
*/  padding:10px;
    top:45%;
    width: 10%;
    /*making the prev,next on top of content*/
    z-index: 20;
}
#prev {
    left:0;
}
#next {
    right:0;
        }

.active {
    z-index: 10;
}

这是我迄今为止尝试过的。 https://jsfiddle.net/w5xm37y4/1/

【问题讨论】:

    标签: html css css-position


    【解决方案1】:

    如果要以百分比设置位置,则必须为父元素定义百分比。所以尝试添加

    html, body { height: 100%; position: relative; }

    和高度:#container 也是 100%。我认为(并希望!)它可以帮助你。

    (对不起我糟糕的英语)

    【讨论】:

    • 谢谢!现在可以了。但我很好奇为什么我必须为 body、html、#container、#carousel 设置 100% 的高度?谁能解释一下?
    • 元素的百分比高度是相对于其容器定义的。如果没有元素具有定义的高度(以像素为单位),则 100% 的 html 元素允许您将高度基于视口。对于#container、#carousel... 默认高度取决于它们的内容,因此您也必须明确定义高度 100%。
    猜你喜欢
    • 2014-04-15
    • 2013-05-09
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多