How to achieve that in css? - html

I'm developping a page where the user can upload some images in my website.
The images the user can upload should have a specific size let's say : 800 x 600 px.
When the user uploads a different size for example : 900 x 500. This image should be rescaled (not proportionally) to 800 x 600.
Now I need the page to be responsive. Meaning that when the whole page is resized, the image region is should be resized proportionally this time.
For example, if the page is 50% the original size, the image region should be 400 x 300;
I have the following html
<div class="ImageRegion">
<img class="Image" src="whatever"/>
</div>
Can this be achieved using CSS only or should I use Javascript please ?
Cheers,

If I understood correctly, you probably can achieve want you want with something like that:
<img src="yourimage.png" class="whatever" />
And in CSS:
img.whatever {
display: inline-block;
max-width: 800px;
width: 100%
}

/* For width smaller than 400px: */
.ImageRegion {
background-repeat: no-repeat;
background-image: url('http://placehold.it/400x300');
width: 400px;
height: 300px;
}
/* For width 400px and larger: */
#media only screen and (min-width: 400px) {
.ImageRegion {
background-repeat: no-repeat;
background-image: url('http://placehold.it/800x600');
width: 800px;
height: 600px;
}
}
<h3>Resize the browser width and the background image will change at <mark>400px</mark>.</h3>
<div class="ImageRegion"></div>

Related

change horizontal center and width of responsive image

I have a 3051 x 1716 pixel image.
When in mobile I want to view it like the following without cropping the image and uploading it second time:
I mean I just want to change the horizontal center of the image and change the width according to sceensize given it is a mobile screen.
How can I do that?
You could do a media query on a background-image like below, but its actually not best practice: only load what you really need on mobile devices to get faster pageloads. And don't use background images to display important content... so you might wanna look into using the picture tag and responsive images: https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images
However, if two seperate image files are not an option, here is a solution:
.container {
background-image: url("https://images.unsplash.com/photo-1615731364858-99013ac4fad3?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=2550&q=80");
background-size: cover;
background-position: left;
height: 100vh;
width: 100%;
}
#media only screen and (min-width: 600px) { // set to your mobile view breakpoint
.container {
background-position: center;
width: 100%;
height: 300px; // or whatever height you want
}
}
<div class='container'></div>
Can you please check the below code? Hope it will work for you. If you will take an image as a background image then you will be able to set the position of the image as per your requirement. We have used background-size and background-position properties to adjust the image.
Please refer to this link: https://jsfiddle.net/yudizsolutions/xhsb7ocL/3/
.banner-bg {
background: no-repeat center center / auto 100%;
position: relative;
display: block;
width: 100%;
height: 400px;
}
#media screen and (max-width: 767px) {
.banner-bg {
background-position: left center;
}
}
<div class="banner-bg" style="background-image:url('https://i.stack.imgur.com/Y3Wi7m.jpg')">
</div>

Auto position header image in HTML

I am trying to put an image in my header and it must auto position it self when the window is resized and the header image must support different screen resolutions.
This is what I have so far:
HTML
<header>
<img class="thumbnail2" src="MyImage.jpg" alt="thumbnail2" />
</header>
CSS
.thumbnail2 {
display: block;
max-width: 123%;
height: auto;
width: auto;
}
header {
padding: 0px 250px 0px;
margin: 0 auto;
}
The reason my max width is 123% is to fit the image when in full screen but as soon I resize the window it does not resize itself and the image becomes smaller in width.
Please assist.
I understand the thought process behind your current code however, you are approaching the issue all wrong. You should be using a css media query to adjust your your header if you are looking for granular control depending on screen size.
Since you only have one image and have not included the dimensions of the image or where it should appear in the header, i will assume you want it to be the entire width of the header.
Additionally max-width should never be over 100%. Here is how I would restructure your code:
Note: if this does not fix your issue, you need to resize your image to be larger. If your image is to small it will not fill up the entire screen.
Codepen link
html:
<header>
<img class="thumbnail2" src="MyImage.jpg" alt="thumbnail2" />
</header>
CSS:
.thumbnail {
display:block;
/* set width to 100% */
max-width: 100%;
height: auto;
width: auto;
}
header {
/* padding:0px 250px 0px; */
padding-bottom: 250px;
margin: 0;
/* set width of the header to 100% */
width: 100%
}
Try put the image inside the css (not an img tag)
.thumbnail{
background-image : url(MyImage.jpg);
background-repeat : no-repeat;
background-position : center;
background-size : cover;
}
then it would auto adjust to the container .thumbnail width...
You could use JavaScript to dynamically adjust the image size with
the window size:
Auto image resize based on browser window dimensions
You could use Media Queries and select multiple points at which your header adjusts it's size: Resizing images using media queries
You could instead create a "banner" class that utilizes the "backgrounds" family of CSS properties: http://www.w3schools.com/css/css_background.asp
Try changing your code to something like this:
<style>
.thumbnail2 {
position: relative;
background-position: 50% 0;
background-repeat: no-repeat;
background-size: cover;
background-image: url("./path/to/image");
min-height: 100%;
}
</style>
<div class="thumbnail2"></div>
You can edit the height of the image shown with min-height, and the width should be responsive.

How to deal with background images with 100% width and height in mobile and tab screens?

I have few pages where I have included a background image with width 100% and height 100% in every page. That means whenever a page is loaded you'll see an image with 100% screen's size. Everything is perfect when I see these pages in my laptop. I'm facing problems when I view them in mobile screens.
I just want to know how do everyone deal in these situations. I mean how to make changes to my main div with width and height 100% in responsive screens?
The image is getting stretched if I set the background size to 100%.
Some part of the image is cut if I set the background size to contain.
I want the image's clarity should be perfect. Should I make the width of the main div to auto? Or else should I change the image in responsive screens?
Is there anyway to make this div look better in small screens?
I just want my main div to be apt in Responsive screens.
It shouldn't look line there is a defect in the image or in my code.
Here's my code of the main div :
html,
body {
width: 100%;
height: 100%;
}
.main-div {
width: 100%;
height: 100%;
background-image: url('http://vignette4.wikia.nocookie.net/marveldatabase/images/8/8c/Wolverine_Vol_3_73_Variant_Frame_Textless.jpg/revision/latest?cb=20090925123509');
}
<div class="main-div">
</div>
There are many options to make image responsive. Please Google out the approach that best suits your requirement. The solution which you will find helpful depends on the image that you are using. It depends on the size and quality of image.
I find the following links helpful.
https://www.w3schools.com/css/css_rwd_images.asp
https://www.w3schools.com/bootstrap/bootstrap_ref_css_images.asp
For Responsive Image
.class_name{
max-width: 100%,
height: auto,
display:block
}
And Full Screen Responsive Image use individual class like
.class_name{
background-image: url(image path) ;
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
background-color: #999;
}
html,
body {
max-width: 2000px;
min-width: 100px;
height: auto;
}
.main-div {
width: 100%;
height: 100%;
background-image: url('http://vignette4.wikia.nocookie.net/marveldatabase/images/8/8c/Wolverine_Vol_3_73_Variant_Frame_Textless.jpg/revision/latest?cb=20090925123509');
}
<div class="main-div">
</div>
this should make it responsive. I don't know what you want the DIV to be like, so I left that alone.

how to resize a image window when container is less than a certain width in css

I have an image that's 1920 pixel wide. I want it to display centered and cropped in the browser window when it's width is greater than 1024 pixels. When the browser width is less than 1024, the image should be centered and cropped to 1024 pixels and then resized to the browser width.
I can't figure out how to do this, and haven't found any solutions on the internet yet.
Can anyone tell me how to do this, or point me at examples?
I am not exactly sure what are you trying to do, BUT I think what are you looking for is:
max-width
http://www.w3schools.com/cssref/pr_dim_max-width.asp
You can use object-fit property:
.image-wrapper {
width: 100%;
height: 100%;
overflow: hidden;
> img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 50% 50%;
}
}
HTML
<div class="image-wrapper">
<img src="" />
</div>
What you are looking for can be achieved using media queries. Following is just an example. Hope you can proceed with that.
#media screen and (min-width: 480px) {
body {
background-color: lightgreen;
}
}
Here background color will be changed when you resize the browser and browser width is less than 480px.

Make image scale depending on resolution

I have a width:100% image as the header of my website, like here:http://directdatanou.comli.com/
How can I make the image scale depending on screen resolution?
I tried using the image as background of a div, like below:
<div id="header" class="latime_100p">
</div>
with css:
#header{
background: #5b6773 url("../grafica/header.jpg") no-repeat top center;
background-size: cover;
height:500px;
}
But it doesn't scale right on big resolutions (it shows only parts of the image as the screen width and height grow).
So I ended up with using the image directly?
How can I make it scale gracefully depending on resolution?
Thank you.
If you need that ALL image is showing:
<img src="...." class="img">
.img {
width: 100%;
height: auto;
}
That's all