将图像设置为屏幕的百分比,而不是像素
make the Image a percentage of the screen and not pixels?
对于我的web开发类,我有3个图像,一个当点击图像开始比赛,其他两个将在屏幕上比赛。在我家里的个人电脑上,屏幕上的图像几乎是完美的,当赛车图像结束时,它们会浏览开始它的图像。在学校里,在屏幕上移动的图像不会超过开始它的图像,因为它更高,其他两个图像更小,因为它是像素。我怎样才能让开始比赛的图像接近一半的屏幕大小,而其他两个图像,因为它们在彼此的顶部,它们将接近一半的大小?
我有JavaScript代码但那只是让图像移动
正文图片
<body>
<div id="location" class="clickme">
<input type="image" id="start" src="death.png" alt="dstar" onclick="startRace()" class="img3">
</div>
<div class="raceTrack">
<img src="tie.png" id="1" alt="tie" class="img1">
<hr>
<img src="xwing.png" id="x-wing" alt="xwing" class="img2">
</div>
<div id="announce" class="hidden">
<span id="message">and the winner is...</span>
<img id="winner" src="1.png" alt="x-wing.png" >
</div>
</body>
样式代码
<style>
body {
background-image: url(home.png);
background-repeat: no-repeat;
background-size: 100%;
}
div.raceTrack {
position: fixed;
margin: auto;
bottom: 0px;
left: 0px;
}
div#location{
position: absolute;
top: 0px;
right: 0px;
}
img {
display: inline-block;
position: relative;
transition: all 0.2s;
}
div#announce {
position: fixed;
margin: auto;
top: 0px;
left: 0;
right: 0;
height: 10em;
width: 10em;
text-align: center;
transition: all 0.5s;
}
.hidden {
opacity: 0;
top: 0px;
bottom: 0px;
}
</style>
你应该在你的CSS中使用相对单位。
%
相对于父(或最接近的固定单元)
vh
和vw
从0到100,代表视口(窗口大小)
:
<div style="width: 500px;">
<div style="width: 50%">This will be 50% of 500px</div>
<div style="width: 50vw;">This will be 50% of window width</div>
</div>
使用vh
(视口高度)和vw
(视口宽度)也会自动调整,如果窗口的大小。
相关文章:
- CSS将百分比转换为像素
- 如何将CSS背景位置属性从百分比转换为像素
- 将 CSS 定位从百分比转换为像素
- 在 Google 评论脚本中将像素更改为百分比
- jQuery .css() 返回像素而不是百分比
- .height() 以百分比返回高度,如何以像素为单位获取它
- 将宽度从百分比转换为像素
- 适用于 IE8 的子像素舍入 JavaScript/jQuery 解决方案,用于具有百分比宽度的表格单元格
- HTML 缩放元素,其尺寸以百分比定义,就像以像素定义一样
- 如何将像素计算成百分比
- 确定 DIV 的高度以百分比而不是像素设置
- 获取基于百分比或基于像素的x&y坐标来自latlng
- 如何使元素中的文本仅在基于百分比而非像素查看页面的特定部分时显示
- jQuery .css返回以像素为单位的 css 属性,而实际上以百分比为单位
- 将像素转换为网页所有元素的百分比
- 仅在页面加载的特定百分比上进行转换/事件像素加载
- 从CSS像素值减去百分比
- 在javascript中计算表格宽度的百分比而不是像素
- 将图像设置为屏幕的百分比,而不是像素
- 如何计算像素大小作为缩放百分比