简体   繁体   English

如何更改<span>CSS 媒体查询中的文本</span>

[英]How to change <span> text in CSS media query

I am trying to change the text inside a <span> element within a media query, and I want to avoid using JavaScript if possible.我正在尝试更改媒体查询中<span>元素内的文本,并且我想尽可能避免使用 JavaScript。 Is there a way to do this with only HTML/CSS?有没有办法只用 HTML/CSS 来做到这一点?

 @media only screen and (max-width: 768px) { header .text-1 { /* property here? */ } header .text-2 { /* property here? */ } }
 <header> <div class="banner"> <span class="text-1">COLMAR</span> <!-- change to "C" --> <span class="text-2">ACADEMY</span> <!-- change to "A" --> </div> </header>

When the window is less than 768px wide, I want the two <span> s in the <header> to be abbreviated to just one character ("C" and "A").当窗口宽度小于 768px 时,我希望<header>的两个<span>被缩写为一个字符(“C”和“A”)。

I looked on W3 for a property that would do this, something similar to (I think) JavaScript's .innerHtml , and I found content , but it doesn't seem to do what I am looking for.我在W3 上寻找可以做到这一点的属性,类似于(我认为)JavaScript 的.innerHtml ,我找到了content ,但它似乎没有做我想要的。

I am willing to use JavaScript if necessary, I just want to avoid it if it only takes a couple lines in CSS.如果需要,我愿意使用 JavaScript,如果它只需要几行 CSS,我只想避免它。

You could set the width of the span tag in CSS.您可以在 CSS 中设置span标签的宽度。 For the width to be able to be used, you need display:block;为了能够使用宽度,您需要display:block; . . You use overflow:none;您使用overflow:none; to hide the rest of the text.隐藏文本的其余部分。

The width of the span will have to depend on font size and font family.跨度的宽度必须取决于字体大小和字体系列。 Using a monospace font will help as all the characters are the same width.使用等宽字体会有所帮助,因为所有字符的宽度都相同。

 @media only screen and (max-width: 768px) { header .text-1, header .text-2{ width: 11px; overflow: hidden; display:inline-block; } }
 <header> <div class="banner"> <span class="text-1">COLMAR</span> <!-- change to "C" --> <span class="text-2">ACADEMY</span> <!-- change to "A" --> </div> </header>

Here's another approach using the ::before pseudo element as well (plus the original content wrapped in another span), but using a custom data attribute to define the alternative text instead of having to add a class for each occurrence.这是另一种使用::before伪元素的方法(加上包装在另一个跨度中的原始内容),但使用自定义数据属性来定义替代文本,而不必为每次出现添加一个类。 This allows to fill in the text via a CMS for example.例如,这允许通过 CMS 填充文本。

 @media only screen and (max-width: 768px) { [data-alttext] > span { display: none; } [data-alttext]::before { content: attr(data-alttext); } }
 <header> <div class="banner"> <span class="text-1" data-alttext="C"><span>COLMAR</span></span> <!-- change to "C" --> <span class="text-2" data-alttext="A"><span>ACADEMY</span></span> <!-- change to "A" --> </div> </header>

You could use the pseudo selector :before as so:您可以像这样使用伪选择器:before

 header .text-1:before { content: "COLMAR"; } header .text-2:before { content: "ACADEMY"; } @media only screen and (max-width: 768px) { header .text-1:before { content: "C"; } header .text-2:before { content: "A"; } }
 <header> <div class="banner"> <span class="text-1"></span> <!-- change to "C" --> <span class="text-2"></span> <!-- change to "A" --> </div> </header>

After doing a little bit of research you don't lose any accessibility with this approach either.在进行了一些研究之后,您也不会因为这种方法而失去任何可访问性。

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

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