【问题标题】:CSS positioning outside of the parent elementCSS 定位在父元素之外
【发布时间】:2020-03-18 02:39:29
【问题描述】:

您好 StackOverflow 社区,

我需要您的帮助来解决 CSS 问题。对于我的一个项目,我需要设计一个纯 CSS 时间间隔查看器。由于图片总是比长文本好,您可以在我的工作结果下方看到:

但是,我不想将开始/结束时间放在时间间隔中,而是将开始时间放在顶部/左侧上方,将结束时间放在底部/右侧下方。我想要这样的结果

这是 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>

这是 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;
}

你可以在这里看到它:https://codepen.io/zannkukai/pen/XWbYXjG

尽管尝试了很多次,但我从来没有得到正确的结果:'( CSS-guru 是否能够帮助我解决我的问题。我认为问题出在label 定位。但是我使用了一个position:absolute; top:-10px 标签范围在父级之外,不显示:'(

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

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

非常感谢

【问题讨论】:

  • 请将您的代码放入问题正文中。 CodePen 和类似的东西很适合“实时预览”,但问题应该在 StackOverflow 上是自包含的
  • 可以分享html代码吗?

标签: html css css-position z-index


【解决方案1】:

ul.day-timeline 中删除 overflow: hidden,然后您可以在父元素之外写入时间。

听到 ul.day-timeline 属性:

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

并听到整个 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-first {
        position: absolute;
        top: -15px;
        left: 0px;
      }

      &.end-time-first {
        position: absolute;
        top: 20px;
        right: 5px;
      }
      &.start-time-second {
        position: absolute;
        top: -15px;
        left: 0px;
      }

      &.end-time-second {
        position: absolute;
        top: 20px;
        right: 5px;
      }
    }
  }
}

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

以及您必须重命名以定位时间的类:

HTML代码sn-p

  <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>

看起来像这样 -> solution

【讨论】:

  • 很好的答案,但可以通过解释你在做什么来改进。你只解释它的一个方面
  • 是的,当然,但他也应该自己做点什么;)
  • @StephenR 现在我有零钱了。这样更好;)?
【解决方案2】:

这是一个带有 CSS 的工作 sn-p,我还包括一个 codepen,基本上你只需要使用左和右 CSS 属性就可以在使用 position:absolute 后工作,结束时间我添加right:0 强制其位置向右,left:0 开始时间强制其位置向左,也用于 x 轴位置的顶部和底部 CSS 属性。

* {
  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>

【讨论】:

    【解决方案3】:

    您已成功移动开始时间,但您在 ul 上设置了 overflow: hidden,因此它已按要求隐藏。设置ul.day-timeline { overflow: visible; } 并从那里开始

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2014-12-27
      • 1970-01-01
      • 2014-10-06
      • 2012-02-10
      相关资源
      最近更新 更多