我正在try 制作一张图片,图片的左中间有一个标题.标题应始终位于图像的左中或左侧的任何位置,其大小应随浏览器大小而变化.

.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>

推荐答案

go 掉position: absolute;,改用相对的.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相关问答推荐

卸载伪元素 destruct 了CSS

Rmarkdown上的垂直标签集

HTML图像无法正确放大和缩小规模

将网格包装在css中

如何在htmlAngular 17的@for中使用索引

未知高度正在应用于前标记

按钮之间的HTML文本居中不正确

单独处理Button:Focus的多行按钮

文本幻灯片显示动画

调整窗口大小时文本重叠导航栏

将图像高度调整到容器 div 中而不使其高度增加

将 HTML 元素粘贴到容器底部,而不剪裁其顶部

什么没有 margin-right 和 width:100% 溢出子元素到左边?

子 div 在父 div 中溢出

在 SQL 中合并 HTML 表中的单元格

每次按下按钮时减小字体大小的 jquery 函数

沿文本对齐双引号

CSS:如何在模糊的背景上剪切文本?

动态使用时波浪号不转换为绝对路径

水平滚动框不显示所有元素