<navigator url="../myInfo/myOrderCenter" style="width: 150rpx;background-color: #39b54a;border-radius: 20rpx;margin-left: 240rpx;height: 50rpx;" > <view style="color: #ffffff;font-size: 28rpx;text-align: center;" >订单中心</view> </navigator>修改代码让元素上下左右都居中
时间: 2024-03-21 17:37:57 浏览: 111
可以使用 flex 布局来实现元素上下左右都居中的效果。修改代码如下:
```html
<navigator url="../myInfo/myOrderCenter" style="display: flex; justify-content: center; align-items: center; width: 150rpx; background-color: #39b54a; border-radius: 20rpx; margin: auto; height: 50rpx;">
<view style="color: #ffffff; font-size: 28rpx; text-align: center;">订单中心</view>
</navigator>
```
这里加了 `display: flex;`,并使用了 `justify-content: center;` 和 `align-items: center;` 来让元素在水平和垂直方向上都居中。同时,将 `margin-left` 改为 `margin: auto;` 也可以让元素在水平方向上居中。
相关问题
优化以下代码:<!--index.wxml--> <view class="container"> <view class="title">桥梁微检测</view> <view class="button-group"> <navigator url="/pages/regular-check/regular-check">桥梁定期检查</navigator> <navigator url="/pages/frequent-check/frequent-check">桥梁经常性检查</navigator> <navigator url="/pages/bridge-info/bridge-info">桥梁信息</navigator> <navigator url="/pages/bridge-map/bridge-map">桥梁地图</navigator> </view> </view> /* index.wxss */ .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; } .title { font-size: 28rpx; font-weight: bold; margin-bottom: 40rpx; } .button-group { display: flex; flex-direction: column; align-items: center; justify-content: center; } button { margin-top: 20rpx; width: 200rpx; height: 80rpx; font-size: 28rpx; background-color: #007aff; color: #fff; border-radius: 10rpx; } /* app.json */ { "pages": [ "pages/index/index", "pages/regular-check/regular-check", "pages/frequent-check/frequent-check", "pages/bridge-info/bridge-info", "pages/bridge-map/bridge-map" ], "window": { "navigationBarTitleText": "桥梁微检测" } }
以下是对代码的优化:
<!--index.wxml-->
<view class="container">
<view class="title">桥梁微检测</view>
<view class="button-group">
<navigator url="/pages/regular-check/regular-check">桥梁定期检查</navigator>
<navigator url="/pages/frequent-check/frequent-check">桥梁经常性检查</navigator>
<navigator url="/pages/bridge-info/bridge-info">桥梁信息</navigator>
<navigator url="/pages/bridge-map/bridge-map">桥梁地图</navigator>
</view>
</view>
/* index.wxss */
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
}
.title {
font-size: 28rpx;
font-weight: bold;
margin-bottom: 40rpx;
}
.button-group {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
button {
margin-top: 20rpx;
width: 200rpx;
height: 80rpx;
font-size: 28rpx;
background-color: #007aff;
color: #fff;
border-radius: 10rpx;
}
/* app.json */
{
"pages": [
"pages/index/index",
"pages/regular-check/regular-check",
"pages/frequent-check/frequent-check",
"pages/bridge-info/bridge-info",
"pages/bridge-map/bridge-map"
],
"window": {
"navigationBarTitleText": "桥梁微检测",
"navigationStyle": "custom"
}
}
优化说明:
1. 去除无用的空格和换行符,使代码更加简洁。
2. 对于不需要使用的样式,可以直接省略,避免代码过于冗余。
3. 将`button`标签改为`navigator`标签,因为在小程序中,`button`标签是需要绑定事件的,而在这里,我们只需要跳转页面,所以使用`navigator`标签更加合适。
4. 在`app.json`中添加了`navigationStyle`字段,用于设置导航栏样式为自定义,这样我们就可以在每个页面单独定义导航栏的样式,提高了页面的灵活性。
<view class="user_info_wrap"> <view class="user_img_wrap" wx:if="{{userInfo.avatarUrl}}"> <image class="user_bg" src="{{userInfo.avatarUrl}}"></image> <view class="user_info"> <image class="user_icon" src="{{userInfo.avatarUrl}}"></image> <view class="user_name">{{userInfo.nickName}}</view> </view> </view> <view class="user_btn" wx:else> <navigator url="/pages/login/login">授权登录</navigator> </view> </view> <view class="user_content"> <view class="user_main"> <view class="recommend_wrap" bindtap="logOut" wx:if="{{userInfo.avatarUrl}}"> 退出登录 </view> </view> <view class="address-box"> <view class="address-manage"> <navigator url="/pages/address/address">地址管理</navigator> </view> <view wx:if="{{hasAddress}}" class="address-list"> <view>{{address.name}}</view> <view>{{address.phone}}</view> <view>{{address.detail}}</view> </view> </view> <view class="orders-box"> <navigator url="/pages/order/order" class="orders">我的订单</navigator> </view> </view>帮我写一个wxss美化这个界面
以下是一个简单的样式美化,你可以根据自己的需求进行调整:
.user_info_wrap {
background-color: #fff;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
border-radius: 6px;
padding: 20rpx;
margin-bottom: 20rpx;
}
.user_img_wrap {
display: flex;
align-items: center;
margin-bottom: 20rpx;
}
.user_bg {
width: 60rpx;
height: 60rpx;
margin-right: 20rpx;
border-radius: 50%;
background-color: #eee;
}
.user_info {
display: flex;
align-items: center;
}
.user_icon {
width: 40rpx;
height: 40rpx;
border-radius: 50%;
margin-right: 10rpx;
}
.user_name {
font-size: 28rpx;
color: #333;
}
.user_btn {
display: flex;
align-items: center;
justify-content: center;
height: 60rpx;
border-radius: 6px;
background-color: #007aff;
color: #fff;
font-size: 28rpx;
}
.user_content {
background-color: #fff;
border-radius: 6px;
padding: 20rpx;
}
.recommend_wrap {
display: flex;
align-items: center;
justify-content: center;
height: 60rpx;
border-radius: 6px;
background-color: #007aff;
color: #fff;
font-size: 28rpx;
}
.address-box {
margin-top: 20rpx;
}
.address-manage {
display: flex;
align-items: center;
justify-content: center;
height: 60rpx;
border-radius: 6px;
background-color: #eee;
font-size: 28rpx;
}
.address-list {
margin-top: 20rpx;
padding: 20rpx;
background-color: #f5f5f5;
border-radius: 6px;
font-size: 28rpx;
}
.orders-box {
margin-top: 20rpx;
}
.orders {
display: flex;
align-items: center;
justify-content: center;
height: 60rpx;
border-radius: 6px;
background-color: #eee;
font-size: 28rpx;
}
阅读全文