悬停时如何使Flex边框变为红色?

时间:2019-03-01 12:56:28

标签: html css css3 flexbox

当光标悬停在弹性框上方时,是否可以在弹性框周围添加红色边框? 如果是这样,请您修改代码。谢谢。 如果有边框动画或看起来不错的东西,请发表您的想法。

      .container {
        width: 80%;
        margin: 0 auto;
        font-size: 14;
        text-align: center;
      }
      .grid-item {
        width: 200px;
        height: 200px;
        display: inline-block;
        margin: 4px;
        font-size: 1rem;
        vertical-align: top;
        padding: 16px;
      }

      .green {
        background-color: darkgreen;
      }
      .blue {
        background-color: cornflowerblue;
      }
      .navy {
        background-color: navy;
      }
      .gray {
        background-color: gray;
      }
<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content ="ie=edge">



<title>Flexbox</title>

<style>


</style>
</head>



<body>



    <div class="container">
      

      <div class="grid-item navy">
        <h2 style="color:white"> 
        Box 1
        </h2>
      </div>


      <div class="grid-item gray">

        <h2 style="color:white"> 
        Box 2
        </h2>
      </div>

      <div class="grid-item green">

        <h2 style="color:white"> 
        Box 3 
        </h2>
      </div>

      <div class="grid-item blue">
        <h2 style="color:white"> 
        Box 4
        </h2>
      </div>


    </div>

</body>

2 个答案:

答案 0 :(得分:2)

是的,您可以将hover选择器添加到CSS中相应的div中,如下所示:

.container {
    width: 80%;
    margin: 0 auto;
    font-size: 14;
    text-align: center;
}

.grid-item {
  width: 200px;
  height: 200px;
  display: inline-block;
  margin: 4px;
  font-size: 1rem;
  vertical-align: top;
  padding: 16px;
}

.green {
  background-color: darkgreen;
}

.blue {
  background-color: cornflowerblue;
}

.navy {
  background-color: navy;
}

.gray {
  background-color: gray;
}

.grid-item:hover {
  border: 5px solid red;
  -webkit-transition: 1s; /* Safari */
  transition: 1s;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content ="ie=edge">

  <title>Flexbox</title>
  <style>
  </style>
  </head>
<body>
    <div class="container">
      <div class="grid-item navy">
        <h2 style="color:white"> 
        Box 1
        </h2>
      </div>
      <div class="grid-item gray">
        <h2 style="color:white"> 
        Box 2
        </h2>
      </div>
      <div class="grid-item green">
        <h2 style="color:white"> 
        Box 3 
        </h2>
      </div>
      <div class="grid-item blue">
        <h2 style="color:white"> 
        Box 4
        </h2>
      </div>
    </div>
</body>

答案 1 :(得分:0)

.grid-item {
    width: 200px;
    height: 200px;
    display: inline-block;
    margin: 4px;
    font-size: 1rem;
    vertical-align: top;
    padding: 16px;
    border: 5px solid transparent;
    transition:1s;
  }
.grid-item:hover{border: 5px solid red;
transition:1s;}

尝试一下