22019

CSS :before content two colors

Question:

I have a structured semantic data that I would like to display, styling it with CSS. Here is a sample:

...<user name="John">...</user>...

I would like it to be rendered like this:

... User John ... ...

If I wanted 'User' and 'John' to have the same color, I could use the following CSS:

user:before{ content: 'User ' attr(name); }

However, I want 'User' to be blue, and 'John' to be red. How can I achieve this with CSS?

P.S. I don't want to transform my data representation. I know I could easily get such formatting by transforming the data to have extra divs, etc, but I would like to display my pure semantic data directly in the form it is.

Answer1:

If the user element is empty, you could use the following CSS:

user::before {content:"User "; color:blue;} user::after {content:attr(name); color:red;}

If the user element contains content, you may use:

user::before {content:"User "; color:blue; float:left; margin-right:0.5em;} user::after {content:attr(name); color:red; float:left; margin-right:0.5em;}

(using float to get the element content behind the "User John" string; using margin to get whitespace between the words; there are probably neater ways to fix those things)

Answer2:

You can't style <em>part</em> of content of :before pseudo differently in CSS. You need an extra element (like a span) somewhere. :first-letter and :first-line (and content of :before as a whole) are exceptions to that but that's not what you want to achieve.

Recommend

  • Why the pattern matches one word while there is two identical word?
  • mapping by ggplot2 geom_polygon goes crazy after merging data
  • serializeData vs. templateHelpers?
  • Performing way to limit double accuracy
  • Ansible syntax best practice, YAML dictionary (key: value) or equal sign (key=value)?
  • How to read xlsx file in an android application?
  • Formatting with mako
  • Is a .txt file created in VB different than one I'd randomly create?
  • UIButton Borders Function Only Gives Back White Borders
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • C++/CLI Thread synchronization including managed and unmanaged code
  • Jquery UI Sortable, move item automatically
  • Responsive left sidebar open close
  • EntLib Way to Bind “Null” Value to Parameter
  • jquery code not working without breakpoint
  • apply a javascript function to draggable copy
  • Aptana 3 remove bundle (jquery)
  • netsh acl setting (need alternative method - registry settings?)
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Django model inheritance, filtering models
  • saving file generated by TCPDF
  • Alternative to overridePendingTransition() - Android
  • d3 v4 drag and drop with TypeScript
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Highlight and Bold text in JTextPane
  • Algorithm for a smudge tool?
  • Xamarin Forms - UWP Fonts
  • How reduce the height of an mschart by breaking up the y-axis
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Is possible to count alias result on mysql
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • jquery mobile loadPage not working
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • Android Google Maps API OnLocationChanged only called once