thom_p
thom_p

Reputation: 219

CSS margin issue with floating dynamic elements withing a centered div

I'm coding a Wordpress theme and I'm having an issue displaying 5 dynamic post thumbnails next to each other (left floated) with a 20px right margin within a centered div.

here's a link to a page: http://www.lesfourchettes.net/info (the issue occurs when you click on "Les Prèfs" in the top navigation: the top menu and the below content moves 10px to the left.)

Here's the issue:

The whole thing is a bit complicated to explain. But i feel like there is surely an easy CSS solution to my problem. Perhaps something to do with the overflow property, display property or someting like that... I'm just not good enough with CSS yet to identify the solution and my issue being so specific, I haven't found the answer on the web.

i feel like the problem lies within those lines of CSS:

#wrapper {
width: 960px;
height: auto;
margin: 0 auto 0 auto;
}

#prefs {
width: 980px;
height: 390px;
margin: 69px 0 0 0;
}

.prefs-thumbs {
position: relative;
float: left;
margin: 0 20px 20px 0;
}

Any help would be greatly appreciated. Cheers!

Upvotes: 2

Views: 697

Answers (3)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196227

First thing to solve is the actual fitting of the elements in your block.

You need to remove the right-margin from the last element in each line..

So either set a class to the last item which overrides the margin with marign-right:0 or (for modern browsers) do it purely in CSS with

.prefs-thumbs:nth-child(5n+1){
    margin-right:0;
}

reference: http://www.w3.org/TR/selectors/#nth-child-pseudo

You also need to remove the border from the images as that gets added and instead of 176 pixels each of your images occupies 180 pixels (it has 2 pixel border around it)

So, correct your math first..

The issue with the moving of the content, is that the scroll bars appear once the content exceeds the page height (as expected). One solution is to keep the vertical scrollbar alwats visible, as @wsanville suggests in his answer.

Upvotes: 0

Vivek Viswanathan
Vivek Viswanathan

Reputation: 1963

I see the centered alignment shifting toward the left because of the vertical scroll that appear on the right side of the page.

Upvotes: 0

wsanville
wsanville

Reputation: 37516

You're noticing the 10px shift because the page height is increasing, and a horizontal scroll bar is being added by the browser.

My preferred solution to this problem is to always show the horizontal scroll bar, with the following CSS (works in all modern browsers):

html { overflow-y: scroll; }

Upvotes: 1

Related Questions