亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
1. Hide the broken image and show fallback text or icon
2. Use JavaScript to replace broken images
3. Alternative: Use a default image as fallback
Summary
首頁 web前端 css教程 如何在CSS中設(shè)計(jì)損壞的圖像圖標(biāo)

如何在CSS中設(shè)計(jì)損壞的圖像圖標(biāo)

Aug 08, 2025 am 08:24 AM

使用CSS的::after偽元素覆蓋默認(rèn)的斷鏈圖片圖標(biāo),通過定位顯示自定義文本或圖標(biāo);2. 結(jié)合JavaScript監(jiān)聽onerror事件,為損壞的圖片添加特定類名以應(yīng)用背景圖或樣式;3. 直接在HTML中使用onerror內(nèi)聯(lián)腳本將失效圖片源替換為默認(rèn)占位圖,從而實(shí)現(xiàn)友好的用戶界面展示。

How to style a broken image icon in CSS

When an image fails to load, browsers display a default broken image icon. While you can't directly style the broken image icon itself (it's rendered by the browser), you can hide it and replace it with custom fallback content using CSS and HTML techniques.

How to style a broken image icon in CSS

Here’s how to handle and style a fallback for broken images:

1. Hide the broken image and show fallback text or icon

You can hide the broken image and display custom content (like text or a placeholder icon) using the ::before or ::after pseudo-elements.

How to style a broken image icon in CSS

HTML:

<img class="image-fallback lazy"  src="/static/imghw/default1.png"  data-src="missing-image.jpg"  alt="如何在CSS中設(shè)計(jì)損壞的圖像圖標(biāo)">

CSS:

How to style a broken image icon in CSS
.image-fallback {
  width: 200px;
  height: 150px;
  object-fit: cover;
  font-family: 'Arial', sans-serif;
  font-size: 14px;
  color: transparent;
  border: 2px solid #ddd;
  border-radius: 6px;
  position: relative;
  background: #f7f7f7;
}

.image-fallback::after {
  content: "Image not available";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #888;
  font-size: 12px;
  white-space: nowrap;
  z-index: 1;
}

.image-fallback[src*=""],
.image-fallback:not([src]) {
  opacity: 0;
}

Note: The ::after element only appears if the image fails to load and the browser still renders the <img src="/static/imghw/default1.png" data-src="photo.jpg" class="lazy" alt="如何在CSS中設(shè)計(jì)損壞的圖像圖標(biāo)" > tag. The trick relies on positioning fallback content over the image area.

2. Use JavaScript to replace broken images

Since CSS alone can't detect image loading errors, combining it with JavaScript gives better control.

JavaScript:

document.querySelectorAll('img').forEach(img => {
  img.onerror = () => {
    img.src = ''; // Clear invalid src
    img.classList.add('broken');
  };
});

Now, when an image fails, it gets a broken class.

CSS for the broken class:

img.broken {
  background: #f0f0f0 url('data:image/svg xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#ccc"><path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"/><path d="m17.7 14.3-2-2c-.4-.4-1-.4-1.4 0l-1.6 1.6-1.6-1.6c-.4-.4-1-.4-1.4 0l-2 2V7h10.4zm-4.3-4.3c.8 0 1.5-.7 1.5-1.5S14.2 7 13.4 7 11.9 7.7 11.9 8.5s.7 1.5 1.5 1.5z"/></svg>') center/40px no-repeat;
  border: 1px dashed #ccc;
  color: #aaa;
}

img.broken::after {
  content: "Image missing";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 12px;
}

This method gives full control — you can use a background SVG, placeholder color, or even a default image.

3. Alternative: Use a default image as fallback

Instead of styling text, swap in a default image when the original fails.

<img src="/static/imghw/default1.png"  data-src="photo.jpg"  class="lazy" onerror="this.onerror=null; this.;" alt="Fallback example">

This is a concise inline solution. The this.onerror=null prevents infinite loops if the fallback image also fails.


Summary

  • You can't style the browser's broken image icon directly.
  • Use CSS with ::after to overlay fallback text/icon.
  • Combine with JavaScript for reliable detection.
  • Use onerror to swap in a default image.

With these techniques, you can create a clean, user-friendly fallback experience.基本上就這些。

以上是如何在CSS中設(shè)計(jì)損壞的圖像圖標(biāo)的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Stock Market GPT

Stock Market GPT

人工智能驅(qū)動(dòng)投資研究,做出更明智的決策

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

熱門話題

如何防止圖像拉伸或縮小CSS 如何防止圖像拉伸或縮小CSS Sep 21, 2025 am 12:04 AM

useobject-fitormax-widthwithheight:自動(dòng)置換式; object-fitControlshowimagesfillcontainersfillcontainerswhilepreservingaspectratios,andmax-width:100%;高度;高度:autoEsoensuresResresresResresRessersRessiveScalingScalingWithOutStertracterging。

如何使用純CSS創(chuàng)建下拉菜單 如何使用純CSS創(chuàng)建下拉菜單 Sep 20, 2025 am 02:19 AM

使用HTML和CSS可創(chuàng)建無需JavaScript的下拉菜單。2.通過:hover偽類觸發(fā)子菜單顯示。3.利用嵌套列表構(gòu)建結(jié)構(gòu),CSS設(shè)置隱藏與懸浮顯示效果。4.可添加過渡動(dòng)畫提升視覺體驗(yàn)。

如何使用CSS中的指針事件屬性 如何使用CSS中的指針事件屬性 Sep 17, 2025 am 07:30 AM

Thepointer-eventspropertyinCSScontrolswhetheranelementcanbethetargetofpointerevents.1.Usepointer-events:nonetodisableinteractionslikeclicksorhoverswhilekeepingtheelementvisuallyvisible.2.Applyittooverlaystoallowclick-throughbehaviortounderlyingelemen

如何使用CSS添加盒子陰影效果 如何使用CSS添加盒子陰影效果 Sep 20, 2025 am 12:23 AM

USETHEBOX-SHADOWPROPERTYTOADDDROPSHADOWS.DEFINEHORIZONTALANDVERTICALESTESETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETESTESTESTESTESTESTEMENG:MMULTIPLESHADOWSARECOMMA-SEPARAWS.MEULTIPLESHADOWSARECOMMA-SEPARATED.EXAMPL

如何將過濾器應(yīng)用于CSS的圖像 如何將過濾器應(yīng)用于CSS的圖像 Sep 21, 2025 am 02:27 AM

thecssfilterpropertyallowseasyagestylinglingwisslikeblur,亮度和格雷斯卡爾(Grayscale.UseFilter):濾波器函數(shù)(值)onimagesorbackgroundImages.commonfunctionsIncludeBlurblur(px),亮度(brightness),亮度(%),偏見(%),損壞(%),sancale(%),飽和度(%)

如何在CSS網(wǎng)格布局中創(chuàng)建空白? 如何在CSS網(wǎng)格布局中創(chuàng)建空白? Sep 22, 2025 am 05:15 AM

使用gap、row-gap或column-gap屬性可在CSSGrid布局中創(chuàng)建網(wǎng)格項(xiàng)之間的間距,gap是設(shè)置行列間距的簡(jiǎn)寫屬性,可接受一個(gè)或兩個(gè)長(zhǎng)度值,row-gap和column-gap則分別單獨(dú)控制行與列的間距,支持px、rem、%等單位。

如何在CSS中使用clamp()函數(shù)進(jìn)行響應(yīng)式版式 如何在CSS中使用clamp()函數(shù)進(jìn)行響應(yīng)式版式 Sep 23, 2025 am 01:24 AM

clamp()函數(shù)通過最小、首選和最大值實(shí)現(xiàn)響應(yīng)式字體縮放;2.語法為clamp(最小值,首選值,最大值),常用rem和vw單位;3.字體在小屏取最小值,隨屏幕增大按vw縮放,不超過最大值;4.合理選擇數(shù)值確保可讀性,避免過大或過?。?.結(jié)合rem類型比例提升設(shè)計(jì)一致性。

如何使用CSS創(chuàng)建響應(yīng)式正方形 如何使用CSS創(chuàng)建響應(yīng)式正方形 Sep 24, 2025 am 03:28 AM

使用aspect-ratio:1/1可創(chuàng)建響應(yīng)式正方形,現(xiàn)代瀏覽器中設(shè)置寬高比即可;若需兼容舊版瀏覽器,可用padding-top:100%技巧,通過相對(duì)單位保持寬高一致;也可用vw單位使正方形隨視口變化。

See all articles