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

Raul Rodriguez
PLUS
Raul Rodriguez
Courses Plus Student 221 Points

BootStrap text over image issue

http://raulphotodesign.com/sample/

Hello i’m really struggling to put text over an image in bootstrap framework, I really appreciate some help. I also used image-background css but this isn’t help either ( background-image: url();

i only need to make the space of the opacity to make look like this. http://raulphotodesign.com/sample/Home.jpg

is this something to do with position? or z-index?

All help welcome, Thankyou.

4 Answers

Christian Lawrence
Christian Lawrence
3,941 Points

There are a number of ways you can do this and you are thinking along the right lines.

I would do something like: http://codepen.io/anon/pen/rxjdRw

<div class="cont">
  <div class="text-box">
    <h2>Title</h2>
  </div>
  <img src="path/to/img.jpg" alt="">
</div>

<style>
.cont
{
  width: 25%;
  height: 200px; /* for demo, the image <img> should create the height */
  background-color: red;
  position: relative;
}
.text-box
{
  position: absolute;
  background-color: rgba(0,0,0,0.5);
  top: 0;
  bottom: 0;
  height: 100%;
  text-align: center;
  width: 100%;
  display: table;
  z-index: 20;
}
.text-box h2
{
  color: #fff;
  display: table-cell;
  vertical-align: middle;
}
.cont img
{
  display: block;
  widht: 100%;
  z-index: 10;
}

</style>

You could also set the image as a background-image of .cont but if you want to reuse the class chances are you'd want it in the HTML as actual content.

Christian Lawrence
Christian Lawrence
3,941 Points

You may also wish to look into flex-box

Raul Rodriguez
Raul Rodriguez
Courses Plus Student 221 Points

i cant see the code that you are trying to show me.

Raul Rodriguez
Raul Rodriguez
Courses Plus Student 221 Points

yea but i have that done aready what i mean is the space in th opacity on top on the botton and in the left and right

Christian Lawrence
Christian Lawrence
3,941 Points

You can either use a div with absolute positioning and display:table with the child being display: table-cell, but a more modern solution being flex-box (but less supported). Looks ok for me on Chrome Mac, what browser are you using?

Christian Lawrence
Christian Lawrence
3,941 Points

Looks good and works. However, consider if the text needs to change and some titles become longer. If the text starts wrapping on smaller screens it may not be center aligned on all the boxes. Of course you could add media queries to change the font-size and margin ect, but if text is in client control via a CMS then there are endless of future possibilities of text being input which you cannot account for with fixed margins/padding.

Raul Rodriguez
PLUS
Raul Rodriguez
Courses Plus Student 221 Points

Yes i will be using wordpress :( so the best way to do this is with code you give me?

The only thing i dont see in the code that you give me is the marging of the opacity.

Christian Lawrence
Christian Lawrence
3,941 Points

Try the code out, but also see what happens with longer words and smaller screens.

I set the opacity as background-color: rgba(0,0,0,0.5);