Html 当标签具有固定宽度时,如何在标签开始前显示星号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13153666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:38:55  来源:igfitidea点击:

how to show asterisk sign before label start when label has fixed width

htmlcss

提问by Prasad

I want to show asterisk sign before my label indicating it is a mandatory field. following is my css id code for label,

我想在我的标签前显示星号,表明它是必填字段。以下是我的标签 css id 代码,

#label{
    float: left;
    height: 30px;
    padding-right: 4px;
    padding-top: 2px;
    position: relative;
    text-align: right;
    vertical-align: middle;
    width: 73px;
}

output of above is, * Label but I want it to be like, *Label Thanks

上面的输出是,* 标签,但我希望它像,*标签谢谢

回答by Sowmya

Use :before pseudo class

使用 :before 伪类

     .label:before{
     content:"*" ;
     color:red   
     }

Or

或者

    #req:before{
    content:"*" ;
    color:red    
    }
?

DEMO Updated

演示更新

回答by Pratap A.K

Simple approach is like this
<font color="red">*</font>UserName<input type="text" name="userID"/>

It will display the asterik.

Edit

编辑

The fonttag is not supported in HTML5. Use CSS instead.

字体标签不支持HTML5。改用 CSS。

font link

字体链接

回答by Steve Benner

Without seeing the HTML it is difficult for us to know what is going on. I suspect you will be able to solve your problem using the little-known white-spaceproperty, if the asterisk is in the same element as #label. Try using white-space: pre.

如果没有看到 HTML,我们很难知道发生了什么。white-space如果星号与#label 位于同一元素中,我怀疑您将能够使用鲜为人知的属性解决您的问题。尝试使用white-space: pre.

Here is some more info: http://www.w3schools.com/cssref/pr_text_white-space.asp

以下是更多信息:http: //www.w3schools.com/cssref/pr_text_white-space.asp