HTML / CSS 입력 유형 =“버튼”에 이미지 아이콘을 추가하는 방법은 무엇입니까?


110

아래 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(type submitinput사용하여) 내부에 요소를 허용한다는 점을 제외하고는 동일하게 작동합니다 (스타일이 적용된 것이 아니라).
Delan Azabani

8
IE <button>구현 의 '버기'부분은 1) POST / GET에서 클릭 한 버튼뿐만 아니라 모든 버튼에 대해 '값'을 제출하고 2) 실제 value속성 을 보내는 대신 IE가 좋아 한다는 사실에서 비롯됩니다. 버튼의 내용 (내부 HTML)을 보냅니다. 이러한 사실로 인해 <button>사용자가 클릭 한 버튼에 따라 수행되는 작업 이 달라지면 태그를 신뢰할 수 없게됩니다.
Duroth

좋은 지적이야, Duroth. 예, "미리보기"및 "제출"등이 포함 된 편집 게시물 양식과 같은 작업에 확실히 적합합니다.
Delan Azabani

3
당신은 사용할 수 있습니다 <button><input type=button>상호 교환. IE에서 <button> 제출을 원하지 않는 경우 다음과 같이 유형을 설정하십시오.<button type="button">Text</button>
Darcy

74

버튼 이미지가 16x16 픽셀이라고 가정하면 원하는 작업을 수행 할 수 있습니다.

<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

이 트릭을 시도해 볼 수 있습니다!

1st) 이렇게 :

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

2nd) 스타일링!

<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에서 성공적으로 테스트했습니다 (2019 년 2 월 9 일 기준).


0

어떻게 생겼는지

이것은 나를 위해 잘 작동하며 배경색을 변경하여 hover 및 click CSS도 처리하고 있습니다.

HTML (여기서는 이미지 2 개, image2 위에 image1 표시) :

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

CSS (내 이미지는 32px X 32px이므로 패딩 및 5px 테두리 반올림에 4px를 부여했습니다) :

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