Html 在CSS3中,如何让图像自动拉伸以适应屏幕?
我试图得到一个垂直的图像拉伸,自动适合任何屏幕的顶部和底部。在CSS3中如何实现这一点 以下是我所拥有的,如果它有帮助的话:Html 在CSS3中,如何让图像自动拉伸以适应屏幕?,html,css,Html,Css,我试图得到一个垂直的图像拉伸,自动适合任何屏幕的顶部和底部。在CSS3中如何实现这一点 以下是我所拥有的,如果它有帮助的话: .logo{ height: 700px; width: 150px; } 它也不是背景图像 .logo{ height: 100vh; } 您可能还希望将宽度设置为auto以保持比例 ()试试这个: <!DOCTYPE html> <html> <head> <style> bo
.logo{
height: 700px;
width: 150px;
}
它也不是背景图像
.logo{
height: 100vh;
}
您可能还希望将宽度设置为auto
以保持比例
()试试这个:
<!DOCTYPE html>
<html>
<head>
<style>
body, html {
height: 100%;
margin: 0;
padding: 0;
}
img {
height: 100%;
width: auto;
}
</style>
</head>
<body>
<img src="https://www.google.com/images/srpr/logo11w.png"/>
</body>
</html>
正文,html{
身高:100%;
保证金:0;
填充:0;
}
img{
身高:100%;
宽度:自动;
}
最好的方法是将其设置为高度和宽度设置为100%的div背景,然后为其设置一个特殊的背景,这样它就不会失真。也许这就是你需要的
/*
This is needed for 100% height of the div as the percentage refers to the parent element.
*/
html, body {
height: 100%;
}
/*
This is the actual image
*/
.image {
background: #000 url(https://www.google.com/images/srpr/logo11w.png) repeat-x;
background-position: 50% 50%;
background-size: cover;
width: 100%;
height: 100%;
}
<body>
<div class="image"></div>
</body>
/*
这对于div的100%高度是必需的,因为百分比指的是父元素。
*/
html,正文{
身高:100%;
}
/*
这是真实的图像
*/
.形象{
背景:#000 url(https://www.google.com/images/srpr/logo11w.png)重复-x;
背景位置:50%50%;
背景尺寸:封面;
宽度:100%;
身高:100%;
}
如果它不是背景图像,并且无法制作成背景图像,那么您可能运气不好。宽度100%或高度100%会这样做吗?如果我理解你的问题。@Wayne…前提是父容器有高度。或者您可以使用javascript获取window.height
并将图像高度设置为该高度。您是否同意使用javascript/jQuery来完成此操作?添加html,正文{width:100%;height:100%;}
,然后添加width:100%;身高:100%
给您希望成为100%的子对象。IE8和更早版本中无法使用视口单位,因此此答案可能不适合所有遇到此问题的人。是的,但OP似乎特别要求CSS3解决方案。