I have a container that positioned horizontally in the middle of the body, this container includes a div inside of it, this div has a background,
I want the background of the div to be responsive when shrinking or resizing the window like this site here ask.fm , I have used background-size: cover; but it didn't work with me.
HTML
<div class="container">
<div class="cover"></div>
</div>
CSS
.container {
width: 851px;
margin: 0px auto;
}
.cover {
background-image: url("wall.jpg");
background-size: cover;
background-repeat: no-repeat;
width: 100%;
height: 300px;
}
/* Media Queries */
#media screen and (max-width: 840px) {
.cover {
width: 100%;
}
.container {
width: 100%;
}
}
#media screen and (max-width: 600px) {
.container {
padding: 0;
}
}
I usually do a combination of these 2 things - depending on screen size - and I grab different src for the image / background or inline depends...
<section class="container">
<div class="inner-w">
<figure>
<img src="https://placehold.it/1600x900" alt="">
</figure>
</div>
</section>
<section class="container section-name">
<div class="inner-w">
<!-- ... -->
</div>
</section>
https://jsfiddle.net/sheriffderek/4j2avj7v/
figure {
margin: 0;
}
figure img {
display: block;
width: 100%;
height: auto;
}
.container {
background: gray;
border: 1px solid blue;
}
.container .inner-w {
max-width: 400px; /* just for this example... likely 900+ */
margin: 0 auto;
background: red;
}
.section-name .inner-w {
min-height: 300px; /* it needs to have some shape */
background-image: url('http://placehold.it/1600x900');
background-size: cover;
background-position: center center;
}
Related
Is there anyway to make my body background image to be responsive in any mobile view? Especially when the height is 412x980? I know how to use some proper background cover
body {
background: url(../../something.jpg);
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
}
But I wanted to not use fixed because I need to stay all elements on that background image.
EDIT:
After using #VIKESIR provided code, I still getting whitespace after trying to resizing every mobile views, I got the mobile view sizes here. Something like this
#media screen and (max-width: 479px) {
* {
margin: 0;
padding: 0;
}
html,body {
width: 100%;
height: 100%;
box-sizing: border-box;
}
body {
background: url(https://www.psdstack.com/wp-content/uploads/2019/08/copyright-free-images-750x420.jpg) no-repeat center cover;
width: 100%;
min-height: 100vh;
}
}
<body></body>
and this is what I meant still getting a whitespace after using the provided code.
By default body height is nothing, so we need to define height.
Here is some css except body tag in last one, that you can configure in every style.css when you create new stylesheet.
* {
margin: 0;
padding: 0;
}
html,body {
width: 100%;
height: 100%;
box-sizing: border-box;
}
body {
background-color: #fa0;
}
.title {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.title h1 {
color: #000;
text-shadow: 1px 1px 0px #fff;
text-align: center;
}
#media (max-width: 479px) {
body {
background: url(https://www.psdstack.com/wp-content/uploads/2019/08/copyright-free-images-750x420.jpg) no-repeat center / cover;
width: 100%;
min-height: 100vh;
}
}
<body>
<div class="title">
<h1>TESTING<br>TESTING TESTING</h1>
</div>
</body>
I am having issues with responsiveness in CSS. my pictures are not shrinking with increase or decrease in browser window. below is my div for the images
<div class="images">
<img src="imagefolder/coding2.jpg" alt="codepic" >
<img src="imagefolder/coding.jpg" alt="codepic">
</div>
also below is my style sheet class for images
#media screen and (max-width:1000px){
.images {
background-color: darkgrey;
padding: 10%;
width: 100%;
float: left;
margin-top: 30px;
margin-bottom: 30px;
object-fit: cover;
height: auto;
background-size: contain;
Make your images fit the parent by givning them a 100% width. Like this
.images img {
width: 100%;
height: auto;
}
#media screen and (max-width:1000px) {
.images {
background-color: darkgrey;
padding: 25px;
width: calc(100% - 50px);
}
.images img {
width: 100%;
height: auto;
}
}
<div class="images">
<img src="https://www.inma.org/files/images/blogs/feature_photos/Oct16_Ideas-Buytaert-1800.jpg" alt="codepic"><img src="https://digitalagencynetwork.com/wp-content/uploads/2020/02/what-you-can-see-from-coca-colas-digital-marketing-strategy.jpg" alt="codepic">
</div>
Give each image a class, <img class="myImage" src="imagefolder/coding.jpg" alt="codepic"> and specify the width for those images: .myImage { width: 100% //for example }
I believe that your problem can be solved by either by putting each <img> tag inside a separate <div> or by doing this:
.imgages {
max-width: 100%;
height: auto;
width: auto\9; /* ie8 */
}
I was wondering how to make an image overlap the container width it is placed in.
So, for example:
<div class="container">
<div class="background-image"></div>
</div>
And the CSS
.container {
max-width: 1170px;
}
.background-image {
background: url(/img/my-image.jpg) no-repeat center center;
}
So on a 1663px wide screen, the .container class is 1170px wide. The background image is 1170px wide as well.
What I'm trying to achieve, is make the image full-width (overlap the .container class so it's 1663px wide) without adjusting the HTML.
Thank you in advance.
For a fixed width of 1663px...
.container {
max-width: 1170px;
background: salmon;
padding: 50px 0;
margin: 0 auto;
}
.background-image {
background: url(https://www.fillmurray.com/800/300) no-repeat center center;
background-size: cover;
height: 300px;
/* The important part... */
width: 1663px;
max-width: 100vw;
position: relative;
left: 50%;
transform: translateX(-50%);
}
<div class="container">
<div class="background-image"></div>
</div>
(remove the max-width: 100vw; if you don't want the width to adjust with the window)
Or if you would rather full width...
.container {
max-width: 1170px;
background: salmon;
padding: 50px 0;
margin: 0 auto;
}
.background-image {
background: url(https://www.fillmurray.com/800/300) no-repeat center center;
background-size: cover;
height: 300px;
/* The important part... */
width: 100vw;
position: relative;
left: 50%;
transform: translateX(-50vw);
}
<div class="container">
<div class="background-image"></div>
</div>
If I understand your question correctly, You need to background image with full width.
.container {
max-width: 500px;
min-height: 500px;
background: blue;
margin: 0 auto;
color: red;
}
.background-image {
background: url(https://www.w3schools.com/cssref/paper.gif) repeat center center;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
opacity: 0.6;
z-index: -1;
}
<div class="container">
<div class="background-image"></div>
container
</div>
If I understand your question correctly, you are looking for the property background-size: cover which will create the desired effect, i have added a code snippet so you can see.
.container {
max-width: 1170px;
}
.background-image {
background: url(/img/my-image.jpg) no-repeat center center;
background-size: cover;
/* This will create the effect you are looking for */
background-color: #000000;
/* This can be removed, for illustration purposes only*/
height: 500px;
/* This can be removed, for illustration purposes only*/
}
<div class="container">
<div class="background-image"></div>
</div>
I have the following HTML
.wrapper {
position: relative;
width: 100%;
overflow: hidden;
}
.no-padding {
padding: 0 !important;
}
.home-slider img {
margin-right: 6px;
float: left;
width: 35px;
position: relative;
top: -4px;
}
.home-slider-wrap {
position: relative;
}
.home-slider-wrap .bg-img {
height: 500px;
width: auto;
max-width: none;
}
.img-full {
width: 100%;
}
#media (max-width: 767px) {
.home-slider-wrap .bg-img {
min-height: auto;
min-width: 100%;
width: 100%;
height: auto;
}
}
<div class="wrapper">
<div class="container-fluid no-padding">
<div class="home-slider-wrap">
<div class="home-slider">
<div>
<img src="https://g5-assets-cld-res.cloudinary.com/image/upload/q_auto,f_auto,fl_lossy/g5/g5-c-1t2d31r8-berkshire-communities/g5-cl-i2pbvvuq-creekstone/uploads/pet-friendly.jpg" class="bg-img img-full ing-responsive" alt="">
</div>
</div>
</div>
</div>
</div>
I am trying to resize this image but at the same time also keep a minimum height for devices less than 768px; However as you can see in the demo below, this ain't working out.
The portion of the cat and dog isn't visible when the page is resized for a mobile device.
Demo
How do I fix this? Any help is appreciated.
You could try using a media query
Maybe you can try this css:
background-size: cover;
background-position: center;
background-repeat: no-repeat;
You can use media query to target below 768px width devices to define style like below -
#media (max-width:767px){
.home-slider-wrap .bg-img {
min-height: auto;
min-width: 100%;
width: 100%;
height: auto;
}
}
I have a list of screenshots. I want to display those inside a desktop-frame. I can achieve the effect easily with CSS but trying to make it responsive is giving me headaches.
My Html structure and style:
<style>
.container {
width: 80%;
margin: 0 auto;
}
.frame {
list-style-type: none;
background-image: url('http://www.hobbysubmarines.com/TV.png');
background-size: contain;
background-position: center;
background-repeat: no-repeat;
margin: 0 auto;
max-width: 651px;
height: 358px;
}
.frame li {
width: 100%;
}
.screenshot {
position: relative;
top: 1px;
left: 5px;
}
</style>
HTML:
<div class="container">
<ul class="frame">
<li><img class="screenshot" src="../img/screenshot1.jpg"></li>
</ul>
</div>
Fiddle: http://jsfiddle.net/faj2rfc8/1/ so you maybe get the idea. If you see the fiddle, it is just to put the cat inside the tv-frame. Making this responsive is my main issue.
I would size the background in 100%, and add a media query to reduce the height of the screenshots for smaller screens. I put a fiddle together for you:
.container {
width: 80%;
margin: 0 auto;
}
.frame {
background-image: url('http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg');
background-size: 100%;
background-position: center;
margin: 0 auto;
max-width: 651px;
height: 358px;
}
.screenshot {
position: relative;
top: 1px;
left: 5px;
height: 100px;
}
// mobile only
#media screen and (max-width: 479px) {
.screenshot {
height: 40px;
}
}
fiddle: http://jsfiddle.net/gg89qwon/1/