Search code examples
htmlcsscss-positionz-index

CSS positioning outside of the parent element


Hi StackOverflow community,

I need your help for a CSS problem. For one of my project I need to design a pure CSS time interval viewer. As a picture is always better than a long text, you can see just below the result of my work :

enter image description here

But instead of place the start/end time into the time interval, I would like to place the start time just above the top/left and the end time just below the bottom/right. I would like to have a result like this

enter image description here

Here's the HTML:

<ul class="day-timeline">
  <li class="time-interval" style="left:4%; width:46%">
    <label class="time start-time">01:00</label>
    <label class="time end-time">12:00</label>
  </li>
  <li class="time-interval" style="left:58%; width:17%">
    <label class="time start-time">14:00</label>
    <label class="time end-time">18:00</label>
  </li>
</ul>

<ul class="day-timeline day-timeline-closed">
  <li class="time-interval closed">
    <label class="time">Closed</label>
  </li>
</ul>

Here's the CSS:

* {
  box-sizing: border-box;
}
ul.day-timeline {
  padding: 0;
  border-left: 1px solid black;
  border-right: 1px solid black;
  height: 20px;
  overflow: hidden;
  position: relative;

  &:after {
    content: '';
    width: 100%;
    height: 1px;
    background: black;
    display: block;
    position: relative;
    top: 10px;
  }

  li {
    display: block;
    position: absolute;
    background-color: #bed4a7;
    border: 1px solid green;
    z-index: 1;
    padding-left: 5px;
    border-radius: 3px;
    height: 100%;
    font-weight: bold;
    width: 100%;

    &.closed {
      background-color: lightgray;
      border: 1px solid black;

      label {
        color :black;
      }
    }

    label {
      font-size: 0.75rem;
      color: green;

      &.start-time {
        position: absolute;
        top:-10px;
        left:0;
      }
    }

    label + label {
      &:before {
        content: '-';
        padding: 0 0.25rem;
      }
    }
  }
}

ul.day-timeline-closed {
  border: none;
}

You can see it live here : https://codepen.io/zannkukai/pen/XWbYXjG.

Despite a lot of tries, I never get a correct result :'( Is a CSS-guru is able to help me to solve my problem. I think the problem are in the label positioning. But is I used a position:absolute; top:-10px the label scope is outside of the parent and it isn't displayed :'(

label {
  font-size: 0.75rem;
  color: green;

  &.start-time {
    position: absolute;
    top:-10px;
    left:0;
  }
} 

Many thanks


Solution

  • Here is a working snippet with CSS and also I'm including a codepen, basically you just have to make use of left and right CSS attribute for this to work after using position:absolute, for the end time I add right:0 to force its position to the right and left:0 for the start time to force it position to the left, also for top and bottom CSS attributes for x axis position.

    * {
      box-sizing: border-box;
    }
    
    ul.day-timeline {
      padding: 0;
      border-left: 1px solid black;
      border-right: 1px solid black;
      height: 20px;
      position: relative;
    }
    ul.day-timeline:after {
      content: '';
      width: 100%;
      height: 1px;
      background: black;
      display: block;
      position: relative;
      top: 10px;
    }
    ul.day-timeline li {
      display: block;
      position: absolute;
      background-color: #bed4a7;
      border: 1px solid green;
      z-index: 1;
      padding-left: 5px;
      border-radius: 3px;
      height: 100%;
      font-weight: bold;
      width: 100%;
    }
    ul.day-timeline li.closed {
      background-color: lightgray;
      border: 1px solid black;
    }
    ul.day-timeline li.closed label {
      color: black;
    }
    ul.day-timeline li label {
      font-size: 0.75rem;
      color: green;
    }
    ul.day-timeline li label.start-time-first {
      position: absolute;
      top: -15px;
      left: 0px;
    }
    ul.day-timeline li label.end-time-first {
      position: absolute;
      top: 20px;
      right: 0;
    }
    ul.day-timeline li label.start-time-second {
      position: absolute;
      top: -15px;
      left: 0px;
    }
    ul.day-timeline li label.end-time-second {
      position: absolute;
      top: 20px;
      right: 0;
    }
    
    ul.day-timeline-closed {
      border: none;
    }
    <ul class="day-timeline">
      <li class="time-interval" style="left:4%; width:46%">
        <label class="time start-time-first">01:00</label>
        <label class="time end-time-first">12:00</label>
      </li>
      <li class="time-interval" style="left:58%; width:17%">
        <label class="time start-time-second">14:00</label>
        <label class="time end-time-second">18:00</label>
      </li>
    </ul>
    
    
    
    
    <ul class="day-timeline day-timeline-closed">
      <li class="time-interval closed">
        <label class="time">Closed</label>
      </li>
    </ul>