set image size with respect to the screen resolution

狂风中的少年 提交于 2019-12-21 17:48:58

问题


I am trying to build a website for myself.. I want to use an image as the header... I have made sure that my background will scale itself for any screen resolution... How do i make sure the image(header) will also scale it self according to the screen resolution...

for example: my image is 350px int width and 130px in height.. i want this to be the size when screen resolution is 1280X768.. and should change proportionally based on screen size..

Please tell me how to do this preferably using CSS.. i am also fine with js or jquery

Thanks in advance Raj


回答1:


If you only specify the width in css, the height will scale automatically. You can easily specify the with of the image relative to its parent.

In your example you would have to specify width: 27% (1280 / 350). Note that the parent will have to be 100% wide.




回答2:


You can get the screen size from the DOM using screen.width and screen.height. Then you can write some JS to load or replace with the desired image or adjust the height/width attributes on the image (which would scale it, but doesn't always look great).




回答3:


There is a good article on fluid grids by Ethan Marcotte, http://www.alistapart.com/articles/fluidgrids/

You can apply these principals to your images using CSS, he does on his personal site. There even good apps to help you out.

this one has a read me -> web duck http://inteldesigner.com/web-duck/about.php

this one has more features -> em calc http://riddle.pl/emcalc/



来源:https://stackoverflow.com/questions/2330889/set-image-size-with-respect-to-the-screen-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!