我目前正在尝试为 friend 设计一个网站,当然它是一个跨平台网站,我包含以下代码以便屏幕元素会相应地改变大小
* {
box-sizing: border-box;
}
.row::after {
content: "";
clear: both;
display: block;
}
[class*="col-"] {
float: left;
padding: 15px;
}
/* For mobile phones: */
[class*="col-"] {
width: 100%;
}
@media only screen and (min-width: 600px) {
/* For tablets: */
.col-m-1 {width: 8.33%;}
.col-m-2 {width: 16.66%;}
.col-m-3 {width: 25%;}
.col-m-4 {width: 33.33%;}
.col-m-5 {width: 41.66%;}
.col-m-6 {width: 50%;}
.col-m-7 {width: 58.33%;}
.col-m-8 {width: 66.66%;}
.col-m-9 {width: 75%;}
.col-m-10 {width: 83.33%;}
.col-m-11 {width: 91.66%;}
.col-m-12 {width: 100%;}
}
还有一个用于桌面的。
它适用于我的所有其他元素,但是当我尝试将图像放入 div 时会出现困难。图像显示但非常小,如拇指大小。最初我使用的是
max-height:100%; max-width:100%;
现在我正在使用下面的代码并且出现了同样的问题,一个拇指大小的图像 HTML
<div class="header">
<div id="container">
<img src="D:\kWebsite\images\websize\cakeA.jpg" alt="cake" />
</div>
<h1>food</h1>
</div>
<script type="text/javascript">
(function() {
var img = document.getElementById('container').firstChild;
img.onload = function() {
if(img.height > img.width) {
img.height = '1000%';
img.width = 'auto';
}
};
}());
</script>
在 CSS 中使用这个
#container {
width: 48px;
height: 48px;
}
#container img {
width: 100%;
max-width:100%;
}
I do have the viewport in my html
<meta name="viewport" content="width=device-width, initial-scale=1.0">
但没有任何作用。
提前感谢您的帮助。
最佳答案
您可以将该图像作为背景添加到 div
#container {
background-image: url("D:\kWebsite\images\websize\cakeA.jpg");
background-size: contain;
background-position: center center;
background-repeat: no-repeat;
}
或者如果你想把它放在一行中,这样做:
#container {
background: rgba(0, 0, 0, 0) url("D:\kWebsite\images\websize\cakeA.jpg") no-repeat scroll center center / contain;
}
关于html - div 中的图像不会更改大小以适应不同尺寸的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39018176/