Talha Munir
Talha Munir

Reputation: 526

Positioning image relative to the background

I want the image to be positioned such that its center is always placed on the bottom of the blue background. It should look like this: enter image description here

I can use top:37v; but resizing the window height will not let it retain its position relative to the background.

.background{
    background-image: url("https://i.imgur.com/5Y5F5fF.png");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 0 100%;
    height: 50vh;
}
header img{
    position: relative;
    height: 100px;
    /*!*top: 37vh;*! Don't want to use this as resizing will effect its position relative to background*/
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<header>
        <div class="container-fluid d-flex justify-content-center background">
            <img src="https://i.imgur.com/3dzn4KM.png" alt="phone">
        </div>

    </header>

Upvotes: 1

Views: 134

Answers (2)

Anurag Srivastava
Anurag Srivastava

Reputation: 14413

You can use a combination of position: relative and absolute like so:

.background {
  background-image: url("https://i.imgur.com/5Y5F5fF.png");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: 0 100%;
  height: 50vh;
  position: relative;
}

header img {
  position: absolute;
  height: 100px;
  bottom: -50px;
  /*!*top: 37vh;*! Don't want to use this as resizing will effect its position relative to background*/
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<header>
  <div class="container-fluid d-flex justify-content-center background">
    <img src="https://i.imgur.com/3dzn4KM.png" alt="phone">
  </div>

</header>

Upvotes: 1

vitomadio
vitomadio

Reputation: 1140

Add this 2 lines to the style:

header img{
     position: relative;
     height: 100px;
     top:50%;
     margin-top:-50px;
 }

This should help.

Upvotes: 2

Related Questions