Solved

HTML: position text on top of image

Posted on 2015-01-24
7
187 Views
Last Modified: 2015-02-01
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.
0
Comment
Question by:BlearyEye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 14

Expert Comment

by:Edwin Hoffer
ID: 40568296
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
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 40568379
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&
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 40571805
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:BlearyEye
ID: 40576608
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/.
0
 
LVL 14

Accepted Solution

by:
Edwin Hoffer earned 500 total points
ID: 40576726
Hello BlearyEye,

The image you are using is not a full width image, but if you still want to use this as background then use the below code in your "body" tag:

body{	
	background-image: url(http://bdluv.com/xmisc/misc/Cover%20Image%20Flowers%20House.png);
	background-position: center;
	background-repeat: no-repeat;
}

Open in new window


In above code you can change background-position to: top left, center, right left, etc. and you want to repeat the background then change "no-repeat" to "repeat"

Thanks
Edwin
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 40582959
Just what I needed, thanks.
0
 
LVL 14

Expert Comment

by:Edwin Hoffer
ID: 40583205
You are welcome :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question