Hey guys I am working on this responsive horizontal gallery, but now I have a dilemma.
I am trying to get 4 list items on a single shot (viewport).
The images are somehow not stretched to the full height.
Have a look and please tell me if my image size is not fine or something like that.
I need at least 4 images on a single shot.
Thanks
FIDDLE
li {
display: inline-block;
max-height: 100%;
width: 25%;
}
li img {
max-width: 100%;
}
Code looks something like this. Please check fiddle. thanks.
Something like this Link
Here's my answer:
http://jsfiddle.net/SKEL2/14/
I set all the widths to be 100% of the window (INCLUDING html and body), this might be a problem if you have other scrollable elements inside the page.
after that I just positioned the images inside the with position absolute, and gave them a height of 100% (this will stretch them to the bottom of the container, keeping the aspect ratio constant).
To keep it centered I used the
top: 50%;
transform: translateX(-50%);
trick, that will keep an absolutely positioned element in the center of the container (as long as the container has position: relative)
I hardcoded a
min-width 420px;
to avoid the white borders around the images when the window became too small.
Hope this helps
Check this fiddle: http://jsfiddle.net/SKEL2/15/
*{
height:100%;
}
body {
width:100%;
margin: 0;
padding: 0;
}
slides {
width: 100%;
overflow: auto;
margin: 0;
padding: 0;
}
ul {
width:100%;
display: inline-block;
white-space: nowrap;
margin: 0;
padding: 0;
}
li {
display: inline-block;
max-height: 100%;
width: 25%;
background-image:url('http://beautifulsoftwares.com/scroll/img/2.jpg');
background-size:cover;
background-position:50% 50%;
}
li img {
max-width: 100%;
}
background-image:url(''); - Sets an image to the element
background-size:cover; - Property to cover the entire width and height with image
background-position:50% 50% - Centers the image
Related
I'd like to fill a div with an img, keeping aspect ratio and stretching either width or height as much as required to fit in.
<div style="width: 80px; height: 80px">
<img src="..." />
</div>
How could I achieve it? If the image is not quadratic, it must be "zoomed in" and either be scropped top-bottom or left-right, depending which side is the bigger one. Moreover the image should afterwards be centered, so that the corners get cut equally.
I tried (but no effect):
.thumb {
max-width:100%;
max-height:100%;
}
If I add additional width: 100%; height:100%;, the images fit perfectly, but are resized not keeping aspect ratio.
the following did the trick:
width:100%;
height:100%;
object-fit: cover;
overflow: hidden;
Using max-width, the image will be contained inside the div, there will be no overflow.
If you use min-width instead, the shorter side will be exactly 100% of the div while the other side can be longer.
To center the image, we can use translate and relative positioning.
The following code works.
div {
overflow: hidden;
}
.thumb {
min-width: 100%;
min-height: 100%;
transform: translate(-50%, -50%);
position: relative;
left: 50%;
top: 50%;
}
To keep an image's aspect ratio, just specify one dimension:
div {
width: 80px;
height: 80px;
border: 2px solid red;
overflow: hidden;
}
img {
height: 100%;
}
This will produce the following effect:
However, as you can see, the kitten is not central, but you can use Flex box to sort this out.
div {
width: 80px;
height: 80px;
border: 2px solid red;
justify-content: center;
display: flex;
flex-direction: row;
}
img {
flex: 1;
height: 100%;
}
.thumb {
max-width:100%;
height:auto;
}
Or ( to allow scale up and down, which will look pixelated if you scale up, where the above will only scale to the max size of the image )
.thumb {
width:100%;
height:auto;
}
Is what you are looking for.
More info on responsive images:
http://demosthenes.info/blog/586/CSS-Fluid-Image-Techniques-for-Responsive-Site-Design
http://www.w3schools.com/css/css_rwd_images.asp
Not sure if anyone still looking at this post. I came across this while I was looking for a way to fit a image into a < div > without getting the unwanted white space around the image, because I was using hover & stick-out effect.
I was inspired by Matt's solution.
Instead of
.thumb {
max-width:100%;
height:auto;}
I added
.thumb {
max-width:100%;
max-height:100%;
height:auto;}
Now my images fit in to the < div > perfectly without having those white space stick out with the image.
use background-size:cover
div{
background-image:url(http://placekitten.com.s3.amazonaws.com/homepage-samples/200/140.jpg);
background-size:cover;
}
<div style="width:80px;height:80px;"></div>
I have a fixed header image as my first div, but then I obviously want to have more divs/sections underneath it to complete the webpage.
However when I try and do this, I can still only see the header image and not the div position underneath it.
Anyone know why? Here is the JSFiddle: http://jsfiddle.net/s5atv9c3/
I tried using things like:
top: 0px; //for the fixed element
margin-top: 100%; //for the sub-divs in the container
position: relative/absolute; //for the sub-divs in the container
But none of them worked :/ So yeah all help is appreciated
The way you defined your .header block, it will have a height of 100% of the screen height.
If you want .packages to appear right below .header, set the top margin of .packages to be 100%.
Since the .header is fixed, you need to set the top offset and the z-index as follows:
.header {
top: 0;
z-index: -1;
}
See demo: http://jsfiddle.net/audetwebdesign/u8bt9wda/
You can do following things.
give fixed height to header
.header {
background: url("http://i.imgur.com/GZJVpxU.jpg")
height: 400px; //fixed height
position: fixed;
width: 100%;
background-size:contain;
}
Add padding of header's height into packages
.packages{
padding-top:400px;
}
It is because you give header position:fixed; So, next div position start from top:0; So, they hide back to first fixed div.
To make div visible give top position to second div and position:relative
.packages {
padding: 40px 0;
background: #FFFFFF;
width: 100%;
position: relative;
top: 500px;
}
Check Fiddle.
http://jsfiddle.net/s5atv9c3/2/
Try this:
html, body {
height: 100%;
position: relative;
}
.wrapper {
height: 100%;
}
header {
position: relative;
height: 100%;
}
Fiddle
I added picture that is wider than viewport I lookat ,, somewhere 1000px wide ,, and I'd like it to be aligned in the middle somehow ..
The smaller picture of eye lower would need to be as is ..
I played with transform: translate(-50%,-50%); and align attributes and margin: -50% but I guess I'd need to use media queries for that ..
This is my blog:
http://blog.mk-dizajn.com/
thanks for any help ..
Just add this rules to your images and it will scale to your viewport
CSS
img {
max-width:100%;
height:auto;
}
But you should be sure that you dont have width and height attributes on your image tag
Add width = "100%" to the img tag.
<img src="http://blog.mk-dizajn.com/media/left_right_side_of_brain-1280x768.jpg" width="100%" alt="Are you right, or left :)">
see http://jsfiddle.net/9nMzK/
Try this:
<div id="image_container">
<img src="picture.png">
</div>
Then add this CSS:
#image_container {
position: absolute; /* Alternative get this relative to the parent */
left: 0;
width: 100%;
padding-left: 50%;
display: block;
float: left;
clear: both;
}
#image_container:after {
clear: both;
}
#image_container > img {
margin-left: -50%;
}
Explanation: You give the parent container a padding left of 50%. To get the image dynamically centered give the image a margin-left of -50%; This way you get it centered.
Give width to your image tag as 100%.Replace this code it will work
img {
display: block;
margin: 0 0 1rem;
border-radius: 5px;
width: 100%;
}
you need add you code css
img {
max-width:100%;
height:auto;
}
this technique will give your image resize when you change the screen size
I am trying to get a full width background or image behind floated items within a max-width container. The page will be responsive so I can't fix the height of the .item objects nor be sure how many will be shown on each row.
I'd like to have a background or image running full length of the window aligned to a position in the .item div. I can use a very long div or image offset to the left without any issue but the right side makes the browser scroll which I don't want.
.bg {
background: red;
bottom: 0;
height: 30px;
left: -1000px;
position: absolute;
width: 2000px;
z-index: 0;
}
Fiddle here: http://jsfiddle.net/K8uAh/4/
The red banner is my background, see how it runs off to the right.
Ideally I would do this just using CSS, I know if I have to go the JavaScript route it all gets a bit clunky on the window resize.
You can use the .container. If you don't want the container to extend the entire width you need to remove overflow: hidden; and add it to an additional wrapper div.
body {
width: 100%;
margin: 0;
}
.container {
overflow: hidden;
}
Hi I tried on your fiddle and altered the width and the left attribute to have percentage instead of px as if we are dealing with px then it will be hard to make it responsive.
Code:
.bg {
background: red;
bottom: 0;
height: 30px;
position: absolute;
width: 125%;
left:-16%;
z-index: 0;
}
Fiddle: http://jsfiddle.net/K8uAh/1/
You can use a clear-fix div at the end of .item.
body {
width: 100%
}
.container{
background: red; /* Change your color here */
margin: 0 auto;
width: 100%
overflow: hidden;
}
.item{
background: #999;
float: left;
margin: 10px 5%;
position: relative;
width: 40%;
}
Fiddle
First : your fiddle css is incorrect :
body {
width: 100%;
}
} /*<- extra closing braces here is ruining your layout*/
see what i mean
second : to have a full width bg use:
background: #ccc url('http://hdwallpaperia.com/wp-content/uploads/2013/11/Flower-Vintage-Background-640x400.jpg');
background-size :100% 100%;
container class should be :
.container {
background: #ccc url('http://hdwallpaperia.com/wp-content/uploads/2013/11/Flower-Vintage-Background-640x400.jpg');
background-size :100% 100%;
margin: 0 auto;
max-width: 300px;
overflow: hidden;
}
working demo
I am trying to position an div element at the bottom right of an image, that is inside a container element. I set position relative to the container, and position absolute to the inner div, but it does not work. Here is the (http://jsfiddle.net/ZC84G/). Please, help.
<div class="container">
<div class="icon"></div>
<img src="/images/someImage.png" />
</div>
CSS:
body {
background-color: black;
}
.container {
position: relative;
}
.container img {
max-width: 75%;
max-height: 80%;
}
.icon{
background-image: url('http://icons.iconarchive.com/icons/iconfactory/star-wars-lego/32/Biggs-No-Helmet-icon.png');
width: 31px;
height: 31px;
position: absolute;
bottom: 5px;
right: 5px;
}
This is because by default div has block display mode, and it's width is 100% of the parent container. Try to add display: inline to .container
.container {
position: relative;
display: inline;
}
Here's the corrected jsfiddle: http://jsfiddle.net/ZC84G/4/
Your container div has no width and height set. And since a <div> is a block-level element by default, it will be set to 100% width ie expand to however much horizontal space is left.
Plus, you're also constraining your image size:
max-width: 75%;
max-height: 80%;
If you replace the img CSS with:
max-width: 75%;
max-height: 80%;
It works fine, and as expected: http://jsfiddle.net/ZC84G/3/
I've modified your CSS on the image a bit.
Basically, I set it to scale properly to the size of its container, and now it sits where I think you wanted it. The way you could find this yourself in the future would be to inspect the element by using right click from your browser, and looking at the size of the different elements to see what was expanding larger/smaller than it should.
.container img {
max-width: 100%;
height: auto;
}