Css Content, Attr and Url in the same sentence
It's neither a bug nor a mistake. The currently supported syntax (CSS2.1) for content is:
content: normal | none |
[ <string> | <uri> | <counter> | attr() |
open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
I.e.:
The literal
normal
,none
orinherit
Or any number of these in succession:
- a string -
"hello"
- a (constant) URI -
url("image.jpg")
- a counter -
counter(section)
- an attribute -
attr(id)
open-quote
,close-quote
,no-open-quote
,no-close-quote
- a string -
The specs don't allow for them to be "nested", they can only follow each other, e.g.:
content: "Photo: " url("../Img/Photo.jpg") attr(id);
/* Which is not what you want */
The current CSS3 drafts don't allow for it either. Possibly - if it's been discussed - because most use cases would have little to do with presentation and more to do with actual content.
How to mix url string & attribute value in CSS content property?
It's not possible to do that, this is the specification, you can't place attr() inside url(), they can only follow each other.
content: normal | none |
[ <string> | <uri> | <counter> | attr() |
open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit
div:before {
content: url("http://example.com/image.jpg") attr(title);
}
CSS attr() concatenation with url path
It is not possible to create a composite url()
value out of two or more strings. On top of the legacy url()
value, which isn't even a proper CSS function (see Is there a way to interpolate CSS variables with url()? — which means you can't even do this with custom properties), the proper CSS function version of url()
as defined in css-values-3 only accepts a single string.1
You can concatenate multiple strings in a content
declaration, but that is a feature of the content
property, not of strings in CSS.
1 Since url()
accepts a single string, this does mean that a single attr()
can be used as a URL value, also new to css-values-3, as attr(image url)
... except browser support is nonexistent.
Multiple content: attr() values
To concatenate two or more string values in CSS, separate them with whitespace:
.element:before {
content: attr(class) ' ' attr(data-size);
}
Note that the whitespace between the attr()
functions and the quotes is not the same as the whitespace within the quotes. The latter is an actual string containing a space character, which will separate the two attribute values in the output. The whitespace between the three parts is the operator that joins them together.
Pass css ::after content as specific word from class name
In this particular case, you can hack is with some negative text-indent
if the prefix is always the same. The code
element is using a monospace font so it's easy using ch
unit
code::before {
content: attr(class);
display:inline-block;
text-indent:-9ch; /* adjust this based on your real case */
clip-path:inset(0); /* hide the overflow */
}
<code class="language-css"> some code here </code>
<code class="language-html"> some code here </code>
<code class="language-javascript"> some code here </code>
CSS3 replace image src with another attr
The problem is, this is just an idea for something that the author thinks would be nice to have in CSS one day, currently this is not supported.
There is no src
property to be set in CSS, but a content
property, however it doesn't seem to accept an URL fetched using attr()
, respectively it doesn't seem to be implemented by any browser yet. Judging from the specs, something like this could be possible in the future:
content: attr(data-original url);
See also: Css Content, Attr and Url in the same sentence
So, as far as I can tell you'll currently have to stick with explicitly defining the URLs in CSS, something like
<img class="lazy gd0006" src="http://beresponsive.net/tcex/img/loadingplaceholder.gif">
<img class="lazy gd0007" src="http://beresponsive.net/tcex/img/loadingplaceholder.gif">
<img class="lazy gd0008" src="http://beresponsive.net/tcex/img/loadingplaceholder.gif">
...
@media print {
.lazy.gd0006 {
content: url('/images/gd-0006.jpg');
}
.lazy.gd0007 {
content: url('/images/gd-0007.jpg');
}
.lazy.gd0008 {
content: url('/images/gd-0008.jpg');
}
...
}
Set css content and url according to attribute value
I'm afraid you're not able to combine url()
and attr()
.
This means it's not possible to achieve the desired effect with CSS alone.
However, you can achieve it with just a little bit of JavaScript code :
var attribute = "data-label";var elements = document.querySelectorAll("["+attribute+"]");
for (var i = 0; i < elements.length; i++) { elements[i].style.content="url("+elements[i].getAttribute(attribute)+")";}
<table> <tr> <td data-label="https://i.stack.imgur.com/mRsBv.png?s=328&g=1"> <span>3</span> </td> </tr> <tr> <td data-label="https://www.gravatar.com/avatar/a42287aaa5cd72c0d29dd65f065e9c51?s=328&d=identicon&r=PG&f=1"> <span>3</span> </td> </tr></table>
Related Topics
Is @Page { Size:Landscape} Obsolete
Css: Clean Solution to the Margin Collapse Issue When Floating an Element
Next.Js Global CSS Cannot Be Imported from Files Other Than Your Custom <App>
In CSS, Differencebetween Cascading and Inheritance
How to Create Fluid Trapezoid Image with CSS
Up/Down Arrow Key Issue with Typeahead Control (Angular Bootstrap Ui)
CSS Change D Property of <Path>
Are There Any Good Reasons for Using Hex Over Decimal for Rgb Colour Values in CSS
How Is Padding-Top as a Percentage Related to the Parent's Width
CSS Text Align Justify Big Spaces
How Do Browsers Deal with Non-Integer Values for Height and Width
Input Height Differences in Firefox and Chrome
Cross Browser Way to Rotate Image Using CSS
How to Colour Backgrounds of Selected Items in HTML Select Options with CSS Only