23

HTML:

<a id="postanad" href="postad.php">Make a Post</a> 

CSS:

#header #postanad { position: absolute; top: 50px; left: 250px ; background: #000 url(postad.gif) repeat-x 0% 100%; font: bold 19px Tahoma, 'trebuchet MS', Sans-serif; padding: 5px; } 

How do I determine the size in pixels/ems that the div element is taking so that I can make the background image postad.gif to be of the same width?

Just to be clear I don't want to determine the width dynamically , but just want to know how much space it is taking so that I can draw a background image in PS.

1
  • it will be different for every client with a different set up. Commented Jan 13, 2011 at 13:58

5 Answers 5

15

If you're comfortable with jQuery, you can just use $("#postanad").width().

Sign up to request clarification or add additional context in comments.

Comments

9

If somebody wants just to get the width of a div with simple javascript use:

document.getElementById('div_id').clientWidth; 

for more possibilitys have a look here.

Comments

4

CSS isn't a 'programming language'. You can't really make decisions, gather data, or manipulate anything the language wasn't designed to do. If you want to do that, javascript is your best choice.

What exactly do you want to have happen with the background? We might be able to help you get a CSS only solution if we know your requirements.

2 Comments

although it seems to allow notion of variables
You can manipulate the width value with calc to get a number
1

The jQuery way, like @orthod0ks said would be to use the width() function. The CSS way would be to declare a width and height

OR, another CSS way would be to:

You can also put a background on the div (like background: red;), take a screenshot, and paste into PS to see how big it is.

Lastly, Firefox has a plugin called (MeasureIt). It is rather useful for determining sizes of things in terms of pixels. So you should look into that if both solutions are not viable for you.

Good luck!

Comments

0

Is the div in question being redrawn dynamically? Because if not, the easiest thing is to set the height and width in the #header #postanad style.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.