简体   繁体   English

在垂直线上垂直居中书写文本

[英]Writing text vertically center on an vertical line

Here is my JsFiddle 这是我的JsFiddle

I wish to write the text "OR" in the center of the border. 我希望在边框的中央写上“ OR”字样。 I did it with absolute position. 我做到了绝对的立场。 Is there any better solution then this. 有没有更好的解决方案呢?

.test {
width: 300px;
height: 300px;
border-right: 1px solid #000;
position: relative;
}
.test::after {
content: 'OR';
position: absolute;
background: #FFF;
left: 291px;
top: 140px;
}

What you are doing is fine. 你在做什么很好。 What is "better" is subjective because that would depend on your specific use-case. “更好”是主观的,因为这取决于您的特定用例。

Assuming from the word "OR", that you want to use that div as a separator for content. 假设单词“ OR”是您要使用该div作为内容的分隔符。

In that case one problem I see is with the large fixed width (300px) which, can put you in a tight spot when trying to use that as a separator for content. 在那种情况下,我看到的一个问题是较大的固定宽度(300px),当尝试将其用作内容的分隔符时,可能会使您陷入困境。 You would have to put one content in that div and the other alternative (to be compared with "or") in another and somehow stack them together. 您必须将一个内容放在该div中,将另一个替换内容(与“或”进行比较)放在另一个div中,并以某种方式将它们堆叠在一起。 You might have trouble getting this in a fluid layout. 您可能无法以流畅的布局获得此效果。

If I am right, you are trying to have something like this: 如果我是对的,那么您正在尝试这样的事情:

Demo: http://jsfiddle.net/abhitalks/rNQjX/15/ 演示: http : //jsfiddle.net/abhitalks/rNQjX/15/

Idea is to use a separate div and use that purely as a separator. 想法是使用一个单独的div并将其纯粹用作分隔符。 This will allow you to change separator independent of content. 这将允许您独立于内容更改分隔符。 With this layout, changes in browser/window width/height will be fluid. 使用此布局,浏览器/窗口宽度/高度的更改将变得流畅。

Example Markup: 标记示例:

<div></div> <!-- regular page content -->
<div class="wrap"> <!-- wrapper for comparison content -->
    <div class="content"></div> <!-- content option 1 -->
    <div class="sep"></div> <!-- *** Seperator *** -->
    <div class="content"></div> <!-- content option 2 -->
</div>
<div></div> <!-- regular page content -->

Example CSS: CSS示例:

div.wrap { /* wrapper for comparison content */
    height: auto;
    background-color: #ccc;
    position: relative;
}
div.content { /* content div */
    float: left;
    width: 50%;
}
div.sep { /* separator div */
    width: 1px;
    border: 1px solid gray;
    margin-left: -3px;
    position: absolute;
    top: 0px; bottom: 0px;
    left: 50%;
}
div.sep::after { /* separator text */
    content:'OR';
    position: absolute;
    top: 48%; left: -14px;
    background-color: #ccc;
}

Your code looks fine to me, I posted below a slightly modified version. 您的代码对我来说看起来不错,我在下面稍加修改后发布了该版本。

http://jsfiddle.net/rNQjX/14/ http://jsfiddle.net/rNQjX/14/

.test {
    width: 300px;
    height: 300px;
    border-right: 1px solid #000;
    position: relative;
}
.test::after {
    content:'OR';
    background: #FFF;
    position: inherit;
    margin-left: 291px;
    top: 140px;
}

You can try this one with exact vertical alignment without position absolute : 您可以尝试使用完全没有位置绝对的垂直对齐方式进行此操作:

http://jsfiddle.net/kongkannika/rNQjX/18/ http://jsfiddle.net/kongkannika/rNQjX/18/

.test {
    width: 300px;
    height: 300px;
    border-right: 1px solid #000;
}
.test::after {
    content: 'OR';
    background: #FFF;
    line-height: 300px;
    margin-left: 290px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM