css image max-width not working with Firefox / IE - css

Here's a JsFiddle.
HTML :
<meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1, maximum-scale=1">
<div data-role="page" >
<div id="contentwrap">
<div id="content" data-role="content">
<img width="300" src="http://upload.wikimedia.org/wikipedia/commons/f/f1/Ski_trail_rating_symbol_red_circle.png" />
asdad asd asd asd sadadada ad sad asd asd asd asd sadasdaad adsa dasd sa
</div>
</div>
</div>
CSS :
html, body {
height: 100%;
}
#contentwrap {
display: table;
height: 100%;
max-width: 500px;
margin: 0 auto;
position: relative;
}
#contentwrap img {
margin-left: auto;
margin-right: auto;
display:block;
margin-bottom: 10px;
max-width:100%;
}
#content {
height: 100%;
display: table-cell;
text-align:center;
vertical-align:middle;
}
As you can see if you test it, the "max-width: 100%" attribute only works on Google Chrome. With Firefox and IE, the image width stay the same... With Chrome, the image adapt to the window... :
How can I fix it ? (at least with IE11)
I found other posts with the same problem but none gave a good solution...

There's actually a really simple solution to this -- you need to set the table-layout property to fixed on the element that is set to display: table.
#contentwrap {
display: table;
table-layout: fixed;
height: 100%;
max-width: 500px;
margin: 0 auto;
position: relative;
}

Here is one way of achieving the desired layout.
I left out some of the jQuery Mobile classes and just used native CSS/CSS3.
For this HTML:
<div id="contentwrap">
<div id="content">
<img width="300" src="http://upload.wikimedia.org/wikipedia/commons/f/f1/Ski_trail_rating_symbol_red_circle.png" />
asdad asd asd asd sadadada ad sad asd asd asd asd sadasdaad adsa dasd sa
</div>
</div>
modify your CSS as follows:
html, body {
height: 100%;
}
#contentwrap {
background-color: beige;
margin: 0 auto;
max-width: 500px;
height: 100%;
display: table;
}
#content {
display: table-cell;
vertical-align: middle;
text-align: center;
}
#content img {
display: block;
margin: 0 auto;
width: 100%;
max-width: 300px;
}
I applied CSS tables to #contentwrap and #content similarly to your original CSS, that way you get the vertical alignment in the middle as required.
You hard coded the image width to 300px by setting the width value in the img tag.
To get the image to scale with the width of #content as it narrows in width, set the width: 100% that way the image will fill the width of #content, and to prevent the image from getting too wide, set a max-width value as needed, 300px in my exammple.
You may run into conflicts with CSS rules in jQuery Mobile, but perhaps someone else can help with any issues is they arise (not my area of expertise).
See demo: http://jsfiddle.net/audetwebdesign/ZMLDD/
Note: If you set the max-width value for the image, then you may not need to set the width attribute in the img tag.
I checked this in the latest versions of Firefox, Chrome, IE and Opera and this solution appears to work.

Always remember, max-width does not inherit from other parent
elements. As the width of 300px has been defined with a max-width of
100%, the initial width value will always override the max-width value
of 100%
So instead use min-width: 300px and max-width: 100% which will make it to work in all the browsers

Responsive images for Firefox, IE, Chrome. Simple solution that works in Firefox
<div class="article"><img></div>
.article {background: transparent 0% 0% / 100% auto;}
.article img {max-width: 100%;}

Related

Flex-grow in IE11 doesn't vertically stretch

I thought that IE 11 had full support for flexbox properties but I get a different behavior than on Chrome/Firefox
I simplified it to this simple example: I'm trying to have a 100% height div with a flex child inside that also grows to 100% height. It works in chrome and Firefox but on IE the flex child doesn't grow in height...
Fiddle: https://jsfiddle.net/7qgbkj0o/
body, html {
background-color: red;
min-height: 100%;
height: 100%;
padding: 0;
margin:0;
}
.p{
display: flex;
min-height: 100%;
}
.c1 {
flex-grow: 1;
background-color: gray;
}
<div class="p">
<div class="c1">
asdasd
</div>
</div>
On IE11: http://imgur.com/a/eNKIJ
On Chrome: http://imgur.com/a/xYmJW
I know there are probably alternatives to achieve this without using flexbox but in my real world case I really need to use flexbox here so what's wrong and how can I achieve this on IE11 using flexbox?
Seems IE11 has an issue with only having min-height.
Try adding a base height.
.p{
display: flex;
min-height:100%;
height: 100%;
}
body,
html {
background-color: red;
min-height: 100%;
height: 100%;
padding: 0;
margin: 0;
}
.p {
display: flex;
min-height: 100%;
height: 100%;
}
.c1 {
flex: 1;
background-color: gray;
}
<div class="p">
<div class="c1">
asdasd
</div>
</div>
I had a similar case where this:
.container {min-height: 500px; height: auto;}
didn't work, but this:
.container {height: 500px;}
was perfectly aligned in IE.
Declaring specific height instead of auto should work..

Why is a max-width / height img not constrained by the parent percentage height / width in Firefox?

I have a set of absolutely positioned fluid divs within a container and want to display an image within each div that is vertically and horizontally centred within the container and fills up as much of the available space as possible. Due to using these images for other purposes they have to be img tags and not background images (otherwise with CSS3 this would be easy!)
I would have thought the following code should do just this but for some reason on Firefox the image displays in it's original dimensions and is not constrained by the parent dimensions. In Chrome the width seems to be correctly linked to the container however the img height is not constrained by the container height.
I could understand it if there was no width/height set on the parent but every element in this example has a percentage width/height set so i don't think this is the problem. FYI if you set a specific width:100% on the img then this constrains the width correctly (but can't be done as it means it's loses the correct aspect) however it still doesn't work for height even if you set the height to 100%,
You can see a jsfiddle at http://jsfiddle.net/deshg/xrzk084d/ and the code is below.
If anyone could point me in the right direction as to what i'm doing wrong that would be greatly appreciated!
body, html, #outer {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#container {
background-color: #ffcc00;
display: table;
position: absolute;
left: 20%;
top: 30%;
width: 60%;
height: 40%;
}
#containerinner {
display: table-cell;
width: 100%;
height: 100%;
vertical-align: middle;
}
#containerinner img {
max-width: 100%;
max-height: 100%;
margin: 0 auto;
display: block;
}
<div id="outer">
<div id="container">
<div id="containerinner">
<img src="https://dl.dropboxusercontent.com/s/wypn5e7n5bgeoic/landscape.png?dl=0" alt="">
</div>
</div>
</div>
Cheers,
Dave
The issue here is that set a vertical alignment of middle is impossible without specifying the height of the containing element. In your situation, you want the height to be relative to the viewport which creates additional difficulty.
However, if we use both the vh (viewport height) and vw (viewport width) units defined in CSS3 we can achieve what you're after. I have then assumed that you want your image to be center aligned and with a max width and height of 60% and 40% of the viewport respective. I have reduced the markup to the following (See JSFiddle):
#container {
position: fixed;
top: 30vh;
left: 20vw;
background-color: #ffcc00;
}
#inner {
display: table-cell;
vertical-align: middle;
text-align: center;
height: 40vh;
width: 60vw;
}
img {
display: block;
max-width: 60vw;
max-height: 40vh;
margin: 0 auto;
}
<div id="container">
<div id="inner">
<img src="https://dl.dropboxusercontent.com/s/wypn5e7n5bgeoic/landscape.png?dl=0">
</div>
</div>

CSS - image in display:table-cell expands beyond parent

I'm trying to size an image in a table-cell to be no larger than the height/width of the table-cell, but it's not working.
.wrapper {
position: fixed;
width: 100%;
height: 100%;
background: rgba(0,0,0,.2);
z-index: 8;
}
.container {
position: fixed;
left: 5%;
top: 5%;
width: 90%;
height: 90%;
}
.table {
display: table;
width: 100%;
height: 100%;
table-layout: fixed;
background: #eee;
}
.table .cell {
display: table-cell;
}
.table .cell.left {
width: 240px;
background: #fff;
}
.table .cell img {
max-width: 100%;
max-height: 100%;
}
As you can see on the screen shot, I have two cells, one on the left which is white and one on the right which contains the image. The table is in the background in #eee, and is the proper height/width (it fits to .container), but the cells overflow the table height when the image is larger than its parent cell, and setting max-height and max-width for the image has no effect - it just sizes to its original size.
HTML
<div class="wrapper">
<div class="container">
<div class="table">
<div class="cell left">
//white
</div>
<div class="cell">
<img src="" /.
</div>
</div>
</div>
you can't set height as a %, any rules set like this will be ignored and the pictures original height will be used.
Those are just two guesses:
Try to add rows with "display: table-row"? You can then apply row height and max-height either by em/px or % each row.
Also try using "overflow: hidden;" or "overflow: none" at your rows and cells.
Well, the best I can do for now is change it to a background image and use background-size: contain;, which is an imperfect solution but more simplistic than using javascript. I can't find another work-around.

Div won't stretch the full height of the page (yup, I've Googled plenty)

I'm trying to stretch the content of a div the height of the page. I've Googled the problem and so far nothing works. The whole thing is starting to give me a headache. Perhaps someone more experienced could take a look at my code? The full stylesheet is >400 lines, so I'm including what is (hopefully) relevant.
"Wrapper" takes up 100% of the page height, whereas "contentShadow" stretches only to the height of the text in the div "content".
Edit: as far as I can tell, every container has its height set to 100%, which whould make "contentShadow" 100% as well. Right...?
Edit 2: I'm starting to see the problem, but don't know how to solve it. While the following code will keep the footer down, it also means that since .wrapper doesn't have height:100%, "contentShadow" will not stretch. The question then is how I keep my footer down while changing this code:
.wrapper {
min-height: 100%;
height: auto !important;
margin: 0 auto -37px;
}
To this:
.wrapper {
height: 100%;
}
Basic structure of the page:
<div id="body">
<div id="headerWrapper"></div>
<div id="wrapper">
<div id="contentShadow">
<div id="#contentWrapper">
<div id="content">
<!-- contentshadow stretches the height of this content and no further, but SHOULD stretch the height of the entire page -->
</div>
</div>
</div>
<div id="push"></div>
</div>
<div id="footer"></div>
Css rules relevant to these divs:
html, body {
height: 100%;
}
#headerWrapper {
height: 314px;
width: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -37px;
}
#contentShadow {
min-height: 100%;
width: 994px;
background-image: url(../images/contentShadow.png);
background-repeat: repeat-y;
margin-right: auto;
margin-left: auto;
}
#contentWrapper {
min-height: 100%;
width: 940px;
margin-right: auto;
margin-left: auto;
padding-right: 16px;
padding-bottom: 16px;
padding-left: 16px;
padding-top: 17px;
background-color: #EDECEC;
overflow: hidden;
}
#content {
min-height: 100%;
}
.footer, .push, {
height: 37px;
}
.footer {
background: white;
clear: both;
height: 37px;
}
You have really wrong code:
.wrapper matched <div class="wrapper"> not <div id="wrapper">.
<div id="#contentWrapper"> is not correct, you should try <div id="contentWrapper">
height: auto; is the problem. The wrapper needs to be 100% height, not auto...
the height: 100% after height: auto !important doesn't make sens, because of the !important keyword.
Maybe it's the default margins and padding, have you tried this?
body {margin: 0px; padding: 0px; }
I had this issue for the better part of my life, but I just solved it for myself, so I'm sharing, just in case somebody else can benefit.
My HTML/BODY selector is set to height:100%.
My container div within the HTML/BODY selector is set to min-height:800px.
My CONTENT div inside of the CONTAINER div didn't have a height, and I had the issue of the div not stretching to the bottom of the page. When I inspected this div, I noticed that for some reason, it was stretching way below its container div, pushing it up and creating that annoying space at the bottom of the page. Once I placed a height on that inside DIV, the issue went away for me.
I hope this helps.
The contentShadow must have overflow: auto. Try this
body, html { height: 100%; margin: 0; padding: 0; }
#container { width: 100%; height: 100%; overflow: auto; display: block; }
<body>
<div id="container">
This should fill the page!
</div>
</body>

CSS horizontal centering, and min-height not working in IE

<div id="wrapper">
<div id="content" style="background-color: white;">
...
</div>
</div>
#content{
float: left;
width: 540px;
padding: 30px 30px 0px 30px;
background-color:#19252f;
text-align: left;
min-height: 500px;
}
#wrapper {
margin: 0 auto;
width: 800px;
}
Firefox look
Internet Explorer 8 look
margin:auto horizontal centering is not supported in IE6. Instead you can either use absolute positioning to center or you can do
body{
text-align:center;
}
For IE min-height you need to add the hacked property _height: 300px in addition to min-height: 300px;
All browsers but IE will ignore the hacked property, and since IE effectively treats height as min-height, you’ll get the effect that you want in all browsers.
background-color works in IE without any problems.

Resources