简体   繁体   English

如何使用纯CSS堆叠文字?

[英]How to stack text with pure CSS?

I want to use a text as the background for another text. 我想使用一个文本作为其他文本的背景。 Basically, this is quite easy by using two div elements with appropriate position and z-index settings, as described in Is there a way to use use text as the background with CSS? 基本上,通过使用两个具有适当positionz-index设置的div元素,这很容易,如CSS是否可以使用将文本用作背景? .

My problem is that I want to align both texts, although they are different in size. 我的问题是,尽管它们的大小不同,但我想同时对齐两个文本。 The background text is bigger, and the foreground text shall be aligned vertically with the background one, so that both texts' vertical center is at the same position. 背景文本较大,前景文本应与背景文本垂直对齐,以使两个文本的垂直中心位于同一位置。

I can achieve this by manually adjusting the position in pixels, but I have to do this for every font-size I want to use individually. 我可以通过手动调整以像素为单位的位置来实现,但是我必须针对要单独使用的每种字体大小执行此操作。 Moreover, I can never be sure that this works in every browser in the same way. 而且,我永远不能确定这在每种浏览器中都以相同的方式起作用。

Is there a better alternative to do this? 有更好的替代方法吗?

I got it by using: 我通过使用它得到的:

<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>

Using left and line-height properties I am able to align both texts, and when I increase the font-size of the container, their relative alignment stays the same. 使用leftline-height属性,我可以对齐两个文本,当我增加容器的font-size时,它们的相对对齐方式保持不变。

Vertical align can be done like this: 垂直对齐可以这样进行:

.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;}

If the verticalcentered elements are youre texts they will be vertically centered in the parent and be on top of each other, if thats what youre looking for. 如果verticalcentered元素是您的文本,则它们将在父级中垂直居中并彼此重叠(如果那就是您要查找的内容)。

i think you are looking like this :- DEMO 我认为您看起来像这样:- 演示

HTML 的HTML

<div id="container">
        <div id="background">Sample Some Text </div>
        B
</div>

CSS 的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;
}

Yeah ofcourse you can user alternative way - Placeholder text 是的,您可以使用其他方式-占位符文本

Text will be automatically removed when you type the new text then default text will be remove 键入新文本时,文本将被自动删除,然后默认文本将被删除

//textbox //文本框

<input  type="text" defaultvalue="Name" value="Name" name="add_name" id="add_name">

//Javasript Code // 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'));
        } 
    });

Try the above option. 试试上面的选项。

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

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