is it possible to have 2 different background colors for a button (well css button)

前端 未结 4 794
走了就别回头了
走了就别回头了 2021-01-16 23:00

What I want to achieve is something like this.

::::::::::
...hi....
..........
..........
The hi is in the middle of the 2 colors.

I have

相关标签:
4条回答
  • 2021-01-16 23:28

    You can achieve this with multiple nested elements, although your mark-up will be less semantic.

    Alternatively you can use gradients; this is a good resource for generating cross-browser CSS - http://www.colorzilla.com/gradient-editor/

    Or you can use the CSS3 multiple background property, which is obviously not yet cross browser compliant. See this link for more info - http://www.zenelements.com/blog/css3-background-images/

    If none of these help please show us the code for what you've achieved so far and it'll be easier to advise.

    0 讨论(0)
  • 2021-01-16 23:42

    You could create a css gradient with two stops really close to each other: firefox css gradients. It won't be cross browser yet. The second example on this page is pretty close to what you want: webkit simple gradients (only in webkit).

    0 讨论(0)
  • 2021-01-16 23:51

    HTML:

    <div class="fancyButton">
        <div class="background top"></div>
        <div class="background bottom"></div>
        <p>hi</p>
    </div>
    

    CSS:

    .fancyButton
    {
        width:100px;
        position:relative;
    }
    
    .fancyButton .background
    {
        width:100%;
        height:50%;
        position:absolute;
    }
    
    .fancyButton .background.top
    {
        top:0;
        background-color:red;
    }
    
    .fancyButton .background.bottom
    {
        bottom:0;
        background-color:blue;
    }
    
    .fancyButton p
    {
        position:relative;
        text-align:center;
    }
    

    Tested, and hopefully copy-pasted correctly. It uses a div that takes it's height from the <p> inside of it. The two backgrounds are set to the top and the bottom of the button div and are 50% of it's height so they meet nicely in the middle, no matter what height the button is. You can take out the fixed width and replace it with a left-right padding declaration for the button div if you want, so that the width is determined by the <p> too. (just realizing this and don't want to retest)

    Nothing fancy; just solid, robust css!

    0 讨论(0)
  • 2021-01-16 23:51

    Probably the only option here is to use CSS3 gradients as other answers have mentioned. However, if you do decide to use images (because some browsers don't support CSS3 for instanct), this tutorial about transparent sprites is very useful.

    0 讨论(0)
提交回复
热议问题