简体   繁体   English

我可以换行 img alt 文本吗?

[英]Can I wrap img alt text?

I have an image set by css style to 100x75.我有一个由 css 样式设置为 100x75 的图像。 When it doesn't load, the alt text loads into the space, but expands the container to beyond 100px width.当它不加载时,替代文本会加载到空间中,但会将容器扩展至超过 100 像素的宽度。

How can I prevent this?我怎样才能防止这种情况? Either by cutting it off or wrapping it.要么把它剪掉,要么把它包起来。

Well, I figured it out to some degree.嗯,我在某种程度上想通了。 I just wrapped the image in a container of the same size.我只是将图像包装在相同大小的容器中。 I guess my browser (Firefox) was not wrapping the text because it was an inline element.我猜我的浏览器(Firefox)没有包装文本,因为它是一个内联元素。

Thanks everyone for your responses.感谢大家的回应。

Alternatively:或者:

  • you can use overflow: hidden;你可以使用overflow: hidden; to the image.到图像。 It will hide the alt text that crosses the image border.它将隐藏跨越图像边框的替代文本。 Though it will not wrap the text.虽然它不会换行文本。
  • also, reduce the alt text size.另外,减小替代文字的大小。

#idlogo img {    
    overflow: hidden;
    font-size: 10px;
}

The purpose of alt Text is to let the screen reader know about the image, so I think reducing the alt text size and hiding the overflow should work out best. alt Text 的目的是让屏幕阅读器了解图像,所以我认为减小 alt 文本大小和隐藏溢出效果最好。 But, of course avoid using very long text.但是,当然要避免使用很长的文本。

In general, there is no way to control how alt texts are displayed, and most browsers produce their own version (some use tooltips, some in the status bar at the bottom).通常,无法控制 alt 文本的显示方式,并且大多数浏览器都会生成自己的版本(有些使用工具提示,有些在底部的状态栏中)。 You cannot use markup in alt texts, though some browsers will represent a line-break as a line break (most won't, though).您不能在 alt 文本中使用标记,尽管某些浏览器会将换行符表示为换行符(但大多数不会)。 See this article for more.有关更多信息,请参阅本文

The alt attribute is for alternate text; alt 属性用于替代文本; it should be short and describe the function it has or the content it presents.它应该简短并描述它所具有的功能或它所呈现的内容。

Here's a good read on the subject from Roger Johansson on 456 Berea Street - Writing good alt text这是罗杰·约翰逊( Roger Johansson)在伯里亚街 456 号的一篇关于这个主题的好读物—— 写好的替代文字

All in all, if you're writing a paragraph, then you're doing it wrong.总而言之,如果你在写一个段落,那么你做错了。 Look at using the longdesc attribute (which is a link to a page describing the image that complements the alt text).查看使用 longdesc 属性(它是一个链接到描述图像的页面,以补充替代文本)。

HTH高温高压

For Firefox you can use the attribute align:left .对于 Firefox,您可以使用属性align:left This will prevent the alt text to cause the alignment issue but will overflow the available space.这将防止 alt 文本导致对齐问题,但会溢出可用空间。 And for that you can use an appropriate font-size attribute.为此,您可以使用适当的字体大小属性。

Just discovered at least in Chrome you can put overflow-wrap: break-word and it will wrap.至少在 Chrome 中发现你可以放置overflow-wrap: break-word并且它会换行。

picture, img
{
   overflow-wrap: break-word;
} 

在此处输入图像描述

如果可能的话,不要让替代文本尽可能长

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

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