HTML / CSS How to add image icon to input type=“button”?

前端 未结 12 1670
误落风尘
误落风尘 2020-11-28 20:21

I\'m using the below CSS, but it puts an image in the center of the button. Any way to left or right align an icon using , so that

相关标签:
12条回答
  • 2020-11-28 20:50

    This is the minimal style needed, fitting the image to the default button size:

    <input type="button" value=" " style="background-image: url(http://www.geppoz.eu/geppoz.png);background-size:100% 100%;">

    the "spaced" value is needed to keep baseline alignment, just in case you need it...

    0 讨论(0)
  • 2020-11-28 20:52

    you can try this trick!

    1st) do this:

    <label for="img">
       <input type="submit" name="submit" id="img" value="img-btn">
       <img src="yourimage.jpg" id="img">
    </label>
    

    2nd) style it!

    <style type="text/css">
       img:hover {
           cursor: pointer;
       }
       input[type=submit] {
           display: none;
       }
    </style>
    

    It is not clean but it will do the job!

    0 讨论(0)
  • 2020-11-28 20:52

    you can try insert image inside button http://jsfiddle.net/s5GVh/1415/

    <button type="submit"><img src='https://aca5.accela.com/bcc/app_themesDefault/assets/gsearch_disabled.png'/></button>
    
    0 讨论(0)
  • 2020-11-28 20:52
    <img src="http://www.pic4ever.com/images/2mpe5id.gif">
                <button class="btn btn-<?php echo $settings["button_background"]; ?>" type="submit"><?php echo $settings["submit_button_text"]; ?></button>
    
    0 讨论(0)
  • 2020-11-28 20:54

    This should do do what you want, assuming your button image is 16 by 16 pixels.

    <input type="button" value="Add a new row" class="button-add" />
    
    input.button-add {
        background-image: url(/images/buttons/add.png); /* 16px x 16px */
        background-color: transparent; /* make the button transparent */
        background-repeat: no-repeat;  /* make the background image appear only once */
        background-position: 0px 0px;  /* equivalent to 'top left' */
        border: none;           /* assuming we don't want any borders */
        cursor: pointer;        /* make the cursor like hovering over an <a> element */
        height: 16px;           /* make this the size of your image */
        padding-left: 16px;     /* make text start to the right of the image */
        vertical-align: middle; /* align the text vertically centered */
    }
    

    Example button:

    example button

    Update

    If you happen to use Less, this mixin might come in handy.

    .icon-button(@icon-url, @icon-size: 16px, @icon-inset: 10px, @border-color: #000, @background-color: transparent) {
        height: @icon-size * 2;
        padding-left: @icon-size + @icon-inset * 2;
        padding-right: @icon-inset;
        border: 1px solid @border-color;
        background: @background-color url(@icon-url) no-repeat @icon-inset center;
        cursor: pointer;
    }
    
    input.button-add {
        .icon-button("http://placehold.it/16x16", @background-color: #ff9900);
    }
    

    The above compiles into

    input.button-add {
      height: 32px;
      padding-left: 36px;
      padding-right: 10px;
      border: 1px solid #000000;
      background: #ff9900 url("http://placehold.it/16x16") no-repeat 10px center;
      cursor: pointer;
    }
    

    JSFiddle

    0 讨论(0)
  • 2020-11-28 20:54

    What I would do is do this:

    Use a button type

     <button type="submit" style="background-color:rgba(255,255,255,0.0); border:none;" id="resultButton" onclick="showResults();"><img src="images/search.png" /></button>
    

    I used background-color:rgba(255,255,255,0.0); So that the original background color of a button goes away. The same with the border:none; it will take the original border away.

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