简体   繁体   English

自定义 html 标签作为 img 标签 - 图像不显示

[英]custom html tag as img tag - image not displayed

i have a blog where i wanted to create a subsite listing my fav apps.我有一个博客,我想在其中创建一个子网站来列出我最喜欢的应用程序。 for code consistency and for not confusing between existing html "normal" code describing website i made my own "subhtml" just for app cards.为了代码的一致性,并且为了不混淆现有的 html 描述网站的“正常”代码,我为应用程序卡片制作了自己的“subhtml”。 for now best i did to this code was to display texted source of image instead of image.现在我对这段代码所做的最好的事情是显示图像的文本源而不是图像。 all css i tried already: content: attr(data-src url) , tried also attr both with url parameter and not, inside var() and not, also background-image: attr(data-src url) , all max-width , max-height , width , height with both 100% and auto properties, attr value in root variable (this gives me text output of image i said above), tried also with ::before thing but also gives nothing special...所有 css 我已经尝试过: content: attr(data-src url) ,也尝试过使用 url 参数而不是在var()内部而不是background-image: attr(data-src url) ,所有max-width , max-height , width , height具有100%auto属性,根变量中的attr值(这给了我上面说的图像的文本 output),也尝试了::before thing 但也没有什么特别的......

current not yet working code:当前尚未工作的代码:

 :root{ --icon: attr(data-src); } app-icon::before{ display: inline-block; content: var(--icon); }
 <app-box><app-title>app name</app-title><app-icon data-src="https://live.staticflickr.com/2552/3794648454_cf0c2a228b_b.jpg"></app-icon></app-box>

few additional notes:一些额外的注意事项:

  • i don't want javascript (a11y)我不要 javascript (a11y)
  • i don't want javascript (i dont like + confusing)我不想要 javascript(我不喜欢 + 令人困惑)
  • yes i already was asking that in few places outside here but with no bigger help是的,我已经在外面的几个地方问过这个问题,但没有更大的帮助
  • i know xml so i would like to use this also in few other projects too我知道 xml 所以我也想在其他几个项目中使用它
  • my browser is firefox but friends with other browsers also have this problem (so its not a browser bug, just my disknowledge of css)我的浏览器是 firefox 但其他浏览器的朋友也有这个问题(所以这不是浏览器错误,只是我对 css 的了解)
  • i know that custom tags doesnt have defined attributes except of id and class so i used custom one as was visible above =D我知道自定义标签没有定义的属性,除了 id 和 class 所以我使用了上面可见的自定义标签 =D
  • i know default css property of img tag inline-block and im using it我知道 img 标签inline-block的默认 css 属性并且我正在使用它

screenshot of current bug:当前错误的屏幕截图:

类似应用商店的网站的屏幕截图,其中第一个应用条目包含“pinky-kde.giftest”文本而不是测试应用卡片

this blog is under: https://hacknorris.neocities.org/app-store.html if someone would to test this bugg-css by themselves此博客位于: https://hacknorris.neocities.org/app-store.html如果有人想自己测试这个 bugg-css

so anyone knows how to display this image from custom tag?所以有人知道如何从自定义标签显示此图像吗?

The apparent problem of your code is setting the variable on the root-element.您的代码的明显问题是在根元素上设置变量。 The root does not have the attribute "data-src", only the app-icon has.根没有属性“data-src”,只有应用程序图标有。 So you are looking for something like this:所以你正在寻找这样的东西:

app-icon {
  --icon: attr(data-src);
}

app-icon::before {
  display: inline-block;
  background-image: url(var(--icon));
  width: 100px;
  height: 100px;
  content: '';
}

But well, this doesn't work.但是,这行不通。 You can't have variable URL tokens.您不能拥有变量 URL 令牌。

But you cannot do this with url(), as url(var(--url)) is parsed not as a url( function token followed by var(--url) followed by a ), but a single url() token that is invalid because the var(--url) is being treated as a URL itself, and unquoted URLs in url() tokens cannot contain parentheses unless they are escaped.但是您不能使用 url() 执行此操作,因为 url(var(--url)) 被解析为不是 url(function 标记后跟 var(--url) 后跟 a ),而是单个 url() 标记无效,因为 var(--url) 本身被视为 URL,并且 url() 标记中未加引号的 URL 不能包含括号,除非它们被转义。 This means the substitution never actually occurs, because the parser never sees any var() expressions in the property value — indeed, your background declaration is completely invalid.这意味着替换从未真正发生,因为解析器从未在属性值中看到任何 var() 表达式——实际上,您的背景声明是完全无效的。

(From https://stackoverflow.com/a/42331003/6336728 ) (来自https://stackoverflow.com/a/42331003/6336728

What you can do is put the URL in inline CSS:您可以做的是将 URL 放入内联 CSS 中:

<app-box>
  <app-title>app name</app-title>
  <app-icon style="--icon: url(https://live.staticflickr.com/2552/3794648454_cf0c2a228b_b.jpg)"></app-icon>
</app-box>

I don't think that's the best solution though.我不认为这是最好的解决方案。 What are your goals of using this method?您使用此方法的目标是什么? I think it would be much better to just do it the usual way and use picture/source/img tags.我认为以通常的方式使用 picture/source/img 标签会更好。 This way you can make your images responsive ( https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images#art_direction )这样你就可以让你的图像响应( https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images#art_direction

I'm afraid it's not possible to do just with css. You should have do something like: content: url(var(--icon)) but this won't work.恐怕只用 css 是不可能的。你应该做类似的事情: content: url(var(--icon))但这行不通。 Anyway you're trying to get value from data-href attribute but in your html code you have data-src无论如何,你试图从 data-href 属性中获取值,但在你的 html 代码中你有 data-src

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM