【问题标题】:How to write css for alt attribute of img tag (word-break: break-all property doesn't work in IE)如何为 img 标签的 alt 属性编写 css(分词:break-all 属性在 IE 中不起作用)
【发布时间】:2015-11-09 21:41:46
【问题描述】:

我在锚标签中有一个 img 标签。 ancher 标签位于 div 标签内。我正在使用 img 标签的 alt 属性。 alt 属性的值是长度为 68 个字符的文本。我的问题是 alt 属性值(文本)超出了 div 并且隐藏在 IE 中。我的意思是在显示几个字符后,通常其余字符会被隐藏或裁剪。这是我的html代码sn-p。

<div class="products">
    <div class="prod-image"  id="catEntryImage">
        <div class="prodClass" id=WCCE>
           <a><img alt="some text total around 70 character" src="someURL"/></a>
           <P class=gasCode></P>
        </div>
   </div>
   <div class="prod-content"></div>
   <script type="text/javaScript"></script>
</div>

给定 html sn-p 的 CSS

div {
    line-height: 1.4em;
    word-break: break-all;
    position: relative;
    margin: 0;
    padding: 0;
    border: 0;
    font-family: arial, helvetica, sans-serif;
    font-size: 100%;
}

img {
    text-decoration: none;
    border:0;
}

a {
    color: #e10014;
    text-decoration: none;
    font-size: 1.2em;
}
.products .prod-image {
    width: 135px;
    float: left;
    margin-right: 20px;
    margin-bottom: 20px;
    text-align: center;
    vertical-align: bottom;
    overflow: hidden;
    max-height: 250px;
    overflow: hidden;
}
.products .prod-content {
    width: 288px;
    float: left;
    margin-bottom: 20px;
    text-align: left;
    vertical-align: bottom;
}

我将 word-wrap & line-break css 属性应用于图像的 alt 属性,但它也不起作用。任何机构都可以帮助我吗?这仅在 IE 中发生。如果我对 img 标签使用“word-break: break-all”css 属性,它在 chrome 中可以正常工作,但在 IE 中不起作用。

【问题讨论】:

  • 能否请您在此处提供html代码..以便我们更好地识别您的问题。
  • 我上面已经提供了html & css sn-p。
  • IEIE11IE10 上工作正常。
  • @vivekkupadhyay 它的 IE 11。请现在检查我已经更新了上面的完整 html sn-p 结构。

标签: html css internet-explorer dojo


【解决方案1】:

只是风格

img { color: red; }

用于 alt 标签文本样式

【讨论】:

  • 您的建议适用于更改 alt 属性内的文本颜色,但我的问题是文本未包含在父 div 中,因此大多数字符未显示,或者我必须说 alt 标记值已隐藏。
【解决方案2】:

请通过我的JSFIddle,我希望这正是您想要实现的目标。

我创建了两个单独的 images 和大 alt 文本。

HTML:

<a class="a1"><img alt="some text total around 70 character some text total around 70 character some text total around 70 character some text total around 70 character some text total around 70 character" src="someURL"/></a>

<a class="a2"><img alt="some text total around 70 character some text total around 70 character some text total around 70 character some text total around 70 character some text total around 70 character" src="someURL"/></a>

CSS:

.a1{
     text-decoration: none;
    border:0; 
    width:270px; 
    word-break:break-all; 
    height:auto;
    display: block;
   }

.a2{
   text-decoration: none;
   border: 0px none;
   width: 270px;
   word-break: break-all;
   height: auto;
   text-overflow: ellipsis;
   overflow: hidden;
   display: block;
   white-space: nowrap;
 }

在第一个 image 上,我使用 word-break:break-allimg 包装器中拆分和包装 alt 文本。而在后一个中,当超过 img 包装器时,我使用 text-overflow:ellipsis 隐藏额外的 alt 文本。

【讨论】:

    【解决方案3】:

    您只需要在图像的父级.prodClass 上设置heightwidth 并在&lt;img&gt; 上设置heightwidth alt 应该照顾好自己。

    https://jsfiddle.net/Lu3tu98b/5/

    <div class="prodClass">
         <a>
            <img alt="This will contain a really long sentence with more than 70 characters right here." src="someURL"/>
        </a>       
    </div>
    
    .prodClass {
        height: 150px;
        width: 150px;
        background-color: wheat;
    }
    
    .prodClass img {
        background-color: pink;
        width: 100%;
       height: 100%;
    }
    

    【讨论】:

    • 更新了 IE 11 中的示例
    【解决方案4】:

    使用这个。

    img{width:100px} // change this as per you requirement
    

    这应该会欺骗 IE 问题..

    也可以在 IE only 样式中指定此项。所以不会影响其他浏览器的工作..

    喜欢:

     <!--[if IE]><style type="text/css"> img{width:100px}</style><![endif]-->
    

    处理IE 11问题:请参考old post

    【讨论】:

    • 请指定 IE 的版本,我认为您正在接受 IE 11,这是一个新的挑战,我已经在我的回答中更新了旧帖子的链接以处理 IE 11
    • 如果我不在 css 中保留背景颜色,那么它会在 div 上显示边框。我保持边界:无;但它仍然显示边界。有什么办法可以去掉这个边框?
    • 必须是图片边框,图片img{border:none;}img { border-width: 0;}也一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    相关资源
    最近更新 更多