Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

CSS Forum


You are currently viewing our CSS Forum as a guest. Please register to participate.
Login



Reply
Old 11-28-2009, 09:15 AM javascript in css
Skilled Talker

Posts: 55
Trades: 0
I got a javascript function (width()) that returns an integer value. How can I use it in a css style for a div width:
Code:
 <div style="background-color:#24464F; position:absolute; top:0; left:0; width:"width()"px; height:350px;">
    	<p>Hello World2 </p>
    </div>
philmetz is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-28-2009, 09:20 AM Re: javascript in css
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You can not.
Css don't allow you to do that.
But you can alter a css value from javascript:
Code:
elm=document.getElementById('someElment')
elm.style.width=width()+"px"
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-28-2009, 09:33 AM Re: javascript in css
Skilled Talker

Posts: 55
Trades: 0
and that is done how:
Code:
....
	<script type="text/javascript"> 
        function width(id) 
        { 
            var width = document.body.clientWidth;
            elm=document.getElementById(id)
			elm.style.width=document.body.clientWidth+"px"
        } 
    </script>
</head>

<body>
    <div "width(this.id)"; style="background-color:#24464F; position:absolute; top:0; left:0; width:"width()"px; height:350px;">
    	<p>Hello World2 </p>
    </div>
</body>
philmetz is offline
Reply With Quote
View Public Profile
 
Old 11-28-2009, 09:43 AM Re: javascript in css
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
I find that

width:100%;

does that just as easily.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-28-2009, 11:17 AM Re: javascript in css
Skilled Talker

Posts: 55
Trades: 0
But then when I resize the browser and divs within that div are compressed and so doesnt look good
philmetz is offline
Reply With Quote
View Public Profile
 
Old 11-28-2009, 11:26 AM Re: javascript in css
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
So you specify everything in relative units (% or em) so they all resize and move relative to each other.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-28-2009, 01:01 PM Re: javascript in css
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Using JavaScript to fix display issues is usually a bad idea, because if it is turned off, your page breaks. I forget the exact percentage, and it's only an estimate anyway, but it is something like 6-10% of users have browsers set with JS disabled.

It's ok to use JS to alter display if it still looks ok without JS, however, but I don't recommend relying on it with certainty, unless you have a web application which will break without it anyway, in which case you should be alerting your users to turn it on.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to javascript in css
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24497 seconds with 12 queries