user3578325
user3578325

Reputation: 239

How to use CSS to style Textbox and Label in ASP.NET

I want to style a Textbox and a Label in ASP.NET ( and ), i have a CSS style that works fine for me with normal inputs and and labels but it wont work with ASP Textboxs and Labels. First of all here is the CSS style :

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

when i try that style with this code it works fine :

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

but when i try it with this code the transition wont work :

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

Upvotes: 0

Views: 12148

Answers (2)

Med.Amine.Touil
Med.Amine.Touil

Reputation: 1235

Remove the and you will find the same result as the first code because it generates a hidden html code.

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

Upvotes: 0

BRBT
BRBT

Reputation: 1487

You are setting your CssClass="field-validation-error" to this class, but in your CSS you don't have a class called ".field-validation-error".

You could give your label a CssClass called "label"

Like this: CssClass="label"

Then in your CSS you can style your label with a class you define in CSS like this.

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

You can do the exact same with your textboxes, error messages, etc by giving the element a defined CssClass. Doing it like this also makes it a lot easier to read and understand and make changes later on.

Upvotes: 1

Related Questions