How can I give a svg a background image? [duplicate] - html

This question already has an answer here:
Fill SVG path element with a background-image
(1 answer)
Closed 10 months ago.
I have a .jpg file and an .svg file and my goal is to make the .jpg file as background of the svg file but I did not find any way, is there such a possibility?
My goal I try to achieve is:

Context
You can use an SVG just like any other image, using
background: url('my.svg') or
<img src="my.svg" />
To show one image over the other, you need two elements, each holding an image.
There are generally two ways to do this:
having two elements, each with a background, overlapping each other
one component with a background, that is displaying an image.
The parent will have to be position: relative and the children, absolute.
Example 1
body {
margin: 0;
}
.wrapper {
position: relative;
width: 100%;
min-height: 100vh;
}
.element1 {
position: absolute;
width: 100%;
height: 100%;
background: url('https://picsum.photos/800/600');
background-size: cover;
}
.element2 {
position: absolute;
width: 100%;
height: 100%;
background: url('https://picsum.photos/200/300') center no-repeat;
}
<div class="wrapper">
<div class="element1" />
<div class="element2" />
</div>
Example 2
body {
margin: 0;
}
.element1 {
width: 100%;
height: 100%;
text-align: center;
background: url('https://picsum.photos/800/600');
background-size: cover;
}
<div class="element1">
<img alt="my text" src="https://picsum.photos/200/300" />
</div>

Related

How do I increase my image width and height according to the viewport of the user while keeping the aspect ratio same?

I'm making a website and the website is basically just a huge image except for the clickable elements and I was wondering how I could make the image size increase according to the the user's screen size so that the image doesn't look stretched, but still fits the whole screen. This is my first time working with viewport, so I can't exactly say I understand it very well. Here's some code to help out:
<div class="Image">
<img src="Background.png"/>
</div>
.Image {
width: 100vw;
height: 100vh;
object-fit: contain;
}
The first part was written in index.html while the second was written in style.css. Just for your info, the html part was written in <body>.
You have to apply
width: 100vw;
height: 100vh;
object-fit: contain;
To img directly , you can use id class or img directly but last method is not recommended as it will apply to all img tags in the document
I have used red background to show how much area image covers but as object-fit: contain; so image ratio's are maintained
Read this to know more about object-fit
Use
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
To remove any unnecessary horizontal scroll bars as tags have some default margin and padding . So , here used the body tag
.Image {
width: 100vw;
height: 100vh;
object-fit: contain;/*Can be cover , fill , none...*/
background-color: red;
}
body {
box-sizing: border-box;
margin: 0;
padding: 0;
}
<div>
<img src="https://www.hdnicewallpapers.com/Walls/Big/Rainbow/Rainbow_on_Mountain_HD_Image.jpg" class="Image" />
</div>
<div class="fullscreen" />
.fullscreen {
position: fixed;
top: 0;
left: 0;
background-image: url(Background.png);
background-size: cover;
background-position: center;
width: 100vw;
height: 100vh;
}

Background-image: Using a link to an external resource within a div

I want to link an image url to a div so that image will be used as a background and watermark for the content within the div.
When I set the url to the body, it repeats the image, which i dont want.
<body style="background-color: white; background-image: url(https://preview.ibb.co/ntRarR/watermark3.png);">
...
</body>
And when I set the url within the div (where i want it and which is inside the body tag), the image does not appear.
<div style="background-image: url(https://preview.ibb.co/ntRarR/watermark3.png); text-align: center">
...
</div>
Any advice is greatly appreciated.
Thanks
use this way for image opacity
.bgdiv {
position: relative;
z-index: 1;
width:450px;
height:450px;
}
.bgdiv .bg {
position: absolute;
background: url(https://preview.ibb.co/ntRarR/watermark3.png) no-repeat center center;
background-size:100%;
opacity: .4;
width: 100%;
height: 100%;
z-index: -1;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
<div class="bgdiv">
<div class="bg"></div>
...
</div>
for body use
background-repeat:no-repeat;background-size:cover;
body{
background-image: url(https://preview.ibb.co/ntRarR/watermark3.png); text-align: center;background-repeat:no-repeat;background-size:cover;}
Make sure your div is not empty.
Use property
background:url('https://preview.ibb.co/ntRarR/watermark3.png');
to give background for your div.
If you apply background property to body tag, it will be applied to the
entire webpage.
In case you want to apply background image to the body tag, use
background-size: cover; (cover the entire page).

absolute positioning with three images

So I have a transparent image I want to place ontop of an image to create a "fade out" effect. I also have a background image. So all up there is three images.
This is my code
<div class="jumbotron">
div class="hero-dashboard">
<img class="center-block" src="../../img/hero-dashboard.png">
<div class="fade-bottom">
</div>
</div>
CSS
.jumbotron{
background-image: url('../img/hero-bg.jpg');
background-repeat: no-repeat;
background-size: cover;
.hero-dashboard img {
position: absolute;
z-index: 500;
height: 30px;
width: 500px;
.fade-bottom{
background-image: url('../img/hero-footer-fade.png');
position: absolute;
z-index:10;
bottom: 70%;
top: 10%;
right: 50%;
width: 100%;
}
}
}
They all have to be inside the "jumbotron" div.
Its on the page but it doesn't seem to be listening to the positioning. Can anyone help?
1- The parent div (jumbotron) should have relative position when children are absolute and should have height and width to be visible.
.jumbotron {
background-image: url('../img/hero-bg.jpg');
background-repeat: no-repeat;
background-size: cover;
position:relative;
top:0;
left:0;
width: 500px;
height:30px;
} // correct this closing tag
.hero-dashboard img {
position: absolute;
z-index: 500;
height: 30px;
width: 500px;
} //correct this
.fade-bottom{
background-image: url('../img/hero-footer-fade.png');
position: absolute;
z-index:10;
bottom: 70%;
top: 10%;
right: 50%;
width: 100%;
}
// } remove this
// } remove this
2- also correct opening tag < before div class="hero-dashboard">
3- correct the order of opening and closing tgas in your css {}. They seem weird!
Thanks for your help.
I closed the css tags {} like that because I need them to sit within the jumbotron div class. As they are the children of it. Correct me if I'm wrong but I was under the impression that it was the right way to do it.

Bootstrap - One Background Image for two divs

I´m a CSS beginner. I´ve got two separate containers which should have one background image. I'm using z-index but I don´t know how to make it work.
<!-- Background Image -->
<div class="bg-img"><img class="img-responsive" src="images/bg/bgtriangle.png">
<!-- First Container -->
<div class="container-main">
<p class="font-relative">Headline 1</p>
</div>
<!-- Second Container -->
<div class="container-fluid" style="background-color: #574c5d; border-top: 2px solid #e57e22;">
<h4 class="text-center" style="padding: 5px;">Headline 2</h4>
</div>
</div>
The CSS is:
.container-main {
width: 100%;
height: 100%;
padding-top: 70px;
padding-bottom: 15px;
background: #453a4b;
background-size: cover;
z-index: 1;
}
.container-fluid {
width: 100%;
height: 100%;
position: absolute;
z-index: 2;
}
.bg-img {
position: fixed;
width: 100%;
height: 100%;
background-size: cover;
z-index: 0;
}
How can I use this one bg-image in full width for two containers? Is that possible?
You need to set your image to the background-image of your bg-img instead of adding it as an HTML img element. Also, you do not need z-index at all for this - they can be removed from your CSS.
.bg-img {
position: fixed;
width: 100%;
height: 100%;
background-size: cover;
background-image: url('images/bg/bgtriangle.png'); /* This */
}
EDIT:
If you want to move the background of the divs behind the background image, while keeping the content above it, you are going to get into a bit of a messy situation. You can do this by removing the contents from the divs, and then positioning all of them as absolute and using z-index (-1 behind image and 1 in-front of image). However, this means that you have to use top/left/etc. to position the contents back into their divs.
Here is a demo of what I accomplished tinkering a bit, maybe it will be helpful to you.

Show gradient image over another image with css and html

I want to show gradient image over another image but the gradient image is not shown over the another image. My code is as follows:
<style>
.splash-gradient
{
background-image: url('images/gradient.png');
background-position: left;
background-repeat: repeat-y;
float: right;
height: 300px;
width: 362px;
}
</style>
<div class="col-md-6"> <img src="images1.jpg" /></div>
<div id="gradient-background" class="splash-gradient"></div>
Avoid to use empty markup only for styling purpose. You could just add a class to the existant markup
<div class="col-md-6 splash-gradient">
<img src="images1.jpg" />
</div>
and then use this style:
.splash-gradient {
position: relative;
}
.splash-gradient:before {
content: "";
position: absolute;
top: 0;
left: 0;
background: url('images/gradient.png') top left repeat-y;
height: 300px;
width: 362px;
}
the gradient is inserted on the :before pseudoelement and since is in position: absolute it overlaps the image (of course feel free to adjust width and height)