How to change the color of a Glyphicon?

2

I'm using glyphicons , however I do not know how the bootstrap does to add the color of it. It always comes with a "standard" color.

How is the mechanism for defining the color of glyphicons and how can I change this "default color"?

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<i class="glyphicon glyphicon-edit""></i>
    
asked by anonymous 23.02.2016 / 15:09

2 answers

3

The bootstrap looks for the color according to the css already applied on the page. For example, if you have an icon inside a link, and you have changed the color of the links in css, the icon will be the same color, for example:

<!DOCTYPE html>
<html lang="pt">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script><scriptsrc="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Glyphicon Examples</h2>
  <p>Envelope icon: <span class="glyphicon glyphicon-envelope"></span></p>    
  <p>Envelope icon as a link:
    <a href="#"><span class="glyphicon glyphicon-envelope"></span></a>
  </p>
  <p>Search icon: <span class="glyphicon glyphicon-search"></span></p>
  <p>Search icon on a button:
    <button type="button" class="btn btn-default">
      <span class="glyphicon glyphicon-search"></span> Search
    </button>
  </p>
  <p>Search icon on a styled button:
    <button type="button" class="btn btn-info">
      <span class="glyphicon glyphicon-search"></span> Search
    </button>
  </p>
  <p>Print icon: <span class="glyphicon glyphicon-print"></span></p>      
  <p>Print icon on a styled link button:
    <a href="#" class="btn btn-success btn-lg">
      <span class="glyphicon glyphicon-print"></span> Print 
    </a>
  </p> 
</div>

</body>
</html>
Home Source: w3schools.

From the bootstrap 3.0 version you can change the forr of an icon just by changing the attribute color of CSS. Example:

.pink{
  color: pink;
}
  <!DOCTYPE html>
    <html lang="pt">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script><scriptsrc="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    </head>
    <body>

    <div class="container">
      <h2>Glyphicon Examplos</h2>
      <p>CSS Inline: <span class="glyphicon glyphicon-envelope" style="color:red;"></span></p>    
      <p>Adicionando classe:
        <a href="#"><span class="glyphicon glyphicon-envelope pink"></span></a>
      </p>

    </div>

    </body>
    </html>

Remembering that you can change the size of the icon with the font-size attribute, as in the example below:

.fonte-maior{
  font-size: 40px;
  }
 <!DOCTYPE html>
        <html lang="pt">
        <head>
          <title>Bootstrap Example</title>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script><scriptsrc="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        </head>
        <body>

        <div class="container">
          <h2>Glyphicon Examplos</h2>  
           <p>Sem fonte:
            <a href="#"><span class="glyphicon glyphicon-envelope"></span></a>
          </p>
          <p>Com fonte:
            <a href="#"><span class="glyphicon glyphicon-envelope fonte-maior"></span></a>
          </p>

        </div>

        </body>
        </html>
    
23.02.2016 / 15:09
2

Just change the color of the :before pseudo-element, this will only affect the icon:

.glyphicon:before {
  color: red 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<a href='#'>
  <i class="glyphicon glyphicon-edit"></i> normal
</a>

<a style='color: orange'>
  <i class="glyphicon glyphicon-edit"></i> link com outra cor
</a>
    
23.02.2016 / 15:23