gpt4 book ai didi

html - CSS 定位在父元素之外

转载 作者:行者123 更新时间:2023-12-02 09:03:44 27 4
gpt4 key购买 nike

StackOverflow 社区大家好,

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

enter image description here

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

enter image description here

这是 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大师是否能够帮助我解决我的问题。我认为问题出在label定位上。但是我使用了 position:absolute; top:-10px 标签范围超出了父级并且不显示:'(

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

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

非常感谢

最佳答案

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 代码片段

  <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

关于html - CSS 定位在父元素之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60726920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com