简体   繁体   English

css 图像出现在文本和另一个图像上

[英]css image appearing over text and another image

I am trying to have an icon with a number on top in the left corner of a div container, to the right of this number and icon is an image.我试图在 div 容器的左上角有一个带有数字的图标,在这个数字的右侧,图标是一个图像。 On the desktop this looks fine, separated accordingly but on the mobile devices, the image on the right appears over the icon and text on the left.在桌面上这看起来很好,相应地分开,但在移动设备上,右侧的图像出现在左侧的图标和文本之上。

HTML looks like this: HTML 看起来像这样:

                        <div class="col-1">
                            <div class="wrap-image">
                            <a
                                th:rowNumber="${status.count}" th:href="@{'/basket/remove/' + ${product.id}}">
                                <img src="images/bin.png" height="60">
                                <span z-index: 10;>[[${status.count}]]</span>
                            </a>
                            </div>

                    </div>
                    <div class="col-3">
                        <img th:src="@{${product.mainImagePath}}" class="img-fluid" />
                    </div>

css: css:

.wrap-image {
  position: relative;
  width: min-content;
}

.wrap-image span {
  position: absolute;
  top: 1px;
  left: 20px;
}

Device: device设备:设备

device2设备2

PC: desktop电脑:台式机

Image and text if they were more in the corner,it might not happen but the sizing for the main image is not correct either for a mobile device, but not sure how to change it.如果图像和文本更多地位于角落,则可能不会发生,但主图像的大小对于移动设备也不正确,但不知道如何更改它。

Set z-index: 10;设置z-index: 10; on your span and create a separate rule set for.wrap-image with a property position: relative;在您的跨度上并为.wrap-image 创建一个单独的规则集,其属性position: relative; . .

A z-index is essentially layering so it always appears on top of your image and when using absolute position you always want to set the position of the parent as relative so that the absolute position of left, right, center, bottom is actually realtive to the parent container rather than the next relatively positioned element or body is one is not defined. z-index 本质上是分层的,因此它总是出现在图像的顶部,当使用绝对 position 时,您总是希望将父级的 position 设置为相对,这样绝对的 position父容器而不是下一个相对定位的元素或主体是未定义的。

Try positioning it directly to the top of the container using the below:尝试使用以下方法将其直接定位到容器顶部:

.wrap-image {
  position: relative;
  width: min-content;
  top: 0px;
  right: 30px;
}

.wrap-image span {
  position: absolute;
  top: 0px;
  right: 30px;
}

The bigger image(in the second div) itself probably needs to be wrapped too.更大的图像(在第二个 div 中)本身可能也需要包装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM