How to maintain proportion on max width and fixed height? - css

I am trying to make something like this. The top image is 100% width of the browser but shorter than 100%height of the browser, so the content peeks up a little bit ( the tops of the two images below are showing ).
Also, when the brower size is scaled, it maintains it's aspect ratio and the two images always show unless the responsive breaking point #media only screen and (min-width:768px) has all of the images stack on top of eachother.
Here is my css:
.thumb-12 {
width: 100%;
height: 100%;
position: relative;
}
.thumb-12 img {
width:100%;
height: 100%;
max-height:700px;
background-position:center center;
-webkit-background-size:cover;
-moz-background-size:cover;
-o-background-size:cover;
background-size:cover;
background-repeat:no-repeat;
z-index:100;
}
this works well until it gets passed 1500px or so width wise, then it starts to stretch the image.
Is this possible to do with pure css?

I added a wrapper div around the image div, and gave it overflow:hidden and the fixed height of 700px; while keeping the height of the actual image 100%;
.thumb-wrapper {
max-height: 700px;
overflow: hidden;
}
.thumb-12 {
width: 100%;
height: 100%;
position: relative;
}
.thumb-12 img {
width:100%;
height: 100%;
height: auto;
background-position:center center;
-webkit-background-size:cover;
-moz-background-size:cover;
-o-background-size:cover;
background-size:cover;
background-repeat:no-repeat;
z-index:100;
}

It makes no sense applying a background to an img tag. Apply this directly to your container (which seems to be thumb-12) and remove the img tag.
.thumb-12 {
width: 100%;
height: 700px;
background: url('your_image.jpg') center center no-repeat;
background-size: cover;
}
Please note that the parent of this element must have a determined height if you want height: 100%; to work on all browsers (except if all parents have the same property, until body).

Related

Why is my banner image collapsing in a100% div?

Tried to use slick banner lib this time around at the request of a client and my image seems to be collapsing inside of the div at 100% width and 500px height. I'm looking for it to re-scale # a height of 500px. Have looked around and seem to be doing as recommended?
http://www.cucoders.com/
.slick-slide img
display: block;
height: 500px;
width: 100%;
Was able to get image fully responsive by using the code below. When just using background-image: cover; the image is not fully responsive and collapses on it's self. The below css works.
.background-image-3 {
width: 100%;
height: 500px;
position: relative;
background-image: url("http://www.cucoders.com/images/background.png");
background-size: cover;
background-attachment: scroll;
background-position: 50% 0px;
overflow: hidden;
}

CSS: background-size cover but not allowing image to grow past its own width

If I am using background-size: cover; for a background image and that image is only 800px wide but the viewport is 1500px wide, how can I make it so that the image does not exceed its own width? (I don't want it stretched any larger than it's original size.)
header.hero {
position: relative;
clear: both;
overflow: hidden;
min-height: 390px;
background-repeat: no-repeat;
background-position: center center;
background-size:cover;
}
Add this css:
max-width: 800px;
But I don't want to add a CSS definition every time I use a different
image...
So you can try add CSS properties:
width: auto; - your image width will be scale to img height
max-width: 100%; - your image never will be wider than container wrapper

Responsive issue on background image

This is My fiddle:
Its a simple issue and i am not getting why i cant have this background image responsive..
I am using the bootstrap class : "img-responsive" to make it responsive.
After 1460px on width ,the image stops adapting to the width.
The image
id="background" class="img-responsive" lies within
The div
div id="innerWrapper"
This it the required css code:
#innerWrapper{
border: 2px solid;
float:none;
position: relative;
overflow:hidden;
width:100%;
display:block;
height: auto;
background: no-repeat scroll 0 0;
background-size: 100% 100%;
}
#background{
max-width:100% !important;
height:auto;
display:block;
}
I was using http://designmodo.com/responsive-test/ for the responsive testing.
Your problem is that .img-responsive class defines the following CSS:
.img-responsive{
display: block;
max-width: 100%;
height: auto;
}
And what this CSS means is:
Whatever my image size is, it will take all the space it has to fit its natural width (1279px) but, if it overflows its wrapper, it will fit to it.
If you want your image to always fit the size of its wrapper, you have to specify the following css:
#background{
width: 100%;
}
But that's not enought, if you want your image to keep its aspect ratio, you also have to specify the height attribute:
#background{
width: 100%;
height: 100%;
}
Tell me if it worked.

Scale Responsive Logo and Keep Ratio

I've got this logo I'm trying to use on a responsive site, but I can't figure out how to have it so it fills the full width of its parent element while maintaining its ratio in height.
When you start resizing the browser window, the logo gets smaller in width but its height doesn't scale properly. Is there a way to maintain this.
Here's my CSS for the logo element:
h1 {
width: 100%;
height: auto;
background: url(http://images.uncyclomedia.co/uncyclopedia/en/thumb/c/ce/Coca-Cola_logo.svg/800px-Coca-Cola_logo.svg.png) no-repeat top left orange;
background-size: contain;
text-indent: -999999px;
text-align: center;
margin-top: 270px;
}
This is the problem I'm having. Look at all that extra space below the
logo.
And here's a CodePen with an example of my issue:
http://codepen.io/realph/pen/LAFsi
Any help with this is appreciated. Thanks in advance!
You could use a padding trick (see CSS-square container) to do what you want with one image
h1 {
background: url(http://images.uncyclomedia.co/uncyclopedia/en/thumb/c/ce/Coca-Cola_logo.svg/800px-Coca-Cola_logo.svg.png) no-repeat top left orange;
background-size: contain;
text-indent: -999999px;
text-align: center;
position:relative;
width:100%;
height: 0;
padding-bottom: 30%;
display:block;
}
Demo

How to stretch the background image to fill a div

I want to set a background image to different divs, but my problems are:
The size of image is fixed(60px).
Varying div's size
How can I stretch the background-image to fill the whole background of the div?
#div2{
background-image:url(http://s7.static.hootsuite.com/3-0-48/images/themes/classic/streams/message-gradient.png);
height:180px;
width:200px;
border: 1px solid red;
}
Check the code here.
Add
background-size:100% 100%;
to your css underneath background-image.
You can also specify exact dimensions, i.e.:
background-size: 30px 40px;
Here: JSFiddle
You can use:
background-size: cover;
Or just use a big background image with:
background: url('../images/teaser.jpg') no-repeat center #eee;
Modern CSS3 (recommended for the future & probably the best solution)
.selector{
background-size: cover;
/* stretches background WITHOUT deformation so it would fill the background space,
it may crop the image if the image's dimensions are in different ratio,
than the element dimensions. */
}
Max. stretch without crop nor deformation (may not fill the background): background-size: contain;
Force absolute stretch (may cause deformation, but no crop): background-size: 100% 100%;
"Old" CSS "always working" way
Absolute positioning image as a first child of the (relative positioned) parent and stretching it to the parent size.
HTML
<div class="selector">
<img src="path.extension" alt="alt text">
<!-- some other content -->
</div>
Equivalent of CSS3 background-size: cover; :
To achieve this dynamically, you would have to use the opposite of contain method alternative (see below) and if you need to center the cropped image, you would need a JavaScript to do that dynamically - e.g. using jQuery:
$('.selector img').each(function(){
$(this).css({
"left": "50%",
"margin-left": "-"+( $(this).width()/2 )+"px",
"top": "50%",
"margin-top": "-"+( $(this).height()/2 )+"px"
});
});
Practical example:
Equivalent of CSS3 background-size: contain; :
This one can be a bit tricky - the dimension of your background that would overflow the parent will have CSS set to 100% the other one to auto.
Practical example:
.selector img{
position: absolute; top:0; left: 0;
width: 100%;
height: auto;
/* -- OR -- */
/* width: auto;
height: 100%; */
}
Equivalent of CSS3 background-size: 100% 100%; :
.selector img{
position: absolute; top:0; left: 0;
width: 100%;
height: 100%;
}
PS: To do the equivalents of cover/contain in the "old" way completely dynamically (so you will not have to care about overflows/ratios) you would have to use javascript to detect the ratios for you and set the dimensions as described...
For this you can use CSS3 background-size property. Write like this:
#div2{
background-image:url(http://s7.static.hootsuite.com/3-0-48/images/themes/classic/streams/message-gradient.png);
-moz-background-size:100% 100%;
-webkit-background-size:100% 100%;
background-size:100% 100%;
height:180px;
width:200px;
border: 1px solid red;
}
Check this: http://jsfiddle.net/qdzaw/1/
You can add:
#div2{
background-image:url(http://s7.static.hootsuite.com/3-0-48/images/themes/classic/streams/message-gradient.png);
background-size: 100% 100%;
height:180px;
width:200px;
border: 1px solid red;
}
You can read more about it here: css3 background-size
by using property css:
background-size: cover;
body{
margin:0;
background:url('image.png') no-repeat 50% 50% fixed;
background-size: cover;
}
Use:
background-size: 100% 100%;
To make background image to fit the div size.
To keep the aspect ratio, use background-size: 100% auto;
div {
background-image: url('image.jpg');
background-size: 100% auto;
width: 150px;
height: 300px;
}
Try something like this:
div {
background-image: url(../img/picture1.jpg);
height: 30em;
background-repeat: no-repeat;
width: 100%;
background-position: center;
}

Resources