background-image url responsive image - html

I want to insert an image by CSS because I want to use captions over my image.
What I want is that the image shows completely and is responsive.
Can anyone help me with this?
Here you can see my HTML im using bootstrap. As you can see i want to let contentblock1 show an responsive image by using the CSS background-image code. I also want this background image to be responsive. But whatever i try it won't show me the background.
<div class="container">
<div class="col-sm-6">
<h1> Dit is een bootstrap site</h1>
<button type="button">click me!</button>
</div>
<div class="col-sm-6">
<div class="contentblock1">
</div>
</div>
I'm using the following CSS
.contentblock1 {
background-image: url('http://1.bp.blogspot.com/_zxdOSKs0ASI/TDMqbnatRwI/AAAAAAAAAkM/nB-DxcVcqqk/s1600/Testbeeld.jpg');
background-size: 100% 100%;
width: 100%;
}

width: 100px; /*you width*/
height: 100px; /*you height*/
/* optional: background-color: white; */
background-image: url('image.png');
background-repeat: none;
background-size: 100% 100%;
/*or
background-size: cover;
background-position: 50% 50%;*/
background-attachment: fixed;

#some-div {
background-image: <imgurl>;
background-repeat: none; //Unless you want something else
backgorund-size: 100% 100%;
}
and for responsiveness, use media-queries or a lib that is made for responsiveness such as Bootstrap.
Below you have an example of media-queries.
#media (min-width: 1200px)
{
#some-div {
width: 600px;
}
}
#media (max-width: 768px)
{
#some-div {
width: 100%;
}
}
EDIT:
http://jsfiddle.net/e8xLf4u3/
Here is your working example, it only needed content. Also, in the future please show some code for faster help and better understanding
Graag gedaan ;)

Related

Background image responsive to mobile view using pure css

I want my background image to be responsive in mobile view.
I used this to scale my background image to the whole desktop:
html {
background:url("image/2.jpg") no-repeat center center fixed;
background-size: cover;
}
But I cant have a great result in this: (this is most requested suggestion in my search)
#media only screen and (max-width: 600px) {
html{
width: 100%;
}
}
This is a desktop background
While this is what happens in my mobile view
One elegant solution would be to better controlling exactly what you want to show in landscape and portrait mode. If you want to be responsive with a landscape image on a portrait screen you necessarily have to "lose something", as per #salff comment
My snippet for being more flexible depending on user screen:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html
{
background:url("https://www.hotelmedici.com/images/slide_home/11_Fori_imperiali_1600X917.jpg") no-repeat center center fixed;
background-size: cover;
width: 100%;
}
#warning-message { display: none; }
#media only screen and (orientation:portrait)
{
#wrapper { display:none; }
#warning-message { display:block; color: white;}
html
{
background:url("https://s3files.core77.com/blog/images/2013/11/ESB-HalloweenLightShow-1.jpg") no-repeat center center fixed;
background-size: cover;
height: 100%;
}
}
</style>
</head>
<body>
<div id="wrapper">
<h2>Responsive Images</h2>
<p>If you want the image to scale both up and down on responsiveness, set the CSS width property to 100% and height to auto.</p>
<p>Resize the browser window to see the effect.</p>
</div>
<div id="warning-message">
this website is only viewable in landscape mode
</div>
</body>
</html>
NOTE: the code was produced crossing and tweaking your code, w3schools snippet and this answer
You could add something like this:
.yourelement {
background-image: url("https://i.stack.imgur.com/viNrB.png");
background-repeat:no-repeat;
background-size:contain;
background-position:center;
height: 247px;
/*Additional code (don't bother adding this):*/
text-align: center;
color: white;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="yourelement">Your content goes in here</div>
</div>
</div>
</div>
JSFiddle
You could also replace contain with cover but requires adding some more code. Something I found for this:
.ratio4-3 {
width: 100%;
background-image: url("https://i.stack.imgur.com/viNrB.png");
background-size: cover;
height: 0;
padding: 0; /* reset */
padding-bottom: calc(100% * 3 / 4);
}
.text-center {
text-align: center;
color: white;
}
<div class="ratio4-3">
<div class="text-center">
content could go in here
</div>
</div>
JSFiddle

CSS and Bootstrap - full page image not working

So I'm trying to make it so that a full page image shows in the page, and resizes responsively on different screens so that it always takes up the whole screen. I looked it up on w3schools and other questions on Stack, but it seems that no matter what I do it never works, I checked if something is overriding my CSS in the browser developer tools but it seems there is nothing wrong, it just simply doesnt work. I'm using bootstrap and the div which background image should be full page is a col-12, would that cause the problem? This is my css:
body, html {
height: 100%;
}
#image-div {
background-image: url("paper.jpeg");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
color: white;
background-color: rgba(0,0,0,0.5);
background-blend-mode:darken !important;
font-size: 20px;
}
and the html:
<div className="row" id="calculator-row">
<div className="col-12" id="image-div">
<div className="over-image">
<p class="try-calculator">
Calculate the possible return of investments
</p>
</div>
</div>
<div className="col-12" id="calculator-div">
<h1>Return of Investments</h1>
<BenefitCalculator />
<strong>*The average conversion percent is 4, but enter yours in case you know it</strong>
</div>
</div>
EDIT: Forgot to mention I am also using REACTJS
Try backgound-size: cover, contain;
If this does not work send an example of you code. Also height in percentage is always a bad idea. If this is for the element to be as tall as the page use 100vh or some other method. Also note that you will probably need a media query for portrait and landscape orientation.
Try this snippit:
body,
html {
height: 100%;
margin: 0;
padding: 0;
}
.row {
display: flex;
flex-wrap: wrap;
margin-right: -15px;
margin-left: -15px;
}
.row-fw {
width: 100vw;
height: 100vh;
}
.col-12 {
flex: 0 0 100%;
max-width: 100%;
}
#image-div {
background-image: url("https://placekitten.com/g/1920/1080");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;
display: block;
width: 100%;
color: white;
background-color: rgba(0, 0, 0, 0.5);
background-blend-mode: darken !important;
font-size: 20px;
}
<html>
<body>
<div class="row row-fw">
<div id="image-div"></div>
</div>
</body>
</html>
You could also be using height: 100vh; & width: 100vw; (vw = viewport width, vh = viewport height).
If the parent gets bigger than the size of your screen, so will the background. 100vw & 100vh will only use the viewport width & height.
just add below class to the parent div of image, it will scale itself as per screen sizes.
.img-responsive -> Makes an image responsive (will scale nicely to the parent element).

background image won't load in responsive.css

So i have this problem with an image, which won't load in my responsive.css code. I'm trying to change a background image according to various resolutions of the website. below i post the parts of my codes in html and responsive.css. I already checked the path to the image and the image is in the file which contains html, css, responsive. Any tips how to make it work? I'm a beginner when it comes to html and css.
HTML:
#background {
width: 100vw;
background-position: center;
background-size: cover;
}
responsive.css: figure {
width: 100%;
}
#media (max-width: 992px) {
#background {
height: 50vh;
}
}
#media (min-width: 993px) and (max-width: 1170px) {
#background {
height: 70%;
width: 100%;
background-image: url('../zadanie-domowe-rwd-materialy/obrazy/man-2.jpg');
}
}
<figure>
<img src="zadanie-domowe-rwd-materialy/obrazy/man-1.jpg" alt="Zdjęcie nr 1" id="background">
</figure>
Your HTML needs an amendment:
<body>
<figure id="background"></figure>
</body>
Remove the img tag and apply an id of background to the figure tag.
Img tags are not background images, so all images will appear on top of backgrounds. To create the condition, both images need to be added within css as background-image
You will need to add this to your css too:
#background {
position: relative;
height: 500px; // Or a different height
width: 100%; // Change to 100% of element or window
background-position: center;
background-size: cover;
// Add the follow
background-image: url('../zadanie-domowe-rwd-materialy/obrazy/man-1.jpg');
}
Update: Entire Code
HTML
<body>
<figure id="background"></figure>
</body>
CSS
#background {
position: relative;
height: 500px; // Or a different height
width: 100%; // Change to 100% of element or window
background-position: center center;
background-size: cover;
background-image: url('http://placehold.it/350x150');
}
#media (min-width: 992px) and (max-width: 1170px) {
#background {
background-image: url('https://unsplash.it/200/300');
}
}
See JsFiddle
I hope this helps
For responsive only change .css file not HTML . if you want to change the img in mobile or other device ing use in css Like this
Try it i thing it's helpful . https://jsfiddle.net/dup1d62k/4/

Absolute positioned image in responsive design

I am in need of some assistance.
I am currently working on a site that is going to be responsive.
Will post a link to an image of the psd of the site so you have an idea what it is supposed to be like.
Link to the site itself
the site has a width of 1200px as you scale the site you will see that the boat will do nothing
Now to the problem.
the problem lays with the boat this img is 1019x1732 and is positioned absolute. And needs to be so because i have to use the z-index to be able to position it above some other divs for it is rather big.
I have tried to put it in as a background-image and as an img tag with a relative div around it. but nothing happens.
My appolagies if my question is a hard to understand. have a hard time explaining problems to other people, it always makes more sence in my head.
Here is my code:
my html
<div id="wrapperHero">
<div id="hero">
<p>The "Banarly Group" has been operating<br>
in Nigerian, Gabonese and Cameroon waters<br>
since 1995.
</p>
<div id="circle">
<p>a fleet of<br>
<span class="number">24</span><span class="trawler">trawlers</p>
</div>
</div>
<div id="boat">
<img src="img/BAN_herofg.png" alt="boat">
</div>
</div>
My css:
#wrapperHero {
width: 100%;
background-image: url(../img/BAN_herobg.png);
background-repeat: repeat-x;
min-height: 795px;
margin-top: -23px;}
#hero {
max-width: 1200px;
width: 100%;
height: 100%;
margin: 0 auto;}
#boat {
background-image: url(../img/BAN_herofg.png);
background-repeat: no-repeat;
position: absolute;
height: 100%;
width: 100%;
z-index: 1;
right: -6%;
top: 240px;}
#circle {
border-radius: 50%;
width: 18.3333%;
min-height: 220px;
background-color: #fff;
background-color: rgba(255,255,255,0.2);
float: right;
margin: -230px 21.6666% 0 0;}
1:
You can use background-size , background-position-x with different media :)
#boat {
background-size: 590px;
background-position-x: 79px;
}
#media (min-width: 450px) and (max-width: 960px) {
#boat {background-size: //use different size;
background-position-x://use different size;}
}
If I got the question right, here's what you should do:
remove the background-image property from #boat
create <img src="img/BAN_herofg"> inside
set max-width: 100% to <img>.
Hope this will solve your problem.
Use css media queries to solve your problem...
ex:
#media (min-width: 600px) and (max-width: 800px) {
html { background: red; }
}

100% width Background Image

I'm trying to do a background image of 100% and have an image as the background. When I upload the image it goes to 100% but it cuts off have the picture. It makes the image wider than my screen. How do I fix it where the picture width is 100% but the image width fits the screen without getting cut off. Here is my tumblr to let you see what I mean (http://ophelialogy.tumblr.com/) and here is the full image to show you the full image and give you an idea for where it's cutting off (http://imageshack.us/a/img7/7103/khb3.png).
Here is my code:
CSS PART
/* --- HEADER --- */
#header {
top: 0;
left: 0;
width: 100%;
{block:IfAdjustableHeader}height:{text:Header Height};{/block:IfAdjustableHeader}
{block:IfNotAdjustableHeader}height:100%;{/block:IfNotAdjustableHeader}
position: fixed;
z-index: 10;
background-image: url('{image:header}');
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
/* --- PAGE CONTENT --- */
#page {
{block:IfAdjustableHeader}top:{text:Header Height};{/block:IfAdjustableHeader}
{block:IfNotAdjustableHeader}top:100%;{/block:IfNotAdjustableHeader}
left: 0;
width: 100%;
min-height: 100%;
position: absolute;
background: {color:Background};
z-index: 99;
}
.container {
margin: 50px auto 0px;
{block:If400Posts}width: 800px;{/block:If400Posts}
{block:If500Posts}width: 900px;{/block:If500Posts}
}
/* --- POSTS --- */
.postcol {
width: 540px;
margin-left: 240px;
}
.posts {
margin-bottom: 20px;
background-color: #fff;
padding: 20px;
}
.posts img, .posts li, .posts blockquote {
max-width: 100%;
}
HTML Part
<body>
<div id="header">
<div class="description">{Description}</div>
</div>
<div id="page">
<div class="container">
<div class="postcol">
{block:Posts}
<div class="posts">
</div>
this excellent blog post explains exactly what you need, without any third party tools:
http://css-tricks.com/perfect-full-page-background-image
also, there are some jQuery plugins for that, including:
https://github.com/jaysalvat/vegas
https://github.com/buildinternet/supersized
SO...
What cover does (in my mind) is take the background image and do it's best to use the most of it that it can depending on the height or width of the box it is in. There are 2 ways to deal with this. One way is to make the box the perfect ratio for the image. The other is to actually use an img that will stretch the box to it's exact size. Here is how to do each. The plus of the background-image version, is that you can easily only serve a small version to small screens with an #media rule.
HTML
<header class="container global-header"></header>
<header class="container global-header2">
<img alt="banner-thing" src="http://placekitten.com/400/100" />
</header>
CSS
html, body {
height: 100%;
margin: 0;
}
.container {
width: 100%;
float: left;
}
.global-header {
width: 100%;
/* this is hacky - but it is your answer */
height: 0;
padding-bottom: 25%;
background-image: url("http://placekitten.com/400/100");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
/* you should have this too */
background-position: center center;
}
.global-header2 {
width: 100%;
/* height will be determined by image size */
}
.global-header2 img {
display: block;
width: 100%;
height: auto;
}
FIDDLE
use:
background-image: url(../images/myimage.jpg);
background-size: cover;
Do you want the background image in the header or on the main page?
It is currently in the header.
Set the background image on the html tag if you want it to cover the whole page.
Nasser's link to do that is a good one (I would leave out the browser specific hacks though).
EDIT
AHH You're talking about width.
I think it might be something to do with the irritating slider tumblr have coming in from the right - it is about that much too stretched.
I suggest trying these styles on jsfiddler - or another separate site - you'll probably find it works fine.