HTML / CSS วิธีเพิ่มไอคอนรูปภาพในประเภทอินพุต =“ ปุ่ม”?


111

ฉันใช้ CSS ด้านล่าง แต่วางรูปภาพไว้ตรงกลางปุ่ม วิธีใดในการจัดแนวไอคอนไปทางซ้ายหรือขวาโดยใช้<input type="button">เพื่อให้ข้อความและภาพพอดีและจัดแนวอย่างสวยงาม?

background: url('/common/assets/images/icons/16x16/add.png');
background-position:center;
background-repeat:no-repeat;

คำตอบ:


143

หากคุณต้องใช้อย่างแน่นอนinputลองสิ่งนี้:

background-image: url(...);
background-repeat: no-repeat;
background-position: <left|right>;
padding-<left|right>: <width of image>px;

ก็มักจะง่ายขึ้นเล็กน้อยที่จะใช้buttonกับimgภายใน:

<button type="submit"><img> Text</button>

อย่างไรก็ตามการใช้งานเบราว์เซอร์buttonสำหรับการส่งไม่สอดคล้องกันเช่นเดียวกับความจริงที่ว่าค่าของปุ่มทั้งหมดจะถูกส่งเมื่อbuttonมีการใช้งานซึ่งจะฆ่าการตรวจจับ "ปุ่มอะไรที่คลิก" ในรูปแบบการส่งหลายรายการ


ไม่จำเป็นต้อง แต่ฉันที่นี่แท็กปุ่มมีข้อผิดพลาดใน IE 6 หรือทำงานแตกต่างกันในบางเบราว์เซอร์?
Brett

เท่าที่ฉันเคยสัมผัสมาbutton(ที่มีประเภทsubmit) จะทำงานเหมือนกันinputยกเว้นการอนุญาตให้ใช้องค์ประกอบภายใน (แทนที่จะใช้เพียงแค่สไตล์เท่านั้น)
Delan Azabani

8
ส่วน 'buggy' ของ<button>การใช้งานIE มาจากข้อเท็จจริงที่ว่า 1) ใน POST / GET จะส่ง 'ค่า' สำหรับทุกปุ่มไม่ใช่แค่คลิกเดียวและ 2) แทนที่จะส่งvalueแอตทริบิวต์จริงIE ชอบ เพื่อส่งเนื้อหา (HTML ภายใน) ของปุ่ม ข้อเท็จจริงเหล่านี้ทำให้<button>แท็กไม่น่าเชื่อถือหากการดำเนินการขึ้นอยู่กับปุ่มที่ผู้ใช้คลิก
Duroth

จุดที่ดี Duroth ใช่นั่นเป็นตัวกระตุ้นสำหรับสิ่งต่างๆเช่นแบบฟอร์มแก้ไขโพสต์ที่มี "ดูตัวอย่าง" และ "ส่ง" เป็นต้น
Delan Azabani

3
คุณสามารถใช้<button>และ<input type=button>แทนกันได้ หากคุณไม่ต้องการส่ง <button> ใน IE ให้กำหนดประเภทดังนี้:<button type="button">Text</button>
Darcy

74

สิ่งนี้ควรทำในสิ่งที่คุณต้องการโดยสมมติว่าภาพปุ่มของคุณมีขนาด 16 x 16 พิกเซล

<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 */
}

ปุ่มตัวอย่าง:

ปุ่มตัวอย่าง

อัปเดต

หากคุณบังเอิญใช้ Less การผสมนี้อาจมีประโยชน์

.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);
}

ข้างต้นรวบรวมเป็น

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


คุณไม่ต้องการคำพูดในอาร์กิวเมนต์ของ url () วิธีการหรือไม่?
ecbrodie


10
<button type="submit" style="background-color:transparent; border-color:transparent;"> 
  <img src="images/button/masuk.png" height="35"/>
</button>
<button type="reset" style="background-color:transparent; border-color:transparent;"> 
  <img src="images/button/reset.png" height="35"/>
</button>

ฉันหวังว่านี่จะช่วยคุณได้


2

หากคุณใช้สไปรท์ชีทสิ่งนี้จะเป็นไปไม่ได้และต้องห่อองค์ประกอบ

.btn{
    display: inline-block;
    background: blue;
    position: relative;
    border-radius: 5px;
}
.input, .btn:after{
    color: #fff;
}
.btn:after{
    position: absolute;
    content: '@';
    right: 0;
    width: 1.3em;
    height: 1em;
}
.input{
    background: transparent;
    color: #fff;
    border: 0;
    padding-right: 20px;
    cursor: pointer;
    position: relative;
    padding: 5px 20px 5px 5px;
    z-index: 1;
}

ลองดูซอนี้: http://jsfiddle.net/AJNnZ/


1

คุณสามารถลองใช้เคล็ดลับนี้ได้!

1) ทำสิ่งนี้:

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

2) สไตล์มัน!

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

มันไม่สะอาด แต่จะทำงาน!


1

เพียงแค่เพิ่มไอคอนในองค์ประกอบปุ่มนี่คือตัวอย่าง

   <button class="social-signup facebook"> 
     <i class="fa fa-facebook-official"></i>  
      Sign up with Facebook</button>


0

สิ่งที่ฉันจะทำคือทำสิ่งนี้:

ใช้ประเภทปุ่ม

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

ฉันใช้ background-color: rgba (255,255,255,0.0); เพื่อให้สีพื้นหลังเดิมของปุ่มหายไป เช่นเดียวกันกับเส้นขอบ: ไม่มี; มันจะเอาเส้นขอบเดิมออกไป


0

นี่คือสไตล์มินิมอลที่จำเป็นโดยปรับรูปภาพให้พอดีกับขนาดปุ่มเริ่มต้น:

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

จำเป็นต้องใช้ค่า "เว้นระยะ" เพื่อให้การจัดแนวพื้นฐานในกรณีที่คุณต้องการ ...


0

คำตอบของ sshow ก็ทำเพื่อฉันเช่นกัน:

navigation.css :

[...]

.buttonConfiguration {
    width: 40px;
    background-color: red; /* transparent; */
    border: none;
    color: white;
    padding: 5px 10px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-family: corbel;
    font-size: 12px;
    font-weight: normal;
    margin: 1px 1px;
    cursor: pointer;

    background-image: url('../images/icons5/gear_16.png');
    background-position: center; /* 0px 0px; */
    background-repeat: no-repeat;
    vertical-align: middle;
}

[...]

frameMenu.php :

[... PHP, Javascript and HTML code ...]

<!-- <li><a target="frameBody" href="admin/conf/confFunctions.php"><input type="button" class="buttonSuperAdmin" value="<?= $oLanguage->getExpression('confSettings', 'title', 'C e n t e r s') ?>"></a> -->
<li><a target="frameBody" href="admin/conf/confFunctions.php"><input type="button" class="buttonConfiguration" value=""></a>
<ul>
<li><a target="frameBody" href="admin/conf/getglobals.php "><input type="button" class="buttonSuperAdminSub" value="<?= $oLanguage->getExpression('centerSettings', 'confAdmin1', 'GetGlobals') ?>"></a></li>
<li><a target="frameBody" href="admin/conf/getcwd.php "><input type="button" class="buttonSuperAdminSub" value="<?= $oLanguage->getExpression('confSettings', 'centerAdmin2', 'GetCWD') ?>"></a></li>
</ul>
</li>

[...]

ฉันได้ทดสอบสิ่งนี้สำเร็จแล้วภายใต้ Firefox และ Chrome เวอร์ชันล่าสุด (ณ วันที่ 9 กุมภาพันธ์ 2019)


0

หน้าตาเป็นอย่างไร

สิ่งนี้ใช้ได้ดีสำหรับฉันและฉันกำลังจัดการโฮเวอร์และคลิก CSS ด้วย (โดยการเปลี่ยนสีพื้นหลัง):

HTML (ที่นี่แสดง 2 ภาพ, image1 อยู่ด้านบนของ image2):

<button class="iconButton" style="background-image: url('image1.png'), url('image2.png')"
  onclick="alert('clicked');"></button>

CSS (รูปภาพของฉันคือ 32px X 32px ดังนั้นฉันจึงให้ 4px สำหรับการขยายและการปัดเศษเส้นขอบ 5px):

.iconButton {
    width: 36px;
    height: 36px;
    background-color: #000000;
    background-position: center;
    background-repeat: no-repeat;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    outline: none;
}

.iconButton:hover {
    background-color: #303030;
}

.iconButton:active {
    background-color: #606060;
}

button::-moz-focus-inner {
    border: 0;
}

-2
<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>

โปรดเพิ่มคำอธิบายในคำตอบของคุณ
warunapww

ต้องการ ">" เพื่อปิดแท็ก img
harrypujols

1
ทำไมคุณถึงใช้ PHP?
Top Cat
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.