使用相对位置和绝对位置时,我的图像消失了

My images disappear when using position relative and absolute

我将此代码用于多篇文章并且我认为它工作正常,除非我尝试通过添加样式 position: relative 和 [=13= 为文章中的其中一张图像添加一种特殊样式] 但是随后图像宽度缩小到 0 或者它们消失了,我不知道我在这里犯了什么样的错误。请指教

尝试删除 position: relativeid="target" 并查看代码的工作原理。

即使有更好的方法来使用 jQuery 或 JS 实现该概念,请分享。

.article {
  border: 1px solid red;
  padding: 0;
  display: flex;
}
 
.article-content {
  border: 1px solid black;
  width: 46.66%;
  text-align: left;
  align-self: center;
  margin-left: auto;
}

.img-control {
  border:1px solid black;
  max-width: 53.66%;
  margin-right: auto;
  margin-left: 1rem;
  overflow: hidden;
}

.image {
  width: auto;
  width:100%;
}

.img-control img {
  display: block;
  max-width: 100%;
  width: auto;
}

img #target {
  transform: scale(2.2) translate(-35%, 29%) rotate(-25deg);
  width: 670px;
  display: block;
  position: absolute;
}

@media only screen and (max-width:480px) {
  .article {
    flex-flow: wrap column;
  }
  .article-content, .img-control {
    margin: 0 auto;
  }
} 
<div class="article">
  <div class="article-content">
    <h3 class="mainTitle">What is Lorem Ipsum?</h3>
    <h5 class="subTitle">Lorem Ipsum</h5>
    <p> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
  </div>
  <div class="img-control" style="position: relative;">
    <div class="image">
      <img src="http://via.placeholder.com/466x585" width="100%" alt="" style="width: 670px;right: -48px;top: -18px; display: block;position: absolute;" id="target" />
    </div>
  </div>
</div>

如果您想使用绝对定位,您需要使用 position:absolute 或 position:relative 的 parent 容器。

此外,为了让元素占据其 parent 的整个高度或宽度,您需要设置 parent 的高度/宽度,然后设置 child' s 高度/宽度为 100%(所有可用 space 中的 parent)。最初您将图像高度设置为 670,因此它超过了 parent 元素块 Per Mdn

Percentages refer to the width of the containing block

    <style>

.article {
  border: 1px solid red;
  padding: 0;
  display: flex;
}

.article-content {
  border: 1px solid black;
  width: 46.66%;
  text-align: left;
  align-self: center;
  margin-left: auto;
}

.img-control {
  border:1px solid black;
  width: 20.66%;
  height:156px;
  margin-right: auto;
  margin-left: 1rem;
  overflow: hidden;
}

.image {
  height:100%;
  width:100%;
}

.img-control img {
  display: block;
  height:100%;
  width:100%;
}

img#target {
  transform: scale(2.2) translate(-35%, 29%) rotate(-25deg);
  width: 670px;
  display: block;
  position: absolute;
}

@media only screen and (max-width:480px) {
  .article {
    flex-flow: wrap column;
  }
  .article-content, .img-control {
    margin: 0 auto;
  }
}     
</style>
</head>
    <body>
<div class="article">
  <div class="article-content">
    <h3 class="mainTitle">What is Lorem Ipsum?</h3>
    <h5 class="subTitle">Lorem Ipsum</h5>
    <p> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
  </div>
  <div class="img-control" style="position: relative;">
    <div class="image">
      <img src="http://via.placeholder.com/466x585" width="100%" alt="" style="top: -18px; display: block;position: absolute;" id="target" />
    </div>
  </div>
</div>

Working Fiddle here

你的 .img-control 缩小了,因为它的宽度没有设置(widthmax-width 是不同的东西)。

另外 img #target 应该是 img#target 没有 space。 img #target表示"element with id='target' within img element",当img#target表示"img element with id='target'".

请避免使用内联样式,它们会让您和其他人感到困惑。