CSS用了clear:left之后,margin-top失效

本文探讨了一个关于CSS浮动布局的问题:当一个元素设置了float:left后,紧接着的另一个元素即便设置了clear:left和margin-top也无法实现预期的垂直间距效果。文章提供了一种解决方案,并表达了作者对于CSS布局复杂性的感慨。

又是一个奇怪的CSS问题,为什么CSS总有这么多不确定因素呢?对前端开发人员实在是中打击,又丧失了点对CSS的热情了。

且看代码:

 

#div1{
  float:left;
}

#div2{
  clear:left;
  margin-top:20px;
}

 照理讲,div2应该和div1差20个像素,但是事实证明他们之间一点空隙也没有,解决方法是不得不在html中加:

<div style="clear:both;"></div>

 意思就是先上这个块把float的效果彻底清空,然后解下去的div2才能和什么事情都没有发生过一样正常发生作用。从HTML代 码结构来讲,以上的这段是没有意义的,只是为了对CSS这种变态的行为做妥协,我觉得这种体验非常糟糕,真希望在将来的某一天CSS能变的好用一点。

<template> <!-- 品牌馆 --> <view class="outBox"> <s-layout title="" navbar="inner"> <view class="brand"> <view class="brand-header" :style="{ backgroundImage: `url('${headerImg}')` }"> <view class="header-box1 flex jus-con center column"> <image class="box1-img" :src="headerTitle"></image> <view class="box1-text">严选品牌好货</view> </view> <view class="main-search flex center"> <image :src="headerSearch" class="search-icon" @click="searchInfoClick"></image> <view class="search-input flex center"> <input class="uni-input" placeholder="请输入品牌名称查找搜索" :value="inputClearValue" @input="clearInput" placeholder-style="color: #baaf9fc9;" /> <uni-icons type="clear" color="#dbdbdb" size="26" v-if="showClearIcon" @click="clearIcon" ></uni-icons> </view> <view class="search-text flex center jus-con" @click="searchInfoClick">搜索</view> </view> </view> <view class="brand-main"> <view class="main-nav flex"> <!-- <view class="inner-box" v-for="(item, index) in mainNavTitle" :key="index" @click="qiehuanNav(index)" :class="item.inline ? 'innerBoxActive' : ''" > {{ item.title }} </view> --> <SuTab v-model="activeTab" :tab="mainNavTitle" scroll :titleStyle="{ activeBg: '#734c2f', activeColor: '#FFF', color: '#fee4bf' }" @change="handleTabChange" /> </view> <view class="main-content"> <scroll-view scroll-y @scroll="handleScrollClick" style="height: 60vh"> <view class="content-box" v-for="value in 4"> <view class="conBox-title flex center"> <view class="title-left flex center"> <image class="leftImg" src="#"></image> <view class="leftText-box"> <view class="box-text1">良品铺子官方旗舰店</view> <view class="tuike flex center"> <view class="pinpai">品牌自营</view> <view>推客专享</view> </view> </view> </view> <view class="title-right flex center jus-con"> <view class="right-text1">进店</view> <uni-icons type="right" size="14" color="#fff"></uni-icons> </view> </view> <view class="conBox-swiper"> <swiper class="swiper flex" circular :autoplay="autoplay" :interval="interval" :duration="duration" > <swiper-item class="swiper-item" v-for="item in 4"> <view class="item-inner"> <image src="#" class="inner-img"></image> <view class="inner-text flex column"> <view class="inner-top">【良品铺子】猪肉脯肉干高蛋白肉脯靖江特...</view> <view class="inner-bottom"> <view class="bottom-textBox1 flex center"> <view class="textBox1-money">¥</view> <view>39.90</view> </view> <view class="flex center"> <view class="bottom-textBox2">预估返</view> <view class="bottom-textBox3">¥3.20</view> </view> </view> </view> </view> </swiper-item> </swiper> </view> </view> </scroll-view> </view> </view> </view> </s-layout> </view> </template> <script setup> import { ref } from 'vue'; import SuTab from '../../sheep/ui/su-tab/su-tab.vue'; const headerImg = ref('/static/img/brand/header-bg.png'); const headerTitle = ref('/static/img/brand/header-title.png'); const headerSearch = ref('/static/img/brand/search.png'); const inputClearValue = ref(''); const showClearIcon = ref(false); const clearInput = (e) => { inputClearValue.value = e.detail.value; if (e.detail.value.length > 0) { showClearIcon.value = true; } else { showClearIcon.value = false; } }; const clearIcon = () => { inputClearValue.value = ''; showClearIcon.value = false; }; const searchInfoClick = () => { console.log('点击了搜索'); }; const currentNavIndex = ref(null); const mainNavTitle = ref([ { title: '全部', inline: true }, { title: '个护清洁', inline: false }, { title: '美食饮品', inline: false }, { title: '服装配饰', inline: false }, { title: '运动', inline: false }, ]); // 标签切换回调 const activeTab = ref(0); const handleTabChange = (e) => { console.log('选中标签索引:', e.index, '标签数据:', e.data); currentNavIndex.value = e.index; }; const handleScrollClick = () => { console.log('滚动'); }; const qiehuanNav = (val) => { mainNavTitle.value.forEach((item) => { item.inline = false; }); mainNavTitle.value[val].inline = true; }; </script> <style lang="scss" scoped> .outBox { width: 100%; height: 100vh; overflow: hidden; position: relative; .flex { display: flex; } .center { align-items: center; } .jus-con { justify-content: center; } .column { flex-direction: column; } :deep(.page-body) { width: 100%; height: 99.7vh; background: #230a00 !important; } .brand { height: 88.5vh; .brand-header { width: 750rpx; height: 800rpx; position: absolute; top: 0; left: 0; background-repeat: no-repeat; background-size: 100% 100%; .header-box1 { width: 424rpx; height: 190rpx; margin: 190rpx 162rpx 0 164rpx; .box1-img { width: 100%; height: 136rpx; object-fit: cover; } .box1-text { font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 32rpx; color: #fee4bf; text-align: left; font-style: normal; text-transform: none; margin-top: -30rpx; } } .main-search { width: 694rpx; height: 56rpx; border-radius: 36rpx; margin: 95rpx 28rpx 0; border: 1px solid #fee4bfb3; .search-icon { width: 36rpx; height: 36rpx; margin: 18rpx 0rpx 18rpx 42rpx; } .search-input { width: 62vw; height: 44rpx; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 28rpx; color: #666666; text-align: left; font-style: normal; text-transform: none; margin: 0 16rpx; .uni-input { width: 100%; height: 100%; color: #baaf9f; } } .search-text { width: 136rpx; height: 100%; border-radius: 36rpx; background-color: #fff2e0; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 28rpx; color: #ff9600; text-align: left; font-style: normal; text-transform: none; } } } .brand-main { position: absolute; top: 560rpx; left: 28rpx; // z-index: 1; height: 65vh; .main-nav { width: 95.4vw; height: 56rpx; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; :deep(.ui-tab.ui-tab-scrolls .ui-tab-scroll-warp .ui-tab-scroll) { line-height: 0 !important; } :deep(.ui-tab-item) { padding: 0rpx 1rpx !important; } :deep(.ui-tab-item .ui-tab-text) { margin-right: 16rpx; padding: 8rpx 24rpx; box-sizing: border-box; border-radius: 70rpx; background-color: #562c1599; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 28rpx; color: #fee4bf; text-align: center; font-style: normal; text-transform: none; } // .inner-box { // margin-right: 16rpx; // padding: 8rpx 24rpx; // box-sizing: border-box; // border-radius: 70rpx; // margin-right: 16rpx; // background-color: #562c1599; // font-family: PingFang SC, PingFang SC; // font-weight: 400; // font-size: 28rpx; // color: #fee4bf; // text-align: center; // font-style: normal; // text-transform: none; // } // .innerBoxActive { // color: #ffffff; // background-color: #734c2f; // } } .main-content { margin-top: 20rpx; margin-bottom: 16rpx; height: 60vh; overflow-y: auto; .content-box { width: 694rpx; height: auto; border-radius: 32rpx; background-color: #fde3be; padding: 32rpx 0 32rpx 32rpx; box-sizing: border-box; margin-bottom: 32rpx; .conBox-title { margin-bottom: 30rpx; margin-right: 32rpx; justify-content: space-between; .title-left { .leftImg { width: 96rpx; height: 96rpx; object-fit: cover; border-radius: 50%; border: 1px solid red; margin-right: 8rpx; } .leftText-box { .box-text1 { font-family: PingFang SC, PingFang SC; font-weight: 500; font-size: 32rpx; color: #000000; text-align: center; font-style: normal; text-transform: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; width: 300rpx; margin-bottom: 8rpx; } .tuike { font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 24rpx; color: #734c2f; text-align: center; font-style: normal; text-transform: none; .pinpai { margin-right: 8rpx; } } } } .title-right { width: 130rpx; height: 56rpx; border-radius: 28rpx; background: #734c2f; margin-top: 20rpx; .right-text1 { margin-right: 2rpx; margin-left: 10rpx; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 28rpx; color: #ffffff; text-align: left; font-style: normal; text-transform: none; } } } .conBox-swiper { height: 372rpx; .swiper { height: 100%; overflow: hidden; .swiper-item { width: 236rpx !important; height: 100%; .item-inner { margin-right: 16rpx; width: 220rpx; height: 100%; border-radius: 16rpx; .inner-img { border-radius: 16rpx 16rpx 0 0; width: 220rpx; height: 220rpx; object-fit: cover; background-color: pink; } .inner-text { padding: 4rpx 12rpx 8rpx 10rpx; box-sizing: border-box; justify-content: space-between; background-color: #fff; margin-top: -8rpx; height: 142rpx; border-radius: 0 0 16rpx 16rpx; .inner-top { width: 100%; font-family: PingFang SC, PingFang SC; font-weight: 400; font-size: 20rpx; color: #333333; text-align: left; font-style: normal; text-transform: none; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; } .inner-bottom { margin-top: 8rpx; .bottom-textBox1 { font-family: PingFang SC, PingFang SC; font-weight: 600; font-size: 20rpx; color: #f00403; text-align: left; font-style: normal; text-transform: none; .textBox1-money { margin-top: 5rpx; } } .bottom-textBox2 { padding: 4rpx; box-sizing: border-box; border-radius: 8rpx; background-color: #fe294c; font-family: PingFang SC, PingFang SC; font-weight: 500; font-size: 16rpx; color: #ffffff; text-align: left; font-style: normal; text-transform: none; } .bottom-textBox3 { width: 67rpx; height: 28rpx; background: #ffe5e9; border-radius: 8rpx; font-family: PingFang SC, PingFang SC; font-weight: 500; font-size: 20rpx; color: #fe294c; text-align: left; font-style: normal; text-transform: none; } } } } } } } } } } } } </style> 为什么中间的 SuTab的滚动为什么不生效vue3小程序uniapp
最新发布
09-26
# 启动浏览器 browser = webdriver.Edge(options=options) browser.set_page_load_timeout(60) browser.get("https://pubmed.ncbi.nlm.nih.gov/")之后,读取/missing_dois.csv表格中doi列第一个编号,并输入PubMed网页的搜索框去寻找文章,搜索框链接为<span class="twitter-typeahead ncbi-autocomplete"><input type="search" name="term" data-skip-ie-scroll-to-top="" data-replace-term-with-exact="" data-exact-query="" placeholder="" class="term-input tt-input" required="required" id="id_term" spellcheck="false" dir="auto" style="position: relative; vertical-align: top;" aria-owns="id_term_listbox" role="combobox" aria-autocomplete="list" aria-expanded="false"><span role="status" aria-live="polite" style="position: absolute; padding: 0px; border: 0px; height: 1px; width: 1px; margin-bottom: -1px; margin-right: -1px; overflow: hidden; clip: rect(0px, 0px, 0px, 0px); white-space: nowrap;"></span><pre aria-hidden="true" style="position: absolute; visibility: hidden; white-space: pre; font-family: BlinkMacSystemFont, -apple-system, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; word-spacing: 0px; letter-spacing: 0px; text-indent: 0px; text-rendering: auto; text-transform: none;"></pre><div role="listbox" class="tt-menu" id="id_term_listbox" aria-expanded="false" style="position: absolute; top: 100%; left: 0px; z-index: 100; display: none;"><div role="presentation" class="tt-dataset tt-dataset-0"></div></div></span>
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值