css设置文字在图片上面显示(使用Position及引入背景图片(background-image: url(path)))

<div class="container">
  <img src="image.jpg" alt="背景图片">
  <div class="h-title">这里是文字</div>
  <div class="config-title">这里是文字2</div>
</div>

方法一:使用绝对定位

CSS代码

 .container {
        position: relative;/相对定位/
        top: 0;
        left: 0;
 }
 .h-title {
        position: absolute;/绝对定位/
        top: 85px;
        left: 20px;
        font-size: 16px;
        font-weight: 700;
 }

 .config-title {
        position: absolute;/绝对定位/
        top: 125px;
        left: 20px;
        font-size: 10px;
        font-weight: 400;
        border: 1px solid #fff;
        border-radius: 4px;
        padding: 2px 8px 3px 8px;
        text-align: center;
        cursor: pointer;
 }

方法二:引入背景图片(background-image: url(path))

在引入背景图片的时候,我们需要注意背景图片的大小,如果和div大小不同,则我们需要调整background-size属性使图片适配div大小。代码如下:

<div class="container">
	<div class="imgbox box1">这是文字</div>
</div>
<style>
  .container .imgbox{
        height: 216px;
        width: 384px;
    }
  .container .box1 {
        font-size: 20px;
        color: ivory;
        background-image: url(../static/images/redLeafage.jpg);
        background-size: 384px 216px;
        background-repeat: no-repeat;
        text-align: right;
    }
</style>

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我之前的回答中有误导。在 `<option>` 元素上应用 CSS 样式,尤其是背景图像,是不可靠的,因为不同浏览器对于 `<option>` 元素的渲染方式存在差异,有些浏览器可能不支持背景图像。 为了在下拉列表中显示带有图片的选项,你可能需要使用自定义的下拉列表插件或使用其他的解决方案,例如使用自定义的HTML和CSS模拟下拉列表的外观,并通过JavaScript来处理选项的选择和图片显示。 以下是一个使用自定义HTML、CSS和JavaScript来实现下拉列表中显示带有图片的选项的示例代码: ```html <div class="custom-select"> <div class="select-selected">Option 1</div> <div class="select-options"> <div data-value="option1" data-image="path/to/image1.jpg">Option 1</div> <div data-value="option2" data-image="path/to/image2.jpg">Option 2</div> <div data-value="option3" data-image="path/to/image3.jpg">Option 3</div> </div> </div> ``` ```css .custom-select { position: relative; width: 200px; } .select-selected { background-color: #f2f2f2; padding: 5px; } .select-selected::after { content: ""; position: absolute; top: 50%; right: 10px; transform: translateY(-50%); width: 10px; height: 10px; border-left: 2px solid; border-bottom: 2px solid; border-color: #888; transition: all 0.3s ease-in-out; } .select-selected.open::after { transform: translateY(-50%) rotate(45deg); } .select-options { position: absolute; top: 100%; left: 0; background-color: #fff; border: 1px solid #ccc; display: none; } .select-options.open { display: block; } .select-options div { padding: 5px; } .select-options div:hover { background-color: #f2f2f2; } .select-options div[data-image] { padding-left: 30px; background-repeat: no-repeat; background-position: left center; } .select-options div[data-image]::before { content: ""; display: inline-block; width: 20px; height: 20px; background-size: cover; background-repeat: no-repeat; background-position: center center; } ``` ```javascript document.addEventListener("DOMContentLoaded", function() { var customSelect = document.querySelector(".custom-select"); var selectedOption = customSelect.querySelector(".select-selected"); var optionsList = customSelect.querySelector(".select-options"); selectedOption.addEventListener("click", function() { customSelect.classList.toggle("open"); optionsList.classList.toggle("open"); }); optionsList.addEventListener("click", function(e) { var selectedValue = e.target.getAttribute("data-value"); var selectedText = e.target.textContent; var selectedImage = e.target.getAttribute("data-image"); selectedOption.textContent = selectedText; // 处理图片显示 if (selectedImage) { var imageElement = document.createElement("div"); imageElement.style.backgroundImage = "url('" + selectedImage + "')"; selectedOption.insertBefore(imageElement, selectedOption.firstChild); } else { selectedOption.removeChild(selectedOption.firstChild); } // 更新选中的值 selectedOption.setAttribute("data-value", selectedValue); customSelect.classList.remove("open"); optionsList.classList.remove("open"); }); document.addEventListener("click", function(e) { if (!customSelect.contains(e.target)) { customSelect.classList.remove("open"); optionsList.classList.remove("open"); } }); }); ``` 这段代码创建了一个自定义的下拉列表,通过使用自定义的HTML、CSS和JavaScript来模拟下拉列表的外观和行为。当选择一个选项时,会更新显示的文本和图片,并通过 `data-value` 属性来存储选项的值。 请注意,这只是一个示例实现,并且可能需要根据你的具体需求进行修改和调整。同时,为了使图片正确显示,你需要将 `path/to/image.jpg` 替换为你实际的图片路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值