css3新发现height:100vh;

本文详细解析了CSS3中的calc()函数及视口单位vh、vw、vmax、vmin的使用方法,介绍了如何利用这些特性实现动态布局,特别关注于流体布局的宽度和高度设置。

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

vh/vw 
vh: 相对于视窗的高度, 视窗被均分为100单位的vh; 
vw: 相对于视窗的宽度, 视窗被均分为100单位的vw;

vmax: 相对于视窗的宽度或高度中较大的那个。其中最大的那个被均分为100单位的vmax; 
vmin: 相对于视窗的宽度或高度中较小的那个。其中最小的那个被均分为100单位的vmin; 
视区所指为浏览器内部的可视区域大小, 
window.innerWidth/window.innerHeight大小,不包含任务栏标题栏以及底部工具栏的浏览器区域大小。

calc 
calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,用来指定元素的长度。比如说,你可以使用calc()给元素的border、margin、pading、font-size和width等属性设置动态值。为何说是动态值呢?因为我们使用的表达式来得到的值。不过calc()最大的好处就是用在流体布局上,可以通过calc()计算得到元素的宽度。

calc是 css3提供的一个在css文件中计算值的函数:

  • 用于动态计算长度值。
  • 需要注意的是,运算符前后都需要保留一个空格,例如:width: calc(100% - 10px);
  • 任何长度值都可以使用calc()函数进行计算;
  • calc()函数支持 “+”, “-“, “*”, “/” 运算;
  • calc()函数使用标准的数学运算优先级规则;
calc(100vh - 10px)  表示整个浏览器窗口高度减去10px的大小
calc(100vw - 10px)   表示整个浏览器窗口宽度减去10px的大小
  • 1
  • 2

一般用来设置流式布局宽高,当然,你可以使用calc()给元素的border、margin、pading、font-size和width等属性设置动态值

calc()的兼容性如下,使用时需注意:

这里写图片描述

``` <!DOCTYPE html> <html> <head> <title></title> <style> body { background-color: #000; margin: 0; height: 100vh; display: flex; justify-content: center; align-items: center; } #centerCrosshair { position: absolute; width: 20px; /* 十字线宽度 */ height: 20px; /* 十字线高度 */ top: 50%; left: 50%; transform: translate(-50%, -50%); } /* 使用伪元素创建水平和垂直线 */ #centerCrosshair::before, #centerCrosshair::after { content: &#39;&#39;; position: absolute; background-color: red; } /* 水平线 */ #centerCrosshair::before { width: 100px; /* 水平线长度 */ height: 3px; /* 水平线粗细 */ left: 50%; transform: translateX(-55%); } /* 垂直线 */ #centerCrosshair::after { width: 3px; /* 垂直线粗细 */ height: 100px; /* 垂直线长度 */ top: 50%; transform: translateY(-55%); } #movableCrosshair { position: absolute; width: 0; /* 容器尺寸清零 */ height: 0; transform: translate(-50%, -50%); /* 中心点对齐 */ top: calc(50% + 20px); /* 初始位置 */ left: calc(50% + 20px); } /* 水平线 */ #movableCrosshair::before { content: &#39;&#39;; position: absolute; width: 100px; /* 十字线水平长度 */ height: 2px; /* 线粗 */ background: #00f7ff; transform: translateX(-45%); /* 向左偏移一半宽度 */ } /* 垂直线 */ #movableCrosshair::after { content: &#39;&#39;; position: absolute; width: 2px; /* 线粗 */ height: 100px; /* 十字线垂直长度 */ background: #00f7ff; transform: translateY(-45%); /* 向上偏移一半高度 */ } </style> </head> <body> <div id="centerCrosshair"></div> <div id="movableCrosshair"></div> <script> document.addEventListener(&#39;keydown&#39;, function(event){ const crosshair = document.getElementById(&#39;movableCrosshair&#39;); let style = window.getComputedStyle(crosshair); let topValue = parseInt(style.getPropertyValue(&#39;top&#39;)); let leftValue = parseInt(style.getPropertyValue(&#39;left&#39;)); switch (event.key) { case &#39;ArrowUp&#39;: crosshair.style.top = `${topValue - 1}px`; break; case &#39;ArrowDown&#39;: crosshair.style.top = `${topValue + 1}px`; break; case &#39;ArrowLeft&#39;: crosshair.style.left = `${leftValue - 1}px`; break; case &#39;ArrowRight&#39;: crosshair.style.left = `${leftValue + 1}px`; break; } }); </script> </body> </html>```给movableCrosshair十字线加上刻度,将优化后的代码直接整体输出
03-09
/* 文件路径: webapp/components/custom-form-modal/index.wxss */ .custom-form-modal { height: 80vh; border-top-left-radius: 8rpx; border-top-right-radius: 8rpx; display: flex; flex-direction: column; overflow: hidden; max-width: 100vw; box-sizing: border-box; } .modal-header { display: flex; justify-content: flex-start; align-items: center; padding: 10rpx 16rpx; /* 极小的内边距 */ border-bottom: 1rpx solid #eee; flex-shrink: 0; } .modal-header .title { font-size: 20rpx; /* 极小的标题字体 */ font-weight: bold; } .form-content { flex: 1; padding: 0 16rpx; /* 极小的内边距 */ overflow-y: auto; } .section-title { font-size: 18rpx; /* 极小字体 */ font-weight: bold; padding: 8rpx 0 4rpx; /* 极小的内边距 */ border-bottom: 2rpx solid #eee; margin-bottom: 4rpx; /* 极小的间距 */ } .form-row { display: flex; align-items: center; min-height: 40rpx; /* 极小的行高 */ padding: 4rpx 0; /* 极小的内边距 */ border-bottom: 2rpx solid #eee; flex-wrap: nowrap; } .row-title { min-width: 20%; font-size: 16rpx; /* 目标字体大小 */ color: #333; padding-right: 0rpx; /* 极小的间距 */ margin-right: 0rpx; box-sizing: border-box; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .title-text { display: inline-block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 16rpx; /* 确保字体统一 */ } .row-content { width: 100%; flex: 1; display: flex; align-items: center; justify-content: flex-start; flex-wrap: wrap; padding: 0 0 0 0; border-bottom: 1rpx solid #555555; gap: 4rpx; /* 极小的间隙 */ } /* 错误提示放在单独一行 */ .error-message { color: #ee0a24; font-size: 14rpx; /* 极小的字体 */ width: 100%; text-align: right; padding: 0 0 4rpx 0; /* 极小的内边距 */ margin: -4rpx 0 0 0; /* 负边距减小空间 */ } .time-picker, .select-picker { padding: 6rpx 0; /* 极小的内边距 */ font-size: 16rpx; /* 目标字体大小 */ text-align: left; flex: 1; min-width: 60%; } .select-container { display: flex; align-items: center; flex: 1; min-width: 70%; } .inline-button { margin-left: 4rpx; /* 极小的边距 */ flex-shrink: 0; font-size: 14rpx; /* 按钮字体更小 */ } .modal-footer { display: flex; padding: 8rpx 16rpx; /* 极小的内边距 */ gap: 12rpx; /* 极小的间距 */ border-top: 1rpx solid #eee; flex-shrink: 0; } .modal-footer van-button { flex: 1; height: 40rpx; /* 极小的按钮高度 */ line-height: 40rpx; font-size: 16rpx; /* 目标字体大小 */ } /* 加载状态样式 */ .loading-overlay { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 100; background: rgba(255, 255, 255, 0.8); padding: 12rpx; border-radius: 8rpx; font-size: 16rpx; /* 目标字体大小 */ } /* 横屏适配 */ @media (orientation: landscape) { .custom-form-modal { height: 90vh; width: 80vw; margin: 0 auto; border-radius: 8rpx; } .form-row { max-width: 80%; min-height: 40rpx; /* 横屏也保持紧凑 */ } .row-title { max-width: 80rpx; flex: 0 0 25%; font-size: 18rpx; /* 横屏稍大 */ } .section-title { font-size: 20rpx; padding: 8rpx 0 4rpx; } .title-text { font-size: 18rpx; } .time-picker, .select-picker { font-size: 18rpx; } .modal-footer van-button { height: 50rpx; line-height: 50rpx; } } /* 超紧凑模式 */ .ultra-compact .form-row { min-height: 32rpx; padding: 2rpx 5rpx; } .ultra-compact .row-title { font-size: 14rpx; } .ultra-compact .title-text { font-size: 14rpx; } .ultra-compact .row-content { gap: 2rpx; } .ultra-compact .time-picker, .ultra-compact .select-picker { padding: 4rpx 0; font-size: 14rpx; } .ultra-compact .modal-footer { padding: 4rpx 16rpx; } .ultra-compact .modal-footer van-button { height: 32rpx; line-height: 32rpx; font-size: 14rpx; } .ultra-compact .error-message { font-size: 12rpx; } /* 文件路径: webapp/components/custom-form-modal/index.wxss */ /* 添加以下样式 */ /* 自定义下拉框容器 */ .filter-picker-container { flex: 1; min-width: 70%; } /* 调整下拉框高度 */ .filter-picker { height: 30rpx; /* 极小的下拉框高度 */ } /* 输入框样式调整 */ .picker-input { height: 30rpx; /* 极小的输入高度 */ line-height: 30rpx; font-size: 14rpx; /* 极小的字体 */ padding: 4rpx 8rpx; /* 极小的内边距 */ } /* 下拉选项样式 */ .dropdown-item { padding: 6rpx 10rpx; /* 极小的内边距 */ font-size: 14rpx; /* 极小的字体 */ } /* 按钮与下拉框的对齐 */ .select-container { display: flex; align-items: center; gap: 4rpx; /* 极小的间距 */ } .inline-button { height: 30rpx; /* 与下拉框高度一致 */ line-height: 30rpx; padding: 0 8rpx; /* 极小的内边距 */ font-size: 12rpx; /* 极小的字体 */ } 优化简化去重
最新发布
08-02
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值