meks meks - 3 months ago 23
ASP.NET (C#) Question

Unable to apply CSS style to an asp label

I have a peculiar result when I want to apply one of my CSS styles to asp labels. Styles actually have only partial effect, like for example such attributes like

background-color
and color are taking effect and width and height are not. I don't understand why.

ASP

<div id="wrap">
<div id="left" class="Tablestyle">
<asp:Label ID="Label1" runat="server" Text="Entry Number: " class="Tablestyle"></asp:Label>
<br /><div class="separator">&nbsp;</div>
<asp:Label ID="Label3" runat="server" Text="ID Number: " class="Tablestyle"></asp:Label>
<br /><div class="separator">&nbsp;</div>
<asp:Label ID="Label2" runat="server" Text="Type: " class="Tablestyle"></asp:Label>

</div>
<div id="Mid">&nbsp</div>
<div id="right">
<asp:Label ID="IdBox" runat="server" class="TableStyleInfo"></asp:Label>
<br /><div class="separator">&nbsp;</div>
<asp:Label ID="IdNumBox" runat="server" class="TableStyleInfo"></asp:Label>
<br /><div class="separator">&nbsp;</div>
<asp:Label ID="TypeBox" runat="server" class="TableStyleInfo"></asp:Label>
</div>
</div>


CSS

#wrap
{
width:555px;
margin:auto;
text-align:center;
}

#Mid
{
width:5px;
height:330px;
float:left;
background-color:White;
}

#right
{
width:400px;
float:left;
text-align:left;
background-color:Yellow;
}

#left
{
width:150px;
float:left;
text-align:right;
background-color:Green;
}

.separator
{
height:4px;
line-height:4px;
background-color:White;
}

.Tablestyle
{
width:150px;
height:20px;
text-align:right;
color:white;
font-weight:bold;
background-color:#507CD1;
}

.TableStyleInfo
{
width:400px;
height:20px;
text-align:left;
color:Black;
background-color:#EFF3FB;
}


I tried also changing
class
to
CssClass
but nothing happens just the same.

I would be very grateful if someone can point me on my mistake or give me a hint why can't I apply width and height properties.

Answer

An <asp:Label> converts to the html <span> element, which is an inline element and has no width or height. You can enforce this by changing them to block level elements in the css:

.Tablestyle {
    display: block; // add this
    width: 150px;
    height: 20px;
    text-align: right;
    color: white;
    font-weight: bold;
    background-color: #507CD1;
}

.TableStyleInfo {
    display: block; // add this
    width: 400px;
    height: 20px;
    text-align: left;
    color: Black;
    background-color: #EFF3FB;
}
Comments