Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Div alignment centering

Posted on 2013-11-21
9
Medium Priority
?
238 Views
Last Modified: 2013-11-21
Hi Guys,

I am not sure why I am still having difficulty trying to get a 1000px div to center on a page.

In fact, it's exactly how the navigation banner or footer works on this experts exchange website.

The 'center' div with width 1000px will remain centered on the page but the background extends all the way to the end on each side.

Thanks again for your kind help.
0
Comment
Question by:CFbubu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 2000 total points
ID: 39666422
To centre a DIV horizontally within it's parent, give it a width and set the left and right margins to auto:

<div style="background-color:blue">
     <div style="width:1000px; margin: 0px auto;"></div>
</div>

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39666432
So is the question about centering (as Chris has answered above) or about how to have the background extend?
0
 

Author Comment

by:CFbubu
ID: 39666469
Hi,

Thanks for your quick reply. Unfortunately, when I used your code, I do get a blue block centered, but the sides do not extend to the edges of the screen...(See picture below)

diagram1
What I trying to do is like how the navigation bar header and footer works on this website. That is the content stays centered but the background extends all the way to the side edges.(See pciture below)

diagram2
Thanks again!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:CFbubu
ID: 39666481
Hey GaryC123,

Good morning to you! Nice to see you around :)

No...unfortunately, the solution submitted by Chris did not work. :(
0
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39666487
Seeing a snippet of code in isolation rarely tells the full story. If you don't set a width on a block level element, such as a DIV then it will automatically stretch to the full width of the parent. If it has no explicit parent, then the window becomes the parent, so if you have only this in your page:

<div style="background-color:red">
     <div style="width:1000px; margin: 0px auto;background-color:blue"></div>
</div>

Open in new window

The red DIV will fill the page and the blue will sit centred inside (assuming your screen is wider than 1000px)
0
 
LVL 58

Expert Comment

by:Gary
ID: 39666497
Sheesh Chris is on the ball - didn't even have time to read the response!
0
 

Author Comment

by:CFbubu
ID: 39666501
I am sorry Chris. Let me check the code again and get back to you.

I do appreciate your help.
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39666515
Here it is in operation, so I know my code works :)

http://jsfiddle.net/ChrisStanyon/78v9z/
0
 

Author Closing Comment

by:CFbubu
ID: 39666581
you are right...its something on my page that is messing it up....sorry. I am still trying to figure out what's causing it.

Thanks for jumping in so quick to help me .
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unordered lists (HTML ul tags) are very handy to create menus of all types, shapes and sizes.  All it takes is a little knowledge of CSS and the possibilities are endless. First off, you should know the general format for menus created using the …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question