HTML+CSS浮动实现图片上有描述文字

请问使用浮动实现一下效果该怎么实现?外部不想让其浮动是要加一个div吗?

img

这个不需要用浮动,绝对定位就可以。

img

<style>
    #contain {
        display: inline-block;
        position: relative;
        width: 150px;
        height: 150px;
    }

    img {
        width: 100%;
        height: 100%;
    }

    #content {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 30px;
        background-color: black;
        text-align: center;
        line-height: 30px;
        color: white;
    }
</style>
<body>
<div id="contain">
    <img/>
    <div id="content">
        这是文字部分
    </div>
</div>
</body>

加个div,然后加个定位就可以

定位就行 ,下面是例子,你调一下 字体和 颜色


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            position: relative;
        }

        .box img {
            width: 100%;
            height: 100%;
        }

        .box .text {
            position: absolute;
            bottom: 0;
            width: 100%;
            height: 20px;
            background: black;
            color: white;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fci.xiaohongshu.com%2Fda88dcf0-98cb-d17f-370e-52caaa17ddf6%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fci.xiaohongshu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648344239&t=b2b036d8beff6df4f6f41e60f6d8711a"
            alt="" srcset="">
        <div class="text">1111</div>
    </div>

</body>
<script>

</script>

</html>