Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically generate text inside 'content' of a CSS pseudo-class

I want to dynamically generate the text within 'content' of a CSS pseudo class in the proper way. I have filled a working jsfiddle with my example so far. This picture shows better how I want to achieve this:

Example of what I want to achieve

This is the relevant code (it's in the fiddle also):

[part of] index.php:

<div class="checkbutton">
  <input type="checkbox" value="None" id="slideThree" name="check"/>
  <label for="slideThree"></label>
</div>

[part of] style.css:

.checkbutton:before
  {
  content: 'Hombre';
  float: left;
  width: 60px;
  text-align: center;

  /* 28 es la altura total */
  font: 12px/28px Arial, sans-serif;
  color: CornflowerBlue;
  z-index: 0;
  font-weight: bold;
  }

I want to be able to reuse that code for two purposes:

  1. Dynamically and internally translate it with PHP.
  2. Different buttons with similar style.

This means, I want to be able to create a similar button but with different names, or just translate it without extra markup. I have no idea how to do this properly. Here's why:

  • The CSS is in a unique .css file, separated from the content. Obviously, this doesn't execute php.
  • It's not possible to have CSS pseudo elements inside inline styles
  • No JavaScript. While this could be achieved with javascript, I prefer to avoid it where possible.

How can I achieve it? I find it odd that I need to use CSS for

like image 457
Francisco Presencia Avatar asked Jan 13 '23 14:01

Francisco Presencia


1 Answers

As long as you have the ability to set the content you want as an attribute, you can make use of the attr() function to abstract your styles:

http://tinker.io/bda2e

.checkbutton:before {
  content: attr(data-checked);
}

<div class="checkbutton" data-checked="Hombre" data-unchecked="Mujer">
  <input type="checkbox" value="None" id="slideThree" name="check"/>
  <label for="slideThree"></label>
</div>

Custom attributes with the prefix of data- are a part of HTML5: Embedding custom non-visible data with the data-* attributes

like image 115
cimmanon Avatar answered Jan 19 '23 10:01

cimmanon