简体   繁体   English

CSS水平居中线

[英]CSS horizontal centered line

How can I achieve this view with CSS: 如何使用CSS实现此视图:

------------------TITLE - - - - - - - - - 标题

or 要么

TITLE------------------ 标题 - - - - - - - - -

I have 我有

<div id="titleBlock">
   <div id="title">Some text</div>
   <div id="titleLine"></div>
</div>

And my styles are: 我的风格是:

#titleLine {
    border-top: 1px solid black;
    width: 84%;
    clear: both;
    height: 20px;
}

#title {
    height: 10px;
    float: right;
}

My approach is here: jsFiddle 我的方法是在这里: jsFiddle

However the line width is defined with percents and I need it adjust automatically with CSS. 但是,线宽是用百分比定义的,我需要使用CSS自动调整。

这可能是您想要的: http : //jsfiddle.net/XpSWX/1/

Hope this helps 希望这可以帮助

<div id="titleBlock">
    <div id="title">Some text</div>
    <div id="titleLine"></div>
</div>​

#titleLine {
    border-top: 1px solid black;
    width: 84%;
    float:left;
    height: 20px;
    margin-top:8px;
}

#title {
    height: 10px;
    float: right;
}​

http://jsfiddle.net/sY2SV/1 http://jsfiddle.net/sY2SV/1

<div id="titleBlock">
    <div id="title">Some text</div>
    <div id="titleLine"></div>
</div>​

#titleLine {
    border-top: 1px solid black;
    width: 84%;
    float:right;
    height: 20px;
    margin-top:8px;
}

#title {
    height: 10px;
    float: left;
}​

http://jsfiddle.net/sY2SV/2 http://jsfiddle.net/sY2SV/2

Here is a solution: 这是一个解决方案:

#titleBlock {
    width:100%;
}


#titleLine {
    background:black;
    position:absolute;
    z-index:1;
    left:0px;
    top:14px;
    width:100%;
    height: 1px;
}

#title {
    display:inline-block;
    padding:4px;
    background:white;
    position:relative;
    z-index:2;
    /* Only variable to change... Just say left and it woulb be title------- */
    float:right;
}​​​​

DEMO 演示

Hey now you can used this 嘿,现在你可以用这个了

HTML 的HTML

<div class="hello"><span>Hello i m sony</span></div>

Css 的CSS

.hello{
background:green;
  text-align:left;
  position:relative;

}
.hello span{
  padding-right:10px;
background:green;
  display:inline-block;
  position:relative;
  z-index:1
}
.hello:after{
content:'';
  border-top:solid 5px red;
  position:absolute;
 right:0;
  left:0;
  top:7px
}

Live demo 现场演示

http://tinkerbin.com/1guJzKcI http://tinkerbin.com/1guJzKcI

Check my answer in Horizontal Line in Background using Css3 使用Css3在背景中的水平线上检查我的答案

You can do it with a 1% gradient like this 您可以像这样以1%的梯度进行

.datedAside {
     background: linear-gradient(0deg, transparent 49%, #000 50%, transparent 51%);
}
.datedAside span{
     background: #FFF;
     padding: 0 0.5rem;
}

You'll nedd the extra span to be the same background color as the background of the component to make it look like it has "deleted" the line going behind the text. 您需要将多余的跨度设置为与组件的背景相同的背景色,以使其看起来像“删除”了文本后面的行。

对于文本,最好使用text-align

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

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