HTML Css 位置绝对不起作用

标签 html css

<分区>

如何将这些按钮准确地保留在背景图片中的框上?当您调整窗口/浏览器的大小时,它们的位置/大小会发生变化,并且它们不再适合背景图像框。

html, body {
    margin: 0;
}

.Div1 {
    background-image: url("https://imgur.com/7NQ3IOt.jpg");
    background-size: 100% 100%;
    background-repeat: no-repeat;

    position: relative;
    height: 76.1vw;
}

.Div2 {
    background-image: url("https://imgur.com/CjVMSqG.jpg");
    background-size: 100% 100%;
    background-repeat: no-repeat;

    position: relative;
    height: 169vw;
}

.Btns {
    position: absolute;
    font-size: 1.9vw;
    left: 17.4vw;
    width: 26.3vw;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div class="Div1">
        <button class="Btns" style="top: 4.8vw">Button 1</button>
    </div>
    <div class="Div2">
        <button class="Btns" style="top: 4vw">Button 1</button>
    </div>
</body>
</html>

最佳答案

我认为这里的问题在于按钮的尺寸和默认填充、边框和浏览器特定样式。我通过一些手动调整用计算值编辑了 fiddle :

简而言之,padding也要以vw为单位,并设置-webkit-appearance: none;

你可以在这里看到一个 CodePen:

https://codepen.io/elujambio/pen/EQQOgK

html, body {
    margin: 0;
}

.Div1 {
    background-image: url("https://imgur.com/7NQ3IOt.jpg");
    background-size: 100% 100%;
    background-repeat: no-repeat;

    position: relative;
    height: 76.1vw;
}

.Div2 {
    background-image: url("https://imgur.com/CjVMSqG.jpg");
    background-size: 100% 100%;
    background-repeat: no-repeat;

    position: relative;
    height: 169vw;
}

.Btns {
    position: absolute;
    font-size: 1.8vw;
    box-sizing:  border-box; 
    padding:  .25vw 0; 
    left: 18vw;
    width: 26.75vw; 
    -webkit-appearance:  none;  
    
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div class="Div1">
        <button class="Btns" style="top: 4.8vw">Button 1</button>
    </div>
    <div class="Div2">
        <button class="Btns" style="top: 4vw">Button 1</button>
    </div>
</body>
</html>

关于HTML Css 位置绝对不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48869725/

上一篇:CSS :how to give clickable text something like OSX dock zoom on hover?

下一篇:html - anchor 在 Chrome 中不起作用,但在 Mozilla 中起作用

相关文章:

css - 如何在左侧和右侧堆叠 div 内的元素?

javascript - 使用 .trigger 将事件传递给子级

html - 我的 vimeo 视频显示不正确请提供一些建议

javascript - 按类别设置所有选择列表

jquery - Bootstrap 下拉菜单不适用于 SlidesJS 3.0

html - 在 Bootstrap 网格中添加堆栈之间的间距

javascript - 为什么我的幻灯片没有循环播放

css - Vim:将 CSS 从单行重新格式化为多行

HTML 三个并排的 div 具有相同的高度

html - 当右上角的 div 高度是动态的时,如何让右下角的 div 不溢出父 div 和填充?