18

I have a asp:Button, I used css styles with cssClass property in asp:Button, but those styles are not working. When I use asp:LinkButton those styles are working well.I don't want any themes or skins for styles.

This is my asp page:

<asp:Button CssClass="smallButton" Text="Sign In" runat="server" ID="submit"></asp:Button>

This is my CSS:

.smallButton 
{
  //styles
}

When I change asp:Button to asp:LinkButton

<asp:LinkButton Text="Sign In" runat="server" ID="submit" CssClass="smallButton"></asp:LinkButton>

or

<span class="smallButton"><asp:LinkButton Text="Sign In" runat="server" ID="submit"></asp:LinkButton></span>

styles are working well. Only problem with the asp:Button control

4
  • please show us code. How do you expect us to guess what you did wrong ? Commented Aug 29, 2012 at 12:14
  • Can you provide the html that the asp.net button is generating. Commented Aug 29, 2012 at 12:23
  • Thanks everyone I found the solution and I add below that coding... Commented Sep 3, 2012 at 4:35
  • This is old, but if someone runs into this, in my case, part of the css in the class didn't work for the button, so it ignored it ignored it. That solved it for me. Commented Nov 9, 2017 at 14:53

9 Answers 9

17

You can assign a class to your ASP.NET Button and then apply the desired style to it.

<asp:Button class="mybtn" Text="Button" runat="server"></asp:Button>

CSS:

.mybtn
{
   border:1px solid Red;
   //some more styles
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am using the same way but this is not working for asp:Button control but this is working with asp:LinkButton and Image Button controls
@Sujanth Maybe you can try to clear the cache from the browser and try again.The code seems alright.
Careful with this. ASP.Net will overwrite the CSS field. For example if you set the button disabled the css class will be overwritten with "disabled". Use the provided setter CssClass so ASP.Net can coordinate when it sets the css classes.
14

I Found the coding...

 input[type="submit"]
    {
     //css coding
    }
 input[type="submit"]:Hover  
    {
     //css coding
    }

This is the solution for my issue..... Thanks everyone for the valuable answers.......

Comments

14

You can use CssClass attribute and pass a value as a css class name

<asp:Button CssClass="button" Text="Submit" runat="server"></asp:Button>` 

.button
{
     //write more styles
}

1 Comment

This is the asp expected way
11

nobody wants to go to the clutter of using a class, try this:

<asp:button Style="margin:0px" runat="server" />

Intellisense won't suggest it but it will get the job done without throwing errors, warnings, or messages. Don't forget the capital S in Style

1 Comment

Downvoted. Good luck doing maintenance / overhauls on that. With a class you will at least have a single point of change.
2

If you have a button in asp.net design page like "Default.asp" and you want to create CSS file and specified attributes for a button,labels or other controller. Then first of all create a css page

  1. Right click on Project
  2. Add new item
  3. Select StyleSheet

now you have a css page now write these code in your css page(StyleSheet.css)

StyleSheet.css

.mybtnstyle
{
 border:1px solid Red;
 background-color:Red;
 border-style:groove;
 border-top:5px;
 outline-style:dotted;
}

Default.asp

{

  <head> 
  <title> testing.com </title>
 </head>
<body>
 <b>Using Razer<b/>
<form id="form1" runat="server">
 <link id="Link1" rel="stylesheet" runat="server" media="screen" href="Stylesheet1.css" />
 <asp:Button ID="mybtn" class="mybtn" runat="server" Width="339px"/>
 </form>
 </body>
</html>

}

Comments

1

You could just style the input element in your css file. That is then independent of ASP.NET.

<form action="">
    Name: <input type="text" class="input" />
    Password: <input type="password" class="input" />
    <input type="submit" value="Submit" class="button" />
</form>
CSS
.input {
    border: 1px solid #006;
    background: #ffc;
}
.button {
    border: 1px solid #006;
    background: #9cf;
}

With the CssClass you can assign the "input" class to it.

3 Comments

But then you aren't using server controls and we're not sure if that's what the user wants here.
It's basically what everybody else here suggested.
Everyone here used server controls in their examples. Not nitpicking, but asp.net utilizes server controls for a reason. Granted, you could have added a runat tag to these controls and voila.
1

The answer you mentioned will be applied to all buttons. You should try this:

input[type="submit"].someclass {
    //somestyle}

And make sure you add this to your button:

CssClass="someclass"

Comments

0
<asp:LinkButton ID="mybutton" Text="Link Button" runat="server"></asp:LinkButton>

With Hover effects :

 #mybutton
        {
            background-color: #000;
            color: #fff;
            font-size: 20px;
            width: 150px;
            font-weight: bold;
        }
        #mybutton:hover
        {
            background-color: #fff;
            color: #000;
        }

http://www.parallelcodes.com/asp-net-button-css/

Comments

0
  <asp:Button CssClass="button" style="color:black" Text="Submit" runat="server"></asp:Button>` 

1 Comment

Both the CssClass and style attributes are addressed by other answers already.

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.