0

I'm trying to use adjust CSS-properties using jQuery:

  • the menu_div should be the same width as the screen:

    $("#menu").css({width: (theWidth - 10)});
    
  • the gallery_img should be at the left side of this div, the contact_img should be on the right side:

    $("#gallery_img").css({left: 0 + 'px'});
    $("#contact_img").css({right: 0 + 'px');
    

The menu-div and the images both have position: absolute. What am I doing wrong? All images are displayed on top of each other.

2
  • If it's 0px you can simply use 0. Commented Nov 8, 2010 at 15:37
  • How are you defining "theWidth" and you said you want the menu width the same as the screen width so why the - 10? Plus as Pekka says we need to see some other code. Your HTML and CSS of the #menu and side bits would help. Commented Nov 8, 2010 at 15:38

3 Answers 3

3

You have a syntax error in your code:

$("#contact_img").css({right: 0 + 'px');
//                                    ^ missing closing brace }

Also, your CSS width assignment for the #menu element isn't followed by "px", which invalidates the rule and may cause it to fail in some browsers:

// Incorrect
$("#menu").css({width: (theWidth - 10)}); 

// Corrent
$("#menu").css({width: (theWidth - 10) + "px" });

Note that, if you're planning on using a numeric literal you may as well just use a string on its own and avoid the concatenation. You can also pass the css property and value as separate arguments:

$("#contact_img").css({right: '0px'});
$("#contact_img").css('right', '0px');
Sign up to request clarification or add additional context in comments.

2 Comments

Good catch, missed the brace. but there's also a missing unit in the #menu bit. Should have a + 'px' as well most likely.
@MarcB: Good catch yourself :-) Added it to the answer.
1

You have some missing braces, perhaps a simpler way to do it would be:

$(document).ready(function () {
        var theWidth = $(window).width();
        $("#menu").css('width', theWidth - 10);
        $("#gallery_img").css('left', 0);
        $("#contact_img").css('right', 0);
    })     

Comments

0

Position absolute will do that. your left for the gallery_img should be 0px... then your contact img's left attribute should be the left for the #menu + #menu's width in px.

if you do not specify the left attribute with a position absolute you will ultimately just stack everything.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.