margin-left: auto;为什么可以使的元素靠右

本文详细解释了在CSS中如何利用margin-left:auto实现元素水平居中。当width设置为固定值,且margin-right不设置时,默认为0,margin-left会占据剩余的全部宽度,使元素向右靠拢,达到居中效果。这一方法适用于包含块的宽度明确的情况。

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

div {
width: 100px;
margin-left: auto;
}
margin-right 不设置的话默认是0,width 定宽之后,margin-left取值为 auto ,自动占据了剩余的全部宽度,具体见 《css权威指南》 P170
除了 width 和 margin-left 其他值都是 0 ,把宽度值带入计算得到 margin-left = 包含块的宽度-100px 。所以最终 div 会靠近包含块的右边。

``` <template> <view class="container"> <!-- 日期筛选 --> <view class="filter-box"> <picker mode="date" @change="startDateChange"> <view class="date-picker">开始日期:{{ startDate || '请选择' }}</view> </picker> <picker mode="date" @change="endDateChange"> <view class="date-picker">结束日期:{{ endDate || '请选择' }}</view> </picker> <button @tap="filterData" class="filter-btn">筛选</button> </view> <!-- 考核进度 --> <view class="progress-card"> <text class="title">个人考核进度</text> <progress :percent="progress" stroke-width="6" activeColor="#4cd964"/> <text class="progress-text">{{ progress }}%(当前阶段:{{ currentStage }})</text> </view> <!-- 选项卡切换 --> <view class="tabs"> <text :class="['tab-item', activeTab === 0 ? 'active' : '']" @tap="switchTab(0)">订单记录</text> <text :class="['tab-item', activeTab === 1 ? 'active' : '']" @tap="switchTab(1)">消费记录</text> </view> <!-- 记录列表 --> <scroll-view scroll-y class="list-container" @scrolltolower="loadMore"> <view v-if="activeTab === 0" class="list"> <view v-for="(item, index) in filteredOrders" :key="index" class="list-item"> <text class="order-no">订单号:{{ item.orderNo }}</text> <text class="order-date">{{ item.date }}</text> <text class="order-amount">金额:¥{{ item.amount }}</text> </view> <view v-if="!filteredOrders.length" class="empty-tip">暂无订单记录</view> </view> <view v-if="activeTab === 1" class="list"> <view v-for="(item, index) in filteredConsumptions" :key="index" class="list-item"> <text class="consume-type">{{ item.type }}</text> <text class="consume-date">{{ item.date }}</text> <text class="consume-amount">¥{{ item.amount }}</text> </view> <view v-if="!filteredConsumptions.length" class="empty-tip">暂无消费记录</view> </view> <view v-if="loading" class="loading">加载中...</view> </scroll-view> </view> </template> <style lang="scss"> .container { padding: 20rpx; } .filter-box { display: flex; align-items: center; margin-bottom: 20rpx; .date-picker { flex: 1; padding: 15rpx; border: 1rpx solid #eee; margin-right: 10rpx; border-radius: 8rpx; } .filter-btn { width: 150rpx; height: 70rpx; line-height: 70rpx; } } .progress-card { background: #fff; padding: 30rpx; border-radius: 16rpx; margin-bottom: 30rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.1); .title { display: block; font-size: 34rpx; margin-bottom: 20rpx; } .progress-text { display: block; margin-top: 15rpx; color: #666; } } .tabs { display: flex; border-bottom: 1rpx solid #eee; .tab-item { flex: 1; text-align: center; padding: 25rpx; color: #666; &.active { color: #007AFF; border-bottom: 4rpx solid #007AFF; } } } .list-container { height: calc(100vh - 400rpx); .list-item { padding: 30rpx; background: #fff; margin-bottom: 20rpx; border-radius: 12rpx; display: flex; justify-content: space-between; box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.05); .order-amount, .consume-amount { color: #e64340; font-weight: bold; } } .empty-tip { text-align: center; color: #999; padding: 50rpx; } .loading { text-align: center; padding: 30rpx; color: #999; } } </style>```我需要消费记录样式和订单记录样式帮我在我这个代码写一下
03-29
<template> <view class="detail-container"> <view class="navigation"> <view class="navigation_icon" @click="gotosearch"> <image class="search-icon" src="/static/搜索.png" mode=""></image> </view> </view> <!-- 导航栏 --> <view class="nav-bar"> <view v-for="(category, index) in categories" :key="index" class="nav-item" :class="{ active: currentIndex === index }" @click="selectCategory(index)"> {{ category.name }} </view> </view> <!-- 商品列表 --> <view class="product-list"> <view v-for="(product, index) in currentProducts" :key="index" class="product-item"> <image :src="product.image" class="product-image"></image> <text class="product-name">{{ product.name }}</text> </view> </view> </view> </template> <script> export default { data() { return { // 分类数据 categories: [{ name: "精选推荐", products: [{ name: "商品1", image: "/static/大商品.png" }, { name: "商品2", image: "/static/商品.png" }, { name: "商品3", image: "/static/商品.png" }, { name: "商品4", image: "/static/商品.png" }, { name: "商品5", image: "/static/商品.png" }, { name: "商品6", image: "/static/商品.png" }, { name: "商品7", image: "/static/商品.png" }, { name: "商品8", image: "/static/商品.png" }, { name: "商品9", image: "/static/商品.png" }, { name: "商品10", image: "/static/商品.png" } ] }, { name: "商务文具", products: [{ name: "商品1", image: "/static/大商品.png" }, { name: "商品2", image: "/static/商品.png" }, { name: "商品3", image: "/static/商品.png" }, { name: "商品4", image: "/static/商品.png" }, { name: "商品5", image: "/static/商品.png" }, { name: "商品6", image: "/static/商品.png" }, { name: "商品7", image: "/static/商品.png" }, { name: "商品8", image: "/static/商品.png" }, { name: "商品9", image: "/static/商品.png" }, { name: "商品10", image: "/static/商品.png" } ] }, { name: "家具生活", products: [{ name: "商品1", image: "/static/大商品.png" }, { name: "商品2", image: "/static/商品.png" }, { name: "商品3", image: "/static/商品.png" }, { name: "商品4", image: "/static/商品.png" }, { name: "商品5", image: "/static/商品.png" }, { name: "商品6", image: "/static/商品.png" }, { name: "商品7", image: "/static/商品.png" }, { name: "商品8", image: "/static/商品.png" }, { name: "商品9", image: "/static/商品.png" }, { name: "商品10", image: "/static/商品.png" } ] }, { name: "数码家电", products: [{ name: "商品1", image: "/static/大商品.png" }, { name: "商品2", image: "/static/商品.png" }, { name: "商品3", image: "/static/商品.png" }, { name: "商品4", image: "/static/商品.png" }, { name: "商品5", image: "/static/商品.png" }, { name: "商品6", image: "/static/商品.png" }, { name: "商品7", image: "/static/商品.png" }, { name: "商品8", image: "/static/商品.png" }, { name: "商品9", image: "/static/商品.png" }, { name: "商品10", image: "/static/商品.png" } ] } ], // 当前选中的分类索引 currentIndex: 0, // 当前显示的商品列表 currentProducts: [] } }, methods: { // 跳转到搜索页面 gotosearch() { uni.navigateTo({ url: "/pages/Classification/search" }); }, // 点击分类项 selectCategory(index) { this.currentIndex = index; this.currentProducts = this.categories[index].products; } } } </script> <style> .detail-container { display: flex; flex-direction: column; height: 100vh; .navigation { width: 50%; height: 50px; margin-left: 50%; /* 以上为让搜索框能点击添加样式切勿修改 */ z-index: 999; display: flex; position: fixed; top: 0; background-color: transparent; justify-content: flex-end; /* 让内容靠右 */ align-items: center; padding-right: 20rpx; /* 右侧留出一些空间 */ } .navigation_icon { display: flex; align-items: center; } .search-icon { width: 40rpx; height: 40rpx; } .nav-bar { display: flex; overflow-x: auto; white-space: nowrap; background-color: #f5f5f5; padding: 10rpx 0; position: fixed; /* 固定导航栏 */ top: 50px; /* 避免与搜索栏重叠 */ width: 100%; z-index: 998; .nav-item { margin: 0 20rpx; padding: 10rpx 20rpx; font-size: 28rpx; color: #333; &.active { color: #1aad19; font-weight: bold; } } } } .product-list { display: flex; flex-wrap: wrap; padding: 20rpx; .product-item { width: 50%; box-sizing: border-box; padding: 10rpx; .product-image { width: 100%; height: 200rpx; border-radius: 10rpx; } .product-name { text-align: center; font-size: 24rpx; color: #333; margin-top: 10rpx; } } } </style>调整上述代码让页面在一开始就默认渲染精选推荐的商品数据
最新发布
05-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值