I am currently trying to modify a wordpress theme that has a full screen image as the homepage. Right now it scales perfectly fine with the browser, the only problem is it doesn't go smaller then the original image size. It will go larger and keep its aspect ratio but when you size down the window it doesn't go smaller then the default image size.
Here is the CSS in the theme for this part:
/* full screen slides */
.slides.full {
position: absolute;
top: 0;
left: 0;
background: #373432;
width: 100%;
height: 100%;
max-height: 100%;
text-align: center;
color: #fff;
text-shadow: 1px 1px 2px rgba(0,0,0,0.2);
}
.slides.full img {
min-height: 100%;
min-width: 100%;
width: auto;
position: absolute;
top: 0;
left: 0;
}
I used another way that made the image go smaller then its original size but it didn't stick to the height of the browser there was just a grey background underneath.
Is someone able to help me change the css I posted to do what I am trying to achieve? It would be much appreciated! Thanks.
Have you tried adding max-widht: 100% and max-height: 100% as well to .slides.full img?
It's just a wild guess. It'd be helpful to see a live example.
this will keep the right proportion for the img
.slides.full img {
height: auto;
width: auto;
max-width: 100%;
max-height: 100%;
position: absolute;
top: 0;
left: 0;
}
and changing to height:100% will stretch the img when resized
.slides.full img {
max-width:100%; /* lower virsion of IE won't work width max-width */
width:100%; /* for IE */
}
Related
OK, this is a bit of a mouthful and very super specific. I will try my best to explain!
The goal is to maintain aspect ratio while scaling an image and keeping it vertically and horizontally centred inside a DIV that is defined only by percentages. The image needs to maintain best fit, so if max width is required then it's used and vice versa.
Use Firefox version 33 (or a few earlier versions) to view this js fiddle to see it working properly:
http://jsfiddle.net/3vr9v2fL/1/
HTML:
<div id="imageviewer" >
<div class="dummy"></div>
<div class="img-container centerer" id="imagevieweroriginal">
<img class="centered" src="http://chrisnuzzaco.com/couch/uploads/image/gallery/smiling_woman_wearing_drivers_cap.jpg" alt="Doctor Concentrating on Work"></img>
</div>
</div>
</div>
CSS:
#imagewrapper{
position:absolute;
width:69%;
height:100%;
top:0px;
bottom:0px;
background-color:gray;
}
#imageviewer{
position:relative;
width:100%;
height:100%;
}
.responsive-container {
position: relative;
width: 100%;
border: 1px solid black;
}
.dummy {
padding-top: 100%; /* forces 1:1 aspect ratio */
}
.img-container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.centerer {
text-align:center; /* Align center inline elements */
font: 0/0 a; /* Hide the characters like spaces */
}
.centerer:before {
content: ' ';
display: inline-block;
vertical-align: middle;
height: 100%;
}
.centered {
vertical-align: middle;
display: inline-block;
max-height: 100%;
max-width: 100%;
}
The Problem:
I originally found my code here on stackoverflow and made a simple mod adding max-height/width to the .centered class. At the time, this worked in all major browsers. The only exception being Opera.
Vertically align an image inside a div with responsive height
There is a big problem however: the latest version of Chrome (Version 38.0.2125.111) no longer works with this code and my users prefer chrome to other browsers by a large margin.
Any ideas on how to solve this? Is this a bug with Chrome? I'm open to javascript suggestions to make this work again.
I came up with this: JSFiddle - centered image keeps aspect ratio in resizable fluid container
.container {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
}
.image {
position: absolute;
max-width: 100%;
max-height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
body {
width: 100%;
height: 100%;
position: absolute;
margin: 0;
}
<div class='container'>
<img class='image' src='http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/sample1_l.jpg'>
</div>
The image stays centered both horizontally and vertically. If the window is scaled down the image shrinks respecting original aspect ratio.
I didn't test it on all browsers though.
Take a look at CSS object-fit property:
You may need a polyfill for older browsers, though.
View browser support for object-fit.
I am working on a website using Nivo Slider here: Website Demo
The problem is, the slider section won't do 100% height following the browser's height. How can I do that?
I have been inspecting and found this css code:
.nivoSlider {
position:relative;
width:100%;
height:auto;
overflow: hidden;
left:0;
margin-top:100px;
}
According to my CSS knowledge, we can change the height:auto; to height:100% to make the slider 100% height. Alas, it does not work!
What am I missing here?
UPDATE
I found that the image of the sliders is taking over the size, which is written here:
.nivoSlider img {
position: absolute;
top: 0px;
left: 0px;
max-width: none;
max-height: 100%; // I ADDED THIS TO MAKE 100% HEIGHT AND IT WORKS!!!
}
nevertheless, the image is being stretched now, how can I make the image cut, instead of stretched?
Look at the following class. You have assigned 900px height.
.nivoSlider:after {
content: '';
background: transparent url(../../images/parallax_slider/pattern.png) repeat top left;
position: absolute;
height: 900px;
left: 0;
top: 0;
width: 100%;
z-index: 5;
}
This is the website I am modifying: sb460training.org
Here is the code snippet:
#apdiv1 {
position: absolute;
width: 2815px;
height: 276px;
z-index: 1;
top: 1px;
left: 0px;
background-color: #000;
}
#apdiv2 {
position: absolute;
width: 3150px;
height: 115px;
z-index: 2;
left: 0px;
top: 230px;
}
#apdiv3 {
position: absolute;
width: 221px;
height: 411px;
z-index: 3;
left: 0px;
top: 259px;
background-color: #FFF;
}
#apdiv4{
position: absolute;
width: 2853px;
height: 115px;
z-index: 4;
left: 219px;
top: 401px;
}
Do you know what the width dimensions should be so I can get rid of the annoyingly extra space that shows up to the right of the web page?
Thanks
Like the other answers, I agree that your CSS should change the fixed widths to 100%.
However, in your HTML you have img elements with explicit widths, to substitute background colours. For example, in the "apDiv2" DIV element, you have an in-line image containing white, "SB460_Pic/Secondary title2.jpg". This image is set to 2128px wide, causing the page to extend horizontally.
I would recommend removing the images that are being used to pad the right of each DIV, and instead set background colours in CSS.
UPDATE
Quick and dirty example:
http://pastebin.com/4PmZN1r4
change all your container widths to 100%.
give your html a width:100%; margin:0;
give your body a fixed width:1200px or so.
set your body with a margin: 0 auto if you want it centered.
I've heard the same similar issue.
all you need to do is try working with margin set to 0 and auto.
in most cases, try eliminating the use of 'position absolute' and work more with margin, padding and position relative.
I'm not finding any information on this. Might be because my approach is incorrect.
I'm having my image positioned absolutely under a div which uses full window width.
I'm using margin-left: 30%, so that images are always positioned 30% from the left border.
Everything else is set for responsive image handling..
I got the code correct that it scales like I want if the browser-window gets resized.
But because the image is not centered, the scaling happens "too late", so the right part will be hidden outside view.
Can I solve this with the scaling starting "earlier"?
Or using sth different than margin-left: 30% or left: 30% ?
see here: JsFiddle - Image out of view when resizing window
img.aaa
{
position: absolute;
max-width: 85%;
max-height: 85%;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin-top: auto;
margin-right: auto;
margin-bottom: auto;
margin-left: 30%;
}
You can use margin-left: auto which will center the image. If it has to be a certain distance from the left, use a container div and the following css. This will prevent the image from going outside the body and getting cut off.
.container {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
padding-left: 30%;
//text-align: center; //remove padding and uncomment this to center image in div
}
img.aaa {
max-width: 100%;
max-height: 85%;
box-shadow: 0 5px 10px rgba(0,0,0,0.8);
}
DEMO
I am trying to get into responsive design/layout with Bootstrap and CSS, but I am kind of confused of how could a change a box to be in the center of the screen.
I have a login pane that in Google Chrome has size 277x256 (that size could fit many smartphone screens). So I made a CSS like that:
.login .pane {
position: absolute;
top: 50%;
left: 50%;
margin: -128px -138.5px; /* half of the size in Google Chrome */
background: #f0f0fd;
background: rgba(240,240,253,0.90);
padding: 22px 32px;
}
You can see the complete code in: http://jsfiddle.net/H5Qrh/1/
=== UPDATE ===
I made a cleaner code and tried using Absolute Centering instead of Negative Margins:
.center-pane {
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
max-width: 277px;
height: 320px;
}
My updated Fiddle: http://jsfiddle.net/H5Qrh/3/
Now the footer is above the box.. that shouldn't occour.
You're using absolute but I'd change that to fixed (this will work on both).
I set your height and widths, but you can change them, and because you want it responsive, you can change them with a few media queries. For example mobile you might want width to be 90% or 100%.
.login .pane {
position: fixed; /* could be absolute */
top: 0; left: 0; bottom: 0; right: 0;
margin: auto;
width: 300px;
height: 250px;
}
Here's a jsfiddle