Lighten my background image to get text in the body visible [duplicate] - html

This question already has answers here:
How to apply a CSS filter to a background image
(22 answers)
Closed 1 year ago.
I got a background image for my homepage, but I can't seem to lighten the background image.
My html file looks like this:
<!DOCTYPE html>
<html>
<head>
<title> Homepage </title>
<style>
body {
background: url('static/img/background1.jpg') no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
color: #000;
font-family: 'Roboto Mono', monospace;
opacity: 0.8;
position: relative;
background-blend-mode:lighten;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1> a title </hi>
<p> some text </p>
</body>
</html>
I thought the opacity setting or the background-blend-mode would help, but this doesn't seem to work.
I also tried the following, based on a comment:
body {
font-family: 'Roboto Mono', monospace;
position: relative;
}
body :after {
opacity: 0.9;
background-img: url('static/img/background1.jpg') no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
But in this case, the background image doesn't shows at all...
This is an image of the page when I use the original setting (first set up with only body{}):
But there is some text in it which we cannot see:
How can I lighten my background in this set up?

It's highly unlikely that you want the whole of your body to have opacity 0.8 - this will bring all its content down in opacity.
Instead you can put the background on a before pseudo element on the body and give that a lower opacity.
This snippet assumes you want the background to stay, so gives the before pseudo element position fixed, but you could of course have it just the same size as the body by giving it position absolute instead.
<!DOCTYPE html>
<html>
<head>
<title> Homepage </title>
<style>
body::before {
content: '';
background: url('https://picsum.photos/id/1004/200/300') no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
opacity: 0.8;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
body {
color: #000;
font-family: 'Roboto Mono', monospace;
opacity: 0.8;
background-blend-mode: lighten;
width: 100vw;
min-height: 100vh;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1> a title </hi>
<p> some text </p>
</body>
</html>
Note: you may get a suitable effect by looking into using filter for example.

I see what you are trying to do. I would instead add a div on top of the image add give it a background color (say grey or white) with some opacity.
<body>
<div>
<div class="bg"/>
<div>
<h1> a title </hi>
<p> some text </p>
<div/>
<div/>
</body>
and style would be something like:
<style>
.
.
.
.bg {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: white;
opacity: 0.5;
z-index: -1;
}
</style>
try adjusting opacity and color such that your text has to best contrast to be visible.

Related

Make footer under cover sized image

I am making a website and I stumbled upon a little problem.
I have the image set to be to height: 100% and width; and background-size: cover;
Is there any way I can make the footer appear so that you scroll UNDER the image?
.bg {
/* The image used */
background-image: url("./Resources/home_bg.jpg");
/* Full height */
height: 100%;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
HTML code looks like:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="bg"></div>
</body>
</html>
It sounds like you only need to add a z-index to your divs.
The footer would have the smaller number z-index, while .bg is the larger one.
Also, I added a container and gave the footer a background color to just show the effect that I think you're going for.
.bg {
/* The image used */
background-image: url("http://placehold.it/400x400");
/* Full height */
height: 100vh;
/* Center and scale the image nicely */
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
z-index: 10;
}
footer {
background: #ff0000;
position: fixed;
bottom: 0;
width: 100%;
z-index: 8;
}
.container {
height: 105vh;
}
<div class="container">
<div class="bg">BG</div>
<footer>footer</footer>
</div>

Resizing a background image in HTML

I'm new to HTML and I currently have the following code stored in a .css file. What is the best way to allow for auto resize if the user changes the window size? I tried to use the following code to allow for this but I was not able to get the intended result.
body {
background-image: url('Images/Space.jpg');
text-align: center;
}
text {
color:white;
}
img {
width: 100%;
height: auto;
}
width 100% makes responsive, image cover does the same, but more usefull no need to set width or height
* { margin: 0; padding: 0; }
body {
background: url(https://www.w3schools.com/css/img_flowers.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>Resize the browser window to see the effect.</p>
<div></div>
</body>
</html>
Try using CSS background-size Property.
body {
background-image: url('Images/Space.jpg');
background-size: 100%;
text-align: center;
}

Background Size Cover not working

HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<link href="StyleSheet.css" rel="stylesheet" />
</head>
<body>
<header>
<h1>California Road Trip</h1>
<h2>Driving the Coast of California</h2>
</header>
<p>
Highway 1 is the infamous winding stretch of road that follows the pacific coast of the U.S. Visit this sit for a virtual experience. <i>Bon voyage!</i>
<br />
<b>Call for help now!</b>
</p>
<p>
<video controls="controls" autoplay height="300" width="500" loop>
<source src="20160628_110323_64628293200884.mp4" type="video/mp4" />
</video>
</p>
<div>
<img src="columbus-nav-850x637.jpg" alt="Background Image" />
</div>
<footer>
Copyright © 2016.
</footer>
</body>
</html>
CSS:
header{
color: #000;
text-align: center;
border: 500px;
background-color: rgba(255, 190, 0, .5);
border-radius: 20px;
}
p{
text-align: left;
margin-left: 20px;
font-family: sans-serif, Arial, 'Myriad Pro';
}
div{
position: fixed;
top: 20px;
z-index: -1;
opacity: .5;
background-size: cover;
}
footer{
position: fixed;
bottom: 10px;
margin-left: 10px;
}
The background image is not taking up the entire screen. Any help is appreciated.
Here is a JSfiddle
You must set div img rather than just div. Give the element a height and width of 100% and it should cover the viewport.
div img {
position: fixed;
top: 20px;
z-index: -1;
opacity: .5;
background-size: cover;
height: 100%;
width: 100%
}
Background image is a css property, but you're trying to apply it to an image tag. You'll want to do something like this:
HTML:
<div class="myBackground"></div>
CSS:
.myBackground{
background-image: url(columbus-nav-850x637.jpg);
background-size: cover;
/*You can make this background fixed on desktop by adding this:*/
background-attachment: fixed;
}
Add these properties to div section in css file
{
-moz-background-size: cover;
-o-background-size: cover;
-webkit-background-size: cover;
}
The image you wish to serve as background for your page is placed in a div smaller than your page's size. And hence even if the image filled the div, it won't fill the page.
One of the possible solutions is to apply background image directly on body as suggested by Richard.
However, if you want your image to be in a separate div, you will first need to make the div cover your entire page. Minor update to CSS properties should do it.
div{
position: fixed;
top: 20px;
left: 0;
right: 0;
bottom: 0;
z-index: -1;
opacity: .5;
background-size: cover;
}
Next thing you need to make the image cover the entire div. You can either do it by setting
width: 100%;
height: 100%;
on img tag, or removing the img tag altogether and adding
background-image: url("columbus-nav-850x637.jpg");
in css for the div itself. You might also need to set proper z-index on your "background" div to layer it behind other contents of the page.
Сheck the "background-attachment" parameter. It should not have the value "fixed"!

How can I make a page with css,which the background image is cover , with scrolling and also responsive?

I want to make a page,which by scroll down ,the content div ,cover the back ground image.so I put a background image for body and create 2 divs, it works in big window size, but when I change the size of window, and make it smaller (to test the responsive), there is a white gap between image and content div.
Would you please help me to remove it?
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>first</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link href="css/first.css" rel="stylesheet">
<style>
body{
background: url("https://kalamarie123.files.wordpress.com/2012/03/img_7815-2.jpg") no-repeat top center fixed;
background-size:100%;
}
</style>
</head>
<body>
<div class="cover"></div>
<div class="content">Lorem Ipsum is simply dummy text of </div>
</body>
</html>
.cover{
height: 1232px;
width: 100%;
margin: 0;
padding: 0;
color: white;
text-align: center;
}
.content{
width: 100%;
height: 100%;
background: gray;
margin: 0;
padding: 0;
}
You need to make the body and the html go to the bottom of the page:
body, html {
min-height: 100%;
}
and then set the background to background-size: cover;: https://css-tricks.com/perfect-full-page-background-image/
You could do something like this:
<div id="yourdiv"></div>
#yourdiv {
width: 100%; height: 100%; top: 0; left: 0;
background: url(images/bg.jpg) no-repeat center top; position: fixed; z-index: -1;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Hope it helps!
Sounds like it's probably due to your background image not filling the full space available.
Try adding this to the body (the element with the BG image);
background-size:cover;
Note that this will not work well on old browsers

How to place background image behind page content?

I've got a background image on my webpage. Now I want to add content that floats over it. The code below places the content behind the image. How to correct it?
Note that I've borrowed (and I'm trying to get the effect) discussed in this link for background image: CSS-Only Technique #2 from: http://css-tricks.com/perfect-full-page-background-image/
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
<!--
#bg {
position: fixed;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
#bg img {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 50%;
min-height: 50%;
}
-->
</style>
</head>
<body>
<div id="bg">
<img src="myimage.jpg">
</div>
<div id="mycontent">
...
</div>
</body>
</html>
Simply set your z-index to a negative value
#bg{
z-index:-1;
}
This has been my goto solution for easy BG images.
You wont need to add the image the the HTML markup - just reference in the css file.
This will also perfectly scale the image for you.
html {
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Source:
http://css-tricks.com/perfect-full-page-background-image
Please refer to this:
Reference 1
Reference 4