ketysek
ketysek

Reputation: 1219

Fixed modal with z-index issue

I've got a fixed modal with z-index: 100, but it's not above the all elements on the page. It's bellow an element with position: relative; z-index: 2; and I don't know why?! Here's the fiddle. Any help would be appreciated. Thanks!

Upvotes: 0

Views: 5838

Answers (2)

Md. Abu Sayed
Md. Abu Sayed

Reputation: 2486

actually when parent element z-index is poor then children is the high their parent.

when you tell 'modal-wrapper' z index=100 it does not work because of this element parent are 'paper-header' and this parent z-index is 2.

So, Children high is 2.

Solutions is

just make the sibling element to paper-header then Work Fine.

.paper-header {
  align-items: flex-start;
  display: flex;
  justify-content: space-between;
  position: relative;
  z-index: 2;
  background: #ffffff;
  min-height: 8rem;
}

.paper-body {
  position:relative;
  z-index: 1;
}

.modal-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  z-index: 100;
  overflow-x: hidden;
  overflow-y: auto;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.modal {
  width: 32.5rem;
  background-color: #FFFFFF;
  border-radius: 4px;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 101;
  padding: 1.25rem;
}

.modal-overlay {
  position: absolute;
  z-index: 100;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1.25rem;
  background-color: rgba(34, 34, 34, 0.5)
}
<div class="wrapper">
  <div class="paper-header">
    <h1>
      Segment 1
    </h1>
    
  </div>
  <div class="modal-wrapper">
    <div class="modal">
    modal content  
    </div>
    <div class="modal-overlay"></div>
  </div>
  <div class="paper-body">
    Segment body
  </div>
  
  <div class="paper-header">
    <h2>
      Segment 2
    </h2>
  </div>
</div>

---- thank you ----

Upvotes: 0

Ismail Rubad
Ismail Rubad

Reputation: 1468

You need to take out the modal-wrapper div from the paper-header div. Here is the full code.

.paper-header {
  align-items: flex-start;
  display: flex;
  justify-content: space-between;
  position: relative;
  z-index: 2;
  background: #ffffff;
  min-height: 8rem;
}

.paper-body {
  position:relative;
  z-index: 1;
}

.modal-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  z-index: 100;
  overflow-x: hidden;
  overflow-y: auto;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.modal {
  width: 32.5rem;
  background-color: #FFFFFF;
  border-radius: 4px;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 101;
  padding: 1.25rem;
}

.modal-overlay {
  position: absolute;
  z-index: 100;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1.25rem;
  background-color: rgba(34, 34, 34, 0.5)
}
<div class="wrapper">
  <div class="paper-header">
    <h1>
      Segment 1
    </h1>
    
  </div>
  <div class="modal-wrapper">
      <div class="modal">
      modal content  
      </div>
      <div class="modal-overlay"></div>
   </div>
  <div class="paper-body">
    Segment body
  </div>
  
  <div class="paper-header">
    <h2>
      Segment 2
    </h2>
  </div>
</div>

Upvotes: 1

Related Questions