【问题标题】:Centered text with background color all the way right背景颜色居中的文本一直向右
【发布时间】:2014-09-05 09:23:37
【问题描述】:
我被要求在一些居中的文本上编写一个不寻常的形状背景颜色。
文本应该居中,并且它的背景颜色应该一直延续到父元素的右侧。
这是所需的输出:
我从来没有见过这样的事情,所以我什至不知道从哪里开始。感谢您的帮助!
【问题讨论】:
标签:
html
css
text
background
background-color
【解决方案1】:
我不太理解你的问题,但尝试混合这些概念:
HTML:
<div id="parent">
<p id="son">Some text with unusual background color</p>
</div>
JS:
<script type="text/javascript">
document.getElementsById("parent").style.background="red";
document.getElementsById("son").style.background="blue";
</script>
尝试改变儿子和父母的颜色。
【解决方案2】:
你可以使用伪元素让黑色背景在右边继续:
DEMO
HTML:
<div>
<span>Some text with</span><br/>
<span>unusual background</span><br/>
<span>color</span>
</div>
CSS:
div{
float:right;
padding-right:150px;
text-align:center;
position:relative;
}
span{
display:inline-block;
background:#000;
color:#fff;
line-height:1.4em;
margin:0;
}
span:after{
content:'';
position:absolute;
width:200px;
height:1.4em;
right:0;
background:inherit;
z-index:-1;
}