http://graduateland.com/
How do i prevent the images from compression. When I reduce the size of my browser window, the image get compressed side way, it's like the human head being compressed.
Looking at that website as an example, the image size isnt affected when screen size changes, only the position of the image changes. How do i do that?
Current CSS
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 500px;
If you want images to be resized when the window shrinks, just change height: 500px to height: auto in the CSS you posted. This will force images to keep their original ratio as the width changes. The way your code works right now is that it resizes the image horizontally so it is never wider than its container, but has a fixed height, which messes up the aspect ratio once it begins to shrink horizontally.
If you want the image to stay the same size and just move position as the browser window shrinks you need to apply them as a background-image. Try this CSS code on the container div you want to apply the image background to:
#container {
background: url(path/to/image.jpg) no-repeat center top;
}
On the site you linked they are appyling this CSS
display: table;
width: 100%;
height: 100%;
margin-top: 0;
padding-bottom: 0;
background-image: url("a/image.jpg");
background-repeat: no-repeat;
background-position: center center;
background-size: 100%;
onto a div. But there are great inspector tools which can inspect that for you, so don't ask if you have a 'living' example.
You should particularly have a look at the background properties.
Here's the answer:
Responsive Images with CSS
CSS:
max-width:100% !important;
height:auto;
display:block;
Use #media, like:
#media screen and (max-width: 1280px) and (max-height: 1024px) {
.splash {
background-image: url('../img/splash-1280.jpg');
}
}
#media screen and (min-width: 1281px) and (max-width: 1920px) and (max-height: 960px) {
.splash {
background-image: url('../img/splash-1920.jpg');
}
}
In their CSS:
#media (max-width: 1280px)
[id="get-started"] {
background-size: auto;
background-position: center top;
}
Which overrides:
background-position: center center;
background-size: 100%;
Related
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>
I'm using background image on main div and different elements positioned in it. Background looks good in desktop and scrollable in mobile version. I would like to have either scrollable of overflow-x in tablet or full view of image in tablet.
When i apply overflow-x:scroll; to the div that has background, i'm able to scroll things on top of it but not background itself.
I tried background-attachment: scroll; it is not working. and background-size: cover is also not working.
What is the best way to tackle this?
What is the media query i can use to make same view for mobile and tablet?
P.S: I'm using two different images for desktop and mobile. I could use either of the images for tablet.
This what i've in tablet view for main div
#media (min-width: 768px)
.virtual {
background-image: url(/*****/stage);
background-position: center -30px;
min-height: 500px;
}
This what i've in mobile view for main div
#media (max-width: 767px)
.virtual {
overflow: hidden;
}
This what i've in mobile view for inner div(of main div)
#media (max-width: 767px)
.hotspot-container {
background-image: url(/****/stage-sm);
background-position: center center;
background-repeat: no-repeat;
background-size: 1341px 768px;
position: relative;
min-height: 500px;
width: 1341px;
top: 2px
}
I have a big background image, but the image is not good in small mobile browser. It displayed a horizontal scroll bar and the image is crop.
The image should be:
The 3 person in the image should be display in the center
No scrollbar
How to fix this?
CSS (responsive)
.header-home-div{
background: url(/testEnvironment/files/homepage-header-mobile.jpg) !important;
height: 700px;
width: auto;
I tried to use this:
background-size: cover !important;
background-position: center top;
but it's not working
Here's the link
I use this to test the responsiveness of the image
On the mobile size, you use an !important to the background property. So you need to use !important for setting a size too like this :
#media screen and (max-width: 992px)
.header-home-div {
height: 1500px;
width: auto;
background-size: 100% !important;
}
It's better to remove the !important property at first instead of overwriting it with the above fix by the way.
The horizontal scrollbar depends on .header-home-h1 margin. The following should fixed it :
#media (max-width: 520px)
.header-home-h1 {
text-align: left;
margin: 0;
width: 100%;
font-size: 2.25em;
}
I have a centered viewport who has a large image as its background. Due to other elements on the page, we elected to have some breakpoints depending on the sizes of of these other elements. So far so good, my layout works fine.
The one gotcha is that since the image used isn't tall enough, once it reaches a certain size the background-color shows and there are bars that are introduced on either side. This has to do with the ratio of the image vs. the ratio of the viewport.
Solution with Bars:
http://output.jsbin.com/vecele
Now, the fix here is to find at what point the bars are introduced. Anything larger than that size, down to the cutoff means the background-size needs to be changed to background-size: 100% auto; vs when before when it was set to background-size: auto 100%;
Fixed Solution:
http://output.jsbin.com/fonozagova/
Now my question is how do I mathematically figure out that "magic number" cutoff? In my example, I just played with the window size to find the actual number I should use here, which really isn't optimal.
Why not set the background size to cover? It will scale the the background image proportionally to fill the container regardless of what the size is.
div.container {
background-size: cover;
}
you can use a pseudo-element with a vertical-padding in % that will use elements width as reference. the ratio will be kept no matter width updates:
.hero {
background-color: #0a6ba1;
}
#header {
background-image: url('https://suretybonds.com/img/bonds/giant-header/inspector-1800.png');
background-repeat: no-repeat;
background-size: 100% auto;
overflow: hidden;
margin: 0 auto 40px;
max-width: 1600px;
position: relative;
}
#header:before {
content:'';
display:inline-block;
padding: 26% 0 0;
}
#media screen and (min-width: 699px) and (max-width: 1465px) {
#header {
}
}
#media screen and (min-width: 0) and (max-width: 699px) {
#header {
}
}
#media screen and (min-width: 0) and (max-width: 500px) {
#header {
}
}
/* extra for demo */
#header {
text-align:center;
font-size:4vw;
}
#header:before {
vertical-align:middle;
}
<div id="header" class="hero">resize window's width</div>
http://jsbin.com/lafosakeke/1/edit?output
I need the following:
emtpy div with no content
background image set to the div the
background image to be fluid/responsive on re-size I cannot set fixed
dimensions on the div
Everything I try fails to force the div open to support the size of the background image. Any help is greatly appreciated...
http://www.everymountain.us/
<header id="header">
<div class="wrapper">
<div class="inner">
<div class="top_banner"></div>
</div>
<div class="clear"></div>
</div>
</header>
.front #header .top_banner { background: url('images/bg_front.jpg') no-repeat; background-size: cover; }
The way to lock a height's aspect ratio to it's fluid width is to use padding-top or padding-bottom percentage. This is because all padding percentages are of the the element container's width. Your background image is 960 x 520, so the height is 54.166666666667%.
html
<div class="top_banner"></div>
css
.top_banner {
background-image: url('images/bg_front.jpg');
background-size: 100%;
width: 100%;
padding-top: 54.166666666667%;
height: 0;
}
Example: http://jsfiddle.net/SsTZe/156/
Essentially the same question: CSS fluid image replacement?
You can handle it after applying CSS
#DivName{
background-size: auto auto;
}
here first auto is for width and second is for height
Since this is a top google result for creating fluid-height divs in general (not just empty ones like the question specifies), I wanted to leave a CSS Calc solution that lets you put content into the div (the padding trick forces it to be empty):
.my-div {
background: #ccc url(https://link-to-image/img.jpg) no-repeat 50% 0;
background-size: 100% auto;
width: calc(100vw - 350px);
height: calc((100vw - 350px) * 0.468795); /* replace the decimal with your height / width aspect ratio */
}
Try to use medie queries in your CSS for different screen sizes to handle different fixed heights.
For example:
#media (min-width: 1200px) {
div { height: 3em; }
}
#media (min-width: 768px) and (max-width: 979px) {
div { height: 2em; }
}
#media (max-width: 767px) {
div { height: 1.2em; }
}
#media (max-width: 480px) {
div { height: 1em; }
}
etc. what you need to customize. You can leave the div width 100% to fit for all screen and the background-size:cover. You can also make different size backgrounds (diff. files) for each screen sizes to give less size to your website for mobile or tablet devices.