0

I have a div called "thumbs" and a javascript function populating it with images

function fillThumbs(){
    for (i=0; i<images.length; i++){
    var imageX = document.createElement("img");
    imageX.src = images[i];
    document.getElementById("thumbs").appendChild(imageX);
    }

}

then my css code

#thumbs{
margin: 0 auto;
}

#thumbs img{
width: 220px;
margin: 0 10 10 0;
}

Problem: thumbs div is not centered, nor the #thumbs img are correctly spaced out. Looks like the margin property is ignored, while the width of the images is correctly passed.

4
  • 2
    jsfiddle my friend would be so nice Commented Sep 3, 2015 at 9:37
  • #thumbs { text-align: center; } should work Commented Sep 3, 2015 at 9:38
  • 3
    Margin:0 10 10 0 -> Margin:0 10px 10px 0; Commented Sep 3, 2015 at 9:38
  • You need to specifiy a unit for your margin in addition to the values. Commented Sep 3, 2015 at 9:43

2 Answers 2

3

The margins aren't being applied because you don't have any units on them. You need px or similar (on the values that aren't 0; CSS is fine with 0s with no units):

#thumbs{
margin: 0 auto;
}

#thumbs img{
width: 220px;
margin: 0 10px 10px 0;
/*          ^^   ^^     */
}

Live Example:

var i;
function fillThumbs(){
    for (i=0; i<5; i++){
    var imageX = document.createElement("img");
    imageX.src = "https://www.gravatar.com/avatar/e459e3d0f28452212c5958e54f658ef8?s=32&d=identicon&r=PG";
    document.getElementById("thumbs").appendChild(imageX);
    }

}
fillThumbs();
#thumbs{
margin: 0 auto;
}

#thumbs img{
width: 220px;
margin: 0 10px 10px 0;
}
<div id="thumbs"></div>

Hint: To diagnose issues like this, right-click the element in your browser and choose "inspect element" (if you don't have that choice, use Chrome or Firefox). Use that, then look at the browser's dev tools to see why the CSS isn't being applied. Here's what Chrome's dev tools shows me:

enter image description here

and then if I hover the handy warning icon:

enter image description here

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

Comments

1

Your should always provide a unit as soon as the value is not equal to 0. Change your css like:

#thumbs img{
    width: 220px;
    margin: 0 10px 10px 0;
}

The margin: 0 auto; will only center the #thumbs node if it's width is known. Try giving it a width.

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.