file-type

前端面试题:px, em, rem, vw, vh 的区别解析

MD文件

下载需积分: 0 | 1KB | 更新于2024-08-03 | 156 浏览量 | 5 评论 | 0 下载量 举报 收藏
download 立即下载
在前端开发中,理解和掌握各种长度单位是至关重要的,尤其是对于网页布局和响应式设计。在面试中,面试官常常会考察候选人对px、em、rem、vw和vh等单位的理解与应用。以下是对这些单位的详细解释: ### 1. px (像素) **像素**是屏幕上的最小可寻址单元,通常用于精确控制元素大小。px 是一个绝对单位,意味着元素的大小不会随浏览器窗口或用户字体设置的变化而改变。例如,设置一个元素宽度为100px,它在任何情况下都将保持100像素宽。 ### 2. % (百分比) **百分比**是相对于父元素尺寸的相对单位。当一个元素的尺寸设置为百分比时,它的大小将取决于其父元素的尺寸。例如,一个宽度设为50%的元素,会占据其父元素宽度的一半。在CSS中,可以利用百分比实现居中对齐,如下所示: ```css .container{ width: 200px; height: 200px; position: relative; } .box{ width: 100px; height: 100px; position: absolute; left: 50%; top: 50%; margin-top: -50px; margin-left: -50px; } ``` 在这个例子中,`.box`会相对于`.container`的中心进行定位。 ### 3. em **em**单位是相对于当前元素的字体大小的单位。这意味着一个元素的尺寸可以是其内文本字体大小的倍数。例如,`text-indent: 2em`表示文本缩进为其字体大小的两倍。然而,使用em单位可能导致嵌套元素的尺寸连锁反应,使得计算变得复杂。 ### 4. rem **rem**(root em)单位与em类似,但它相对于根元素(通常是`<html>`元素)的字体大小而不是当前元素。这样,可以更方便地在整个文档中统一调整元素尺寸,特别是在实现响应式设计时。例如,通过媒体查询根据设备宽度动态设置根元素的字体大小,可以实现不同屏幕尺寸下的适配: ```css @media only screen and (max-width: 374px) { html { font-size: 86px; } } @media only screen and (min-width: 375px) and (max-width: 413px) { html { font-size: 100px; } } @media only screen and (min-width: 414px) { html { font-size: 110px; } } ``` ### 5. vw 和 vh **vw**(viewport width)和**vh**(viewport height)是基于视口尺寸的单位。1vw等于视口宽度的1%,1vh等于视口高度的1%。因此,`width: 50vw`会使元素的宽度等于视口宽度的一半。同样,`height: 50vh`则使元素的高度等于视口高度的一半。`vmin`和`vmax`分别代表视口宽度和高度中的较小值和较大值。 在实际开发中,了解并灵活运用这些单位可以帮助创建更具响应性和适应性的页面。面试时,候选人应该能够清楚地解释这些单位的区别,并提供示例来展示它们在不同场景下的应用。

相关推荐

filetype

@font-face { font-family: 'spider'; src: url(../Fonts/恐怖蜘蛛.ttf) format("truetype"); } * { box-sizing: border-box; } html { font-family: 'spider', serif; min-width: 100vw; min-height: 100vh; } body { margin: 0; background: radial-gradient(#F15F66, #b46065); } .game-title { color: bisque; text-align: center; font-size: 3em; } .game-container { display: grid; grid-template-columns: repeat(8, auto); justify-content: center; gap: 10px; perspective: 500px; } .game-info-box { grid-column: 1/-1; display: flex; justify-content: space-between; } .game-info { color: rgb(13, 233, 233); font-size: 30px; } .card { position: relative; height: 180px; width: 130px; } /* .card:hover .card-back { transform: rotateY(-180deg); } .card:hover .card-front { transform: rotateY(0deg); } */ .card-back { transform: rotateY(0deg); } .card-front { transform: rotateY(-180deg); } .card[data-turn="front"] .card-back { transform: rotateY(-180deg); } .card[data-turn="front"] .card-front { transform: rotateY(0deg); } .card-face { position: absolute; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; overflow: hidden; border-radius: 12px; border: 1px solid; font-size: 5em; backface-visibility: hidden; transition: transform 500ms ease-in-out; } .card-back { background-color: black; border-color: #FF6D00; } .card-front { background-color: antiquewhite; border-color: black; transform: rotateY(180deg); } .cobweb { width: 45px; height: 45px; position: absolute; transition: width 100ms ease-in-out, height 100ms ease-in-out; } .card-face:hover .cobweb { width: 50px; height: 50px; } .cobweb-top-left { top: 0; left: 0; transform: rotate(270deg); } .cobweb-top-right { top: 0; right: 0; } .cobweb-bottom-left { bottom: 0; left: 0; transform: rotate(180deg); } .cobweb-bottom-right { bottom: 0; right: 0; transform: rotate(90deg); } .spider { align-self: flex-start; transform: translateY(-20px); transition: transform 100ms ease-in-out; } .card-back:hover .spider { transform: translateY(0); cursor: pointer; } .card::before, .card::after { content: attr(data-value); font-size: 30px; position: absolute; } .card::before{ top: .1rem; left: .1rem; } .card::after{ bottom: .1rem; right: .1rem; transform: rotate(180deg); } .card.red{ color:red; display: flex; justify-content: center; align-items: center; } .card .love{ font-size: 4rem; } .card.black{ color:black; display: flex; justify-content: center; align-items: center; } .end{ width: 200px; margin: 50px auto; /* background-color: white; */ } .startBtn{ width: 200px; height: 50px; outline: none; text-align: center; background-color: transparent; border-radius: 10px; border: 2px solid salmon ; color: aliceblue; line-height: 50px; font-size: 20px; font-family: 'spider', serif; cursor: pointer; }这是css

filetype

请将该vue项目中的响应式布局调整为可以自适应各个打开端屏幕大小尺寸的方法:<template>
<NavBar />
<responsive-image class="hd01" :small-image="hd01.smallImage1" :medium-image="hd01.mediumImage1" :large-image="hd01.largeImage1" alt-text="Description of image" />

{{ $t('Business') }}

{{ $t('text7') }}

{{ $t("more14") }}

{{ $t('trip') }}

{{ $t('text8') }}

{{ $t("more15") }}

<responsive-image class="hd02" :small-image="hd02.smallImage2" :medium-image="hd02.mediumImage2" :large-image="hd02.largeImage2" alt-text="Description of image" />

{{ $t('Guest') }}

<RoomSwiper :bannerList="roomBannerList" />

{{ $t('Apartment') }}

<RoomSwiper :bannerList="apartBannerList" />

{{ $t('Facilities') }}

  • {{ $t('WiFi') }}
  • {{ $t('individually') }}
  • {{ $t('TV') }}
  • {{ $t('laundry') }}
  • {{ $t('safes') }}
  • {{ $t('coffee') }}
  • {{ $t('Minibar') }}
</template> <script> import RoomSwiper from "@/components/swiper/RoomSwiper.vue"; export default { name: "experience", components: { RoomSwiper, }, data() { return { msg: "Welcome to Your Vue.js App", // 响应式图片数据 hd01: { smallImage1: require("../../assets/experience/hd-01-mobile.jpg"), mediumImage1: require("../../assets/experience/hd-01.png"), largeImage1: require("../../assets/experience/hd-01.png"), }, hd02: { smallImage2: require("../../assets/experience/hd-02-mobile.jpg"), mediumImage2: require("../../assets/experience/hd-02.png"), largeImage2: require("../../assets/experience/hd-02.png"), }, roomBannerList: [ { id: 1, imgUrl: require("../../assets/room/room-01.png"), title: "豪华套房", prompt: "尊享私人空间,尽享奢华体验", }, { id: 2, imgUrl: require("../../assets/room/room-02.png"), title: "豪华楼层/豪华雅致大床房", prompt: "面积约为42平方米,装饰现代,舒适典雅。卧室和大理石浴室设气派的落地窗玻璃,可以俯瞰草原城市胜景或乌兰木伦湖滨美景。", }, { id: 3, imgUrl: require("../../assets/room/room-03.png"), title: "豪华双床房", prompt: "面积约为42平方米,装饰现代,舒适典雅。卧室和大理石浴室设气派的落地窗玻璃,可以俯瞰草原城市胜景或乌兰木伦湖滨美景。", }, ], apartBannerList: [ { id: 1, imgUrl: require("../../assets/room/room-01.png"), title: "豪华套房", prompt: "尊享私人空间,尽享奢华体验", }, { id: 2, imgUrl: require("../../assets/room/room-02.png"), title: "豪华楼层/豪华雅致大床房", prompt: "面积约为42平方米,装饰现代,舒适典雅。卧室和大理石浴室设气派的落地窗玻璃,可以俯瞰草原城市胜景或乌兰木伦湖滨美景。", }, { id: 3, imgUrl: require("../../assets/room/room-03.png"), title: "豪华双床房", prompt: "面积约为42平方米,装饰现代,舒适典雅。卧室和大理石浴室设气派的落地窗玻璃,可以俯瞰草原城市胜景或乌兰木伦湖滨美景。", }, ], }; }, }; </script> <style scoped> .content-banner { width: 100vw; height: 60rem; background-image: url("../../assets/banner/banner-04.jpg"); background-size: 100% 100%; position: relative; } .content-wrapper { max-width: 1920px; /* 限制最大宽度 */ margin: 0 auto; padding: 0 4rem; /* 添加基础内边距 */ } /* 优化商务/度假模块 */ .business, .tourism { display: grid; grid-template-columns: 1fr 1fr; gap: 6rem; align-items: center; padding: 8rem 0; max-width: 1600px; /* 限制最大内容宽度 */ } .title-en { font-size: 3rem; color: rgb(202, 171, 98); font-weight: bold; line-height: 2em; font-family: "Times New Roman", Times, serif; } .title-zh { font-size: 3.6rem; color: rgb(202, 171, 98); text-align: left; } .content { font-size: clamp(1.6rem, 2vw, 2.2rem); /* 响应式字体 */ line-height: 1.8; margin: 3rem 0; font-family: "Fangsong"; } /* 图片容器优化 */ .img-box { width: 100%; max-width: 800px; /* 限制最大宽度 */ margin: 0 auto; transition: transform 0.3s; } .img-box:hover { transform: scale(1.02); /* 添加悬停效果 */ } /* 文本区域优化 */ .text-box { max-width: 600px; margin: 0 auto; } .more { width: 10rem; font-size: 1.6rem; line-height: 2em; color: rgb(202, 171, 98); text-align: center; cursor: pointer; background-color: rgb(238, 235, 235); } /* 通用属性 */ /* 文本居中 */ .text-center { text-align: center; } /* div居中 */ .div-center { margin: 2rem auto; } /* 上外边距2rem */ .margin-top-20 { margin-top: 2rem; } /* 上外边距5rem */ .margin-top-50 { margin-top: 5rem; } /* 下外边距2rem */ .margin-bottom-20 { margin-bottom: 2rem; } /* 度假旅行 */ /* .tourism { width: 120rem; margin: 0 auto; display: flex; justify-content: space-between; margin-bottom: 10rem; } */ /* 查看客房 */ .content-kefang { width: 100vw; height: 75rem; } /* 客房设施 */ .device-content { display: grid; grid-template-columns: 1fr 1fr; gap: 8rem; max-width: 1400px; margin: 8rem auto; align-items: center; } .device-list { columns: 2; /* 增加列数 */ column-gap: 4rem; font-size: clamp(1.6rem, 1.8vw, 2rem); } .device { width: 100rem; margin: 0 auto; display: flex; flex-direction: column; align-items: center; justify-content: center; margin-bottom: 10rem; } .textEn { font-family: "Times New Roman", Times, serif !important; } /* 新增响应式断点 */ @media screen and (min-width: 1025px) { .content-banner { height: 50vh; } .business, .tourism { grid-template-columns: 55% 45%; /* 调整比例 */ } } /* 1024px以下 */ @media screen and (max-width: 1024px) { .content-banner { height: 70rem; } .business, .tourism { grid-template-columns: 1fr; gap: 4rem; padding: 4rem 0; } .content-wrapper { width: 160rem; } .title-en { font-size: 4.8rem; } .title-zh { font-size: 5.6rem; } .content { width: 74rem; font-size: 3rem; } .img-box { max-width: 100%; height: auto; } .more { width: 15rem; font-size: 2rem; } .device-content { grid-template-columns: 1fr; gap: 4rem; } /* .device-content { width: 100rem; margin: 10rem auto; display: flex; margin-bottom: 1rem; } */ .device-list { columns: 1; } .device { width: 100rem; margin: 0 auto; display: flex; flex-direction: column; align-items: center; justify-content: center; margin-bottom: 10rem; } } @media screen and (max-width: 768px) { .content-banner { height: 82rem; background-image: url("../../assets/banner/banner-04-mobile.jpg"); } .business { flex-direction: column; align-items: center; } .tourism { flex-direction: column; align-items: center; } .content-wrapper { padding: 0 2rem; } .img-box { order: 2; } .text-box { order: 1; } .title-en { font-size: 6rem; } .title-zh { font-size: 6.2rem; } .content { font-size: 1.8rem; } .img-box { width: 110rem; height: 60rem; margin-top: 5rem; margin-bottom: 2rem; } .more { width: 20rem; font-size: 2.4rem; margin-bottom: 5rem; margin-left: 80rem; } .device-content { width: 120rem; margin: 10rem auto; display: flex; margin-bottom: 1rem; flex-direction: row; margin-right: 10rem; } .device-list { font-size: 1.6rem; } .device { width: 150rem; margin: 0 auto; display: flex; flex-direction: column; align-items: center; justify-content: center; margin-bottom: 10rem; margin-right: 10rem; } } </style>

filetype

针对这个Vue项目进行响应式布局调整,主要目标是让页面在不同设备上都能自适应显示:<template>
<NavBar />
<hotel-reservation class="hotel-reservation"></hotel-reservation>

{{ $t('Family') }}

{{ $t('trip2') }}

{{ $t('text28') }}

{{ $t('more7') }}

{{ $t('Professional') }}

{{ $t('text20') }}

{{$t('more8')}}

{{ $t('progress') }}

{{ $t('text21') }}

{{ $t('more9') }}

</template> <script> export default { name: "family", data() { return { }; }, }; </script> <style scoped> .content-banner { width: 100vw; height: 62rem; margin: 0 auto; background-image: url("@/assets/wedding/flower.png"); background-size: 100% 100%; position: relative; } .hotel-reservation { width: 120rem; height: 12rem; margin: 0 auto; position: relative; top: 45rem; } .content-wrapper { width: 120rem; margin: 0 auto; } .title-box { text-align: center; margin: 6rem 0; } .content-box { display: flex; justify-content: space-between; margin-bottom: 10rem; } .title-en{ font-size: 3rem; font-weight: bold; line-height: 2em; font-family: "Times New Roman", Times, serif; } .textEn { font-family: "Times New Roman", Times, serif !important; } .banner-text { position: absolute; top: 10rem; left: 20%; padding: 1rem 24rem; font-size: 3.2rem; line-height: 2em; font-weight: bold; color: #fff; text-align: center; background-color: rgba(1, 1, 1 1.5); } .text { margin-top: 2rem; font-family: "FangSong"; font-size: 10rem; line-height: 2em; text-align: left; padding: 0 4rem; text-align: justify; -webkit-text-justify: inter-word; text-justify: inter-word; } .top1{ font-family: "Times New Roman", Times, serif; font-size: 6rem; font-weight: bold; } .title-zh { font-size: 4.6rem; /* font-weight: bold; */ color: rgb(202, 171, 98); text-align: left; } .content { width: 60rem; font-size: 2rem; line-height: 2; font-family: "Fangsong"; text-align: justify; } .img-box img { width: 50rem; height: 30rem; } .more { width: 15rem; font-size: 2rem; line-height: 2em; color: rgb(202, 171, 98); text-align: center; cursor: pointer; background-color: rgb(238, 235, 235); } .text-box{ text-align: left; } /* 通用属性 */ /* 文本居中 */ .text-center { text-align: left; } /* div居中 */ .div-center { margin: 5rem auto; text-align: center; } /* 上外边距2rem */ .margin-top-20 { margin-top: 2rem; } /* 下外边距2rem */ .margin-bottom-20 { margin-bottom: 2rem; } /* 页面修改区域 */ /* 响应式调整 */ @media screen and (max-width: 1024px) { .content-banner { height: 70rem; } .hotel-reservation { width: 160rem; top: 54rem; } .content-wrapper { width: 160rem; } .title-en { font-size: 4.8rem; } .title-zh { font-size: 5.6rem; } .content { width: 74rem; font-size: 3rem; } .img-box{ width: 54rem; height: 42rem; } .img-box img { width: 54rem; height: 42rem; } .more { width: 20rem; font-size: 2.4rem; } } @media screen and (max-width: 768px) { .content-banner { height: 82rem; background-image: url("@/assets/wedding/flower.png"); } .hotel-reservation { width: 160rem; top: 66rem; } .content-wrapper { width: 160rem; } .content-box { flex-direction: column; } .img-box { order: 2; } .img-box img { width: 100rem; height: auto; } .text-box { order: 1; } .title-en { font-size: 6rem; } .title-zh { font-size: 6.2rem; } .content { width: 160rem; font-size: 4rem; } .img-box { width: 160rem; height: 90rem; } .more { width: 30rem; font-size: 3.2rem; margin-bottom: 5rem; margin-left: 130rem; } } </style>

资源评论
用户头像
两斤香菜
2025.08.24
大厂面试常考内容,值得仔细阅读
用户头像
滚菩提哦呢
2025.07.14
深入解析前端单位区别,适合面试备考
用户头像
胡说先森
2025.05.31
内容清晰,适合巩固前端基础知识点🦔
用户头像
图像车间
2025.05.10
讲解透彻,对求职有实际帮助
用户头像
懂得越多越要学
2025.03.19
前端基础知识必备资料,推荐收藏
学习记录wanxiaowan
  • 粉丝: 2564
上传资源 快速赚钱