How to center background image on a narrow screen vertically [duplicate] - css

This question already has answers here:
How do I center an image if it's wider than its container?
(10 answers)
Flexbox: center horizontally and vertically
(14 answers)
Center image that is bigger than the screen
(9 answers)
Closed 2 years ago.
I use an image as a backround, something like this:
<div class="container">
<img src="http://somestorage.com/img.png" />
</div>
css:
.container {
position: absolute;
height: 100vh;
width: 100%;
overflow: hidden;
text-align: center;
}
.container img {
min-width: 100%;
min-height: 100%;
}
The problem is thought, that I wanna have a central top part on narrow screens. Now it shrinks like this:
I wanna it be like here:
JsFiddle
Could anyone suggest a solution?

Simply I changed min-width: 100%; with width: 100%; and added object-fit: cover;
.container {
position: absolute;
height: 100vh;
width: 100%;
overflow: hidden;
text-align: center;
}
.container img {
width: 100%;
min-height: 100%;
object-fit: cover;
}
<div class="container">
<img src="http://img.ohandroid.com/android/TJ0mc.png" />
</div>

I would try something like this:
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
width: 100%;
overflow: hidden;
text-align: center;
}
.container img {
min-width: 720px;
min-height: 480px;
}
You could also change out the images for differing sizes of screens using media queries.
#media (max-width: 480px) {
body {
background-image: url(images/background-mobile.jpg);
}
}
#media (min-width: 481px) and (max-width: 1024px) {
body {
background-image: url(images/background-tablet.jpg);
}
}
#media (min-width: 1025px) {
body {
background-image: url(images/background-desktop.jpg);
}
}
ref: https://web.dev/optimize-css-background-images-with-media-queries/

Related

Using media queries in CSS

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>

How to position a banner background image using img tag instead of inline-css ( position center of image)

I appreciate your help with css. I was placing my banner image using background-image with inline-css. I need now to place out using img tag and well to target the parent with css. Unfortunately my image doesn't resize as when it was inside the inline-css.
Here is my css code. Please see on full page and resize page.
The "effect" I like is that on mobile image only center of image is shown, while on increasing page width image is resized by keeping center of image as the base for the position. See second image of example (called banner-two).
So how to recreate exactly as the image in banner-two using background-image but now placing the image as img src.
I also created a codepen with my code ( link to my code in codepen - please also resize ).
.banner-two {
background-size: cover;
background-position: center center;
height: 240px;
width: 100%;
}
#media (min-width: 768px) {
.banner-two {
height: 480px;
}
}
#media (min-width: 1200px) {
.banner-two {
height: 680px;
}
}
<div class="banner-one">
<img alt="" src="https://pictr.com/images/2018/10/06/06cVw2.jpg" />
</div>
<section>
<div class="banner-two" style="background-image: url('https://pictr.com/images/2018/10/06/06cVw2.jpg')"></div>
</section>
Edit: The below solution only works in Firefox; I can't seem to make it work in the other browsers.
I'll leave it up for now, in case it helps people in the right direction, but I'll delete it when a better working solution comes along.
Just position the banner in the right place.
.banner-one {
height: 240px;
display: inline-block;
position: relative;
left: 50vw;
transform: translateX(-50%);
}
.banner-one img {
height: 100%;
}
.banner-two {
background-size: cover;
background-position: center center;
height: 240px;
width: 100%;
}
#media (min-width: 768px) {
.banner-one,
.banner-two {
height: 480px;
}
}
#media (min-width: 1200px) {
.banner-one,
.banner-two {
height: 680px;
}
}
<div class="banner-one">
<img alt="" src="https://pictr.com/images/2018/10/06/06cVw2.jpg" />
</div>
<section>
<div class="banner-two" style="background-image: url('https://pictr.com/images/2018/10/06/06cVw2.jpg')"></div>
</section>
But the question is, why do you want to do this. You need a lot more CSS to make this work! My gut feeling would be to simply hide the img and then go on with what you were doing with banner-two. I can understand there being restraints that you have to work with though, so I hope this helps!
.banner-one {
width: 100%;
height: 640px;
display: flex;
justify-content: center;
}
.banner-one img {
max-width: 100%;
}
Until now, here is the code. Banner-flex isn't working as image is being centered completely. Banner-transform is working fine. link to examples on codepen
I do wonder if there is a way to do it that could be said is better or more efficient.
Thanks again !
<div class="banner-flex">
<img alt="" src="https://pictr.com/images/2018/10/06/06cVw2.jpg" />
</div>
<div class="banner-transform">
<img alt="" src="https://pictr.com/images/2018/10/06/06cVw2.jpg" />
</div>
.banner-flex {
width: 100%;
height: 240px;
display: flex;
justify-content: center;
}
.banner-flex img {
max-width: 100%;
}
.banner-transform {
height: 240px;
display: inline-block;
position: relative;
left: 50vw;
transform: translateX(-50%);
}
.banner-transform img {
height: 100%;
}
#media (min-width: 768px) {
.banner-flex,
.banner-transform {
height: 480px;
}
}
#media (min-width: 1200px) {
.banner-flex,
.banner-transform {
height: 680px;
}
}
#media (min-width: 1800px) {
.banner-transform img {
width: 100vw;
}
}

Make DIV float to the bottom instead on top

I have two DIV's, first one is auto width (the content), second one is fixed width.
When the screen gets too narrow/window scaled, the fixed width DIV goes on the top and becomes 100% width as well. I would like to replicate this, but I want the fixed DIV to go on the bottom, not top, when browser gets too narrow. How can I accomplish this? Thanks.
(Please check in 'Full-Page' mode)
.container-wrapper
{
overflow: hidden;
}
.fixed-right
{
overflow: hidden;
min-height: 100px;
min-width: 400px;
float: right;
}
.auto-left
{
overflow: hidden;
min-height: 100px;
}
.fancy
{
border-radius: 2px;
background-color:lightgray;
margin-left: 5px;
padding: 5px;
}
#media
only screen and (max-width: 764px), (min-device-width: 764px) and (max-device-width: 1024px)
{
.fixed-right
{
float: none;
width: auto;
margin-left: 0px;
margin-bottom: 5px;
}
}
<div class="container-wrapper">
<div class='fixed-right fancy'>
Fixed
</div>
<div class="auto-left fancy">
Auto
</div>
</div>
Try adding this into your #media-query;
.container-wrapper {
display: flex;
flex-direction: column-reverse;
}
Pretty easy, change the order of the Divs in the HTML DOM:
<div class="container-wrapper">
<div class="auto-left fancy">
Auto
</div>
<div class='fixed-right fancy'>
Fixed
</div>
</div>
The float right, will make it as you wanted on desktop
You can use display: flex; on your wrapper. Switch the flex-direction in the two different viewports. Note this solution becomes more flexible if you add more elements to the wrapper, being able to set the order.
Then define the order of the divs.
in the large view you set the right div to order 1
in smaller you set it to 0, that means it will be first (on top)
.container-wrapper {
display: flex;
flex-direction: row;
}
.fixed-right {
overflow: hidden;
min-height: 100px;
min-width: 400px;
order: 1;
}
.auto-left {
overflow: hidden;
min-height: 100px;
}
.fancy {
border-radius: 2px;
background-color: lightgray;
margin-left: 5px;
padding: 5px;
}
#media only screen and (max-width: 764px),
(min-device-width: 764px) and (max-device-width: 1024px) {
.container-wrapper {
flex-direction: column;
}
.fixed-right {
width: auto;
margin-left: 0px;
margin-bottom: 5px;
order: 0;
}
.fancy {
margin-left: 0;
}
}
<div class="container-wrapper">
<div class='fixed-right fancy'>
Fixed
</div>
<div class="auto-left fancy">
Auto
</div>
</div>

change position fixed to relative, vice-versa in Foundation

I would like position:fixed to only work when I'm displaying the app on a large screen. If I am in mobile, I don't want position:fixed. What is wrong with my scss? I actually just took the idea from another stackoverflow post.
.page {
overflow: hidden;
min-height: 100vh;
.sidebar {
position: fixed;
#media #{$small} {
position: relative;
}
}
}
I figured it out and it's working great!
.page {
overflow: hidden;
min-height: 100vh;
.sidebar {
#media only screen and (min-width: 400px) {
position: fixed;
}
}
}
according to zurb foundation
.page {
overflow: hidden;
min-height: 100vh;
.sidebar {
position: relative;
#media screen and (max-width: 63.9375em), screen and (min-width: 75em) {
position: fixed;
}
}
}

media query for portrait orientation doesn't seem to work

My media query for portrait orientation doesn't seem to work. Any ideas what I'm doing wrong, please?
html:
<div class="titleCard" id="first" style="background-color:white"></div>
css:
html, body { -webkit-backface-visibility:hidden;}
#first {
background-color: #000;
width: 100%; max-width: 1920px;
height: 100%; max-height: 1080px;
background: url('images/bg.png') no-repeat center;
background-size:cover;
position:relative;
}
.bgwidth { width: 100%; max-width: 1920px; }
.bgheight { height: 100%; max-height: 1080px; }
#media all and (orientation:portrait) {
background: url('images/Mobile_Art.jpg') no-repeat center;
}
It works (simplified test). It's just that you're not telling it for what element it should change the background when the orientation changes.
You probably meant to have something like:
#media all and (orientation: portrait) {
#first {
background: url('images/Mobile_Art.jpg') no-repeat center;
}
}
instead of:
#media all and (orientation:portrait) {
background: url('images/Mobile_Art.jpg') no-repeat center;
}

Resources