Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

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>
like image 791
3gwebtrain Avatar asked May 13 '16 14:05

3gwebtrain


3 Answers

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/

like image 190
Ahmed Salama Avatar answered Oct 13 '22 12:10

Ahmed Salama


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/

like image 26
user2530671 Avatar answered Oct 13 '22 11:10

user2530671


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>
like image 29
Shaggy Avatar answered Oct 13 '22 10:10

Shaggy