span元素float:right 后右对齐换行原因及其解决

本文探讨了一个在Internet Explorer浏览器中出现的布局问题,即非浮动元素与浮动元素在同一行显示时发生的错位现象。文章提供了两种解决方案:一是调整元素顺序使浮动元素优先显示;二是将所有元素设置为浮动状态,确保它们在同一行正确排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天遇到一个奇怪的问题:

有个这样的布局:

<div>
这里是标题
<span class="right">这里是时间</span>
</div>

在FF 和 chrome 都正常,显示一行

可是到了IE下就不对了,变成了两行!

在网上找了些资料,算是明白了,具体如下:

原因:

当非float的元素和float的元素在一起的时候,如果非float元素在先,那么float的元素将被排斥也就是说,你的span是float:right,但是你文本还是float:none

如果要让两者占据同一行,一般有两个解决方法:

第一种:把span先于文本显示

如:

<div>
<span class="right">这里是时间</span>
这里是标题
</div>

第二:把文本也设成float

如:

<div>
<span class="right">这里是时间</span>
<span class="left">这里是标题</span>
</div>

转载于:https://www.cnblogs.com/freedom831215/archive/2010/03/22/1691707.html

``` <div style=" float: left; width: 50%; height: 500px;background-color: white;border-radius: 10px ;margin-top: 10px "> <div style="width: 5px;height: 25px;background-color: #1f78ff;margin-top: 22px;margin-left: 10px;border-radius: 10px;float: left"> </div> <div style="float: left;margin-left: 10px;margin-top: 20px;font-size: 20px;font-weight: bold"> 今日有效单量:{{ userinfo.today_order || '0' }}单 </div> <div style="float: right;font-size: 14px;color: gray;padding-top: 20px;padding-right: 20px">更新时间:{{ userinfo.nowtime }} <button @click="showdrawer" type="primary" style="color: rgb(64,158,255);background-color: white;border: none">数据表格 ></button> </div> <div style="clear: both"></div> <div style="float: left;margin-left: 8px;margin-top: 10px"> 日期A:<span style="color: rgb(64,158,255);font-weight: bold">{{ this.totalBa }}</span>单 日期B:<span style="color: rgb(64,158,255);font-weight: bold">{{ this.totalAa }}</span>单 升幅: <span style="font-weight: bold">{{ this.totalBa - this.totalAa }}</span>单</div> <div style="clear: both"></div> <div style="float: left;margin-left: 8px;padding-top: 20px"> 对比日期A: </div> <div style="clear: both"></div> <div style="float: left;margin-left: 106px;margin-top: -26px"> <el-date-picker value-format="yyyy-MM-dd" style="width: 140px;" v-model="timeA" type="date" @change="getOrderChart" placeholder="选择日期"> </el-date-picker> </div> <div style="clear: both"></div> <div style="float: left;margin-left: 260px;margin-top: -32px"> 对比日期B: </div> <div style="clear: both"></div> <div style="float: left;margin-left: 360px;margin-top: -36px"> <el-date-picker value-format="yyyy-MM-dd" style="width: 150px;" v-model="timeB" type="date" @change="getOrderChart" placeholder="选择日期"> </el-date-picker> </div> <div style="margin-top: -36px;float: right;margin-right: 150px"> <el-select style="width: 120px" v-model="current_time" placeholder="当前对比" @change="getOrderChart"> <el-option v-for="item in option_time" :label=item.label :value="item.value"> </el-option> </el-select> </div> </div> <div style="clear: both"></div> <div style="background-color: #FFFFFF;float: right;sborder-radius: 10px;margin-top: -29%;width: 30%"> <div style="width: 5px;height: 25px;background-color: #1f78ff;margin-top: 32px;margin-left: 10px;border-radius: 10px;float: left"> </div> <div style="float: left;margin-left: 10px;margin-top: 30px;font-size: 20px;font-weight: bold"> 我的寄件数量 </div> <div style="clear: both"></div> <div style="float: left;margin-top: 20px;font-size: 14px;color: gray;padding-left: 10px"> 更新时间:{{ userinfo.nowtime }} </div> <div style="clear: both"></div> <div ref="chinaMap" style="width: 600px; height: 400px;background-color: white"> </div> <div style="clear: both"></div> <div style="float: right;margin-top:-470px;margin-right: 30px"> <el-radio-group v-model="radio1"> <el-radio-button label="今日"></el-radio-button> <el-radio-button label="昨日"></el-radio-button> <el-radio-button label="本周"></el-radio-button> </el-radio-group> </div> </div>```<div style="margin-top: -36px;float: right;margin-right: 150px"> <el-select style="width: 120px" v-model="current_time" placeholder="当前对比" @change="getOrderChart"> <el-option v-for="item in option_time" :label=item.label :value="item.value"> </el-option> </el-select> </div> <div style="background-color: #FFFFFF;float: right;sborder-radius: 10px;margin-top: -29%;width: 30%">窗口缩小后这俩块地方位置错乱,要求在我的代码基础上改动
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值