Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z-index on borders

Tags:

I have been searching around for a way to add z-index to borders but can't seem to find one so I thought I'd ask here.

Say I have a div with a parent div. The parent has a border and I want that border to overlap the child div but I don't want the parent to overlap it.

like image 383
ItsNotAbyss Avatar asked Mar 06 '15 10:03

ItsNotAbyss


People also ask

What is Z Index 9999?

In CSS code bases, you'll often see z-index values of 999, 9999 or 99999. This is a perhaps lazy way to ensure that the element is always on top. It can lead to problems down the road when multiple elements need to be on top.

What does Z Index do?

The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order.

How do you use Z index without absolute positioning?

Yes: use position:relative; z-index:10 . z-index has no effect for position:static (the default).


2 Answers

You cannot do that with a border.

Interestingly though, you can do it with an outline

* {  box-sizing: border-box;  }    .parent {  width: 200px;  height: 200px;  margin: 25px auto;  position: relative;  background: #bada55;  border:12px solid #663399;  outline: 12px solid red;  padding:25px   }    .child {  width: 220px;  height: 100px;  background: lightblue;  }
<div class="parent">    <div class="child"></div>  </div>

Other Options

Using pseudo-elements

1. Pseudo-element with border

Requires some additional transforms to nudge into place.

* {    box-sizing: border-box;  }  .parent {    width: 200px;    height: 200px;    margin: 25px auto;    position: relative;    background: #bada55;    padding: 25px;  }  .child {    width: 220px;    height: 100px;    background: lightblue;  }  .absolute::after {    content: '';    position: absolute;    top: 50%;    left: 50%;    transform: translate(-50%, -50%);    width: 100%;    height: 100%;    border: 12px solid red;  }
<div class="parent absolute">    <div class="child"></div>  </div>

2. Pseudo-element with box-shadow

* {    box-sizing: border-box;  }  .parent {    width: 200px;    height: 200px;    margin: 25px auto;    position: relative;    background: #bada55;    padding: 25px;  }  .child {    width: 220px;    height: 100px;    background: lightblue;  }  .shadow::after {    content: '';    position: absolute;    top: 0%;    left: 0%;    width: 100%;    height: 100%;    box-shadow: 0 0 0 12px red;  }
<div class="parent shadow">    <div class="child"></div>  </div>
like image 183
Paulie_D Avatar answered Sep 27 '22 18:09

Paulie_D


style border on pseudo element

  1. for higher z-index use pseudo element eg. ::before
  2. to be able to interact with div under - use pointer-events: none; on the pseudo element (see spec)

important part of code:

.parent {     position: relative;     ... } .parent::before {     pointer-events: none;     position: absolute;     border: 1px solid #000;     ... } 

full example: https://codepen.io/Michal-Miky-Jankovsky/pen/QoXbLz

like image 35
Michal Miky Jankovský Avatar answered Sep 27 '22 18:09

Michal Miky Jankovský