css - Making an image width:100% inside a inline-block element -
If I have an image on the page width set to 100% in CSS, then the width of the browser is fine. However, if I do a canning divide: inline-block, then the image is no longer set to width: 100% instead, it is shown as the actual width of the image only:
img {width: 100%;}
& lt; Img src = "http://www.gannett-cdn.com/-mm-/0c9109c71ea0524d9fe840f91fabd67bb94a26a9/r=537&c=0-0-534-712/local/ - / Media / USATODAY / USATODAY / 2013/05 / 30/1369920769000-grumpycat-1305300933_3_4.jpg "/> & Lt; Div style = "display: inline-block;" & Gt; & Lt; Img src = "http://www.gannett-cdn.com/-mm-/0c9109c71ea0524d9fe840f91fabd67bb94a26a9/r=537&c=0-0-534-712/local/-/media/USATODAY/USATODAY/2013/ 05 / 30/1369920769000-grumpycat-1305300933_3_4.jpg "/> & Lt; / Div & gt; So, basically, the inline-blocking div wants to expand into its content, and width: 100 on the image. Wants to expand as a% element, so it seems that they are both confused and are only defending the width of the image. I know that I can set the width of that divis to 100% and get the desired results, but what I am actually doing is not an option. Is IMG the only way to force 100% width with CSS on the image? I think I am trying to set a square on the parent of an element basically, which I do not think is possible ... thoughts?
That's because the value of the percent is relative to the width of the box containing the box. While a block-level container ( & lt; div & gt;
element, for example) takes the full width of the block containing it, there is no inline-level element.
So obviously cover + code> & lt; Div & gt; You must specify the width of as a thumb rule, when you call 100%
, then you should ask yourself 100%
?
img {width: 100%; } Div {Display: Inline-Block; Width: 100%; }
& lt; Img src = "http://www.gannett-cdn.com/-mm - / 0c9109c71ea0524d9fe840f91fabd67bb94a26a9 / R = 537 & amp; c = 0-0-534-712 / local / - / media / USATODAY / USATODAY / 2013 / 05/30/1369920769000-grumpycat-1305300933_3_4.jpg "/> & Lt; Div & gt; & Lt; Img src = "http://www.gannett-cdn.com/-mm-/0c9109c71ea0524d9fe840f91fabd67bb94a26a9/r=537&c=0-0-534-712/local/-/media/USATODAY/USATODAY/2013/ 05 / 30/1369920769000-grumpycat-1305300933_3_4.jpg "/> & Lt; / Div & gt; Alternatively, in cases where you want to set the width of the elements in the form of the width of the viewport / window , You can use instead. For example: img {width: 100vw; } / * 1vw = 1/100 Viewout width * /
Demo:
img {width: 100vw; }
& lt; Img src = "http://www.gannett-cdn.com/-mm - / 0c9109c71ea0524d9fe840f91fabd67bb94a26a9 / R = 537 & amp; c = 0-0-534-712 / local / - / media / USATODAY / USATODAY / 2013 / 05/30/1369920769000-grumpycat-1305300933_3_4.jpg "/> & Lt; Div & gt; & Lt; Img src = "http://www.gannett-cdn.com/-mm-/0c9109c71ea0524d9fe840f91fabd67bb94a26a9/r=537&c=0-0-534-712/local/-/media/USATODAY/USATODAY/2013/5 / 30/1369920769000-grumpycat-1305300933_3_4.jpg "/> & Lt; / Div & gt;
Comments
Post a Comment