0

I am facing the following problem : My .line div comes above the .list div and I do not understand why. I defined no new stacking context through z-index so shouldn't stacking order only be defined by the order of appearance in HTML? Or is there something else defining a new stacking context in my case that I don't see?

Thanks a bunch

body {
    font-family:helvetica, sans-serif;
}

.line {
    background-color:red;
    height: 150px;
    width:1px;
    float:left;
    position: relative;
    left: 57px;
    top:20px;
}

.list {
    list-style-type:none;
    display: inline-block;    
}

.written {
    width:200px;
    position: relative;
    top: 5px;
    
}

.dot {
    border: 1px solid blue;
    border-radius: 20px;
    width: 20px;
    height: 20px;
    float:left;
    margin: 5px;
}
<html>
    <head>
        <link href="./style.css" type="text/css" rel="stylesheet">
    </head>
    <body>
        <div class=line>
        </div>
        <ul class=list>
            <li>
                <div class=dot>
                </div>
                <p class=written>First thing</p>
            </li>
            <li>
                <div class=dot>
                </div>
                <p class=written>Second thing</p>
            </li>
            <li>
                <div class=dot>
                </div>
                <p class=written>Third thing</p>
            </li>
        </ul>
    </body>
</html>
Oleg
  • 11
  • 3

3 Answers3

2

From the CSS 2 specification.

Step 4: For all its in-flow, non-positioned, block-level descendants in tree order

Step 8: All positioned descendants with 'z-index: auto' or 'z-index: 0', in tree order.

<ul class=list> is painted at step 4. <div class=line> is painted at step 8. i.e. on top.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks a lot, looks pretty complete but that's a bit chinese to me. Is it because my list is not positioned? If I add position:relative; it keeps doing the same so that couldn't be it right? Is it because my
      it is a block-level element? If yes shouldn't putting it within a
      solve the problem (which it doesn't when I try)?
    – Oleg Dec 14 '18 at 13:28
0

Your <div class=line> comes above your <ul class=list> because of its position:relative; property. If you add the same to your .list selector, your <ul class=list> will be on top! View example here : https://jsfiddle.net/Lsq0tc12/

Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/position

hexangel616
  • 346
  • 2
  • 19
0

try this code.

body {
    font-family:helvetica, sans-serif;
}

.line {
    background-color:red;
    height: 150px;
    width:1px;
    float:left;
    position: relative;
    left: 57px;
    top:20px;
}

.list {
    list-style-type:none;
    display: inline-block;    
}

.written {
    width:200px;
    position: relative;
    top: 5px;
    
}

.dot {
    border: 1px solid blue;
    border-radius: 20px;
    width: 20px;
    height: 20px;
    float:left;
    margin: 5px;
}

.list > li {
  margin-left: -56px;
}
<html>
    <head>
        <link href="./style.css" type="text/css" rel="stylesheet">
    </head>
    <body>                
        <div class=line>
          <ul class=list>
            <li>
                <div class=dot>
                </div>
                <p class=written>First thing</p>
            </li>
            <li>
                <div class=dot>
                </div>
                <p class=written>Second thing</p>
            </li>
            <li>
                <div class=dot>
                </div>
                <p class=written>Third thing</p>
            </li>
        </ul>
        </div>
    </body>
</html>

i hope this help you. thanks.

dev_ramiz_1707
  • 671
  • 4
  • 20