Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to center text links within an image 1

Status
Not open for further replies.

jwhite68

IS-IT--Management
Jun 14, 2007
77
BG
Hi,

Can anyone advise me how to achieve the centering of text links within an area of a gif image.

The idea is that the top gif image will have one text link inside, centred between the end of the 'upload multiple listings' and the right end of the gif.

The bottom gif image will have five text links inside which need to be also centred in the area between the end of the 'upload multiple listings' and the right hand side of the gif.

Here is my current code:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="test-textinimage.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="0" cellspacing="20" cellpadding="0" class="t874">
    <td class="div_b">
			<span class="titext"><strong>Select one of the options below</strong></span>
			  <div class="div_b_1">
			    <div class="div_b_1_indent">
			    <a href="index2.php?mode=abc" ><span>Option 1</span></a>
			    </div>
			 </div>
			 <div class="div_b_2">
			   <div class="div_b_2_indent">
			    <a href="index2.php?mode=abc"><span>Option 2a</span></a>
			    <a href="index2.php?mode=def" ><span>Option 2b</span></a>
			    <a href="index2.php?mode=ghi" ><span>Option 2c</span></a>
			    <a href="index2.php?mode=jkl" ><span>Option 2d</span></a>
			    <a href="index2.php?mode=mno"><span>Option 2e</span></a>
			   </div>
			 </div>
			 <br><br>
  </td>
</table>
</body>
</html>


Here is the css:

Code:
td.div_b{
	min-height: 80px;
	background: url(elements/asb.gif) top left no-repeat;
	background-color: #F4F3EE;
	border: 1px solid #CDCDCD !important;
}
.div_b_1{
    width:868px;
	height: 45px;
	background-color: #F4F3EE;
    background: url(elements/01.gif) top left no-repeat;
}
.div_b_1_indent a{
    width: 628px;
	border-top: 55px;
	border-left: 240px;
    display: block;
	float: left;
	font-weight: bold;
	text-align: center;
}
.div_b_2 {
    width:868px;
	height: 45px;
	background-color: #F4F3EE;
    background: url(elements/01.gif) top left no-repeat;
}
.div_b_2_indent a{
    width: 628px;
	border-top: 55px;
	border-left: 240px;
    display: block;
	float: left;
	font-weight: bold;
	text-align: center;
	}
table.t874{
	width: 874px;
	/*margin: 0px 63px 0px 63px !important;*/
}
span.titext{
	display:block;
	padding-left:50px;
	color:#606060;
	margin:10px;
	line-height:28px;
}


You can see the current (wrong) result here:


What I really need is for the div_b_1_indent and div_b_2_indent to be positioned so that the divs themselves start at the position just after the end of 'upload multiple listings' and have a width until the end of the gif image; and I need to centre the text links within the area available.
 
Hi,

The images are already in the divs as background images - elements/01.gif. The problem is that I dont understand how to align the text links within these, because I need the centering to be with an offset taken into account - so that the 'upload multiple listings' part of the image is ignored.
 
I have worked out a solution, that works.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="test-textinimage2.css" rel="stylesheet" type="text/css">
</head>
<body>
<table border="0" cellspacing="20" cellpadding="0" class="t874">
    <td class="div_b">
			<span class="titext"><strong>Select one of the options below</strong></span>
			  <div class="div_b_1">
			    <div class="div_b_1_indent">
			    <a href="index2.php?mode=abc" ><span>Option 1</span></a>
			    </div>
			 </div>
			 <div class="div_b_2">
			   <div class="div_b_2_indent">
			    <a href="index2.php?mode=abc"><span>Option 2a</span></a>
			    <a href="index2.php?mode=def" ><span>Option 2b</span></a>
			    <a href="index2.php?mode=ghi" ><span>Option 2c</span></a>
			    <a href="index2.php?mode=jkl" ><span>Option 2d</span></a>
			    <a href="index2.php?mode=mno"><span>Option 2e</span></a>
			   </div>
			 </div>
			 <br><br>
  </td>
</table>
</body>
</html>

The new version of the css:

Code:
td.div_b{
	min-height: 80px;
	background: url(elements/asb.gif) top left no-repeat;
	background-color: #F4F3EE;
	border: 1px solid #CDCDCD !important;
}
.div_b_1{
    width:868px;
	height: 45px;
	background-color: #F4F3EE;
    background: url(elements/01.gif) top left no-repeat;
}
.div_b_1_indent {
	display: block;
    margin-top: 8px;
	margin-left: 190px;
	text-align: center;
}
.div_b_1_indent a{
	padding-left: 30px;
	font-weight: bold;
}
.div_b_2 {
   width:868px;
   height: 45px;
   background-color: #F4F3EE;
   background: url(elements/01.gif) top left no-repeat;

}
.div_b_2_indent {
	display: block;
    margin-top: 8px;
	margin-left: 190px;
	text-align: center;
}
.div_b_2_indent a{
	padding-left: 30px;
	font-weight: bold;
}
table.t874{
	width: 874px;
	/*margin: 0px 63px 0px 63px !important;*/
}
span.titext{
	display:block;
	padding-left:50px;
	color:#606060;
	margin:10px;
	line-height:28px;
}

You can see how this looks here:


Hope this helps anyone who is trying to get text links aligned within an image, where they need the text centred from a particular offset. In this case, the margin-left 190px roughly equates to the end of the 'upload multiple listings' text - so that the text links get centred in the remaining horizontal section of the image.

If anyone has a more elegant solution, then I'd love to hear. I can already see that I dont need to have 4 divs used here. I could just have used 2 css entries instead of 4 - re: div_b_1 and div_b_1 a, and the div_b_2 and div_b_2 a. Could have just used one set, and referenced that in both places in the code.
 
Actually the correct property in that case should be the padding-left property, and you need to subtract the amount of padding from your width, so that your overall width is what you wanted it to be.

You should only need to set the padding for the div that holds your image.

So:

Code:
.div_b_1{
    width:[red]643[/red]px;
    height: 45px;
    background-color: #F4F3EE;
    background: url(elements/01.gif) top left no-repeat;
    [red]padding-left:225px;
    text-align:center;[/red]

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
How can I center this div (div_b_1) within the div_b?
The graphic is not perfectly centred, so getting div_b_1 centred as a div should address this. Any ideas?
 
Vacunita,

I noticed that your solution works fine in Firefox, but to make it work in IE I have to set the width: 868px. I read somewhere that IE doesnt implement the box model well. Do you know if there is a workaround to have a common solution for both?

Thanks.
 
Since your div_b isn't really a div but a table cell <td> you can use the table's cell native align property and set it to center.

You'll notice however that it affects your <span> title as well, so you may need to apply a text-align:left to the span if you want to keep the title text to the left.

With that said, I would strongly advice that you drop the table, there really is no point to it, as its a single celled table, and your not actually displaying tabular data.

Use a real div instead, and then use the margin-left and right properties for div_b_1, and set them to auto. This will make the Div be centered.

Remember to give the new div the width you are assigning to the surrounding table.

All in all your CSS should be:
CSS:
.div_b{
        width:874px;
	min-height: 80px;
	background: url([URL unfurl="true"]http://www.varnarentals.com/elements/asb.gif)[/URL] top left no-repeat;
	background-color: #00F3EE;
	border: 1px solid #CDCDCD !important;
text-align:center;

align:center;
}
.div_b_1{
    width:868px;
margin-left:auto;
margin-right:auto;
height: 45px;
	background-color: #F4F3EE;
    background: url([URL unfurl="true"]http://www.varnarentals.com/elements/01.gif)[/URL] top left no-repeat;
}
.div_b_1_indent {
	display: block;
    margin-top: 8px;
	margin-left: 190px;
	text-align: center;
}
.div_b_1_indent a{
	padding-left: 30px;
	font-weight: bold;
}
.div_b_2 {
margin-left:auto;
margin-right:auto;

   width:868px;
   height: 45px;
   background-color: #F4F3EE;
   background: url([URL unfurl="true"]http://www.varnarentals.com/elements/01.gif)[/URL] top left no-repeat;

}
.div_b_2_indent {
	display: block;
    margin-top: 8px;
	margin-left: 190px;
	text-align: center;
}
.div_b_2_indent a{
	padding-left: 30px;
	font-weight: bold;
}
table.t874{
	width: 874px;
	/*margin: 0px 63px 0px 63px !important;*/
}
span.titext{
	display:block;
	padding-left:50px;
	color:#606060;
	margin:10px;
	line-height:28px;
text-align:left;
}

And your HTML would be:
HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html><head>


<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled Document</title>
<link href="test-textinimage2_files/test-textinimage2.css" rel="stylesheet" type="text/css">
</head><body>
<div class="div_b">
			<span class="titext"><strong>Select one of the options below</strong></span>
			  <div class="div_b_1">
			    <div class="div_b_1_indent">
			    <a href="[URL unfurl="true"]http://www.varnarentals.com/index2.php?mode=abc"><span>Option[/URL] 1</span></a>
			    </div>
			 </div>
			 <div class="div_b_2">
			   <div class="div_b_2_indent">
			    <a href="[URL unfurl="true"]http://www.varnarentals.com/index2.php?mode=abc"><span>Option[/URL] 2a</span></a>
			    <a href="[URL unfurl="true"]http://www.varnarentals.com/index2.php?mode=def"><span>Option[/URL] 2b</span></a>
			    <a href="[URL unfurl="true"]http://www.varnarentals.com/index2.php?mode=ghi"><span>Option[/URL] 2c</span></a>
			    <a href="[URL unfurl="true"]http://www.varnarentals.com/index2.php?mode=jkl"><span>Option[/URL] 2d</span></a>
			    <a href="[URL unfurl="true"]http://www.varnarentals.com/index2.php?mode=mno"><span>Option[/URL] 2e</span></a>
			   </div>
			 </div>
			 <br><br>
</div>
</body></html>

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I will look at this. Ive removed the div_b_1 and div_b_2 based on your earlier suggestion now, and need to apply your thoughts to the revised version.

My bigger issue now is:

I noticed that your solution works fine in Firefox, but to make it work in IE I have to set the width: 868px. I read somewhere that IE doesnt implement the box model well. Do you know if there is a workaround to have a common solution for both?



 
Wait, I never said to remove div_b_1 or 2, just modify their widths.

Yes IE has problems with the box model in that instead of adding the padding and border and margins to the overall dimensions of an object, it takes it from the object dimensions, so it reduces the available space for the objects content instead of increasing an objects dimensions.

Look here for potential workarounds.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Sorry, I meant that I removed div_b_1_indent and div_b_2_indent. ??????????????????????????????????????????????
 
Sorry, I meant that I removed div_b_1_indent and div_b_2_indent.?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top