0

I'm trying to change some of my ASP.Net pages to use s and replace some s.

I had some trouble aligning the labels and text boxes and then I found this awesome JSFiddle: http://jsfiddle.net/p54SE/

I had no idea that CSS could be used to give specific instructions to a Label and an input[type=text] separately. I have referred to these as subtypes of CSS, but if you know the correct terminology please inform me.

How would I be able to recreate this with:

<div class='label-wrapper'>
  <asp:Label runat="server" ID="lbl_Title" Text="Title" />
  <asp:TextBox runat="server" ID="tb_Title"  />
</div>

I was so certain just changing the name would work, but no luck.

div.label-wrapper > asp:Label {

1
  • 1
    CSS attribute selectors. Commented Feb 24, 2014 at 17:28

3 Answers 3

2

The CSS selector you need is div.label-wrapper > span

You are confusing your server side controls and client side HTML. The server side control <asp:Label /> renders as a <span> element. When applying CSS rules, make sure you look at the HTML that is rendered by your ASP.NET application.

Option 2

You can also add a CssClass attribute to your server side control and reference said class directly.

<asp:Label runat="server" Text="My Label Text" CssClass="label-text" />

and use the CSS selector

.label-text{
  //your rules here
}
Sign up to request clarification or add additional context in comments.

Comments

1

asp:Label control produces span by default, so use this to target the produced element:

div.label-wrapper > span {
    // your styles here
}

Furthermore, you can assign a class to the the control to specifically target it:

<asp:Label CssClass="yourClass" runat="server" ID="lbl_Title" Text="Title" />

CSS:

.yourClass {
    //your styles here
}

2 Comments

Span works and the TextBox was correct all along. I was just assuming it was broken because the label wasn't working. Thanks this will help me to align everything.
@StuckOnSimpleThings You are welcome! Glad to be of help to you.
1

In .Net, the <asp:Label> will be turned into <span> when the server actually parses the HTML. The <asp:Label> is part of .Net that automagically inserts the necessary markup for that element. That said, to target that <span>, change your CSS selector to .label-wrapper > span and that should work better for you.

1 Comment

<asp:Label> renders as a <span>, not a <label>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.