【问题标题】:How to change background when I hover the card? [duplicate]悬停卡片时如何更改背景? [复制]
【发布时间】:2022-01-05 16:19:55
【问题描述】:

当我将鼠标悬停在卡片上时,我想更改背景。我曾尝试添加悬停类,但它不起作用,所以我现在在这里创建这个社区。​​p>

@import url("https://fonts.googleapis.com/css2?family=Work+Sans:wght@400;500;700&display=swap");

*,
*::before,
*::after {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

:root {
  --default-text-color: white;
  --default-border-color: white;
  --hover-background: rgba(220, 20, 60, 0.699) 0px 48px 100px 0px;
  --hover-color: crimson;
}

p,
h1,
h6,
span {
  font-family: "Work Sans", sans-serif;
}

html {
  background-color: black;
}

.title {
  text-align: center;
  font-weight: 400;
  font-size: 3rem;
  color: white;
}

.job-role {
  text-align: center;
  font-weight: 500;
  font-size: 2.8rem;
  letter-spacing: -0.2rem;
  position: relative;
  bottom: 20%;
}

.job-label {
  border: 1px solid transparent;
  border-radius: 1.1rem;
  margin: 2rem;
  padding: 0.5rem;
  font-weight: 500;
  background-color: rgba(255, 255, 255, 0.2);
  color: var(--default-text-color);
}

.job-title {
  color: var(--default-text-color);
}

.job-description {
  color: rgba(255, 255, 255, 0.3);
}

.location {
  font-weight: 500;
  font-size: 1.2rem;
  font-style: bold;
  color: var(--default-text-color);
  margin: 1rem;
}

.card-container {
  border: 1px solid var(--default-border-color);
  border-radius: 1.1rem;
  max-width: 350px;
  height: 30rem;
  margin: 1.2rem;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  position: relative;
  transition: 300ms linear all;
}




.showcase {
  display: flex;
  justify-content: center;
}

@media screen and (max-width: 710px) {
  .showcase {
    flex-direction: column;
  }
}
<main>
  <h1 class="title">Cards</h1>

  <div class="showcase">
    <section class="card-container card-1" role="button" tabindex="0">
      <span class="job-label">Sports, Chess</span>
      <div class="job-role">
        <p class="job-title">Mankuda Giri</p>
        <p class="job-description">Chess Grand Master</p>
      </div>

      <p class="location">India</p>
    </section>

   
  </div>
</main>

我想在背景中添加任何颜色。对于我发布问题的方式给您带来的任何不便,我们深表歉意。这是我关于 SO 的第一个问题。 提前致谢。

【问题讨论】:

  • 鼠标悬停在卡片上时是否要更改卡片背景?您可以为此使用 :hover。
  • 您可以在“.card-container:hover”中添加样式,当您将鼠标悬停在具有此类的 DOM 对象上时会调用该样式。
  • 这能回答你的问题吗? How to use 'hover' in CSS

标签: html css bootstrap-4


【解决方案1】:

如果您希望在悬停时更改卡片的背景,可以将其添加到您的 css 中:

.card-container:hover {
    background: cadetblue;
  }

【讨论】:

    【解决方案2】:

    使用这个

    .showcase .card-1:hover{
    background-color:green;
    }
    

    @import url("https://fonts.googleapis.com/css2?family=Work+Sans:wght@400;500;700&display=swap");
    
    *,
    *::before,
    *::after {
      box-sizing: border-box;
      padding: 0;
      margin: 0;
    }
    
    :root {
      --default-text-color: white;
      --default-border-color: white;
      --hover-background: rgba(220, 20, 60, 0.699) 0px 48px 100px 0px;
      --hover-color: crimson;
    }
    
    p,
    h1,
    h6,
    span {
      font-family: "Work Sans", sans-serif;
    }
    
    html {
      background-color: black;
    }
    
    .title {
      text-align: center;
      font-weight: 400;
      font-size: 3rem;
      color: white;
    }
    
    .job-role {
      text-align: center;
      font-weight: 500;
      font-size: 2.8rem;
      letter-spacing: -0.2rem;
      position: relative;
      bottom: 20%;
    }
    
    .job-label {
      border: 1px solid transparent;
      border-radius: 1.1rem;
      margin: 2rem;
      padding: 0.5rem;
      font-weight: 500;
      background-color: rgba(255, 255, 255, 0.2);
      color: var(--default-text-color);
    }
    
    .job-title {
      color: var(--default-text-color);
    }
    
    .job-description {
      color: rgba(255, 255, 255, 0.3);
    }
    
    .location {
      font-weight: 500;
      font-size: 1.2rem;
      font-style: bold;
      color: var(--default-text-color);
      margin: 1rem;
    }
    
    .card-container {
      border: 1px solid var(--default-border-color);
      border-radius: 1.1rem;
      max-width: 350px;
      height: 30rem;
      margin: 1.2rem;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: center;
      position: relative;
      transition: 300ms linear all;
    }
    
    
    
    
    .showcase {
      display: flex;
      justify-content: center;
    }
    .showcase .card-1:hover{
    background-color:green;
    }
    @media screen and (max-width: 710px) {
      .showcase {
        flex-direction: column;
      }
    }
    <main>
      <h1 class="title">Cards</h1>
    
      <div class="showcase">
        <section class="card-container card-1" role="button" tabindex="0">
          <span class="job-label">Sports, Chess</span>
          <div class="job-role">
            <p class="job-title">Mankuda Giri</p>
            <p class="job-description">Chess Grand Master</p>
          </div>
    
          <p class="location">India</p>
        </section>
    
       
      </div>
    </main>

    【讨论】:

      【解决方案3】:

      您可以使用:hover状态在卡片上添加悬停效果

      .card-container:hover {
          background: green;
      }
      

      【讨论】:

        【解决方案4】:
        .card-container:hover {
          background-color: blue;
        }
        

        只需添加此 CSS 即可完成

        【讨论】:

          【解决方案5】:

          你需要使用:hover选择器用于在鼠标悬停时选择元素

          section.card-container:hover{
          background:#969696;
          }
          

          【讨论】:

            【解决方案6】:

            你可以在css类之后使用:hover来改变背景颜色

            .card-container:hover{background-color: blue;}
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-08-06
              • 2023-03-14
              • 1970-01-01
              • 2019-03-17
              • 2020-07-24
              • 2022-01-18
              • 2013-07-21
              • 1970-01-01
              相关资源
              最近更新 更多