像这样?
#outer {
width: 400px;
height: 300px;
border: 1px solid red;
}
#inner {
height: 125px;
border: 1px solid blue;
position: relative;
}
#line {
position: absolute;
width:1px;
height: 50px;
bottom: -25px; /*half the height*/
left: 50%;
border-left: 1px solid green;
}Run Code Online (Sandbox Code Playgroud)
<div id="outer">
<div id="inner">
<div id="line"></div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
外部div没什么特别的.
内部div获得相对位置,线div获得绝对位置.
通过将行div作为子元素和上面提到的位置,相对于它的父元素定义位置.因此,当使用left: 50%这意味着,50%的父母.
#outer {
width: 400px;
height: 300px;
border: 1px solid red;
}
#inner {
height: 125px;
border: 1px solid blue;
position: relative;
}
#inner:after {
content: '';
position: absolute;
width:1px;
height: 50px;
bottom: -25px; /*half the height*/
left: 50%;
border-left: 1px solid green;
}Run Code Online (Sandbox Code Playgroud)
<div id="outer">
<div id="inner">
</div>
</div>Run Code Online (Sandbox Code Playgroud)