CSS Image auto width and height - css

I have a div which has image inside it. I want the image to have maximum height or width as the div but not exceed it. Fiddle - Something like this
div.gcontainer{
position: fixed;
margin: 0;
padding: 0;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
div.gcontainer img{
position:relative;
max-width:100%;
max-height:100%;
}
Not getting it. What should I do?

The problem is image's container has fixed position with width/height of 100%, so it can take the whole page. You can instead put .gcontainer in a div with fixed position and with specified dimensions:
#container {
position: fixed;
left: 0;
top: 0;
width: 400px;
height: 400px;
}
div.gcontainer {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
div.gcontainer img {
position: relative;
max-width: 100%;
max-height: 100%;
}
<div id="container">
<div class="gcontainer">
<img src="http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg" />
</div>
</div>

in your image container set width or height (one only) to auto.
div.gcontainer img
{
position:relative;
width:100%;
height:auto;
}

Related

CSS div flexible 3-row layout

I have made a JS Bin here to demonstrate what I want to achieve. I had achieved with display: flex but unfortunately it does not work well with IE11 and Opera.
I want a cross browser CSS solution such that, #row_1 has a max-height of 400px and min-height of 0.
When #row_1 has no content, #row_2 should take up all height in #container
When #row_1 has content, #row_2 should resize such that #row_1 has content and #row_2 fills the height of container.
row_1 can have a max-height: 400px, min-height: 0
row_2 can have a max-height: #container - #row_1 and min-height: 500px
<div id="header"><h1>Height 80px</h1></div>
<div id="container">
<div id="row_1">
Here can be something or nothing at all<br>
</div>
<div id="row_2">this should fill the all available height of parent div</div>
html, body {
height: 100%;
width:100%;
padding: 0;
margin: 0;
box-sizing: border-box;
}
div#header {
background: red;
height: 80px;
}
div#container {
position: relative;
bottom: 0;
left: 0;
right: 0;
}
div#row_1 {
max-height: 400px;
overflow: auto;
}
div#row_2 {
min-height: 500px;
}
Configure the style properties for the element h1.
div#header h1 {
padding: 0;
margin: 0;
}

Sidebar thumbnail image stretching

How to prevent my images from stretching on the sidebar in blogger? I've tried many different CSS for example:
.container {
max-height: 400px;
max-width: 400px;
}
The thumbnails are still stretched and I've tried the overflow:hidden; property as well, which works but cuts out too much of the image and I'd much rather them be resized.
Here's the HTML:
<div id="container">
<img src="http://2.bp.blogspot.com/-THybP4vxGMA/VpwN3LXD-jI/AAAAAAAAAcA/kpZkxwEH9P8/s1600/4afd422d987dac3041f33ffbf34f9367.jpg"/>
</div>
You have to add max-width: 100% and max-height: 100%; to the image in order to not overflow the container. You also have to change max-width and max-height in the #container to width and height. Finally, you used .container instead of #container.
#container {
height: 400px;
width: 400px;
}
#container img {
max-width: 100%;
max-height: 100%;
}
<div id="container"><img src="http://2.bp.blogspot.com/-THybP4vxGMA/VpwN3LXD-jI/AAAAAAAAAcA/kpZkxwEH9P8/s1600/4afd422d987dac3041f33ffbf34f9367.jpg"/></div>
I've figured a solution:
#container {
width: 150px;
height: 150px;
display: block;
position: relative;
overflow: hidden;
}
#container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
}

Scroll bar on the right side of the text div is not visible. I am creating a layout like the image attached

http://jsfiddle.net/P8g3C/
I am trying to create the layout above. I am not getting the scroll bar to the right side of the content.
Also, suggest if there is any alternate way which better than my current approach
My html code is
<div class="header"></div>
<div class="content">
<div class="content-left">Menu</div>
<div class="content-right">Content which should be scrollable</div>
</div>
<div class="footer"></div>
My CSS is
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
.header {
position: fixed;
width: 100%;
height: 100px;
top: 0;
left: 0;
background-color: aqua;
}
.content {
position: fixed;
top: 100px;
bottom: 35px;
left: 0;
width: 100%;
}
.content-left {
position: absolute;
top: 0;
left: 0;
width: 200px;
height:100%;
background-color: aquamarine;
}
.content-right{
position:absolute;
top:0;
left:200px;
width:100%;
height:100%;
overflow:auto;
background-color:blanchedalmond;
}
.footer {
position: fixed;
width: 100%;
height: 35px;
bottom: 0;
left: 0;
background-color: yellow;
}
You can just remove width:100% of .content-right:
Update:
Because you use absolute positiong for the .content-right we can just set the left and right for it to make the width dynamic:
.content-right{
position:absolute;
top:0;
/* add this */
right:0;
left:200px;
height:100%;
overflow:auto;
background-color:blanchedalmond;
}
Demo.
It's because you are assigning a width of 100% to .content-right, yet already occupy 200px with the menu column, hence pushing the scrollbar off.
Try this:
.content-right {
width:calc(100% -200px);
}
Alternately, you can remove the width property altogether, as #King King suggested
Here's a Fiddle of your original demo code showing the fix in action.
Please correct a width of class .content-right{ width:61%;}. because you have give a width of 100% that why you are not able to see a overflow scroll.

Image height within absolute div?

Safari for Windows is not calculating img height correctly within absolutely positioned div. The styling works fine on Chrome and Firefox.
http://jsfiddle.net/Wh2Tr/
HTML:
<div class="image">
<div class="image-inner">
<img src="http://lorempixel.com/400/200" />
</div>
</div>
CSS:
.image {
position: relative;
max-width: 100%;
height: 0;
padding-bottom: 75%;
}
.image-inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
}
.image img {
width: auto;
max-height: 100%;
margin: 0 auto;
display: block;
}
Caveats:
This is a simplification of the HTML. There are multiple images of varying sizes which need to have the same height (so using width:100%;height:auto; won't work)
This needs to be adaptive/responsive, so I can't set an explicit width or height to the image or container.
Same problem, I've used jquery. I couldn't find any solution till now:
$('.image-inner').css('height','100%').height($('.image-inner').height());
On the class image put height to auto. That should fix it. http://jsfiddle.net/Wh2Tr/1/
.image {
position: relative;
max-width: 100%;
height: auto;
padding-bottom: 75%;
}

auto height & scroll in middle div - possible?

I want:
<div style="height:100%">
<div style="height:70px;">header</div>
<div style="overflow-y:scroll;">main</div>
<div style="height:60px;">footer, alw. at bottom parent-div</div>
</div>
The real (px) container height may change dep on client window-size,
height of footer and header set in css-theme.
All positioning should be relative. Is JS required to solve this?
(Tried height:auto on main, seem to have no effect.)
You can use absolute positioning to achieve this quite easily, why should it be positioned relative?.
#header, #main, #footer {
left: 0;
right: 0;
position: absolute;
}
#header {
top: 0;
height: 70px;
background-color: yellow;
}
#main {
top: 70px;
bottom: 60px;
background-color: lime;
overflow: auto;
}
#footer {
bottom: 0;
height: 60px;
background-color: red;
}
JSFiddle: http://jsfiddle.net/Tg8g5/

Resources