You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React图片轮播组件背景图片无法在UI显示问题求助

问题排查:React轮播组件背景图不显示

你的代码里有一个明显笔误导致背景图无法展示:内层div的className中f-full是错误写法,应改为h-full。外层容器虽设置了高度,但内层容器高度为0时,背景图没有可渲染的空间,所以无法显示。

修正后的代码:

import React,{useState} from 'react'

const Featured = () => {
    const sliders=[
      {
        url: 'https://res.cloudinary.com/ehizeex-shop/image/upload/v1672672076/NetflixApp/burger_emxbtv.jpg'
      },
    ]
    const [currentIndex,setCurrenindex]=useState(0) 
  return (
    <div className='max-w-[1520px] h-[500px] w-full py-4 px-4 relative'>
        {/* 修正className中的f-full为h-full */}
        <div className='w-full h-full rounded-2xl bg-center bg-cover duration-500'
        style={{backgroundImage: `url(${sliders[currentIndex].url})`}}>
        </div>
    </div>
  )
}

额外排查点:

  • 直接在浏览器地址栏打开图片URL,确认图片资源是否可正常访问
  • 检查是否有其他CSS规则覆盖了内层div的高度或背景属性
  • 强制刷新浏览器(Ctrl+F5)清除缓存,避免缓存导致的资源加载异常

内容的提问来源于stack exchange,提问作者Harsh Tiwari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 13:32:12