CSS - How to get div on top of background image? - html

I have a background image half the page, and I have a div underneath it which I also want to put a quarter of it above the background image. I tried to put padding-top on the profile class but it doesn't work whilst wrapping background-pic and profile in a div.
CSS:
.background-pic{
background-image: url('https://images.pexels.com/photos/825262/pexels-photo-825262.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260');
height: 60%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-color: rgba(0,0,0,0);
}
.profile{
padding-top: 300px;
}
HTML:
<div class="background-pic">
</div>
<div class="container">
<div class="profile" style="background-color: grey; height : 300px;">
<div class="profile-pic">
</div>
</div>
</div>

You can apply a negative margin-bottom to the first element.
.background-pic {
background-image: url(https://www.placecage.com/600/400);
height: 400px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
margin-bottom: -100px;
}
.content {
padding-top: 300px;
background: rgba(10, 150, 10, .4);
}
<div class="background-pic"></div>
<div class="content">Profile content</div>

All you need to do is put a negative margin on the .profile element. I cleaned up your HTML and CSS so you could follow this:
.background-pic{
background-image: url('https://images.pexels.com/photos/825262/pexels-photo-825262.jpeg');
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 500px;
width: 100%;
}
.profile{
background-color: pink;
height: 500px; /* optional if you put enough content in */
margin: -100px auto 0 auto;
padding: 25px 50px;
width: 600px;
}
<div class="background-pic"></div>
<div class="profile">
Content goes here.
</div>
Demo:
https://codepen.io/staypuftman/pen/QrNZPz

In CSS of the .background-pic add a property position : absolute.
In CSS of the .profile add property position : relative, then change the padding of text inside .profile according to your need.

Related

How do i set a background image for a division with an image already in it?

I've ran out of hope for this for the past few days,what I'm basically trying to do is to do this:
CSS:
.div1{
/* background-image code */
}
HTML:
<div class="div1">
<!--Image here-->
</div>
Is it even possible to have a background image larger than the image in the div itself?
See the following example to achieve what you are looking for. Basically you can combine a color and an image by using both the background-color and background-image props at the same time. Position and scale the image with background-size and background-position. background-repeat: no-repeat; is important to be able to see the area that is the simple color background.
.div1 {
background-color: blue;
background-image: url(https://thumbs.dreamstime.com/b/forrest-27720334.jpg);
background-size: 50%;
background-position: center;
background-repeat: no-repeat;
width: 400px;
height: 300px;
}
<div class="div1">
</div>
For two images layered in this way:
.div1 {
background-image: url(https://www.realtree.com/sites/default/files/styles/site_xl/public/content/inserts/2022/imagebybarriebird-ducklings.jpg);
width: 400px;
height: 300px;
position: relative;
color: white;
background-size: 50%;
background-repeat: no-repeat;
background-position: center;
/*to center the text */
display: flex;
align-items: center;
justify-content: center;
}
.div1::before {
content: "";
position: absolute;
width: 100%;
height: 100%;
background-image: url(https://thumbs.dreamstime.com/b/forrest-27720334.jpg);
background-size: cover;
/*to set this image layer behind the duck one */
z-index: -1;
}
<div class="div1">
Example content text
</div>
you can add width and height in each img and background-image
.div1{
width: 100vw;
height: 500px;
/* background-image code */
}
img {
width : 200px;
height : 200px;
}
<div class="div1">
<img src="code.png" alt="">
<!--Image here-->
</div>
Give the img some padding and put the background image on it.
div {
width: fit-content;
}
img {
background-image: url(https://picsum.photos/id/1015/300/300);
background-size: cover;
padding: 30px;
}
<div>
<img src="https://picsum.photos/id/1016/200/300">
</div>

Slight transparent space between two divs

I have two div's above one another. The top div has a background .svg at the bottom with the same color as the background of the bottom div. These should align perfectly, however, they do not. There is an ever so slight amount of transparent space between them. This space disappears when zooming in and reappears when zooming in even further (see screenshots).
.top {
width: 100%;
height: 100vh;
background-color: #3772ff;
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20viewBox%3D%220%200%20378%20378%22%3E%3Cpath%20d%3D%22M0%2C378l378%2C-0l-0%2C-47.25l-378%2C47.25Z%22%20style%3D%22fill%3A%2301161e%3B%22%2F%3E%3C%2Fsvg%3E");
background-repeat: no-repeat;
background-position: left bottom;
background-attachment: scroll;
background-size: 100%;
margin: 0;
}
.bottom {
background-color: #01161e;
padding: 128px 20%;
}
<div class="top"></div>
<div class="bottom"></div>
Screenshots:
100% zoom:
A bit zoomed in:
Zooming in even further:
There may be a more elegant solution to be had, but simply pulling the lower element up a fraction of a pixel overcomes the sub-pixel rounding issue.
.top {
width: 100%;
height: 100vh;
background-color: #3772ff;
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20viewBox%3D%220%200%20378%20378%22%3E%3Cpath%20d%3D%22M0%2C378l378%2C-0l-0%2C-47.25l-378%2C47.25Z%22%20style%3D%22fill%3A%2301161e%3B%22%2F%3E%3C%2Fsvg%3E");
background-repeat: no-repeat;
background-position: left bottom;
background-attachment: scroll;
background-size: 100%;
margin: 0;
}
.bottom {
background-color: #01161e;
padding: 128px 20%;
margin-top: -.5px;
/* transform: translateY(-.5px); alternative approach */
}
<div class="top"></div>
<div class="bottom"></div>
Of course, you could just set the body background (or that of a container element) to hide it as well:
.container {
background-color: #01161e;
}
.top {
width: 100%;
height: 100vh;
background-color: #3772ff;
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%25%22%20height%3D%22100%25%22%20viewBox%3D%220%200%20378%20378%22%3E%3Cpath%20d%3D%22M0%2C378l378%2C-0l-0%2C-47.25l-378%2C47.25Z%22%20style%3D%22fill%3A%2301161e%3B%22%2F%3E%3C%2Fsvg%3E");
background-repeat: no-repeat;
background-position: left bottom;
background-attachment: scroll;
background-size: 100%;
margin: 0;
}
.bottom {
background-color: #01161e;
padding: 128px 20%;
}
<div class="container">
<div class="top"></div>
<div class="bottom"></div>
</div>

CSS: Make a background-image appear without having content and/or fixed sizes?

I've got this code (with no header-image visible) so far:
.wrap {
margin: 50px auto;
}
html,
body {
height: 100%;
}
body {
background-image: url('https://placebear.com/1000/800');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
background-attachment: fixed;
}
.header {
background-image: url('https://placebear.com/940/248');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
<div class="wrap">
<div class="row">
<div class="medium-12 columns header"></div>
</div>
</div>
If you give the header-image a fixed size of 248px you can see it appear. Example with visible header-image:
.wrap {
margin: 50px auto;
}
html,
body {
height: 100%;
}
body {
background-image: url('https://placebear.com/1000/800');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
background-attachment: fixed;
}
.header {
background-image: url('https://placebear.com/940/248');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
height: 248px;
border: 3px solid white;
}
<div class="wrap">
<div class="row">
<div class="medium-12 columns header"></div>
</div>
</div>
Is there a way to make it appear without using a fixed height?
Like when using a classic img-tag with just src- and alt-attribute. Then height is read out of the image-file self.
I would like to avoid fixed heights in there because if the image changes then it becomes all wrong.
I think you have to define a height if using background-image. Might be better to use img tag and just put a width of 100%, like so:
.wrap {
margin: 50px auto;
}
html,
body {
height: 100%;
}
body {
background-image: url('https://placebear.com/1000/800');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
background-attachment: fixed;
}
.header {
border: 3px solid white;
}
img {
width: 100%;
}
<div class="wrap">
<div class="row">
<div class="medium-12 columns header">
<img src="https://placebear.com/940/248" alt="Bear Image">
</div>
</div>
</div>

background-size: contain height always 0

I'm trying to make an image responsive, using contain for the background-size, but the height is always 0. I've set the height on the parent element, what am I missing?
.masthead .container {
height: 300px;
}
.masthead-title {
margin-top: 0;
margin-bottom: 0;
color: #505050;
background-image: url('../logo.png');
background-repeat: no-repeat;
background-size: contain;
background-position: center center;
}
<div class="masthead">
<div class="container">
<label for="sidebar-checkbox" class="sidebar-toggle"></label>
<h3 class="masthead-title">
</h3>
</div>
</div>
You need to specify a height for your .masthead-title.
.masthead .container {
height: 300px;
border: 1px dashed gray; /* for demo only */
}
.masthead-title {
margin-top: 0;
margin-bottom: 0;
color: #505050;
background-image: url('http://i.imgur.com/60PVLis.png');
background-repeat: no-repeat;
background-size: contain;
background-position: center center;
height: 100%; /* new */
}
<div class="masthead">
<div class="container">
<label for="sidebar-checkbox" class="sidebar-toggle"></label>
<h3 class="masthead-title">
</h3>
</div>
</div>
jsFiddle
The problem is that the <h3> tag you're using for the background-image has zero as height. So either give the .masthead-title a fixed height or do some content into the <h3> or the <a> within.
Option 1: fixed height on element which has property background-image
.masthead-title {
height: 300px;
}
Option 2: content inside the element which has the background-image property
<h3 class="masthead-title">
Test Test Test
</h3>
Option 3:
keep the fixed height on the container and set the child container with the background-image property to 100% height
.masthead .container {
height:300px;
}
.masthead-title {
height:100%;
}

Making a div "overlap" container div

I am trying to making a page similar to this:
I am trying to make the profile picture div go "below" the wallpaper div. Here is my relavent code:
CSS code:
.profile-picture {
display: inline-block;
margin-left: 10px;
width: 150px;
height: 150px;
-ms-border-radius: 50%;
border-radius: 50%;
background-repeat: no-repeat;
background-position: center center;
-ms-background-size: cover;
background-size: cover;
background-image: url("../Images/profiles/default.png")
}
.wallpaper {
width: 100%;
-ms-background-size: cover;
background-size: cover;
background-image: url("../images/Wallpapers/default.png")
}
HTML Code:
<div class="col-md-6 roundedCorners" style="background-color: white">
<div class="wallpaper">
<div class="row">
<div class="col-md-6" style="text-align: left">
<div class="profile-picture">
</div>
</div>
<div class="col-md-6">
</div>
</div>
</div>
</div>
Right now, everything is working except its putting the rounded profile picture centered vertically within the image. I would like it to float outside the wallpaper, as shown in the image above. What am I missing?
Currently your code doesn't contain any positioning styles.
You most likely want to use position: absolute for the profile picture (with a bottom and left property to set the position). When you do this, you'll also want to add position: relative to the wallpaper, so that the absolute positioning is in relation to the wallpaper.
Here's the modified code, that will get you going in the right direction:
.profile-picture {
/* set the position to absolute */
position: absolute;
/* set the bottom to be slightly outside the wallpaper boundary. Adjust this number as desired */
bottom: -50px;
/* set the left where desired. Note I removed your margin-left, since you don't need both left and margin-left */
left: 10px;
display: inline-block;
width: 150px;
height: 150px;
-ms-border-radius: 50%;
border-radius: 50%;
background-repeat: no-repeat;
background-position: center center;
-ms-background-size: cover;
background-size: cover;
background-image: url("../Images/profiles/default.png")
}
.wallpaper {
/* set the position to relative, so the profile picture position is in relationship to the wallpaper container */
position: relative;
/* you may (or may not) need this. The profile is outside the wallpaper, so would be considered "overflow". */
overflow: visible;
width: 100%;
-ms-background-size: cover;
background-size: cover;
background-image: url("../images/Wallpapers/default.png")
}
To understand more about why you want the wallpaper set to position: relative and how that works, check out this article: https://css-tricks.com/absolute-positioning-inside-relative-positioning/
In addition to the other answers, you can put the profile picture "below" the wallpaper by using the z-index style. Add the following to each class style:
.profile-picture {
z-index:4;
}
.wallpaper {
z-index:5;
}
A possible solution would be, to position your elements.
With position:relative; in your container (.wallpaper) you can position your profile picture absolute in relation to the container, so with bottom: -70px; you can push it down to the exact position you need.
.profile-picture {
position: absolute;
bottom: -70px;
display: inline-block;
margin-left: 10px;
width: 150px;
height: 150px;
-ms-border-radius: 50%;
border-radius: 50%;
border: 5px solid white;
background-repeat: no-repeat;
background-position: center center;
-ms-background-size: cover;
background-size: cover;
background-image: url(https://unsplash.it/150/150)
}
.wallpaper {
position: relative;
width: 100%;
height: 200px;
-ms-background-size: cover;
background-size: cover;
background-image: url(https://unsplash.it/2000/200)
}
<div class="col-md-6 roundedCorners" style="background-color: white">
<div class="wallpaper">
<div class="row">
<div class="col-md-6" style="text-align: left">
<div class="profile-picture">
</div>
</div>
<div class="col-md-6">
</div>
</div>
</div>
</div>