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

为何使用float: left属性后,新增文本会显示在图片下方?

解决float:left文本环绕失效问题

问题现象

为.chess类设置float: left实现文本环绕图片,但添加更多文本后,文本未在图片右侧显示,而是跑到图片下方。

解决方案

1. 清除浮动对容器的影响

浮动元素会导致父容器高度塌陷,进而打乱布局。给包含浮动元素的.skill-row添加overflow: hidden,让容器包裹住浮动元素:

.skill-row {
    width: 50%;
    margin: 100px auto 100px;
    text-align: left;
    line-height: 2;
    overflow: hidden; /* 添加此行 */
}

如果需要兼容更复杂的布局,也可以使用clearfix伪元素方案:

.skill-row::after {
    content: "";
    display: table;
    clear: both;
}

2. 确保文本有足够横向空间

当前.chess图片宽度为25%,加上margin-right:30px,剩余空间足够容纳文本。后续若调整图片宽度,需保证.skill-row的50%容器宽度能同时放下图片和文本,避免文本因空间不足被迫换行。

3. 排查样式干扰

确认.skill-row内的h3、p等元素未设置clear: left或width: 100%,这类属性会强制元素换行,直接打破文本环绕效果。

修改后的完整CSS代码

body {
    margin: 0;
    text-align: center;
    font-family: 'Cambo', serif;
    font-size: 20px;
}

h1 {
    margin: 50px auto 0 auto;
    font-family: 'Sacramento', cursive;
    font-size: 5.625rem;
    color: #66BFBF;
}

h2 {
    color: #66BFBF;
    font-size: 2.5rem;
    font-weight: normal;
}

h3 {    
    color: #66BFBF;
}

.top-container {
    background-color: #E4F9F5;
    position: relative;
    padding-top: 100px;
}

.pro {
    text-decoration: underline;
}

.top-cloud {
    position: absolute;
    right: 300px;
    top: 50px;
}

.bottom-cloud {
    position: absolute;
    left: 300px;
    bottom: 300px;
}

.chess {
    width:25%;
    float: left;
    margin-right: 30px;
}

.pc-games {
    width: 25%;
    border-radius: 100%;
}

.skill-row {
    width: 50%;
    margin: 100px auto 100px;
    text-align: left;
    line-height: 2;
    overflow: hidden; /* 新增:清除浮动影响 */
}

内容的提问来源于stack exchange,提问作者Shivam Bansal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 20:55:40