Putting Images Inside a BUTTON Element (HTML & CSS)

后端 未结 5 1667
闹比i
闹比i 2020-12-28 21:53

I have a simple button (as shown below) on which I need to display two pictures, one on either side of the button text. Im battling to create the CSS that will work in both

5条回答
  •  执念已碎
    2020-12-28 22:36

    Here is how to do it

    The Theory

    Block elements (like DIV) although displayed in order of creation, will position themselves adjacent to the previous element or when short of space, on the next line. Because we dont want to give the button a width (we want the button to be automatically sized based on the content of the button) the block elements continued to appear on the next line (see IE8 image in the question above). Using white-space:nowrap forces inline elements (like SPAN and EM) to be displayed on the same line, but is ignored by block elements, hence the solution below.

    CSS

    button{
        margin: 0px;
        padding: 0px;
        font-family:Lucida Sans MS, Tahoma;
        font-size: 12px;
        color: #000; 
        white-space:nowrap;
        width:auto;
        overflow:visible;
        height:28px;
    }
    
    button em{
        vertical-align:middle;
        margin:0 2px;
        display:inline-block;
        width:16px;
        height:16px;
        background-image: url(images/ui-icons_3d3d3d_256x240.png);      
    }
    
    button em.leftImage{
        background-position: -96px -112px;
    }
    
    button em.rightImage{
        background-position: -64px -16px;
    }
    

    HTML

    
    

    The Result

    Internet Explorer 6, 7, 8 and Firefox 1.5, 2, 3

提交回复
热议问题