[英]Display data attribute value with CSS
I'm using a WordPress plugin to display events on a calendar. 我正在使用WordPress插件在日历上显示事件。 It has the following markup to display days:
它具有以下标记来显示日期:
<th id="tribe-events-monday" title="Monday" data-day-abbr="Mon">Monday</th>
Instead of displaying "Monday", I'd like it to display "Mon". 我希望不显示“星期一”,而是显示“星期一”。 Is it possible to output the data attribute (data-day-abbr) using CSS?
是否可以使用CSS输出数据属性(data-day-abbr)?
This would be a prefered workaround, otherwise, I'll need to create child template files. 这将是首选的解决方法,否则,我将需要创建子模板文件。
You can do this by hiding the th
content's visibility, and replacing it using CSS' content
and attr
: 您可以通过隐藏要这样做
th
内容的知名度,并使用CSS”替换它content
和attr
:
[data-day-abbr]:before { content: attr(data-day-abbr); visibility: visible; } th { visibility: hidden; }
<table> <tr> <th id="tribe-events-monday" title="Monday" data-day-abbr="Mon">Monday</th> </tr> </table>
Access data-day-abbr in CSS and set its value on a pseudo element (before): 在CSS中访问data-day-abbr并将其值设置为伪元素(之前):
#tribe-events-monday:before {
content: attr(data-day-abbr);
}
What you could do is "hide" the text in the th
by setting it's font-size to zero. 您可以通过将font-size设置为零来“隐藏”
th
的文本。
Than add an :after
element and fill it with the data attribute. 比添加
:after
元素并用data属性填充它。
More information 更多信息
th { border: 1px solid black; font-size: 0; } th:after { content: attr(data-day-abbr); font-size: 12px; }
<table> <tr> <th id="tribe-events-monday" title="Monday" data-day-abbr="Mon">Monday</th> </tr> </table>
You can use text-indent and ::before 您可以使用text-indent和:: before
[data-day-abbr] { text-indent: -100vw;/* at least enough to hide text */ } [data-day-abbr]::after { content: attr(data-day-abbr); float:left;/* or display: block;*/ text-indent: 0; }
<table> <tr> <table> <tr> <th title="Monday" data-day-abbr="Mon">Monday</th> <th title="Tuesday" data-day-abbr="Tue">Tuesday</th> <th title="wednesday" data-day-abbr="Wed">Wednesda</th> <th title="Thursday" data-day-abbr="Thu">Thursday</th> <th title="friday" data-day-abbr="Fri">friday</th> <th title="Saturday" data-day-abbr="Sat">Saturday</th> <th title="Sunday" data-day-abbr="Sun">Sunday</th> </tr> </table> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.