Making a div "overlap" container div - html

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>

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>

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

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.

CSS image inside another image with overflow:hidden

So I want to center an image inside another image (a computer screen), but it seem not to work with the overflow:hidden. I put posiition:absolute on the image inside, but then the other image disappered.
My CSScode:
.desktop-image {
background-image: url("image-1.png");
background-repeat: no-repeat;
background-size: contain;
background-position: center;
}
.desktop-image img {
width: 20%;
position: relative;
z-index: -999;
left: 40%;
}
My HTML:
<div class="desktop-image">
<img src="image-2.jpg" alt="">
</div>
</div>
set your "computer screen" image as a background of a div
div {
background: url('computer-screen.png');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}

CSS - Center Image at all times

I have an image that I am using inside a Div.
I was wondering how I could center the image both vertically and horizontally at all times.
At the moment, when I stretch it, it seems to be stretching from the top left corner, is there a way I could have it centered at all times.
I have searched the web and tried various things but can not seem to get it to work.
this is my html:
<div class="container"></div>
here is my css:
.container {
width: 100%;
height: 75vh;
background-image: url("http://i.stack.imgur.com/2OrtT.jpg");
background-size: cover;
background-repeat: no-repeat;
background-position: 50% 50%;
}
Any help or advice is appreciated, thank you in advance.
EDIT you can see that when I stretch the browser, it
seems to be stretching from the left, i want to be able to have the
Image centered so it stretches from all sides equally*
I got the solution..!
Here is your html file :
<div class="container"></div>
CSS file :
.container {
width: 100%;
height: 75vh;
position: absolute;
margin: auto;
background-image: url("http://i.stack.imgur.com/2OrtT.jpg");
background-size: cover;
background-repeat: no-repeat;
background-position: 50% 50%;
top: 0;
left: 0;}
Here is working demo
background-position: center will align your image in vertical and horizontal center.
Check below snippet:
* {
margin: 0;
padding: 0;
}
.container {
width: 100%;
height: 100vh;
background-image: url("http://i.stack.imgur.com/2OrtT.jpg");
background-repeat: no-repeat;
background-position: center;
}
<div class="container"></div>
.container {
width: 100%;
height: 75vh;
background-image: url("http://i.stack.imgur.com/2OrtT.jpg");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
background-size:auto;
}
<div class="container"></div>
HTML :
<div class="container"></div>
CSS :
.container{width: 100%;height: 75vh;position: absolute;margin: auto;background-image: url("http://i.stack.imgur.com/2OrtT.jpg");background-size: cover;background-repeat: no-repeat;background-position: 50% 50%;top: 0;left: 0;}

Full screen responsive background - then normal

I'm trying to create a full screen background image that re sizes across devices.
The first content section I want to always appear at the "bottom" of the devices screen after the full image. I've done this already.
I now want to carry on adding div elements but the div gets pushed back to the top.
I think it may be my position tags..?
Code
body {
background-image: url(http://www.gettyimages.co.uk/gi-resources/images/CreativeImages/Hero-527920799.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.content {
position: absolute;
top: 100%;
width: 100%;
background: #FF6969;
}
.moreContent {
height: 100px;
top: 100%;
width: 100%;
background: white;
}
html:
<div class="content">
Hello this is my content
<br/>
<br/>
<br/>
<br/>Lots of content
</div>
<div class="moreContent">
this should be below 'content' div
</div>
(also using bootstrap for the real project)
Fiddle
From what I understand you are looking for something like this:
body {
background-image: url(http://www.gettyimages.co.uk/gi-resources/images/CreativeImages/Hero-527920799.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
margin:0
}
.content {
position: absolute;
bottom: -100px;
width: 100%;
background: #FF6969;
height: 100px;
}
.moreContent {
position: absolute;
height: 100px;
bottom: -200px;
width: 100%;
background: white;
}
<body>
<div class="content">
Hello this is my content
<br/>
<br/>
<br/>
<br/>Lots of content
</div>
<div class="moreContent">
more content...
</div>
</body>
I would do it a bit differently, considering I don't want to keep adding -100px, -200px etc to my div classes. That is a very dirty way of writing css.
So I would create a container Class for all my div elements:-
<body>
<div class="container">
<div class="content">
Hello this is my content
Lots of contentxxx
</div>
<div class="moreContent">
more content...vcbcvbcv
</div>
</div>
</body>
and then use the following css:-
body {
background-image: url(http://www.gettyimages.co.uk/gi-resources/images/CreativeImages/Hero-527920799.jpg);
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
.container{
bottom:0;
position:absolute;
width:100%;
}
.content {
height: 100px;
width: 100%;
background: #FF6969;
}
.moreContent {
height: 100px;
width: 100%;
background: white;
}
I personally find it much more general and elegant.
https://jsfiddle.net/8xrap3o5/6/