我该如何在一个div的中间画一条竖线?也许我应该在这个div里面放置两个div,一个左边加上左边框,另一个右边加上右边框?我有一个DIV标记,我需要在左边放置一个ascx(它会不时地被另一个ascx替换),然后在右边放置一个静态ascx。您有什么好的建议吗?也许我已经回答了自己的问题。
任何指针都将非常有用。
任何指针都将非常有用。
虽然这个问题是9年前提出的,很多答案都可以“work”。但是CSS已经发展到现在,你现在可以使用一行代码来完成它,而不必使用calc
。
background: linear-gradient(#000, #000) no-repeat center/2px 100%;
linear-gradient(#000, #000)
创建一个background-image
,以便我们以后可以使用background-size
来包含它。no-repeat
防止渐变在放上background-size
时重复。center
- 这是background-position
,也就是我们放置“分界线”的位置。/2px 100%
- 将图像设置为2像素宽,并占用其所在元素的100%。 background-image: linear-gradient(#000, #000);
background-size: 2px 100%;
background-repeat: no-repeat;
background-position: center center;
也许这可以帮助你
.here:after {
content:"";
position: absolute;
z-index: -1;
top: 0;
bottom: 0;
left: 50%;
border-left: 2px dotted #ff0000;
transform: translate(-50%);
}
div {
margin: 10px auto;
width: 60%;
height: 100px;
border: 1px solid #333;
position:relative;
text-align:center
}
<div class="here">Content</div>
这是一个JSFiddle演示。
z-index:-1
更改为z-index:0
。如果此答案中的div和类在浏览器的开发人员控制台中可见,但该行未出现,请首先尝试更改z-index值。 - Rick Gladwinpointer-events:none
替换z-index:-1
,并保持:after
在最上层。 - V.Volkov这里有一种更为现代的方法,在一个 div 中画一条线。只需要一点 css:
.line-in-middle {
width:400px;
height:200px;
background: linear-gradient(to right,
transparent 0%,
transparent calc(50% - 0.81px),
black calc(50% - 0.8px),
black calc(50% + 0.8px),
transparent calc(50% + 0.81px),
transparent 100%);
}
<div class="line-in-middle"></div>
适用于所有现代浏览器。 http://caniuse.com/#search=linear-gradient
刚刚测试过,可行:
<div id="left" style="width:50%;float:left;background:green;">left</div>
<div id="right" style="margin-left:50%;border-left:solid 1px black;background:red;">right</div>
这是我做的版本,使用 linear-gradient
制作渐变中线。
.block {
width:400px;
height:200px;
position:relative;
}
.block:before {
content:"";
width:1px;
height:100%;
display:block;
left:50%;
position:absolute;
background-image: -webkit-linear-gradient(top, #fff, #000, #fff);
background-image: -moz-linear-gradient(top, #fff, #000, #fff);
background-image: -ms-linear-gradient(top, #fff, #000, #fff);
background-image: -o-linear-gradient(top, #fff, #000, #fff);
background-image: linear-gradient(top, #fff, #000, #fff);
}
<div class="block"></div>
:before
? - Ndrslmpk我认为你需要一个带有背景图的包装div。如果没有,那么你就不能保证边框从顶部到底部都是一样的。
<div class="wrapper">
<div>Float this one left</div>
<div>float this one right</div>
</div>
请务必在左右留出空格,以便图片能够显示。
你需要使用以下样式:
.wrapper{background:url(img.jpg) 0 12px repeat-y;}
我认为你的多DIV方法是处理这个问题最明智的方式:
<DIV>
<DIV style="width: 50%; border-right: solid 1px black">
/* ascx 1 goes here */
</DIV>
<DIV style="width: 50%">
/* ascx 2 goes here */
</DIV>
</DIV>
三个 div?
<div>
<div>
/* ascx 1 goes here */
</div>
<div style="width:1px; background-color: #000"></div>
<div>
/* ascx 2 goes here */
</div>
</div>