如何使用CSS在ASP.NET中设置Textbox和Label的样式

时间:2015-03-06 17:26:26

标签: css asp.net

我想在ASP.NET(和)中设置文本框和标签的样式,我有一个CSS样式,对我来说可以正常输入和标签,但它不适用于ASP文本框和标签。 首先是CSS样式:

form h1 {
color: #ff4a56;
font-weight: 100;
letter-spacing: 0.01em;
margin-left: 15px;
margin-bottom: 35px;
text-transform: uppercase;
}

form button {
margin-top: 35px;
background-color: white;
border: 1px solid #ff4a56;
line-height: 0;
font-size: 17px;
display: inline-block;
box-sizing: border-box;
padding: 20px 15px;
border-radius: 60px;
color: #ff4a56;
font-weight: 100;
letter-spacing: 0.01em;
position: relative;
z-index: 1;
}

form button:hover, form button:focus {
    color: white;
    background-color: #ff4a56;
}

form .question {
position: relative;
padding: 10px 0;
}

form .question:first-of-type {
    padding-top: 0;
}

form .question:last-of-type {
    padding-bottom: 0;
}

form .question label {
    transform-origin: left center;
    color: #ff4a56;
    font-weight: 100;
    letter-spacing: 0.01em;
    font-size: 17px;
    box-sizing: border-box;
    padding: 10px 15px;
    display: block;
    position: absolute;
    margin-top: -40px;
    z-index: 2;
    pointer-events: none;
}

 form .question input[type="text"] {
    -webkit-appearance: none;
    background-color: none;
    border: 1px solid #ff4a56;
    line-height: 0;
    font-size: 17px;
    width: 100%;
    display: block;
    box-sizing: border-box;
    padding: 10px 15px;
    border-radius: 60px;
    color: #ff4a56;
    font-weight: 100;
    letter-spacing: 0.01em;
    position: relative;
    z-index: 1;
}

    form .question input[type="text"]:focus {
        outline: none;
        background: #ff4a56;
        color: white;
        margin-top: 30px;
    }

    form .question input[type="text"]:valid {
        margin-top: 30px;
    }

    form .question input[type="text"]:focus ~ label {
        -moz-transform: translate(0, -35px);
        -ms-transform: translate(0, -35px);
        -webkit-transform: translate(0, -35px);
        transform: translate(0, -35px);
    }

    form .question input[type="text"]:valid ~ label {
        text-transform: uppercase;
        font-style: italic;
        -moz-transform: translate(5px, -35px) scale(0.6);
        -ms-transform: translate(5px, -35px) scale(0.6);
        -webkit-transform: translate(5px, -35px) scale(0.6);
        transform: translate(5px, -35px) scale(0.6);
    }

  form .question input[type="password"] {
    -webkit-appearance: none;
    background-color: none;
    border: 1px solid #ff4a56;
    line-height: 0;
    font-size: 17px;
    width: 100%;
    display: block;
    box-sizing: border-box;
    padding: 10px 15px;
    border-radius: 60px;
    color: #ff4a56;
    font-weight: 100;
    letter-spacing: 0.01em;
    position: relative;
    z-index: 1;
  }

    form .question input[type="password"]:focus {
        outline: none;
        background: #ff4a56;
        color: white;
        margin-top: 30px;
    }

    form .question input[type="password"]:valid {
        margin-top: 30px;
    }

    form .question input[type="password"]:focus ~ label {
        -moz-transform: translate(0, -35px);
        -ms-transform: translate(0, -35px);
        -webkit-transform: translate(0, -35px);
        transform: translate(0, -35px);
    }

    form .question input[type="password"]:valid ~ label {
        text-transform: uppercase;
        font-style: italic;
        -moz-transform: translate(5px, -35px) scale(0.6);
        -ms-transform: translate(5px, -35px) scale(0.6);
        -webkit-transform: translate(5px, -35px) scale(0.6);
        transform: translate(5px, -35px) scale(0.6);
    }

当我使用此代码尝试该样式时,它可以正常工作:

   <div class="question">
        <input type="text" required="">
        <label>Last Name</label>
    </div>
    <div class="question">
        <input type="text" required="">
        <label>Email Address</label>
    </div>

但是当我尝试使用此代码时,转换不会起作用:

<li class="question">
                        <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
                        <asp:TextBox runat="server" ID="UserName" />
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                    </li>
                    <li class="question">
                        <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                        <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                    </li>

2 个答案:

答案 0 :(得分:1)

您正在将CssClass="field-validation-error"设置为此课程,但在您的CSS中,您没有名为".field-validation-error"的课程。

您可以为标签指定CssClass "label"

像这样:CssClass="label"

然后在你的CSS中你可以用你在CSS中定义的类来设置你的标签样式。

.labels
{
  color: grey;
  width: 150px;
  display:inline-block;
}

通过为元素提供定义的CssClass,您可以对文本框,错误消息等执行完全相同的操作。这样做也使得阅读和理解更容易,并在以后进行更改。

答案 1 :(得分:0)

删除,您将找到与第一个代码相同的结果,因为它会生成隐藏的html代码。

 <div class="question">
 <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
 <asp:TextBox runat="server" ID="UserName" />           
</div>
<div class="question">
<asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
<asp:TextBox runat="server" ID="Password" TextMode="Password" />
 </div>