html - 如何让几个div在同一个位置动态

标签 html css

我想要一个带有图像的 div,然后是底部和顶部带有渐变的一段文本。

.cbr-product-square {
  padding: 15px;
  border: 1px solid rgba(0, 0, 0, 0.4);
  position: relative;
  height: 350px;
}

.cbr-product-click {
  cursor: pointer;
  width: 100%;
  height: 100%;
}

.cbr-product-buttons {
  color: rgba(255, 255, 255, 0.8);
  background: linear-gradient(180deg, rgba(0, 0, 0, 1) 0%, rgba(0, 0, 0, 0.65) 70%, rgba(0, 0, 0, 0) 100%);
  z-index: 10;
  height: 40px;
  line-height: 40px;
  width: 100%;
  margin-bottom: -40px;
  text-align: center;
}

.cbr-product-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cbr-product-desc {
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  z-index: 10;
  background: linear-gradient(0deg, rgba(0, 0, 0, 1) 0%, rgba(0, 0, 0, 0.65) 70%, rgba(0, 0, 0, 0) 100%);
  height: 80px;
  margin-top: -80px;
  padding-top: 30px;
  padding-left: 10px;
  width: 100%;
}
<div class="row cbr-product-square">
  <div class="cbr-product-click" [routerLink]="['productsOptions', element.id]">
    <div class="cbr-product-buttons">
      <button class="cbr-delete-btn" *ngIf="checkRole('administration')" mat-icon-button (click)="deletePosition(element.id,element.name)"><i class='far fa-times-circle'></i></button>&nbsp
      <button mat-icon-button *ngIf="checkRole('administration')" class="cbr-edit-btn" (click)="editProduct(element.id)"><i class='far fa-edit'></i></button>
    </div>
    <img class="cbr-product-image" [src]=element.imageBlob />
    <div class="cbr-product-desc">
      {{element.name}} <br/> {{element.price }} PLN
    </div>
  </div>
</div>

现在,我可以看到 div 中的图像以及底部和顶部的文本,但看不到渐变。它用作底部和顶部 div 的背景。当图像消失时,渐变出现(就像它总是在图像下方)。我试图对它们进行 z-index,但它什么也没给我。

最佳答案

给图片添加z-index:-1position:relative:

.cbr-product-square{
  padding: 15px;
  border: 1px solid rgba(0,0,0,0.4);
  position: relative;
  height: 350px;
}
.cbr-product-click
{
    cursor: pointer;
    width: 100%;
    height: 100%;
}
.cbr-product-buttons{
  color: rgba(255,255,255,0.8);
  background: linear-gradient(180deg, rgba(0,0,0,1) 0%, rgba(0,0,0,0.65) 70%, rgba(0,0,0,0) 100%);
  z-index: 10;
  height: 40px;
  line-height: 40px;
  width: 100%;
  margin-bottom: -40px;
  text-align: center;
}
.cbr-product-image{
    z-index:-1;
    position:relative;
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.cbr-product-desc{
    text-align: center;
    color: rgba(255,255,255,0.8);
    z-index: 10;
    background: linear-gradient(0deg, rgba(0,0,0,1) 0%, rgba(0,0,0,0.65) 70%, rgba(0,0,0,0) 100%);
    height: 80px;
    margin-top: -80px;
    padding-top: 30px;
    padding-left: 10px;
    width: 100%;
}
<div class="row cbr-product-square">
          <div class="cbr-product-click" [routerLink]="['productsOptions', element.id]"  >
            <div class="cbr-product-buttons">
              <button class="cbr-delete-btn" *ngIf="checkRole('administration')" mat-icon-button (click)="deletePosition(element.id,element.name)" ><i class='far fa-times-circle'></i></button>&nbsp
              <button  mat-icon-button *ngIf="checkRole('administration')" class="cbr-edit-btn" (click)="editProduct(element.id)" ><i class='far fa-edit'></i></button>
            </div>
            <!-- Free Photo by https://www.pexels.com/@nathan-moore-1300563
-->            <img class="cbr-product-image" src="https://images.pexels.com/photos/2479642/pexels-photo-2479642.jpeg?cs=srgb&dl=altitude-conifers-frost-2479642.jpg&fm=jpg&w=200" />
            <div class="cbr-product-desc">
              {{element.name}} <br/>
              {{element.price }} PLN
            </div>
          </div>
        </div>

关于html - 如何让几个div在同一个位置动态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56732626/

相关文章:

html - 防止网格在 flex 盒内溢出

php - 将变量传递给 wordpress php 函数

javascript - 根据文本更改背景

html - CSS 按钮图像调整大小

javascript - 如何在按钮单击时切换下拉菜单并在外部单击时关闭下拉菜单?

html - 如何在不影响周围其他DIV的情况下垂直扩展一个DIV?

javascript - 如何使用 JavaScript 更改所有 div 的样式属性?

php - 我使用 FPDF 和 PHP 制作了一个 PDF 生成器,但它只能在刷新后才能工作

javascript - 按钮单击所需的验证不起作用 JavaScript

jquery - 将 CSS 添加到特定类,这很奇怪