3gwebtrain
3gwebtrain

Reputation: 15307

On hover how can I make my child border overlap the parent border?

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>

Upvotes: 21

Views: 9252

Answers (5)

Shaggy
Shaggy

Reputation: 6796

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>

Upvotes: 7

Christian Hill
Christian Hill

Reputation: 378

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

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

Upvotes: -2

user2530671
user2530671

Reputation: 458

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/

Upvotes: 14

Ahmed Salama
Ahmed Salama

Reputation: 2825

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/

Upvotes: 14

Gavin Thomas
Gavin Thomas

Reputation: 1206

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

https://jsfiddle.net/9Lmwp0e7/

Something like this?

Upvotes: -2

Related Questions