Styling an anchor tag to look like a submit button

后端 未结 10 1468
谎友^
谎友^ 2020-11-29 04:19

I have a form where there\'s a \"Submit\" button and a \"Cancel\" anchor. The HTML is this:




        
10条回答
  •  一向
    一向 (楼主)
    2020-11-29 05:09

    The best you can get with simple styles would be something like:

    .likeabutton {
        text-decoration: none; font: menu;
        display: inline-block; padding: 2px 8px;
        background: ButtonFace; color: ButtonText;
        border-style: solid; border-width: 2px;
        border-color: ButtonHighlight ButtonShadow ButtonShadow ButtonHighlight;
    }
    .likeabutton:active {
        border-color: ButtonShadow ButtonHighlight ButtonHighlight ButtonShadow;
    }
    

    (Possibly with some kind of fix to stop IE6-IE7 treating focused buttons as being ‘active’.)

    This won't necessarily look exactly like the buttons on the native desktop, though; indeed, for many desktop themes it won't be possible to reproduce the look of a button in simple CSS.

    However, you can ask the browser to use native rendering, which is best of all:

    .likeabutton {
        appearance: button;
        -moz-appearance: button;
        -webkit-appearance: button;
        text-decoration: none; font: menu; color: ButtonText;
        display: inline-block; padding: 2px 8px;
    }
    

    Unfortunately, as you may have guessed from the browser-specific prefixes, this is a CSS3 feature that isn't suppoorted everywhere yet. In particular IE and Opera will ignore it. But if you include the other styles as backup, the browsers that do support appearance drop that property, preferring the explicit backgrounds and borders!

    What you might do is use the appearance styles as above by default, and do JavaScript fixups as necessary, eg.:

    
    

提交回复
热议问题