简体   繁体   English

将CSS添加到动态创建的标签和文本框

[英]Add Css to dynamically created label and textbox

I am dynamically creating the label and textbox in my C# code and am having issues with css formatting. 我在C#代码中动态创建标签和文本框,并且CSS格式出现问题。 Since the label and textbox are generated separately adding css to it to float left and add padding has been a challenge. 由于标签和文本框是分别生成的,因此向其添加CSS以向左浮动并添加填充一直是一个挑战。 I am new to c# and would appreciate any help. 我是C#的新手,不胜感激。

I tried adding the cssClass to both the text box and label but the formatting is off. 我尝试将cssClass添加到文本框和标签,但是格式已关闭。

Here is my css: 这是我的CSS:

.form-control{
    margin-bottom:10px;
    width:300px;
    float:left;
}

.form-control-label{
    display:block;
    float:left;
}

I added the css class in the script side using 我在脚本端使用添加了CSS类

label.CssClass = "form-control-label";
textbox.CssClass = "form-control"

Here is the output and expected result 这是输出和预期结果 在此处输入图片说明

Not sure about the text alignment that you expect for the Labels, but you can try the following style classes: 不确定标签所期望的文本对齐方式,但是可以尝试以下样式类:

.form-control
{
    float: left;
    width: 300px;
}
.form-control-label
{
    clear: left;
    float: left;
    width: 120px;
    text-align: left;
}

If you prefer the Label texts to be right aligned, you can change the style for: 如果您希望标签文本右对齐,则可以更改以下样式:

.form-control-label
{
    clear: left;
    float: left;
    width: 120px;
    text-align: right;
    margin-right: 8px;
}

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

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