Html 使用内联 css 设置图像宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16167922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Image width using inline css
提问by Rajesh Biswas
I want to set the width of Image. here is my code :-
我想设置图像的宽度。这是我的代码:-
<div style="width: 630px;">
<img src="Image/welcomeimage.jpg" alt="no image" />
</div>
Here is my situation, I don't have to set the width of Image. Only I can set the width of Div. So I have to set the image width using div. How can I do this?
这是我的情况,我不必设置图像的宽度。只有我可以设置 Div 的宽度。所以我必须使用 div 设置图像宽度。我怎样才能做到这一点?
[Edit] Thanks to all for helping me. Now I solved my problem. Initially, I was told not to use CSS, I had to solved this issue using Inline CSS. But It was not possible. So I decided to use CSS.
[编辑] 感谢所有帮助我的人。现在我解决了我的问题。最初,我被告知不要使用 CSS,我不得不使用内联 CSS 解决这个问题。但这是不可能的。所以我决定使用CSS。
采纳答案by hitautodestruct
Use max-width
用 max-width
You can use the max-width
property which is supportedin all modern browsers and IE 8 and above.
您可以使用所有现代浏览器和 IE 8 及更高版本支持的max-width
属性。
To use it call it in your css like so:
要使用它,请在您的 css 中调用它,如下所示:
div img { max-width: 100%; }
This will stretch your image to the div
's width.
这会将您的图像拉伸到div
的宽度。
回答by NielsC
Just do this if you want to set the width inline:
如果要设置内联宽度,请执行以下操作:
<div>
<img src="Image/welcomeimage.jpg" alt="no image" width="630" />
</div>
回答by Rajender Joshi
回答by Artur
Try in css file sth like this:
在 css 文件中尝试这样:
div img {
width: 630px;
}
Can you use JavaScript (for example jQuery)?
您可以使用 JavaScript(例如 jQuery)吗?
回答by Jacob
You can se the width of the image dynamically using Javascript:
您可以使用 Javascript 动态设置图像的宽度:
Pseudo:
伪:
var div = document.getElementById("yourDiv");
var width = div.style.height;
yourImg.style.width = width;
回答by user2255273
Use the following css
使用以下css
img {
width: 100%;
}