It seems you are using the material design css from here.
The style is defined in the .btn.btn-fab
class of that stylesheet. The icon is the content property of the ::before
pseudo-element of the button, which is inline-block
. The pseudo-element is inheriting the font-size
property of the button and hence you are not able to re-style it properly. Also, there is padding defined on that which will cause the text to move out of the button.
You need to override the styles for your use-case, i.e. #fixed-button
and reset its ::before
pseudo-element.
To be able to control it easily, it would be better if you made the button as relatively positioned and its pseudo-element absolutely positioned. Then carefully adjust the padding to accommodate the extra text you are putting into the button.
A rough example (explanation given in the code comments):
$("#fixed-button2").text('4');
$("#fixed-button3").text('42');
#fixed-button1, #fixed-button2, #fixed-button3 {
position: relative;
text-align: right; /* keep the text to right */
font-size: 11px; /* reduce the font-size to accomodate */
padding-top: 0px; padding-right: 6px; /* adjust the padding to keep the text on top-left */
}
#fixed-button1::before, #fixed-button2::before, #fixed-button3::before {
display: block; position: absolute; /* position relative to the parent button */
top: 0%; left: 45%; /* position to center with enough space for text */
font-size: 26px; padding-top: 15px; /* reset the changed properties of parent */
transform: translate(-50%, 0%); /* to make it centered */
}
#fixed-button1:empty::before, #fixed-button2:empty::before, #fixed-button3:empty::before {
left: 50%; /* if no text, we need to shift the icon to center */
}
/* following style is only for demo purpose in this snippet */
div, #fixed-button1, #fixed-button2, #fixed-button3 { margin: 24px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-material-design/0.3.0/css/material-fullpalette.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div>
<button
id="fixed-button1"
class="btn btn-fab btn-raised btn-material-yellow-600 mdi-action-shopping-cart"></button>
<button
id="fixed-button2"
class="btn btn-fab btn-raised btn-material-yellow-600 mdi-action-shopping-cart"></button>
<button
id="fixed-button3"
class="btn btn-fab btn-raised btn-material-yellow-600 mdi-action-shopping-cart"></button>
</div>
Also a fiddle for you to play with: http://jsfiddle.net/abhitalks/efma9bt9/
Update
(per op comments)
If you need to use the text as a notification bubble, then you can easily do that using another pseudo-element ::after
on the button. You will have to make a little change in the way you use the text.. instead of using text content of the button to indicate the quantity, use a data-
attribute on the button. Use that attribute as content for the ::after
pseudo-element.
In order to be able to hide the notification bubble when the quantity is zero, apply the styles based on a class. Then add/remove the class in your Javascript code where you are updating the quantity based on your ajax call.
Example Snippet 2: (In this example, I've used data-qty
as the attribute which can be used to update the quantity. The class .qty
is used to control the ::after
pseudo-element which you can add/remove based on the quantity)
$("#fixed-button2").attr('data-qty', '4').addClass('qty');
$("#fixed-button3").attr('data-qty', '42').addClass('qty');
#fixed-button1, #fixed-button2, #fixed-button3 {
position: relative;
font-size: 11px; /* reduce the font-size to accomodate */
padding-top: 0px; padding-right: 6px; /* adjust the padding to keep the text on top-left */
}
#fixed-button1::before, #fixed-button2::before, #fixed-button3::before {
display: block; position: absolute; /* position relative to the parent button */
top: 0%; left: 50%; /* position to center with enough space for text */
font-size: 26px; padding-top: 15px; /* reset the changed properties of parent */
transform: translate(-50%, 0%); /* to make it centered */
}
#fixed-button1.qty::after, #fixed-button2.qty::after, #fixed-button3.qty::after {
content: attr(data-qty);
display: block; position: absolute;
top: 0px; right: 0px; height: 20px; width: 20px;
font-size: 11px; line-height: 18px;
background-color: #f00; color: #fff;
border-radius: 50%; padding: 0;
}
/* following style is only for demo purpose in this snippet */
div, #fixed-button1, #fixed-button2, #fixed-button3 { margin: 24px; }
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-material-design/0.3.0/css/material-fullpalette.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<button
id="fixed-button1" data-qty=""
class="btn btn-fab btn-raised btn-material-yellow-600 mdi-action-shopping-cart"></button>
<button
id="fixed-button2" data-qty=""
class="btn btn-fab btn-raised btn-material-yellow-600 mdi-action-shopping-cart"></button>
<button
id="fixed-button3" data-qty=""
class="btn btn-fab btn-raised btn-material-yellow-600 mdi-action-shopping-cart"></button>
</div>
Fiddle 2: http://jsfiddle.net/abhitalks/z63qf4xb/1/
.