html - 如何在 CSS(或引导程序)的所有视点上保持按钮居中在图像上?
问题描述
我有一个以我的英雄形象为中心的按钮,但以我目前的方式,我必须使用许多媒体查询来保持它以所有观点为中心。
<style>
.hero-container{
position: relative;
text-align: center;
}
.hero-btn-div{
position: absolute;
top: 50%;
left: 42%;
display: inline-block;
}
.hero-btn{
background-color: transparent;
font: var(--main-sans-serif);
font-size: 1.2em;
border: 1px solid black;
border-radius: 100px;
width: 300px;
height: 50px;
}
</style>
<div class="hero-container">
<img src="images/dream%20of%20winter%20cut.jpg" class="img-fluid" width="1920px" alt="Artwork">
<div class="hero-btn-div"><a href="#"><button class="hero-btn">Button</button></a></div>
</div>
解决方案
你将更新left:50%
并添加这个transform:translateX(-50%;)
<style>
.hero-container {
position: relative;
text-align: center;
}
.hero-btn-div {
position: absolute;
top: 50%;
left: 50%;
transform:translateX(-50%);
display: inline-block;
}
.hero-btn {
background-color: transparent;
font: var(--main-sans-serif);
font-size: 1.2em;
border: 1px solid black;
border-radius: 100px;
width: 300px;
height: 50px;
}
</style>
<div class="hero-container">
<img src="images/dream%20of%20winter%20cut.jpg" class="img-fluid" width="1920px" alt="Artwork">
<div class="hero-btn-div"><a href="#"><button class="hero-btn">Button</button></a></div>
</div>
推荐阅读
- angular - 访问嵌套的 formGroupControl,Angular-6
- angular - Observable 在模板中具有 observable 成员
- javascript - 应用css时查看顶部文字:“overflow:hidden;”
- python - Tensorflow Keras 也使用 tfrecords 进行验证
- swift - 将字符串拆分为数组,在 Swift 中保留分隔符/分隔符
- outlook - “新建”Outlook Online 关闭任务窗格加载项
- javascript - 空 Guid 字符串在角度/打字稿中显示为 0
- java - 如何避免将一个变量从一种方法传递到另一种方法
- mongodb - 如何查找任何字段中包含“Amazon”作为值的所有文档?MongoDB
- ruby-on-rails - 如何在 Rails OAuth 请求中设置参数?