【问题标题】:Changing one of the pink squares to a different color将其中一个粉红色方块更改为另一种颜色
【发布时间】:2022-12-04 22:19:51
【问题描述】:

这会将所有方块更改为粉红色。 https://jsfiddle.net/7cye4hfu/1/

如果我想将其中一个粉红色方块更改为另一种颜色,我该怎么做?

.channel-browser__channel-grid-item::after {
  content: "";
  position: absolute;
  inset: 0;
  background-color: pink;
}

.channel-browser__channel-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}

.channel-browser__channel-grid-item {
  position: relative;
}

.content-item-container--aspect-square .horizontal-content-browser__content-item {
  padding-top: 100%;
}

.horizontal-content-browser__content-item .horizontal-content-browser__fallback-image,
.horizontal-content-browser__content-item .responsive-image-component {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: auto;
  border-radius: 4px;
  background-color: #1a1a1a;
  -webkit-box-shadow: 0 2px 3px 0 rgb(0 0 0 / 20%);
  box-shadow: 0 2px 3px 0 rgb(0 0 0 / 20%);
}

.box-color-red {
  background: red;
  border-radius: 4px;
}

.box-color-blue {
  background: blue;
}

.box-color-yellow {
  background: yellow;
}

.box-color-green {
  background: green;
}
<div class="channel-browser__channels channel-browser__channel-grid content-item-container content-item-container--aspect-square">
  <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible box-color-red"></div>
  <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible box-color-blue"></div>
  <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible box-color-yellow"></div>
  <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible box-color-green"></div>
  <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible">
    <img width="280" height="280" src="https://via.placeholder.com/465x465" class="responsive-image-component">
  </div>
  <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible">
    <img width="280" height="280" src="https://via.placeholder.com/465x465" class="responsive-image-component">
  </div>
  <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible">
    <img width="280" height="280" src="https://via.placeholder.com/465x465" class="responsive-image-component">
  </div>
  <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible">
    <img width="280" height="280" src="https://via.placeholder.com/465x465" class="responsive-image-component">
  </div>
</div>

【问题讨论】:

    标签: html css background background-color


    【解决方案1】:

    很多种方法。这是两个。

    注意不要有内容:“”如果你想有内容

    .channel-browser__channel-grid-item.box-color-red::after {
      background-color: red;
    }
    .channel-browser__channel-grid-item:nth-of-type(4)::after {
      background-color: green;
    }
    

    .channel-browser__channel-grid-item::after {
      position: absolute;
      inset: 0;
      background-color: pink;
    }
    
    .channel-browser__channel-grid-item.box-color-red::after {
      background-color: red;
    }
    .channel-browser__channel-grid-item:nth-of-type(4)::after {
      background-color: green;
    }
    
    .channel-browser__channel-grid {
      display: grid;
      gap: 16px;
      grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
      margin: 0;
      padding: 0;
      border: 0;
      font-size: 100%;
      font: inherit;
      vertical-align: baseline;
    }
    
    .channel-browser__channel-grid-item {
      position: relative;
    }
    
    .content-item-container--aspect-square .horizontal-content-browser__content-item {
      padding-top: 100%;
    }
    
    .horizontal-content-browser__content-item .horizontal-content-browser__fallback-image,
    .horizontal-content-browser__content-item .responsive-image-component {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: auto;
      border-radius: 4px;
      background-color: #1a1a1a;
      -webkit-box-shadow: 0 2px 3px 0 rgb(0 0 0 / 20%);
      box-shadow: 0 2px 3px 0 rgb(0 0 0 / 20%);
    }
    
    .box-color-red {
      background: red;
      border-radius: 4px;
    }
    
    .box-color-blue {
      background: blue;
    }
    
    .box-color-yellow {
      background: yellow;
    }
    
    .box-color-green {
      background: green;
    }
    <div class="channel-browser__channels channel-browser__channel-grid content-item-container content-item-container--aspect-square">
      <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible box-color-red"><button>Click</button></div>
      <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible box-color-blue"></div>
      <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible box-color-yellow"></div>
      <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible box-color-green"></div>
      <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible">
        <img width="280" height="280" src="https://via.placeholder.com/465x465" class="responsive-image-component">
      </div>
      <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible">
        <img width="280" height="280" src="https://via.placeholder.com/465x465" class="responsive-image-component">
      </div>
      <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible">
        <img width="280" height="280" src="https://via.placeholder.com/465x465" class="responsive-image-component">
      </div>
      <div class="horizontal-content-browser__content-item content-item channel-browser__channel-grid-item content-item--channel visible">
        <img width="280" height="280" src="https://via.placeholder.com/465x465" class="responsive-image-component">
      </div>
    </div>

    【讨论】:

    • 在我添加代码将所有方块更改为单一颜色后,删除了所有按钮,现在它们没有出现在屏幕上。 jsfiddle.net/fxo1qLje你知道怎么解决吗?
    • 我添加了按钮消失的原因
    【解决方案2】:

    代替 :

    .box-color-red {
        background: red;
        border-radius: 4px;
     }
    
     .box-color-blue {
         background: blue;
     }
    
     .box-color-yellow {
         background: yellow;
     }
    
     .box-color-green {
         background: green;
     }
    

    尝试使用:

    .box-color-red::after {
        background: red;
        border-radius: 4px;
     }
    
     .box-color-blue::after {
         background: blue;
     }
    
     .box-color-yellow::after {
         background: yellow;
     }
    
     .box-color-green::after {
         background: green;
     }
    

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 1970-01-01
      • 2018-12-17
      • 2014-11-24
      • 1970-01-01
      • 2013-08-15
      • 2021-06-07
      • 2013-04-27
      • 2011-10-01
      相关资源
      最近更新 更多