Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS - Adding an Icon to a button

Tags:

html

css

I making some css buttons and I want to add an icon before the text, "Button Text".

But I dont know how I should do this...

HTML <div class="btn btn_red"><a href="#">Crimson</a><span></span></div>

CSS

body {
    margin: 0;
    padding: 10px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 14px;
}
/* Glassy Buttons */
.btn {
    float: left;
    clear: both;
    background: url(imgs/button_left.png) no-repeat;
    padding: 0 0 0 10px;
    margin: 5px 0;
}
.btn a{
    float: left;
    height: 40px;
    background: url(imgs/button_middle.png) repeat-x left top;
    line-height: 40px;
    padding: 0 10px;
    color: #fff;
    font-size: 1em;
    text-decoration: none;
}
.btn span {
    background: url(imgs/button_right.png) no-repeat;
    float: left;
    width: 10px;
    height: 40px;
}
.btn_gray {background-color: #808080;}
.btn_green { background-color: #ADFF2F;}
.btn_blue {background-color: #0000CD;}
.btn_red {background-color: #DC143C;}
like image 604
user377419 Avatar asked Aug 16 '10 02:08

user377419


People also ask

How do you put an icon inside a button?

It is as simple as applying Font Awesome icon on any button. The <i> tag and <span> tag are used widely to add icons on the webpages. To add any icons on the web pages, it needs the font-awesome link inside the head section. The font-awesome icon can be placed by using the fa prefix before the icon's name.

How do I add an icon to a tag?

To insert an icon, add the name of the icon class to any inline HTML element. The <i> and <span> elements are widely used to add icons. All the icons in the icon libraries below, are scalable vector icons that can be customized with CSS (size, color, shadow, etc.)

How can I add icons in CSS?

The simplest way to add an icon to your HTML page, is with an icon library, such as Font Awesome. Add the name of the specified icon class to any inline HTML element (like <i> or <span> ). All the icons in the icon libraries below, are scalable vectors that can be customized with CSS (size, color, shadow, etc.)

Can an icon be a button?

An icon button is an icon that triggers some sort of action on the page. More accurately, technically speaking, an icon button is a button that contains an icon and no (visible) accompanying text. These buttons can be found in the majority of app and user interfaces today.


2 Answers

You could add a span before the link with a specific class like so:

<div class="btn btn_red"><span class="icon"></span><a href="#">Crimson</a><span></span></div>

And then give that a specific width and a background image just like you are doing with the button itself.

.btn span.icon {
    background: url(imgs/icon.png) no-repeat;
    float: left;
    width: 10px;
    height: 40px;
}

I am no CSS guru but off the top of my head I think that should work.

like image 52
Jeff Treuting Avatar answered Oct 15 '22 20:10

Jeff Treuting


Here's what you can do using font-awesome library.

button.btn.add::before {
  font-family: fontAwesome;
  content: "\f067\00a0";
}

button.btn.edit::before {
  font-family: fontAwesome;
  content: "\f044\00a0";
}

button.btn.save::before {
  font-family: fontAwesome;
  content: "\f00c\00a0";
}

button.btn.cancel::before {
  font-family: fontAwesome;
  content: "\f00d\00a0";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<!--FA unicodes here: http://astronautweb.co/snippet/font-awesome/-->
<h4>Buttons with text</h4>
<button class="btn cancel btn-default">Close</button>
<button class="btn add btn-primary">Add</button>
<button class="btn add btn-success">Insert</button>
<button class="btn save btn-primary">Save</button>
<button class="btn save btn-warning">Submit Changes</button>
<button class="btn cancel btn-link">Delete</button>
<button class="btn edit btn-info">Edit</button>
<button class="btn edit btn-danger">Modify</button>

<br/>
<br/>
<h4>Buttons without text</h4>
<button class="btn edit btn-primary" />
<button class="btn cancel btn-danger" />
<button class="btn add btn-info" />
<button class="btn save btn-success" />
<button class="btn edit btn-link"/>
<button class="btn cancel btn-link"/>

Fiddle here.

like image 25
vohrahul Avatar answered Oct 15 '22 21:10

vohrahul