Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

John O'Connor
seal-mask
.a{fill-rule:evenodd;}techdegree
John O'Connor
Front End Web Development Techdegree Student 14,113 Points

Positioning a Button Over an Image in Bootstrap 4

Hey everyone I am new to development and looking for some guidance. I am working on a project that requires placing a large hero image directly below the nav. Over the image I would like to place a call to action button. I want to have the image span the full width of the screen from edge to edge and for the button and image to be responsive and adjust according to the size of the viewport. Currently I have the image and button over it but I am struggling to position the image to match the full width of the screen.

          <body>
             <div class="img-wrapper">
                <img class="img-responsive" src="img/TheFEWHeaderImage.jpg">
             <div class="img-overlay">
                <button class="btn btn-md btn-success">Button</button>
             </div>
             </div>
          </body>
          ```
```css
 .img-wrapper {
  position: relative;
  width: 100%;
 }

.img-overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  text-align: center;
}

.img-overlay:before {
  content: ' ';
  display: block;
  /* adjust 'height' to position overlay content vertically */
  height: 33%;
}

So as I said the image comes up small and not taking up the full viewport. Thanks for the help!

1 Answer

Máire van Blerck
Máire van Blerck
11,475 Points

You'll first have to make sure they have the same "position" and give the button a larger "z-index". Zo write by both of the element in your css and give the element on top for example a z-idex of 10.

divA, #divB{

position: absolute; }

divB{

z-index: 10; }

If you're about to use this trick for the menu you can also use position fixed.

divB{

position:fixed; top:10px; left:10px; }

I think you'll need to work on your HTML as well because the way your using a wrapper or an extra div isn't logical for me.

You'll need to use the wrapper to wrap up the image/menu and the button. So they're forming a group together.

<div id="wrapper_menu"> <menu></menu> <button></button> </div> <!-- end of wrapper_menu --> <div id="content"> </div>

You can now give "menu" a background-image in the css and the button a z-index. Visit CSS Tricks for more information on how to use background-image property.

Good luck! Máire