- This topic is empty.
-
AuthorPosts
-
November 23, 2010 at 4:01 pm #30790
noahgelman
ParticipantI have a div that appears after a clicking action. The div is fixed 150px from the top but I need help centering it in the middle. Normally, it would be really easy to center it, but the div width isn’t static, it changes width so I’m having a bit of a problem. I don’t want to use javascript. I want to be able to do this with just CSS. Any ideas?
btw, margin: 0 auto isnt gonna work since the position is fixed
November 23, 2010 at 9:59 pm #72422noahgelman
ParticipantNo help?
Nooooooooo!!
And I thought the friendly title would get me some helpNovember 23, 2010 at 10:59 pm #72420Chris Coyier
KeymasterLink?
November 23, 2010 at 11:22 pm #72421TheDoc
MemberThe non-fixed width of the div makes things a little tough, because left: 50%; won’t work. Hmmmmm.
November 24, 2010 at 2:39 am #72424noahgelman
ParticipantSorry, on a private server so i can’t provide a link. I could go right:0 left:0, margin:auto and it would center it for me, but it would have to have a set width. I could also left:50% and use some margin to center it but again I would need a set width. But because it changes I’m not sure what to do. I could just use javascript to get the width and center it and yada yada yada but I want to do this with CSS alone.
November 24, 2010 at 4:29 am #72401sylenix
Memberuse this css hack to center your div:
text-align: center
firstly, wrap your div to be centered with 2 more divs;
put that css hack to the outermost div;
also, specify the width of your page or use:margin: 0 auto;
next wrapper div:
display: table;
margin: 0 auto;
your div:
display: table-cell;
and lastly, this:
html div.yours {
display: inline;
}
November 24, 2010 at 7:44 am #72362jamygolden
Member@sylenix I don’t think that would work as the div is set to
position: fixed
November 24, 2010 at 12:17 pm #72385noahgelman
ParticipantYeah, that wouldn’t work because it’s fixed. However it did give me an idea. If I wrap my current div in another div, and set that to fixed from the top and 100% width, I should be able to give my real div margin: 0 auto
That should work right?
November 24, 2010 at 12:41 pm #72393noahgelman
ParticipantFigured it out. My top answer wasn’t quite right when I tried it. But, then I remembered an article I read about how to center unordered lists in a nav when you don’t know their width. This is pretty much the same thing.
First I wrap my main div in another div, which I fix to the top. I then put left:50% on it. Then on my main div I make position relative and put left:-50% on it. And that centers it no matter what the width is.
November 26, 2010 at 1:57 am #72243soap
ParticipantIf a div doesn’t have a fixed width, isn’t it automatically 100%?
November 26, 2010 at 2:04 am #72239noahgelman
Participantyeah, unless you display it inline-block which will allow it to stretch as wide or as thin as I want
November 28, 2010 at 4:21 am #72005soap
ParticipantI thought inline-block wasn’t supported by all browsers.
November 28, 2010 at 4:55 pm #71933noahgelman
ParticipantThere is a bit of incompleteness. IE 6/7 accepts the value only on elements with a natural display: inline. But there are always workarounds
-
AuthorPosts
- The forum ‘CSS’ is closed to new topics and replies.