How do I make a background image transparent in CSS? - html

<style>
header {
text-align: center;
background-image: url('http://i.imgur.com/7L79iny.jpg');
background-size: 100% 100%;
background-repeat: no-repeat;
}
This is the code I'm using to add a background image to my header. How to I make the image semi-transparent without making the rest of the header transparent.
<header>
<center><img src="http://i.imgur.com/EPd7PBU.jpg" height=350 title="source: imgur.com" /></center>
<h1>Taylor's Blog</h1>
<ul>
<li>About Me</li>
<li>Blogs</li>
<li>Contact Me</li>
</ul>
</header>

You cannot change the opacity of a background image with css, only the opacity of an element. However, there is a very easy work-around, that only requires css:
header {
text-align: center;
position:relative;
}
header:after{
content:'';
position:absolute;
z-index:-1;
top:0;
bottom:0;
left:0;
right:0;
background-image: url('http://i.imgur.com/7L79iny.jpg');
background-size: 100% 100%;
background-repeat: no-repeat;
opacity:0.5;
}
<header>
<img src="http://i.imgur.com/EPd7PBU.jpg" height=350 title="source: imgur.com" />
<h1>Taylor's Blog</h1>
<ul>
<li>About Me</li>
<li>Blogs</li>
<li>Contact Me</li>
</ul>
</header>
On another note, <center> is depreciated, and should not be used. In fact, in the case of the html you have, the link and image are already centered with text-align:center;, making the center tag useless.

The best way would be to create an absolutely positioned div that is transparent and is layered under the content.
<style>
header {
position: relative;
}
header .background {
position: absolute;
width: 100%;
height:100;
opacity: .5;
background-image: url('http://i.imgur.com/7L79iny.jpg');
background-size: 100% 100%;
background-repeat: no-repeat;
}
</style>
sadsad
<header>
<div class="background"></div>
<center><img src="http://i.imgur.com/EPd7PBU.jpg" height=350 title="source: imgur.com" /></center>
<h1>Taylor's Blog</h1>
<ul>
<li>About Me</li>
<li>Blogs</li>
<li>Contact Me</li>
</ul>
</header>

Related

How do I get my 'logo' image to show in the header?

I'm very, very new to HTML and CSS, so sorry for my ignorance! I'm trying to add an image to my header, to go to the left and above the navigation. Any help anyone can give would be amazing!!
I have tried two ways of adding the image, the first using , but it did not show (I could see the image 'content' highlighted in blue on the page when i was in the console, but i couldn't see the image. The second way I used a , then the css below:
body {
background-color:#4A4849;
margin: 0 auto;
font-family:Arial, helvetica, sans-serif;
}
.Logo {
display: inline;
float:left;
width: 100%;
height: 100%;
background-image: url('../Images/Logo.png');
}
header {
text-align: center;
width: 100%;
height: 15%;
background-color: white;
margin: 0;
}
My full CSS includes the below...i feel like the problem is to do with the , as in the console the dimensions show as 1304x0 (but I am able to see the navigation) I therefore tried adjusting the header, which is why it duplicates with the .topnav.(see below) :)
.topnav {
font-weight: bold;
background-color: white;
overflow: hidden;
position: fixed;
top: 0;
left 0;
width:100%;
height:15%;
}
.topnav ul {
list-style-type:none;
overflow: hidden;
margin:0;
padding: 0;
}
.topnav li {
display:inline;
padding: 0px;
margin: 0px;
}
.topnav a {
float: right;
color: #4A4849;
text-align: center;
padding: 20px 10px;
text-decoration:none;
width:10%;
}
It would be great if anyone could help, as I've tried different things based on resources i've found online!
*HTML, in case you need it:
<body>
<header>
<div class="Logo"></div>
<nav>
<div class="topnav">
<ul>
<li>CONTACT US</li>
<li>ABOUT US</li>
<li>GRAPHIC DESIGN</li>
<li>MARKET</li>
<li>HOME</li>
</ul>
</div>
</nav>
</header>
There are a few things that can be changed with how you are setting things up:
I would recommend not making your logo the background-image on a div, when an <img> tag will work better in your case (<img src="path/to/your/logo.png">).
If you do use it as a background-image on a div, you have to remember that background images do not affect the height of an element.
Setting a % height on the .Logo div will also not work, since a percentage height needs to be relative to a containing element and you also set it to an inline element (height will not apply). Since, its parent (header) has as height of 15%, but that element also has no reference to 15% - e.g. 15% of what? The body tag would need to have a height set to 100%.
Only for your logo, I would simply do this:
<header>
<img src="your logo link">
<nav>
<div class="topnav">
<ul>
<li>CONTACT US</li>
<li>ABOUT US</li>
<li>GRAPHIC DESIGN</li>
<li>MARKET</li>
<li>HOME</li>
</ul>
</div>
</nav>
</header>
If you absolutely want to make it a background-image:
.Logo {
height: whatever your logo height is;
display: block;
background-image: url( ../Images/logo.png );
background-repeat: no-repeat;
}
<header>
<div class="Logo"></div>
<nav>
<div class="topnav">
<ul>
<li>CONTACT US</li>
<li>ABOUT US</li>
<li>GRAPHIC DESIGN</li>
<li>MARKET</li>
<li>HOME</li>
</ul>
</div>
</nav>
</header>

background image doesn't show up although has set the absolute and relative display for the tag

This is the html
</head>
<body>
<header>
<h2>Mountain Travel</h2>
<nav>
<li>Tour</li>
<li>About</li>
<li>Contact</li>
</header>
<section class="introduction">
<div class="background-image" style="background-image: url(assets/image/main.jpg);" >
</div>
<div class="content-area">
<h1>Mountain Travel</h1>
<h3>Unmissable Adventure Tours Around The World</h3>
Contact Us Now
</div>
</section>
</body>
</html>
This is the CSS:
.introduction.background-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
z-index: -1;
}
.introduction{
position:relative;
}
However, the image doesn't show up at all. I am sure that the url is correct. Could anyone help me with it?
So first you don't have a valid html structure (your nav tag was not close). Secondly the .background-image is a child of .introduction. So to access it in css you will need to use .introduction .background-image and not .introduction.background-image:
.introduction .background-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
z-index: -1;
}
.introduction{
position:relative;
}
<header>
<h2>Mountain Travel</h2>
<nav>
<ul>
<li>Tour</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav> <!-- Don't forget to close your nav tag-->
</header>
<section class="introduction">
<div class="background-image" style="background-image: url(https://www.google.fr/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png);" ></div>
<div class="content-area">
<h1>Mountain Travel</h1>
<h3>Unmissable Adventure Tours Around The World</h3>
Contact Us Now
</div>
</section>
I use in this example a reliable source (google logo) for your image path. So if in your example it's still not working maybe the source of the image is not the good one.
As mentionned by #KoshVery please don't forget that a li need to be in a ul or ol html tag.

Apply CSS effect to parent but not children

I have applied an effect that I want only for a div parent but it applies also to the children.
This is my HTML code:
#wrapper {
height: 100%;
background-image: url("../asset/banner.jpg");
background-attachment: fixed;
background-position: top;
background-repeat: no-repeat;
background-size: cover;
filter: grayscale(100%);
}
#top-wrapper {
width: 100%;
height: 10%;
background-color: rgba(39, 35, 30, 0.5);
}
<div id="wrapper">
<header id="top-wrapper" class="inline-content">
<div id="logo" class="inline-content">
<img src="asset/logo.svg" alt="Logo">
<h1>portfolio</h1>
</div>
<nav>
<ul id="menu" class="inline-content">
<li>Home</li>
<li>About</li>
<li>Project</li>
<li>Contact</li>
</ul>
</nav>
</header>
<section id="banner" class="inline-content">
<h2>Hello World!</h2>
<h2>and I am a Web Developer</h2>
<h3>based in London</h3>
</section>
</div>
The logo is a multi colour logo but it appears in greyscale.
Insert an additional div and just position it absolute:
<div id="wrapper">
<div id="test" style="
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-image: url(banner.jpg);
background-attachment: fixed;
background-position: top;
background-repeat: no-repeat;
background-size: cover;
filter: grayscale(100%);
z-index: -2;
"></div>
<header id="top-wrapper" class="inline-content">
<div id="logo" class="inline-content">
<img src="logo.svg" alt="Logo">
<h1>Tuffi portfolio</h1>
</div>
<nav>
<ul id="menu" class="inline-content">
<li>Home</li>
<li>About</li>
<li>Project</li>
<li>Contact</li>
</ul>
</nav>
</header>
<section id="banner" class="inline-content">
<h2>Hello World!</h2>
<h1>I am Tuffi</h1>
<h2>and I am a Web Developer</h2>
<h3>based in London</h3>
</section>
</div>
Obviously, remove everything from #wrapper except the height of 100%.
I agree with all comments: save your background as greyscale. It will save you some KBytes and it will work in IE, too. Much more impressive than using a CSS rule that IE doesn't care about ;-)

How to get an image behind a navigation bar

Kinda new to HTML, want to edit my navigation bar so that it has an image behind it and the nav bar sits at the bottom of the image. How do I do this?
After looking at a few forum posts, this is what I tried:
#cover {
background-image: url("Images\FB Cover.png");
}
<div id="cover">
<div>
<nav>
<li>Home</li>
<li>About DRC</li>
<li>Our Products</li>
<li>Contact</li>
</nav>
</div>
</div>
#cover {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: url(http://lorempixel.com/800/600) no-repeat 50% 50% / cover;
}
menu {
position: absolute;
left: 20px;
bottom: 20px;
}
a {
color: #fff;
}
<div id="cover">
<menu>
<li>Home</li>
<li>About DRC</li>
<li>Our Products</li>
<li>Contact</li>
</menu>
<div>
simple example
to place image as background of DOM element you can use, background-image css property.
For example
background-image: url('https://static.pexels.com/photos/87646/horsehead-nebula-dark-nebula-constellation-orion-87646.jpeg');
W3School has great documentation about css properties.
here is the link

Trying to repeat a non-background image

Im trying to repeat-y an image, but everything I have tried does not work.
HTML Body Code:
<body>
<h1>Native Flowers</h1>
<div id="header"></div>
<div id="navBar">
<ul>
<li>Home</li>
<li>Other</li>
<li>Other</li>
<li>Other</li>
<li>Other</li>
</ul>
</div>
<img class="pinkTex" src="res/pinkTex.jpg"/>
</body>
This is the code for the body of my HTML file currently.
CSS pinkTex:
.pinkTex {
left: 0px;
height: 100%;
width: 200px;
}
At the moment this is the code for the pinkTex class for the img.
So if I can please get some help on how to repeat this y-axis, that would be great. Thanks You.
if you give image in body tag use this code and please make sure your image path is proper.
body
{
background-image: url("res/pinkTex.jpg");
background-repeat: repeat-y;
}
you can use also
.pinkTex
{
left: 0px;
height: 100%;
width: 200px;
background-image: url("res/pinkTex.jpg");
background-repeat: repeat-y;
}