I'm working on a Shopify theme and wanted to add a media query to only the carousel images when viewport is under 1024px. Right now the images are 100% width and height and cropping on the left and right. I don't want the image to crop so I don't want to apply anything to the height.
This is the code from the theme:
.objFit {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
img {
display: block;
width: 100%;
height: 100%;
-o-object-fit: cover;
object-fit: cover;
-o-object-position: center;
object-position: center;
font-family: 'object-fit: cover;object-position:center;';
}
}
And this is what I'm assuming I should add.
#media(max-width: 1023px) {
.objFit {
width: 100%;
position: relative;
overflow: hidden;
img {
display: block;
width: 100%;
-o-object-fit: cover;
object-fit: cover;
-o-object-position: center;
object-position: center;
font-family: 'object-fit: cover;object-position:center;';
}
}
}
I tried to switch the height and width so width came first but it broke the carousel.
I can't seem to get the preview to work to test my code so wanted to ask here first.
You can keep the height: 100% on both elements. Simply change cover to contain in your media query. You also do not need to repeat rules that remain the same in your media query.
I added two examples, one for a landscape orientation image and the other for portrait, so you can see what happens.
Here is the documentation on object-fit: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
.objFit {
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
#media (max-width: 1023px) {
.objFit img {
object-fit: contain;
}
}
<div class="objFit">
<img src="https://via.placeholder.com/900x300">
</div>
<div class="objFit">
<img src="https://via.placeholder.com/300x900">
</div>
Related
I've used this code to automatically resize the image to fit the container it's in, this code is working in other areas of my site, so I have no idea why it's not working here. Am I missing something?
<div className='image-container'>
<img src={imageRoutes} alt='post' />
</div>
.image-container {
position: relative;
height: 400px;
width: 100%;
margin-top: 10px;
overflow: hidden;
}
.image-container img {
max-height: 100%;
max-width: 100%;
}
It's displaying the image to fit the container corner to corner without maintaining it's initial pixel ratio, so essentially it's stretching the images to 100% of the container width and a height of 400px.
instead of :
.image-container img {
max-height: 100%;
max-width: 100%;
}
do:
.image-container img {
object-fit: cover;
display: block;
height: 100%;
width: 100%;
}
in this page I am trying to make the rectangular image on the top to be full width. I tried to give it several different properties, but the nearest effect to what I want is given by:
.dla-ciebie__services .dlaciebie-header-image {
background-image: url(images/dla-ciebie/dlaciebie-1600.jpg);
background-size: contain;
background-position-y: top;
background-repeat: no-repeat;
width: 100vw;
min-height: 100vh;
}
Here is the HTML that contains this element and the next block:
<div class="dla-ciebie">
<section class="dla-ciebie__services">
<div class="dlaciebie-header-image"></div>
<em class="services-intro">Chcę powiedzieć Ci coś bardzo ważnego...<br>nie urodziłaś się po to, by żyć nieświadomie, w bólu i z dużym bagażem zmartwień. Jesteś tu dlatego, że masz w sobie <strong>ogromną siłę, która czeka, aż pozwolisz jej udowodnić swoją moc!</strong>
</em>
<div class="services-background">
And here is the full SCSS:
.dla-ciebie__services {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
box-sizing: border-box;
margin-top: 40px;
.dlaciebie-header-image {
background-image: url(images/dla-ciebie/dlaciebie-1600.jpg);
background-size: contain;
background-position-y: top;
background-repeat: no-repeat;
width: 100vw;
/* height: 289px; */
min-height: 100vh;
}
.services-intro {
font: 40px/60px "Libre Baskerville";
color: $bluedark;
width: 60%;
text-align: center;
br {
margin: 0;
}
strong {
font-weight: bold;
}
#media (max-width: 900px) {
width: 88%;
font-size: 28px;
line-height: 54px;
}
#media (max-width: 500px) {
font-size: 18px;
line-height: 32px;
}
}
.services-background {
background-image: url("images/blue-painting-1200.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
margin-top: 80px;
width: 100%;
}
}
Here I cannot give a static height in px, because if I give it a static height, on smaller browsers, the image will change it's proportions.
At the same time what i did is not good too, because it adds some kind of white space below the image.
If you shorten the width of the browser window you will see how much the white space below the image increases.
I'd like to ask you for help to solve this issue (if possible avoiding JS solutions). I tried different tutorials, but it seems that how that site is constructed, other tutorial don't seem to be applicable, thus I see the need of a specific solution to this page.
Thanks in advance.
Don't use <div> as a image by adding background-image to it. Instead, use <img> element, that is why it's made into HTML.
To make it responsive, use width: 100% without height. If you set it like that, Image will auto resize with browser.
This should work perfectly:
.dla-ciebie__services {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
box-sizing: border-box;
margin-top: 40px;
}
.dlaciebie-header-image {
max-width: 100%;
}
.services-intro {
font: 40px/60px "Libre Baskerville";
color: $bluedark;
width: 60%;
text-align: center;
}
#media (max-width: 900px) {
.services-intro {
width: 88%;
font-size: 28px;
line-height: 54px;
}
}
#media (max-width: 500px) {
.services-intro {
font-size: 18px;
line-height: 32px;
}
}
<div class="dla-ciebie">
<section class="dla-ciebie__services">
<img src="https://unsplash.it/1600/500" class="dlaciebie-header-image">
<p class="services-intro">Chcę powiedzieć Ci coś bardzo ważnego...<br>nie urodziłaś się po to, by żyć nieświadomie, w bólu i z dużym bagażem zmartwień. Jesteś tu dlatego, że masz w sobie <strong>ogromną siłę, która czeka, aż pozwolisz jej udowodnić swoją moc!</strong>
</p>
</section>
<!-- Other code -->
</div>
I try to make a feature for something like a forum. People can use a custom BBcode to generate a thumbnail of an image and onclick i see an image modal with an image with max-height & max-width for dynamic sizes.
And there is my problem with the image modal. I cant get the result to properly center to screen. When formatting like this, the image centers horizontally but sticks vertically to the top. What can I do?
HTML:
<div class="wrapperDiv">
<img class="fullimage" src="$someUrl">
</div>
CSS:
.wrapperDiv {
position: fixed;
display: block;
height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 10000;
}
.fullimage {
height: 100%;
max-height: 500px;
max-width: 500px;
}
You should try to you flex instead, like this :
.wrapperDiv {
position: fixed;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
.fullimage {
height: 100%;
max-height: 500px;
max-width: 500px;
}
https://codepen.io/GuillaumeGautier/pen/rNxxKJZ
I have the following html:
<div class="me">
<img src="IndianRemovalAct-final.jpg">
</div>
and the following styles:
.me {
position: fixed;
height: 100%;
width: 100%;
}
/* method 1*/
img {
width: 100%;
height: auto;
}
/* method 2*/
img {
width: auto;
height: 100%;
}
I tried the above two ways to make the image responsive. However, if I make the browser narrower or shorter, part of the image is outside the viewport. I want to display the image fully and responsively inside its fixed-positioned container. The image is quite big and I am just doing the implementation.
I went a different route since I'm assuming that you want to maintain the aspect ratio of the image:
.me {
position: fixed;
height: 100%;
width: 100%;
background: url('http://placehold.it/350x150') no-repeat center center fixed;
background-size: cover;
}
<div class="me"></div>
Example: http://jsbin.com/woqijaxoqu/edit?html,output
is what I'm suggesting what you want?
<div class="me">
<img src="https://unsplash.it/900/900">
</div>
.me {
position: fixed;
height: 100%;
width: 100%;
}
img {
max-width: 100%;
}
Here is the solution that seems working. Based on input from responses to my post.
img {
max-width: 100%;
max-height: 100%;
}
This solution was based on folks suggesting "max-".
Thank you, SO folks!
I have created a responsive div that uses background-image, only problem is when I resize the browser window, the image leaves a white space below it.
HTML
html, body {
height: 100%;
width: 100%;
}
#container {
height: 100%;
width: 100%;
}
#background-image {
background-image:url('http://tiny.cc/pm334x');
height: 320px;
width: 100%;
display: block;
background-size: contain;
background-repeat: no-repeat;
}
CSS
<div id="container">
<div id="background-image"></div>
<p>Hello</p>
</div>
http://jsfiddle.net/qnwe098m/1/
At the moment I'm out of options, I have tried a number of different "solutions" but none have worked. I could go for creating multiple images, for different resolutions using media queries, but I'd rather keep it simple.
Many Thanks
Use cover as background-size instead of contain
background-size: cover;
html, body {
height: 100%;
width: 100%;
}
#container {
height: 100%;
width: 100%;
}
#background-image {
background-image:url('http://tiny.cc/pm334x');
height: 320px;
width: 100%;
display: block;
background-size: cover;
background-repeat: no-repeat;
}
<div id="container">
<div id="background-image"></div>
<p>Hello</p>
</div>
The snippet above may not actually show the difference, so here's the fiddle
Just use this code below to #background-image id
background-position:center;
background-size:cover;
Looks like you need:
background-size: cover;
rather than:
background-size: contain;
I have updated the fiddle.
http://jsfiddle.net/qnwe098m/2/
Have you tried:
#background-image {
background-image:url('http://tiny.cc/pm334x');
height: 320px;
width: 100%;
display: block;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
http://jsfiddle.net/qnwe098m/1/