在悬停时如何让我的孩子边框与父边框重叠?

时间:2021-07-25 20:05:32

I have a parent with red border. When the user hovers on children, I am trying to change the children's border to overlap the parent. How can I achieve this?

我的父母有红色边框。当用户在孩子上盘旋时,我试图改变孩子的边框以与父母重叠。我怎样才能做到这一点?

.parent{
  border:1px solid red;
  display:inline-block;
}

.parent a {
  display:block;
  padding:1em;
  border-bottom:1px solid #ddd;
}

.parent a:hover{
  border:1px solid #ddd;
}
<div class="parent">
      <a href="#">1</a>
      <a href="#">2</a>
      <a href="#">3</a>
      <a href="#">4</a>
      <a href="#">5</a>
    </div>

5 个解决方案

#1


8  

So long as you know the width of the borders and are able to use relative positioning... I have exaggerated the borders here for clarity.

只要你知道边框的宽度并且能够使用相对定位......为了清楚起见,我在这里夸大了边框。

.parent {
  border:5px solid red;
  display:inline-block;
  position: relative;
}

.parent a {
  display:block;
  padding:1em;
  border-bottom:1px solid #ddd;
  position: relative;
}

.parent a:hover{
  border:5px solid #ddd;
  margin: -5px;
}

https://jsfiddle.net/kd0gf31z/

https://jsfiddle.net/kd0gf31z/

#2


13  

I made some changes on your style:

我对你的风格做了一些改变:

.parent{
  display:inline-block;
}

.parent a {
  display:block;
  padding:1em;
  border-right:1px solid red;
  border-left:1px solid red;
  border-bottom:1px solid #ddd;
  border-top: none;
}

.parent a:first-of-type{
  border-top:1px solid red;
}

.parent a:last-of-type{
  border-bottom:1px solid red;
}

.parent a:hover{
  border-color:#ddd;
}

see the result here https://jsfiddle.net/IA7medd/343ydvhs/

在这里看到结果https://jsfiddle.net/IA7medd/343ydvhs/

#3


3  

You could achieve this through the use of absolutely positioned pseudo elements, like so:

你可以通过使用绝对定位的伪元素来实现这一点,如下所示:

*,*::before,*::after{box-sizing:border-box;font-family:sans-serif;}
.parent{
    border:1px solid red;
    display:inline-block;
}
.parent a{
    display:block;
    padding:1em;
    border-bottom:1px solid #ddd;
    position:relative;
}
.parent a:last-child{
    border:0;
}
.parent a:hover::after{
    border:1px solid #ddd;
    bottom:-1px;
    content:"";
    left:-1px;
    position:absolute;
    right:-1px;
    top:-1px;
}
<div class="parent">
    <a href="#">1</a>
    <a href="#">2</a>
    <a href="#">3</a>
    <a href="#">4</a>
    <a href="#">5</a>
</div>

#4


-2  

.parent a:hover{
  border:1px solid #ddd;
  width:120%;
}

https://jsfiddle.net/9Lmwp0e7/

https://jsfiddle.net/9Lmwp0e7/

Something like this?

像这样的东西?

#5


-2  

Why not just change the parent CSS on the child hover?

为什么不直接更改子悬停上的父CSS?

.parent a:hover .parent {
//Some CSS
}

#1


8  

So long as you know the width of the borders and are able to use relative positioning... I have exaggerated the borders here for clarity.

只要你知道边框的宽度并且能够使用相对定位......为了清楚起见,我在这里夸大了边框。

.parent {
  border:5px solid red;
  display:inline-block;
  position: relative;
}

.parent a {
  display:block;
  padding:1em;
  border-bottom:1px solid #ddd;
  position: relative;
}

.parent a:hover{
  border:5px solid #ddd;
  margin: -5px;
}

https://jsfiddle.net/kd0gf31z/

https://jsfiddle.net/kd0gf31z/

#2


13  

I made some changes on your style:

我对你的风格做了一些改变:

.parent{
  display:inline-block;
}

.parent a {
  display:block;
  padding:1em;
  border-right:1px solid red;
  border-left:1px solid red;
  border-bottom:1px solid #ddd;
  border-top: none;
}

.parent a:first-of-type{
  border-top:1px solid red;
}

.parent a:last-of-type{
  border-bottom:1px solid red;
}

.parent a:hover{
  border-color:#ddd;
}

see the result here https://jsfiddle.net/IA7medd/343ydvhs/

在这里看到结果https://jsfiddle.net/IA7medd/343ydvhs/

#3


3  

You could achieve this through the use of absolutely positioned pseudo elements, like so:

你可以通过使用绝对定位的伪元素来实现这一点,如下所示:

*,*::before,*::after{box-sizing:border-box;font-family:sans-serif;}
.parent{
    border:1px solid red;
    display:inline-block;
}
.parent a{
    display:block;
    padding:1em;
    border-bottom:1px solid #ddd;
    position:relative;
}
.parent a:last-child{
    border:0;
}
.parent a:hover::after{
    border:1px solid #ddd;
    bottom:-1px;
    content:"";
    left:-1px;
    position:absolute;
    right:-1px;
    top:-1px;
}
<div class="parent">
    <a href="#">1</a>
    <a href="#">2</a>
    <a href="#">3</a>
    <a href="#">4</a>
    <a href="#">5</a>
</div>

#4


-2  

.parent a:hover{
  border:1px solid #ddd;
  width:120%;
}

https://jsfiddle.net/9Lmwp0e7/

https://jsfiddle.net/9Lmwp0e7/

Something like this?

像这样的东西?

#5


-2  

Why not just change the parent CSS on the child hover?

为什么不直接更改子悬停上的父CSS?

.parent a:hover .parent {
//Some CSS
}