我有一个图像,我需要在其中放置一个按钮,问题是我不知道如何放置按钮并在缩小浏览器时自动调整大小和位置,现在我有按钮就位,但当我重新调整浏览器大小时按钮移动变小,我尝试在 css 购买中使用百分比不起作用,我该怎么办?
<div id="discover" class="container-fluid">
<div class="row-fluid">
<div class="col-lg-12 col-sm-12 col-xs-12 col-md-12 withimg">
<img id="discoveryour" src="img/x.png" class="img-responsive">
</div>
</div>
<div class="row-fluid">
<div id="bttnimg" class="col-lg-12 col-sm-12 col-xs-12 col-md-12">
<form id="start" method="post" action="x.php">
<button class="btn-primary">text</button>
</form>
</div>
</div>
</div>
CSS:
.withimg {
width: 100%;
overflow:hidden;
padding: 0px;
margin: 0px;
}
#discover{
position: relative;
}
#bttnimg{
float: left;
position: absolute;
left: 62%;
top: 25%;
max-width: 750px;
}
最佳答案
啊,“如何在响应式图像之上叠加内容——响应式”这个老问题。
有点棘手,但还算不错。棘手的一点是如何使东西的垂直位置在图像尺寸变化时响应。
别害怕,这里有一个简单的方法可以做到这一点:
HTML:
<div class="img-wrapper">
<img class="img-responsive"
src="http://lorempixel.com/output/people-q-c-1200-400-4.jpg">
<div class="img-overlay">
<button class="btn btn-md btn-success">Button</button>
</div>
</div>
CSS:
.img-wrapper {
position: relative;
}
.img-responsive {
width: 100%;
height: auto;
}
.img-overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
text-align: center;
}
.img-overlay:before {
content: ' ';
display: block;
/* adjust 'height' to position overlay content vertically */
height: 50%;
}
img-overlay:before
伪类通过将 img-overlay
div 从图像顶部向下推来处理垂直定位作业。在此示例中,按钮的顶部始终位于图像下方 50%(如果您希望按钮更高或更低,请更改 height: 50%
属性)。
要使按钮大小 响应窗口宽度,您可以为按钮创建一个新类。我们称它为 btn-responsive
(这取代了上面示例中的 btn-md
)。然后使用@media
查询来调整不同窗口宽度的btn-responsive
属性。像这样:
.btn-responsive {
/* matches 'btn-md' */
padding: 10px 16px;
font-size: 18px;
line-height: 1.3333333;
border-radius: 6px;
}
@media (max-width:760px) {
/* matches 'btn-xs' */
.btn-responsive {
padding: 1px 5px;
font-size: 12px;
line-height: 1.5;
border-radius: 3px;
}
}
对于其他屏幕宽度依此类推。
关于html - Bootstrap : How to "stick" a button over an image when re-sizing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35647044/