Enrico Polanski
Enrico Polanski

Reputation: 182

<header> and <footer> element stay on top unless given negative z-index

I was trying to write some semantically correct HTML and I'm using a header and footer in my document.

The document is structured like this

*{
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: 'Roboto', sans-serif;
    -webkit-font-smoothing: antialiased;
    text-align: center;
}

body, html, .container{
    height: 100%;
}

button:focus{
    outline: none;
}

.container{
    display: flex;
    flex-direction: column;
}

.headerBar{
    position: fixed;
    height: 56px;
    background-color: #94D04F;
    width: 100%;
    padding-left: 4px;
    padding-right: 4px;
    box-shadow: 0px 2px 4px -1px rgba(0, 0, 0, 0.2), 0px 4px 5px 0px rgba(0, 0, 0, 0.14), 0px 1px 10px 0px rgba(0, 0, 0, 0.12);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid black;
    z-index: 0;
}


.button-raised{
    /* box-shadow: 0 0 2px rgba(0,0,0,.12), 0 2px 2px rgba(0,0,0,.2); */
    display: flex;
    justify-content: center;
    align-items: center;
    text-transform: uppercase;
    height: 30px;
    padding: 0 2px;
    background-color: transparent;
    color: #212121;
    border: none;
    font-weight: 400;
}

.button-raised:hover{
    /* background-color: white; */
    color: white;
    font-weight: 400;
    /* border: 1px solid #44d057; */
}

.button-raised:focus{
    outline: none;
}

.button-raised i{
    line-height: 1;
}

.button-raised span{
    line-height: 1;
    font-size: 16px;
}

#dateSpan{
    font-family: 'Open-Sans', sans-serif;
    font-size: 24px;
    font-weight: 500;
    color: #fff;
}


.footerBar{
    height: 56px;
    background-color: #94D04F;
    position: fixed;
    bottom: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 4px;
    border-top: 1px solid black;
}

.button-raised--white{
    /* box-shadow: 0 0 2px rgba(0,0,0,.12), 0 2px 2px rgba(0,0,0,.2); */
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-transform: uppercase;
    padding: 0 2px;
    background-color: transparent;
    color: #212121;
    border: none;
}

.button-raised--white:hover{
    color: white;
}

.button-raised--white span{
    font-size: 12px;
    font-weight: 400;
}

main{
    background-color: #f9f9f9;
    position: fixed;
    display: flex;
    flex-direction: column;
    top: 56px;
    bottom: 56px;
    width: 100%;
    z-index: -1;
}

.blocksCounter{
    width: 100%;
    height: 30px;
    padding: 0 8px;
    display: flex;
    justify-content:space-around;
    align-items: center;
    background-color: #bdbdbd52;
    color: #212121;
}

.blocksCounter span{
    font-size: 18px;
    font-weight: 500;
}

.studyBlockElement{
    margin-top: 4px;
    margin-left: auto;
    margin-right: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 4px;
    width: 300px;
    height: 36px;
    position: relative;
    border: 1px solid black;
}

.studyBlockElement > i{
    position: absolute;
    left: 4px;
}

.current{
    background-color: #9DDC54;
}

.todo{
    background-color: #39501E;
    color: white;
}

.studyBlockElementRightButtons{
    position: absolute;
    right: 4px;
    display: flex;
}

#settingsModal{
    position: fixed;
    height: 100%;
    width: 100%;
    z-index: 3;
    background-color: #212121;
}
    <header class="headerBar">
    
        <section>
            <button class="button-raised">
                <i class="material-icons">arrow_back</i>
                <span class="button-span">Feb-11</span>
            </button>
        </section>

        <section>
            <span id="dateSpan">
                12-FEB-2018
            </span>
        </section>

        <section>
            <button class="button-raised">
                <span class="button-span">Feb-13</span>
                <i class="material-icons">arrow_forward</i>
            </button>
        </section>
        
    </header>

    <main>
        <section class="blocksCounter">
            <span id="totalCounter">Total:</span>
            <span id="toDoCounter">ToDo:</span>
            <span id="doneCounter">Completed:</span>
        </section>

        <div class="studyBlockElement current">
            <i class="material-icons">navigate_next</i>
            <span class="studyBlockElementTextSpan">
                Yo man!
            </span>
            <div class="studyBlockElementRightButtons">
                <i class="material-icons"></i>
                <i class="material-icons"></i>
                <i class="material-icons">clear</i>
            </div>
        </div>

        <div class="studyBlockElement todo">
            <i class="material-icons"></i>
            <span class="studyBlockElementTextSpan">
            Yo man!
            </span>
            <div class="studyBlockElementRightButtons">
                <i class="material-icons"></i>
                <i class="material-icons"></i>
                <i class="material-icons">clear</i>
            </div>
        </div>
    </main>

    <footer class="footerBar">
        <button class="button-raised--white">
            <i class="material-icons">settings</i>
            <span>settings</span>
        </button>
        <button class="button-raised--white">
            <i class="material-icons">pie_chart_outlined</i>
            <span>stats</span>
        </button>
                <button class="button-raised--white">
            <i class="material-icons">school</i>
            <span>studies</span>
        </button>
                <button class="button-raised--white">
            <i class="material-icons">query_builder</i>
            <span>timer</span>
        </button>
    </footer>

    <div id="settingsModal" class="modal"></div>

Now, header main and footer take 100% of the height. Modal should too, once a button is pressed.

The problem is that even if I set every element's z-index to 0 and my div's z-index to anything higher footer and header will still sit on the top of the div.

I found a way to fix it tho. Giving header and footer a negative z-index, but I don't really like this solution long term, and regardless, I'd like to know why z-indexing the div higher than footer and header element does not produce any result.

Upvotes: 0

Views: 44

Answers (1)

rpivovar
rpivovar

Reputation: 3438

It's because you have to give #settingsModal a position of absolute. Because that element's position is currently set to the default value of static, it's not going to move how you want it to.

From CSS-Tricks:

The z-index property in CSS controls the vertical stacking order of elements that overlap. As in, which one appears as if it is physically closer to you. z-index only effects elements that have a position value other than static (the default).

Upvotes: 1

Related Questions