【问题标题】:How to stack text with pure CSS?如何用纯 CSS 堆叠文本?
【发布时间】:2012-11-30 19:52:14
【问题描述】:
【问题讨论】:
标签:
html
css
text
alignment
【解决方案1】:
我通过使用得到它:
<div style="position: relative; font-size: 100pt; height: 100px;">
<div style="font-size: 2em; color: #f00; position: absolute; top: 0; left: -0.37em; bottom: 0; right: 0; z-index: -1; line-height: 0.4em;">
B
</div>
Sample text
</div>
使用left 和line-height 属性我可以对齐两个文本,并且当我增加容器的font-size 时,它们的相对对齐方式保持不变。
【解决方案2】:
垂直对齐可以这样完成:
.parent {
position:relative;
zoom:1;/*has layout for ie*/
}
.verticalcentered1, .verticalcentered2 {
position:absolute;
top:50%;
height:20px;
margin-top:-10px;/*half the height*/
}
.verticalcentered1 {z-index:1;}
.verticalcentered2 {z-index:2;}
如果verticalcentered 元素是你的文本,它们将在父元素中垂直居中并相互重叠,如果你正在寻找的话。
【解决方案3】:
我认为你看起来像这样:- DEMO
HTML
<div id="container">
<div id="background">Sample Some Text </div>
B
</div>
CSS
#container {
position: relative;
font-size:70px;
color:#00c7ff;
z-index:-1;
}
#background {
left: 20px;
position: absolute;
right: 0;
top: 25%;
z-index: 10;
color:black;
font-size:20px;
}
【解决方案4】:
是的,当然你可以使用其他方式 - 占位符文本
当您输入新文本时,文本将被自动删除,然后默认文本将被删除
//文本框
<input type="text" defaultvalue="Name" value="Name" name="add_name" id="add_name">
//Javasript 代码
//For the placeholder
$('#add-category input[type=text]').focus(function() {
if ($(this).val() == $(this).attr('defaultValue')) {
$(this).val('');
}
});
$('#add-category input[type=text]').blur(function() {
if ($(this).val() == '') {
$(this).val($(this).attr('defaultValue'));
}
});
试试上面的选项。