Bootstrap Button Glyphicon - css

My question is pretty simple, but I can't get it to work.
I understand that you can make a button with Bootstrap + Glyphicon like so:
<span class="glyphicon glyphicon-chevron-left"></span> Default text here
But, I want the content & styling seperate (as is normal, right?), because it makes editting a lot easier (just one scss file instead of 54 html files with buttons).
Can I use ::before or ::after on the button and then use "content" in scss to include a Glyphicon? If so, how?

The first idea probably is do something like this
#import "variables";
#import "glyphicons";
.my-btn{
&:before{
.glyphicon;
.glyphicon-chevron-left;
}
}
but that compile into something like:
.my-btn:before {
position: relative;
top: 1px;
display: inline-block;
font-family: 'Glyphicons Halflings';
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.my-btn:before:before { /*this is wrong*/
content: "\e079";
}
And nested pseudo-elements It is not possible if I am not mistaken see this Nesting pseudo-elements inside pseudo-elements
So you have to do this:
//HTML
Default text here
//CSS
#import "variables";
#import "glyphicons";
.my-btn{
&:before{
.glyphicon;
}
&.my-chevron-left{
.glyphicon-chevron-left;
}
}
PD: I'm using less, in SCSS is quite similar

Related

Changing a specific glyphicon?

I am using UI-Grid, part of the AngularUI suite, but I want to switch out some of the glyphicons they use, in particular the arrows for sorting columns.
Everywhere else, I use Bootstrap's glyphicon-chevron-down or glyphicon-chevron-up on my custom tables. So basically I just want to override the UI-Grid sorting icons with the Bootstrap ones. Is there any way to do this? I've tried overriding the UI-grid sorting classes in my CSS but it doesn't seem to have any effect... maybe I'm doing it wrong though.
Here is what the Bootstrap classes look like:
.glyphicon-chevron-up:before {
content: "\e113";
}
.glyphicon-chevron-down:before {
content: "\e114";
}
Here is what the UI-grid classes look like:
.ui-grid-icon-sort-alt-up:before {
content: '\c360';
}
.ui-grid-icon-sort-alt-down:before {
content: '\c361';
}
So in my web-app's CSS file I did this:
.ui-grid-icon-sort-alt-up:before {
content: '\e113';
}
.ui-grid-icon-sort-alt-down:before {
content: '\e114';
}
This might be totally crazy, I have no idea how this stuff works under the hood.
Angular UI's Bootstrap addition is only reformatting the original bootstrap JS into Angular directives. It doesn't include glyphicons. If you add the fully angular-boostrap, you'll get what you are looking for.
You could also just download the glyphicons from bootstrap directly and put this code into your stylesheet or scss partial somewhere:
#font-face {
font-family: 'Glyphicons Halflings';
src: url('/fonts/glyphicons-halflings-regular.eot');
src: url('/fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),
url('/fonts/glyphicons-halflings-regular.woff') format('woff'),
url('/fonts/glyphicons-halflings-regular.ttf') format('truetype'),
url('/fonts/glyphicons-halflings-regular.svg#glyphicons-halflingsregular') format('svg');
}
You're changing the content correctly, but you are missing some additional properties that glyphicon uses, most importantly being the font-family.
Whenever the .glyphicon class is added to an element it adds the following:
.glyphicon {
position: relative;
top: 1px;
display: inline-block;
font-family: "Glyphicons Halflings";
font-style: normal;
font-weight: 400;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
So you will want to replicate those properties as well since your .ui-grid-icon-sort-alt-up likely does not have .glyphicon on it.
I would recommend adding something like this to your custom CSS as well:
.ui-grid-icon-sort-alt-up,
.ui-grid-icon-sort-alt-down {
position: relative;
top: 1px;
display: inline-block;
font-family: "Glyphicons Halflings";
/* ... */
}
To explain what is going on here, Glyphicon is using a custom font that has a bunch of icons for various Unicode characters. \e114 is a reference to a specific character in the font that they created.
You can override css based on how you arrange your <link> tags at the head of your html. For example:
<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/override.css" />
Moving your css after bootstrap will allow your css to "overide" bootstrap's css.

CSS Smooth font styling

I'm trying to copy the following font styling in my page:
https://d13yacurqjgara.cloudfront.net/users/104117/screenshots/2080529/attachments/373853/real-pixels.png (scroll down to "typography")
I've tried the following:
#import url(http://fonts.googleapis.com/css?family=Roboto:400,300,500,700);
body {
font-family: 'Roboto', sans-serif;
color: #334455;
background-color: #EEF2F4;
font-weight: 400;
}
All i'm achieving with this is this poor styling:
http://i.imgur.com/kwIzWMs.png
I even feel like it's not the same font. Even more, the different font-weight I've tried never matched the one from the model... I have no idea where to go from here, can somebody enlight me on how to improve this?
I had the same problem you described in your comment once and I realised it was because my
#import url
was after the reset. I suggest you try the following:
#import url(http://fonts.googleapis.com/css?family=Roboto:400,300,500,700);
* {
margin: 0;
padding: 0;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body { ... }
Use
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased equivalent in firefox?
The CSS seems correct.
I tried the same with a dummy HTML page and it renders correctly:
<h2>Hello How are you?</h2>
<p>CSS Smooth font styling CSS Smooth font styling CSS Smooth font styling </p>
http://jsfiddle.net/qhz1vm4v/1
* {
margin: 0;
padding: 0;
}
I had this soft reset at the beginning of my css file. After deleting it the rendering was good. I never thought it would have an influence on the way fonts are rendered.

Importing font awesome in SASS and applying icons to elements using uni code

Any light that could be shed on this would be hugely appreciated.
I have a SPAN in which I'm not able to apple the fontawesome FA class to this span. So I need to write a custom style to add the icon to my SPAN.
And I'm using SASS for my stylesheets.
OK, in my vendor.scss I am importing font awesome like so..
#import url(//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css);
And font awesome works a treat when using their defined classes..
<i class="fa fa-camera-retro fa-lg"></i>
But when I try and create my own by using css content it does not work? See below..
.addthis_sharing_toolbox {
SPAN {
color: #ffffff;
background: #000000;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
}
.at-svc-facebook SPAN {
content: '#f09a';
}
.at-svc-twitter SPAN{
content: '#f099';
}
.at-svc-google_plusone_share SPAN {
content: '#f0d5';
}
}
Can anyone see where I'm going wrong?
Thanks
You can't use Unicode like that in CSS. You need to format it with a backslash in place of your pound marker.
Example
.at-svc-twitter span:before {
content: '\f099';
}
Also, content is only valid when in use with pseudo elements in CSS. So we need to set it on the :before pseudo instead of the span option.

Redefine Awesome stacked icons as one icon with CSS rules only

FontAwesome 3.2.1
I want to use Awesome stacked icons (font icons that placed one upon the other) using only CSS. Is it possible to define concrete icons some special CSS rule ?
Now stacked icons are used so, so icon-check-empty will be shown over icon-twitter.
<span class="icon-stack">
<i class="icon-check-empty icon-stack-base"></i>
<i class="icon-twitter"></i>
</span>
I want to define CSS rules (myclass) so that I can use it in one span instead.
my.css
icon-twitter-on-icon-check-empty { }
.....
my.html
<span class="icon-twitter-on-icon-check-empty" />
If I understand you correctly, certainly.
You should create a generic class for establishing the spacing for your icons and the background image to be added, then a chained or complementary class for determining the sprite's background position.
Edit: I didn't realize you wanted to actually display several icons on top of one another. That's a very different matter. You would want to use positioning to do that.
Through CSS only, this has the Face Time icon in the before of the CSS and the pause on top in red in the after. I'm using this code to override a web theme, which is why there is a background-image:none.
So in your css:
.videoIcon {
background-image:none!important;
position:relative;
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
font-size:small;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.videoIcon:before {
content: "\f03d";
vertical-align: middle;
position: absolute;
left: 0;
}
.videoIcon:after{
content:"\f04c";
position: absolute;
right: 0;
bottom:0;
color:red;
font-size:smaller;
}

Use Font Awesome Icons in CSS

I have some CSS that looks like this:
#content h2 {
background: url(../images/tContent.jpg) no-repeat 0 6px;
}
I would like to replace the image with an icon from Font Awesome.
I do not see anyway to use the icon in CSS as a background image. Is this possible to do assuming the Font Awesome stylesheets/fonts are loaded before my CSS?
You can't use text as a background image, but you can use the :before or :after pseudo classes to place a text character where you want it, without having to add all kinds of messy extra mark-up.
Be sure to set position:relative on your actual text wrapper for the positioning to work.
.mytextwithicon {
position:relative;
}
.mytextwithicon:before {
content: "\25AE"; /* this is your text. You can also use UTF-8 character codes as I do here */
font-family: FontAwesome;
left:-5px;
position:absolute;
top:0;
}
EDIT:
Font Awesome v5 uses other font names than older versions:
For FontAwesome v5, Free Version, use: font-family: "Font Awesome 5 Free"
For FontAwesome v5, Pro Version, use: font-family: "Font Awesome 5 Pro"
Note that you should set the same font-weight property, too (seems to be 900).
Another way to find the font name is to right click on a sample font awesome icon on your page and get the font name (same way the utf-8 icon code can be found, but note that you can find it out on :before).
Actually even font-awesome CSS has a similar strategy for setting their icon styles. If you want to get a quick hold of the icon code, check the non-minified font-awesome.css file and there they are....each font in its purity.
Consolidating everything above, the following is the final class which works well
.faArrowIcon {
position:relative;
}
.faArrowIcon:before {
font-family: FontAwesome;
top:0;
left:-5px;
padding-right:10px;
content: "\f0a9";
}
To use font awesome using css follow below steps -
step 1 - Add Fonts of FontAwesome in CSS
/*Font Awesome Fonts*/
#font-face {
font-family: 'FontAwesome';
//in url add your folder path of FontAwsome Fonts
src: url('font-awesome/fontawesome-webfont.ttf') format('truetype');
}
Step - 2 Use below css to apply font on class element of HTML
.sorting_asc:after {
content: "\f0de"; /* this is your text. You can also use UTF-8 character codes as I do here */
font-family: FontAwesome;
padding-left: 10px !important;
vertical-align: middle;
}
And finally, use "sorting_asc" class to apply the css on desired HTML tag/element.
You can try this example class. and find icon content here: http://astronautweb.co/snippet/font-awesome/
#content h2:before {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
transform: translate(0, 0);
content: "\f007";
}
I am bit late to the party. Just like to suggest another way.
button.calendar::before {
content: '\f073';
font-family: 'Font Awesome 5 Free';
left: -4px;
bottom: 4px;
position: relative;
}
position, left and bottom are used to align the icon.
Sometimes adding font-weight: 600 or above also helps.
No need to embed content into the CSS. You can put the badge content inside the fa element, then adjust the badge css. http://jsfiddle.net/vmjwayrk/2/
<i class="fa fa-envelope fa-5x" style="position:relative;color:grey;">
<span style="
background-color: navy;
border-radius: 50%;
font-size: .25em;
display:block;
position:absolute;
text-align: center;
line-height: 2em;
top: -.5em;
right: -.5em;
width: 2em;
height: 2em;
border:solid 4px #fff;
box-shadow:0px 0px 1px #000;
color: #fff;
">17</span>
</i>
#content h2:before {
content: "\f055";
font-family: FontAwesome;
left:0;
position:absolute;
top:0;
}
Example Link:
https://codepen.io/bungeedesign/pen/XqeLQg
Get Icon code from:
https://fontawesome.com/cheatsheet?from=io
Alternatively, if using Sass, one can "extend" FA icons to display them:
.mytextwithicon:before {
#extend .fas, .fa-angle-double-right;
#extend .mr-2; // using bootstrap to add a small gap
// between the icon and the text.
}
It seems that the given answers don't give a real background as the fontawesome is rendered outside the bloc you want the background in.
Here is my solution to have a "real" background effect :
html :
<div id="bloc" class="bg_ico_outer" style="">
<i class="fa fa-bookmark-o bg_ico"></i>
<div class='bloc_inner'>
<h2>test fontawesome as background</h2>
</div>
</div>
css :
.bg_ico {
position: absolute;
top: 0px;
right: -10px;
font-size: 17em;
color: green;
transform: rotate(25deg);
}
.bg_ico_outer{position: relative; overflow: hidden;}
#bloc{
height: 200px;
width:200px;
background: blue;
margin:50px auto;
}
.bloc_inner{
position: absolute;
}
h2{color: white;}
For this you just need to add content attribute and font-family attribute to the required element via :before or :after wherever applicable.
For example: I wanted to attach an attachment icon after all the a element inside my post. So, first I need to search if such icon exists in fontawesome. Like in the case I found it here, i.e. fa fa-paperclip. Then I would right click the icon there, and go the ::before pseudo property to fetch out the content tag it is using, which in my case I found to be \f0c6. Then I would use that in my css like this:
.post a:after {
font-family: FontAwesome,
content: " \f0c6" /* I added a space before \ for better UI */
}
This seems to be the simplest solution :-)
#content h2:before {
font-family: FontAwesome;
content: "\f055";
position:absolute;
left:0;
top:0;
}

Resources