Link to home
Start Free TrialLog in
Avatar of BlearyEye
BlearyEyeFlag for United States of America

asked on

HTML: position text on top of image

I'm working on an epub eBook in calibre. It is intended to be responsive. I can make full use of CSS 3.

In one part of the book I'd like to put text on top of an image.
The image must be horizontally centered on the page.
The text must be horizontally centered on top of the image, 15% down from the top.
The image has a blank section 15% from the where the text is to go. In a responsive environment, as the size of the viewport changes, the image and consequently the blank section will also change in size. I want the text to be positioned in that blank section.

I'm having trouble getting the image horizontally centered and in getting the text to both center and be the right proportional distance down.

Here's what I have so far. There's a corresponding JSFiddle at I created a JSFiddle for what I have now: http://jsfiddle.net/blearyeye/d888kv7w/.
CSS
.block_17 {
    display: block;
    color: #773A77;
    font-family: "Filosofia", sans-serif;
    font-size: 2em;
    font-weight: bold;
    line-height: 1.5;
    page-break-after: avoid;
    text-align: center;
    text-indent: 0;
    margin: 0;
    padding: 0
    }

Open in new window

HTML
<div style="display:inline;position:relative">
<img src="TheImage.png" style="position:absolute;"/>
  
<p class="block_17" id="calibre_pb_20" style="inline;top:70;width:100%;position:absolute;">The Desired Text</p>
</div>

Open in new window


There are a number of problems I'm having.  The image isn't being centered in the viewport. The text isn't being centered at all. The text isn't being moved down at all, even with an absolute top attribute.
Avatar of Edwin Hoffer
Edwin Hoffer
Flag of United States of America image

Hello BlearyEye,

Don't use the <p> tag in <div> code

Here as an example:

<div style="display:inline;position:relative">
<img src="http://bdluv.com/xmisc/misc/Cover%20Image%20Flowers%20House.png" style="position:absolute;"/>
</div>  
<p class="block_17" id="calibre_pb_20" style="top:70;width:100%;position:absolute;">Text To Display</p>

Open in new window


Hope this helps you
Thanks
Edwin
Part of you CSS is not valid because you are not specify a unit of measure... what does top:70 mean? 70px; 70%? 70vh?

If you want it responsive you probably should be using vh and vw for most units of measure; but I am not going to waste my time guessing at what the page looks like.  Post a link to the page, which I suspect probably has a lot of problem when I see inline styles being used.

Cd&
Avatar of BlearyEye

ASKER

Being an ebook, it's kind of hard to post a link to the page. But you're asking for more context, which is reasonable. I'll add in the rest of the CSS.
Well, this has been a learning experience. It seems that epub generally assumes that the program that renders it to the viewer will choose the base font size and line spacing.  (This lets the reader specify a preferred size and spacing.) So unless I fix it, the font size can be inconsistent with the size of the graphic as it's scaled according to viewport.

So ... I made the change that Edwin Hoffer suggested and things started looking better. As COBOLdinosaur noted the 70 by itself is meaningless. I changed it to 40% and it seems about right. So ... the current problem is that the text is centered in the viewport but the graphic is not. I think if I can get that sorted I'll be well on my way.

I updated the fiddle, http://jsfiddle.net/d888kv7w/10/.
ASKER CERTIFIED SOLUTION
Avatar of Edwin Hoffer
Edwin Hoffer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just what I needed, thanks.
You are welcome :)