Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position DIV under Background Image, regardless of viewport (responsive)

I have a big background image of 1920x550px and I want to place a div directly under it. Since I don't know how to display the full image, I used a dirty trick and actually filled the image with a transparent part so it is a 1920x1080 image, then I display it with this:

.bg-image-big{
    background: url(../images/header-bg-big.png) no-repeat center center fixed;
    background-color: #f7f7f7;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

This stretches it to be always full screen. Two problems now: That does not work on mobile (cuts the image) and I have no idea how to place the inner div exactly below the "end" of the actual banner. In theory and a 1920x1080 screen it is a 550px margin-top.

<div class="section bg-light-gray bg-image-big">
   <div class="inner">
      <!-- placed right under the end of the banner -->
   </div>
</div>

Any better approach to that (pretty sure there is). Any hint appreciated!

For the matter of it, I am using Bootstrap3 and FullPage.js

//EDIT:

Visualization as per request:

What I want: enter image description here

What I have: Full Desktop enter image description here

Responsive enter image description here

This is not about the 6/8/12 wide but about the position of those elements. hope this helps a little more...

like image 544
PrimuS Avatar asked Feb 23 '16 09:02

PrimuS


People also ask

Can a background image be responsive?

Here's how to create responsive background images with CSS: Use the background-size property to encompass the viewport. Give this property a cover value that will tell a browser to scale the background image's heights and width so that they always remain equal to or greater than the height/width of the device viewport.

Which attribute is used to control the position of an image in the background?

To control the position of an image in the background, use the background-position property.


2 Answers

A slightly different solution. You could control the ratio of the image container by using a canvas HTML Element inside of it. The only thing we would have to fix is the width of the container under the image. Have a look at this page to get a basic understanding how to use CSS Media Queries. http://www.w3schools.com/css/css_rwd_mediaqueries.asp

body {
  margin: 0;
}
canvas {
  display: block;
  width: 100%;
  height: auto;
}
.bg-image-big {
  background: url(http://viceland-assets-cdn.vice.com/viceblog/47451647MCQ-cloudcity.jpg) no-repeat center center;
  background-color: #f7f7f7;
  background-size: cover;
}
.wrapper {
  width: 100%;
  padding: 10px;
  box-sizing: border-box;
  background-color: #f0f0f0;
}
.inner {
  position: relative;
  display: block;
  width: 50%;
  padding: 30px;
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid black;
  text-align: center;
  box-sizing: border-box;
}
.inner p {
  display: none;
}


@media only screen and (min-width: 280px) {
  .inner {
    width: 100%;
  }
  .inner p.col-12 {
    display: block;
  }
  /* 12-Columns Width */
}

@media only screen and (min-width: 768px) {
  .inner {
    width: 66.66%;
  }
  .inner p.col-8 {
    display: block;
  }
  /* 8-Columns Width */
}
@media only screen and (min-width: 1024px) {
  .inner {
    width: 50%;
  }
  .inner p.col-6 {
    display: block;
  }
  /* 6-Columns Width */
}
<div class="bg-image-big">
  <canvas width="100" height="25" />
</div>
<div class="wrapper">
  <div class="inner">
    <p class="col-6">min-width: 1024px</p>
    <p class="col-8">min-width: 768px</p>
    <p class="col-12">min-width: 280px</p>
    <span>Behold, some content!</span>
  </div>
</div>
like image 64
Roland Krinner Avatar answered Oct 23 '22 05:10

Roland Krinner


I had the same problem. Here's how I solved it.

The background image was attached to the body element. The original image size was 1920px by 1080px.

I calculated the ratio of the height to width, which came to 56.25.

For the element that I wanted to position below the image, I used this:

#page-area {
    margin-top: 56.25vw;
}

As the viewport width changes, the image height does too. So I set the margin-top to be the same height as the image.

like image 24
millionleaves Avatar answered Oct 23 '22 05:10

millionleaves