【问题标题】:button on top of an image (but it's not staying put)图像顶部的按钮(但它不会保持不变)
【发布时间】:2018-05-01 19:10:21
【问题描述】:

编辑我的原始帖子---

我有一个图像,其中包含作为图像的文本,图像上的一个按钮不会停留在响应式滚动中。

所以我只放了图像,将文本设置为 div。我已将图像设置为相对,将文本设置为绝对。 ???现在我无法在我的巨型图像上获得我的文本 div,我对此感到非常厌烦。 (我还需要文本背后的透明度,但也放弃了定位。)

HTML

<div class="w3-jumboimagecontainer> 
<div class="w3-display-container w3-jumboimage w3-center">
<img src="images/assistingelder.jpg" style="width:100%"></div>
<h5 class="ontop">
<h5 class="jumbonow">Now hiring</h5> <h5       
class="caregiversnow">Caregivers</h5> 
<h5 class="birminghamnow">in the Birmingham area</h5></h5>
<div class="w3-jumbotextsquare"">
<div class="w3-jumbobutton w3-button w3-xlarge w3-center">
<a href="caregiversapp.html" class="w3-button w3-xlarge">apply now! 
</a></div></div></div></div>

CSS

.w3-jumboimagecontainer {width: 100%;}
.w3-jumboimage {position: relative;}
h5.ontop {position: absolute;}
.w3-jumbobutton .w3-button {
background-color: #d7d78f;
border-color: #d7d78f;
color: #FFF; 
border-radius: 8px;
text-transform: uppercase;
letter-spacing: 1px;
object-position: center;
}
h5.jumbonow {font-family: "proxima-nova"; helvetica; arial; sans-   
serif;
font-weight: 200;
font-style: normal;
font-size: 50pt;
letter-spacing: 2px;
text-transform: uppercase;
color: white;
text-align: center;
top:0px;}
h5.caregiversnow {
font-family: georgia; serif;
font-weight: 300;
font-style: italic;
text-transform: uppercase;
font-size: 60pt;
letter-spacing: 2px;
color: white;
text-align: center;
margin-top:px;}
h5.birminghamnow {font-family: "proxima-nova"; helvetica; arial;   
sans-serif;
font-weight: 100;
font-style: normal;
font-size: 30pt;
letter-spacing: 2px;
color: white;
text-align: center;
margin-top:px;}

【问题讨论】:

  • 这是一个标准问题绝对定位问题。请花一些时间来了解绝对、相对和固定定位。最终,您将希望对标记结构采取不同的方法。没有理由不使用背景图像并将按钮放在具有所述背景图像的元素内。
  • 我可以试试...谢谢。

标签: css image button w3.css


【解决方案1】:

它飞走的原因是left:630px; 是绝对的。这意味着当屏幕变小时,630px 将不再居中。

要解决这个问题,请使用相对单位。有很多方法可以在 css 中居中,howtocenterincss.com 可以很好地为您提供所需的 css。

或者left: 50%; transform: translate(-50%, 0); 应该可以工作。

请注意,bottom 也是如此,但在这里它可能不是问题。


但是,由于您的图像上也有文字,因此在确定按钮位置时最好考虑到这一点。

基本上都在文本和按钮周围制作一个包装器,并将该包装器对齐在图像的中间。然后按照您想要的方式布置它们。

【讨论】:

  • 我很确定这不是防弹的。图像缩小时按钮位置会发生什么变化?它可能会覆盖图像中的文本。
  • @hungerstar 不一定。他的问题只是水平居中,所以我认为他在垂直对齐方面没有问题。然而,bottom: 225px; 会给他留下同样的问题,这就是我警告他的原因。
  • @hungerstar 也许他想将文本放在图像之外的较小屏幕上,这将解决覆盖问题。 idk,但他只抱怨居中不正确。
  • 我作弊了,白色文字是图像的一部分。这是懒惰的设计吗? :P 我会尝试将它作为背景图像分配给 div。
  • 理想情况下,在绝大多数情况下,文本不应包含在图像中。这是糟糕的 SEO 并会产生可访问性问题。最好避免,直到你知道什么时候可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 2011-07-28
  • 2020-12-21
  • 1970-01-01
相关资源
最近更新 更多