question

MalamMalam-4042 avatar image
0 Votes"
MalamMalam-4042 asked YijingSun-MSFT answered

Label's Background Color

How do I set a lable's background color to highlight ONLY the text, not the padding or spaces before and after the text?
I've tried the following but it highlight the space before the label

 ![<style>
     .auto-style1 {
     width: 18.4em;
     background-color:red;
     }
     .input1[type="text"]{
         width: 40em;
     }
    
 </style>
 <p>
     <label class="auto-style1" for="txtName">NAME:</label>
     <input class="input1" type="text" id="txtName" name="tbName" />
 </p>][1]


[1]: /answers/storage/attachments/175460-label.png

dotnet-aspnet-webforms
label.png (804 B)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I can't reproduce the problem with the code shown. You might have a cascading issue with other styles. Use the browser's dev tools to view the styles assigned to the element.

Or try wrapping the text in a span...

 <label class="auto-style1" for="txtName"><span>NAME:</span></label>

... then specifically overriding the styles. This is not a production solution but it might isolate the problem.

 label span {
     margin: 0;
     padding:0;
     background-color:yellow;
 }




0 Votes 0 ·

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @MalamMalam-4042 ,
Could you post your result? I suggest you could press F12 when you run the page and you could look at your elements.
175691-test1.png

Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


test1.png (716 B)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.