Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
Code of Conduct .
Join Stack Overflow to learn, share knowledge, and build your career.
Email Sign Up
or sign in with
Formatting a number as currency using CSS
Just wondering if anyone knows whether it is possible to format the content of an element as currency using only CSS. It would be nice to have how the value is presented in CSS if possible, can’t find anything though so I’m not holding my breath 🙂
<!DOCTYPE html> <html> <head> <style type="text/css"> .dollars:before content:'$'; </style> </head> <body> Pure CSS: <span >25153.3</span> <br /> Ideal format: <span>$25,153.30</span> </body> </html>
That example comes out as:
Pure CSS: $25153.3
Ideal format: $25,153.30
- Nope. Not with pure css.
Feb 21 ’12 at 5:29
- possible duplicate of stackoverflow.com/questions/8677805/…
Feb 21 ’12 at 5:34
add a comment |
1) Convert the value to number (adds the commas based on the locale)
2) Add a class to determine if it is negative or positive value (i.e. red color)
.enMoney::before content:"$"; .negMoney color:red;
See more detail here with the sample code and css:
- Can’t affect the middle thousand sepparators, but before and after a number is enough for the goal…
Dec 17 ’16 at 23:37
add a comment |
If you’re asking about number formatting in CSS (that is, parsing a number from a string and then formatting it with thousands separator, decimal separator, fixed decimal digits number etc), then no, it is impossible in CSS, and this is not what CSS was designed for.
If you want to do any formatting, then you’d better to use XSLT. For example:
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="span[@class='dollars']"> <span> <xsl:text>$</xsl:text> <xsl:value-of select="format-number(current(), '###,###.00')"/> </span> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
- 59"and this is not what CSS was designed for"… I would reserve judgement about what CSS was designed for. Pushing common functionality into the browser is exactly what technologies like CSS were designed for. There was a time when "responding to screen sizes" was "not something CSS was designed for" Also: wiki.csswg.org/ideas/content-formatting
Mar 19 ’13 at 22:41
- 4aha – so that’s what XSLT was designed for 😉
Feb 10 ’15 at 9:21
- 14"Formatting a number" is a function of presentation, and that’s exactly what CSS was designed for.
– Aaron Cicali
Feb 12 ’16 at 17:37
add a comment |
Well, this doesn’t fit your own use case unfortunately, and it’s fairly obvious, but you could do this for integer numbers from -999 to 999.
.currency:before content:'$'; .currency:after content: '.00';
Then, a possible, annoying, solution if you’re working server side, is to convert your number to a reversed string and loop over each character. If you really wanted you could place the characters into li’s and css could then do the formatting you wanted as follows. However this is incredibly pointless as you may as well simply author the string at that point.
.currency li:before content: ' ,'; .currency li:first-child:before content:'$' !important; .currency * display: inline-block; .currency, .currency > * display: inline-block; .currency:after content: '.00';
<ul ><li>123</li><li>456</li><li>789</li></ul> <ul ><li>456</li><li>789</li></ul> <ul ><li>789</li></ul>
For an even deeper dive into pointless effort, you could prepend a
<style> .currency:after content: '.00'; </style>
<ul>, allowing your script to change the decimal value in CSS, lol
add a comment |
Not the answer you’re looking for? Browse other questions tagged html css format or ask your own question .
6 years, 6 months ago
9 months ago
Formatting numbers (decimal places, thousands separators, etc) with CSS
How do I display large numbers with commas? HTML
Format the display of a field
Set cellpadding and cellspacing in CSS?
Convert HTML + CSS to PDF with PHP?
How do I give text or an image a transparent background using CSS?
Is there a CSS parent selector?
When to use margin vs padding in CSS
Change an HTML5 input’s placeholder color with CSS
How can I transition height: 0; to height: auto; using CSS?
How do CSS triangles work?
How do I vertically center text with CSS?
Is it possible to apply CSS to half of a character?
Hot Network Questions
non parametric alternative to simple t-test
Can you switch which favored class bonus you take on each level up?
ssh "-P" option, what’s it used for?
Is it possible for an ancient contraption to survive modern-day attempts of breaking in?
A magician can draw magical power from other planets in a star system, but not their own – why?
Can you sneak into Premium Economy Class?
Tanach verses with narrative similar to Quran 2:61
Listing a Stack Overflow user as co-author for having provided substantial programming support
How to print multiline variables in side-by-side columns (bash)?
How to rotate scene via python script?
How do I proceed to seriously study Buddhism?
Inductive and deductive arguments and mathematical induction
Range of a rational function with radicals
Bookshelf Arrangement Dilemma
Should I tilt my DIY self-standing bookshelf backwards to prevent it falling down?
Where did Dumbledore live while working at Hogwarts?
What’s the pyramid joke?
Why is storing passwords in version control a bad idea?
Local char* – keeps its value
Why do airliners have to park so accurately?
TikZ: centering nodes and paths relative to each other
Why were floppy disks invented after hard disks?
How to solve this kind of nonlinear differential equation?
What are the mechanical and role-playing advantages of playing a human?
more hot questions