Answered by:
BackColor for label not getting the color unless label.Text is assigned something

Question
-
User154499744 posted
I have a label, like this.
<asp:Label ID="someLabel" runat="server" Width="1.0in" BackColor="LightGray" ></asp:Label>
But the color doesn't show up until I assign a value to it. Is there a way to have it get colored even when the label is empty?
Monday, July 25, 2016 8:57 PM
Answers
-
User475983607 posted
Ah... a span is an inline element and has no height or width. you'll need to use a different server control, turn the inline span to a block, or set the label to an .
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LabelBg.aspx.cs" Inherits="TestingWeb.LabelBg" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type ="text/css"> .SetWidth { width: 5em; height: 1em; background-color: gray; display:block; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="" CssClass="SetWidth"></asp:Label> </div> </form> </body> </html>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 25, 2016 9:30 PM -
User61956409 posted
Hi NewKid1nTown,
As mgebhard said, if you check the html source of label control in F12 developer tools, you could find it is rendered as <span> that is an inline element. We could display an element as block element by setting display property to block. Besides, you could check this documentation to compare HTML block and inline elements.
http://www.w3schools.com/html/html_blocks.asp
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 29, 2016 8:29 AM
All replies
-
User475983607 posted
A label renders as a span. If there is no text then the span has no width and therefore you can't see the background color. You'll need to set a width. I'm not sure is incbes will work. Consider using CSS.Monday, July 25, 2016 9:05 PM -
User475983607 posted
Ah... a span is an inline element and has no height or width. you'll need to use a different server control, turn the inline span to a block, or set the label to an .
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LabelBg.aspx.cs" Inherits="TestingWeb.LabelBg" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type ="text/css"> .SetWidth { width: 5em; height: 1em; background-color: gray; display:block; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" Text="" CssClass="SetWidth"></asp:Label> </div> </form> </body> </html>
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 25, 2016 9:30 PM -
User61956409 posted
Hi NewKid1nTown,
As mgebhard said, if you check the html source of label control in F12 developer tools, you could find it is rendered as <span> that is an inline element. We could display an element as block element by setting display property to block. Besides, you could check this documentation to compare HTML block and inline elements.
http://www.w3schools.com/html/html_blocks.asp
Best Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 29, 2016 8:29 AM