如何在文字上方放置图片?

时间:2018-12-17 19:51:20

标签: html css

top属性似乎不适用于html。我正在尝试使用图像上的top属性将图像移到顶部并放置在文本上方,但是css的top属性从不移动图像这是代码段

<div class="stl_02">
            <div class="stl_03">
                <img src="" 
                alt=""style="top: 4.4538em;" class="stl_04">
            </div>
            <div class="stl_view">
                <div class="stl_05 stl_06">
//other texts here

这是CSS规则

.stl_02 {
    height: 46em;
    font-size: 1em;
    margin: 0em;
    line-height: 0.0em;
    display: block;
    border-style: none;
    width: 51em;
}
.stl_03 {
    position: relative;
}
.stl_04 {
    width: 100%;
    clip: rect(-0.041667em,51.04167em,66.04166em,-0.041667em);
    position: absolute;
    pointer-events: none;
}

请使用该属性style =“ top:4.4538em;”如何将图像推到顶部。是一个挑战

1 个答案:

答案 0 :(得分:-1)

您的元素确实已应用top属性。这可以在以下内容中看到:

.stl_02 {
  height: 46em;
  font-size: 1em;
  margin: 0em;
  line-height: 0.0em;
  display: block;
  border-style: none;
  width: 51em;
}

.stl_03 {
  position: relative;
}

.stl_04 {
  width: 100%;
  clip: rect(-0.041667em, 51.04167em, 66.04166em, -0.041667em);
  position: absolute;
  pointer-events: none;
}
<div class="stl_02">
  <div class="stl_03">
    <img src="http://placehold.it/100" alt="" style="top: 4.4538em;" class="stl_04">
  </div>
  <div class="stl_view">
    <div class="stl_05 stl_06">
    </div>
  </div>
</div>

如果您没有看到此效果,则可能是您的规则被更高的特异性所覆盖,或者您在应用此规则之前已经缓存了样式。

还值得注意的是,top仅适用于定位元素。您必须在position: relative上放置position: absolute.stl-04或类似名称才能将其放置在top上。

或者,您可能正在寻找margin-top,它基于包含元素垂直放置。

顺便说一句,基于字体大小(以em单位为单位的边距)通常是不明智的做法;您应该真正使用固定单位(最好不要使用那么多小数位)。

相关问题