uniapp设置页面背景颜色

在uniapp中,给当前页面添加满屏背景颜色,需要给当前组件的根元素添加绝对定位,宽高百分百,然后设置背景颜色

<template>
	<view class="loginContent">
		<view class="logo">
			<image src="../../static/QTDD.jpg"></image>
		</view>

		<input type="text" v-model="email" placeholder="邮箱" />
		<input type="password" v-model="password" placeholder="密码" />
		<button class="btn">登录</button>
		<view class="alertNative">
			<view class="retrievePassword">找回密码</view>
			<view class="register">注册</view>
		</view>
	</view>
</template>
<style>
.loginContent{
position: absolute;
width: 100%;
height: 100%;
background: linear-gradient(to bottom, #ffdde1, #ee9ca7);
}
//其他不相关的CSS省略....
</style>

自己写的时候遇到的几种问题:

第一种:直接给当前组件的根元素添加背景颜色,会导致背景颜色只会出现在内容区域,不会满屏显示

<template>
	<view class="loginContent">
		//省略部分代码
	</view>
</template>
<style>
.loginContent{
background: linear-gradient(to bottom, #ffdde1, #ee9ca7);
}
//其他不相关的CSS省略....
</style>

第二种:给当前组件根元素添加height:100vh,然后添加背景颜色,这种方式虽然能达到效果,但是此时屏幕会出现滚动条,这种体验感不太好

<template>
	<view class="loginContent">
		//省略部分代码
	</view>
</template>
<style>
.loginContent{
height:100vh;
background: linear-gradient(to bottom, #ffdde1, #ee9ca7);
//其他不相关的CSS省略....
</style>
}

第三种:在当前组件中给page添加背景颜色,这种方式也能达到效果,但是组件根元素会继承背景颜色,导致背景颜色重叠了

<template>
	<view class="loginContent">
		//省略部分代码
	</view>
</template>
<style>
page{
	background: linear-gradient(to bottom, #ffdde1, #ee9ca7);	
//其他不相关的CSS省略....
}
</style>
}

posted @   QTDD  阅读(3594)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示