Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Box+text over image

Firstly, I would like to show you a image(made in paint).

alt text

Okay "current" is what I have now. I want to place a box over the image to the right, with black background, and then have text inside this box.

I tried myself using z-index and so, but without any success. Here's what I tried:

<div> <!-- start div for image -->
  <img style="z-index: -1;" src="1.jpg" width="860" height="240"> <!-- the image -->
</div> <!-- end div -->
<div style="z-index: 1; width: 300px; background: #000; position: relative;">
  <div style="margin: auto;">
    text text text
  </div>
</div>

but this didnt turn out any good. How can i do this?

like image 242
Karem Avatar asked Nov 11 '10 12:11

Karem


People also ask

How do you overlay text on an image in CSS?

CSS position property is used to set the position of text over an image. This can be done by enclosing the image and text in an HTML “div”. Then make the position of div “relative” and that of text “absolute”. The absolute elements are positioned relative to their parent (div).

How do I overlay text boxes in photos?

Use a text box to add text on top of a photoOn the Insert tab, in the Text group, click Text Box, drag to draw a text box anywhere near the picture, and then type your text. To change the font or style of the text, highlight the text, right-click it, and then select the text formatting you want on the shortcut menu.

How do I make text over an image responsive?

The simplest method to get text over image (or an image under text) is to add a background image using cascading style sheets, or CSS. There are a few drawbacks to this simple technique: You can't use an alt tag to describe the image, which hurts your site's accessibility.


1 Answers

Something like this?

http://jsfiddle.net/QGMPB/1/

HTML:

<div id="wrap">
    <img src="http://jsfiddle.net/img/logo.png" />
    <div id="text">text</div>
</div>

CSS

#wrap {
    position:relative; /* make this relative to have the inner div absolute without     breaking out */
    width: 200px;  /* fix the width or else it'll be the entire page's width */
    background: silver; 
    border: 1px solid grey
}

#text {
    position: absolute; 
    width: 40px; 
    right: 0; 
    top: 0; 
    bottom: 0; 
    background: black; 
    color:white
}
like image 200
Stephan Muller Avatar answered Oct 29 '22 08:10

Stephan Muller