html - 如何使标题具有响应性?

标签 html css

我正在尝试制作一张在左中带有标题的图像。标题应始终位于图像的左侧中间或左侧的任何位置,并且其大小应同时随浏览器大小而变化。 问题是,当我调整窗口大小时,标题的大小不会改变,而且它的位置也不会固定在图像上。

.page-overview{
        width: 100%;
        border: 1px solid black;
    }

    .overview-content img{
        width: 100%;
    }
    
    .caption{
        position: absolute;
        top: 20%;
        left: 18px;
        font-size: 80%;
        font-family: 'Source Sans Pro', sans-serif;
        z-index: 1;
    }
    .caption span{
        display: block;
        margin: 8px 0;
        padding: 10px 17px;
        border-radius: 3px;
        background-color: #00000033;
    }
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>test YOU</title>
  
</head>
<body>

    <!--Here can have other content too-->
    <div class="page-overview">
        <div class="overview-content"><a href="#"><img src="https://images5.alphacoders.com/481/481903.png" alt="Delilious & Yum" height="auto"></a>
            <div class="caption">
                <span>Test your best here</span>
                <span>It's Yum 😋</span>
            </div>
        </div>
    </div>

最佳答案

删除position:absolute;并使用relative代替。 absolute 不会考虑浏览器收缩,它仍然会停留在指定区域。

.page-overview {
  width: 100%;
  border: 1px solid black;
}

.overview-content img {
  width: 100%;
}

.caption {
  position: relative;
  width: fit-content;
  margin: auto 0;
  top: 20%;
  left: 18px;
  font-size: 80%;
  font-family: 'Source Sans Pro', sans-serif;
  z-index: 1;
}

.caption span {
  display: block;
  margin: 8px 0;
  padding: 10px 17px;
  border-radius: 3px;
  background-color: #00000033;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>test YOU</title>

</head>

<body>

  <!--Here can have other content too-->
  <div class="page-overview">
    <div class="overview-content">
      <a href="#"><img src="https://images5.alphacoders.com/481/481903.png" alt="Delilious & Yum" height="auto"></a>
      <div class="caption">
        <span>Test your best here</span>
        <span>It's Yum 😋</span>
      </div>
    </div>
  </div>

关于html - 如何使标题具有响应性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71443724/

相关文章:

javascript - ReactJS:单击按钮下载 CSV 文件

html - 如何使用 CSS 或 Javascript 强制浏览器在浏览器左侧显示滚动条?

HTML、CSS 动画 : floating on iphone safari not like in other browsers

javascript - Mini-css-extract-plugin 没有使用 sass 将我的 css 捆绑到一个文件中

隐藏在外面的CSS溢出

html - 如何让我的菜单在 Firefox 中工作?

html - 响应悬停代码

html - Vuejs v-for 在更新 vuex 对象时重新构建

html - css stack divs 在彼此之上

css - 可重复使用的旋转器 CSS3 动画。 Sass变了吗?