homepage Welcome to WebmasterWorld Guest from 54.237.54.83
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / CSS
Forum Library, Charter, Moderator: open

CSS Forum

    
center div contents?
Coupee36




msg:4359520
 3:28 pm on Sep 7, 2011 (gmt 0)

Hello all,

I am having trouble trying to center the div contents currently created. I have searched and tried different methods but to no luck. As of right now, the contents looks fine on a 800x600 resolution but when viewed on a high resolution the contents are off the left and the unused space on the right. I would simply like the contents to be centered in the middle of the page regardless of resolution the page is being viewed under. The page i am creating is for a family member to serve as a temporarily landing page.

Here is the current Code I have
*note i am using a template from a example contact form which made it easier for me to tweak and use it to my needs*

/*
* HTML5 ✰ Boilerplate
*
* What follows is the result of much research on cross-browser styling.
* Credit left inline and big thanks to Nicolas Gallagher, Jonathan Neal,
* Kroc Camen, and the H5BP dev community and team.
*
* Detailed information about this CSS: h5bp.com/css
*
* ==|== normalize ==========================================================
*/


/* =============================================================================
HTML5 display definitions
========================================================================== */

article, aside, details, figcaption, figure, footer, header, hgroup, nav, section { display: block; }
audio, canvas, video { display: inline-block; *display: inline; *zoom: 1; }
audio:not([controls]) { display: none; }
[hidden] { display: none; }

/* =============================================================================
Base
========================================================================== */

/*
* 1. Correct text resizing oddly in IE6/7 when body font-size is set using em units
* 2. Force vertical scrollbar in non-IE
* 3. Prevent iOS text size adjust on device orientation change, without disabling user zoom: h5bp.com/g
*/

html { font-size: 100%; overflow-y: scroll; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }

body { margin: 0; font-size: 13px; line-height: 1.231; }

body, button, input, select, textarea { font-family: sans-serif; color: #222; }

/*
* Remove text-shadow in selection highlight: h5bp.com/i
* These selection declarations have to be separate
* Also: hot pink! (or customize the background color to match your design)
*/

::-moz-selection { background: #fe57a1; color: #fff; text-shadow: none; }
::selection { background: #fe57a1; color: #fff; text-shadow: none; }


/* =============================================================================
Links
========================================================================== */

a { color: #00e; }
a:visited { color: #551a8b; }
a:hover { color: #06e; }
a:focus { outline: thin dotted; }

/* Improve readability when focused and hovered in all browsers: h5bp.com/h */
a:hover, a:active { outline: 0; }


/* =============================================================================
Typography
========================================================================== */

abbr[title] { border-bottom: 1px dotted; }

b, strong { font-weight: bold; }

blockquote { margin: 1em 40px; }

dfn { font-style: italic; }

hr { display: block; height: 1px; border: 0; border-top: 1px solid #ccc; margin: 1em 0; padding: 0; }

ins { background: #ff9; color: #000; text-decoration: none; }

mark { background: #ff0; color: #000; font-style: italic; font-weight: bold; }

/* Redeclare monospace font family: h5bp.com/j */
pre, code, kbd, samp { font-family: monospace, monospace; _font-family: 'courier new', monospace; font-size: 1em; }

/* Improve readability of pre-formatted text in all browsers */
pre { white-space: pre; white-space: pre-wrap; word-wrap: break-word; }

q { quotes: none; }
q:before, q:after { content: ""; content: none; }

small { font-size: 85%; }

/* Position subscript and superscript content without affecting line-height: h5bp.com/k */
sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; }
sup { top: -0.5em; }
sub { bottom: -0.25em; }


/* =============================================================================
Lists
========================================================================== */

ul, ol { margin: 1em 0; padding: 0 0 0 40px; }
dd { margin: 0 0 0 40px; }
nav ul, nav ol { list-style: none; list-style-image: none; margin: 0; padding: 0; }


/* =============================================================================
Embedded content
========================================================================== */

/*
* 1. Improve image quality when scaled in IE7: h5bp.com/d
* 2. Remove the gap between images and borders on image containers: h5bp.com/e
*/

img { border: 0; -ms-interpolation-mode: bicubic; vertical-align: middle; }

/*
* Correct overflow not hidden in IE9
*/

svg:not(:root) { overflow: hidden; }


/* =============================================================================
Figures
========================================================================== */

figure { margin: 0; }


/* =============================================================================
Forms
========================================================================== */

form {
margin: 0;
position: absolute;
top: 373px;
right: 52px;
width: 451px;
height: 192px;
}
fieldset { border: 0; margin: 0; padding: 0; }

/* Indicate that 'label' will shift focus to the associated form element */
label { cursor: pointer; }

/*
* 1. Correct color not inheriting in IE6/7/8/9
* 2. Correct alignment displayed oddly in IE6/7
*/

legend { border: 0; *margin-left: -7px; padding: 0; }

/*
* 1. Correct font-size not inheriting in all browsers
* 2. Remove margins in FF3/4 S5 Chrome
* 3. Define consistent vertical alignment display in all browsers
*/

button, input, select, textarea { font-size: 100%; margin: 0; vertical-align: baseline; *vertical-align: middle; }

/*
* 1. Define line-height as normal to match FF3/4 (set using !important in the UA stylesheet)
* 2. Correct inner spacing displayed oddly in IE6/7
*/

button, input { line-height: normal; *overflow: visible; }

/*
* Reintroduce inner spacing in 'table' to avoid overlap and whitespace issues in IE6/7
*/

table button, table input { *overflow: auto; }

/*
* 1. Display hand cursor for clickable form elements
* 2. Allow styling of clickable form elements in iOS
*/

button, input[type="button"], input[type="reset"], input[type="submit"] { cursor: pointer; -webkit-appearance: button; }

/*
* Consistent box sizing and appearance
*/

input[type="checkbox"], input[type="radio"] { box-sizing: border-box; }
input[type="search"] { -webkit-appearance: textfield; -moz-box-sizing: content-box; -webkit-box-sizing: content-box; box-sizing: content-box; }
input[type="search"]::-webkit-search-decoration { -webkit-appearance: none; }

/*
* Remove inner padding and border in FF3/4: h5bp.com/l
*/

button::-moz-focus-inner, input::-moz-focus-inner { border: 0; padding: 0; }

/*
* 1. Remove default vertical scrollbar in IE6/7/8/9
* 2. Allow only vertical resizing
*/

textarea { overflow: auto; vertical-align: top; resize: vertical; }

/* Colors for form validity */
input:valid, textarea:valid { }
input:invalid, textarea:invalid { background-color: #edeeee; }


/* =============================================================================
Tables
========================================================================== */

table { border-collapse: collapse; border-spacing: 0; }
td { vertical-align: top; }


/* ==|== primary styles =====================================================
Author: example[size=2][/size]
========================================================================== */
#contact-form {
width: 800px;
margin: 0 auto;
min-height: 600px;
padding:20px;
margin: 50px auto;
border: 6px none #3194ce;
-moz-border-radius:15px;
-webkit-border-radius:15px;
border-radius:15px;
position:absolute;
background-image: url(img/bg.jpg);
background-repeat: no-repeat;
background-position: center center;

}

#contact-form h1 {
font-size:42px;

}

#contact-form h2 {
margin-bottom:15px;
font-style:italic;
font-weight:normal;
color: white;
position:absolute;
left: 336px;
top: 329px;
width: 370px;
}

#contact-form input,
#contact-form select,
#contact-form textarea,
#contact-form label {
font-size:15px;
margin-bottom:2px;

}

#contact-form input,
#contact-form select,
#contact-form textarea {
width:450px;
border: 1px solid #CEE1E8;
margin-bottom:20px;
padding:4px;
}

#contact-form input:focus,
#contact-form select:focus,
#contact-form textarea:focus {
border: 1px solid #AFCDD8;
background-color: #EBF2F4;
}

#contact-form textarea {
height:150px;
resize: none;
}

#contact-form label {
display:block;
}

#contact-form .required {
font-weight:bold;
color:#F00;
}

#contact-form #submit-button {
width: 100px;
background-color:#333;
color:#FFF;
border:none;
display:block;
float:rightright;
margin-bottom:0px;
margin-right:6px;
background-color:#3194ce;
-moz-border-radius:8px;
}

#contact-form #submit-button:hover {
background-color: #A6CFDD;
}

#contact-form #submit-button:active {
position:relative;
top:1px;
}

#contact-form #loading {
width:32px;
height:32px;
background-image:url(../img/loading.gif);
display:block;
position:absolute;
rightright:130px;
bottombottom:16px;
display:none;
}

#errors {
border:solid 1px #E58E8E;
padding:10px;
margin:25px 0px;
display:block;
width:437px;
-webkit-border-radius:8px;
-moz-border-radius:8px;
border-radius:8px;
background:#FFE6E6 url(../img/cancel_48.png) no-repeat 405px center;
display:none;
}

#errors li {
padding:2px;
list-style:none;
}

#errors li:before {
content: ' - ';
}

#errors #info {
font-weight:bold;
}

#errors #info:before {
content: '';
}

#success {
border:solid 1px #83D186;
padding:25px 10px;
margin:25px 0px;
display:block;
width:437px;
-webkit-border-radius:8px;
-moz-border-radius:8px;
border-radius:8px;
background:#D3EDD3 url(../img/accepted_48.png) no-repeat 405px center;
font-weight:bold;
display:none;
}

#errors.visible, #success.visible {
display:block;
}

#req-field-desc {
font-style:italic;
}

/* Remove box shadow firefox, chrome and opera put around required fields. It looks rubbish. */
input:required, textarea:required {
-moz-box-shadow:none;
-webkit-box-shadow:none;
-o-box-shadow:none;
box-shadow:none;
}

/* Normalize placeholder styles */

/* chrome, safari */
::-webkit-input-placeholder {
color:#CCC;
font-style:italic;
}

/* mozilla */
input:-moz-placeholder, textarea:-moz-placeholder {
color:#CCC;
font-style:italic;
}

/* ie (faux placeholder) */
input.placeholder-text, textarea.placeholder-text {
color:#CCC;
font-style:italic;
}


/* ==|== non-semantic helper classes ========================================
Please define your styles before this section.
========================================================================== */

/* For image replacement */
.ir { display: block; border: 0; text-indent: -999em; overflow: hidden; background-color: transparent; background-repeat: no-repeat; text-align: left; direction: ltr; }
.ir br { display: none; }

/* Hide from both screenreaders and browsers: h5bp.com/u */
.hidden { display: none !important; visibility: hidden; }

/* Hide only visually, but have it available for screenreaders: h5bp.com/v */
.visuallyhidden { border: 0; clip: rect(0 0 0 0); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute; width: 1px; }

/* Extends the .visuallyhidden class to allow the element to be focusable when navigated to via the keyboard: h5bp.com/p */
.visuallyhidden.focusable:active, .visuallyhidden.focusable:focus { clip: auto; height: auto; margin: 0; overflow: visible; position: static; width: auto; }

/* Hide visually and from screenreaders, but maintain layout */
.invisible { visibility: hidden; }

/* Contain floats: h5bp.com/q */
.clearfix:before, .clearfix:after { content: ""; display: table; }
.clearfix:after { clear: both; }
.clearfix { zoom: 1; }



/* ==|== media queries ======================================================
PLACEHOLDER Media Queries for Responsive Design.
These override the primary ('mobile first') styles
Modify as content requires.
========================================================================== */

@media only screen and (min-width: 480px) {
/* Style adjustments for viewports 480px and over go here */

}

@media only screen and (min-width: 768px) {
/* Style adjustments for viewports 768px and over go here */

}



/* ==|== print styles =======================================================
Print styles.
Inlined to avoid required HTTP connection: h5bp.com/r
========================================================================== */

@media print {
* { background: transparent !important; color: black !important; text-shadow: none !important; filter:none !important; -ms-filter: none !important; } /* Black prints faster: h5bp.com/s */
a, a:visited { text-decoration: underline; }
a[href]:after { content: " (" attr(href) ")"; }
abbr[title]:after { content: " (" attr(title) ")"; }
.ir a:after, a[href^="javascript:"]:after, a[href^="#"]:after { content: ""; } /* Don't show links for images, or javascript/internal links */
pre, blockquote { border: 1px solid #999; page-break-inside: avoid; }
thead { display: table-header-group; } /* h5bp.com/t */
tr, img { page-break-inside: avoid; }
img {
max-width: 100% !important;
position: absolute;
height: 300px;
width: 225px;
left: 51px;
top: 197px;
}
@page { margin: 0.5cm; }
p, h2, h3 { orphans: 3; widows: 3; }
h2, h3 { page-break-after: avoid; }
}

 

rocknbil




msg:4359537
 4:08 pm on Sep 7, 2011 (gmt 0)

Welcome aboard Coupee36, I think your first mistake is grabbing a generated style sheet (or one created by someone else, or a service) and just going at it without fully understanding what it does.

I can't see your "container." Simply, put, to center a block level container of a specific width (fixed or fluid) you use margin: auto. For text and some inline elements, text-align: center;

#container { width: 800px; margin: auto; }
/* or width 75%, etc. */
h1 { text-align: center; }

<div id="container">
<h1> I'm Grounded and Centered</h1>
<p>The heading will be centered inside container due to text-align: center and this text will default to left aligned within container.</p>
</div> <!-- end container -->

So whatever your "container" is, margin:auto should do it - unless the container is floated or you use absolute positioning, but that's another story.

Coupee36




msg:4359580
 5:19 pm on Sep 7, 2011 (gmt 0)

ROCKNBIL,

thank you for the prompt reply. I do agree i made a mistake by using a pregenerated style, but at that time i just wanted a contact form then i figure if i tweaked it here and there i could get it to look how i want it to look, one thing led to another and now im stuck here..

anyhow, thank you again, I will give your suggestion a try.

This is the code in the index.php :

<body>
<div id="contact-form" class="clearfix">
<h1>first header here</h1>
<h2>Get connected with the latest updates!</h2>

<?php
//init variables
$cf = array();
$sr = false;

if(isset($_SESSION['cf_returndata'])){
$cf = $_SESSION['cf_returndata'];
$sr = true;
}
?>
<ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
<li id="info">There were some problems with your form submission:</li>
<?php
if(isset($cf['errors']) && count($cf['errors']) > 0) :
foreach($cf['errors'] as $error) :
?>
<li><?php echo $error ?></li>
<?php
endforeach;
endif;
?>
</ul>
<p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks, we will keep you connected!</p>
<form method="post" action="process.php">
<Font color="#319cce" ><label for="name">Name: <span class="required">*</span></label>
<input type="text" id="name" name="name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" placeholder="John Doe" required="required" autofocus="autofocus" />

<label for="email">Email Address: <span class="required">*</span></label></font>
<input type="email" id="email" name="email" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" placeholder="johndoe@example.com" required="required" />
<span id="loading"></span>
<input type="submit" value="Connect!" id="submit-button" />
<p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
</form>
</div>

<!--! end of #contact form -->

lucy24




msg:4359664
 8:59 pm on Sep 7, 2011 (gmt 0)

Some versions of MSIE simply ignore anything involving {margin-left: auto;} such as centered tables or positioned divs. (Personal Experience.) So you have to either add redundancies to the code or :: horror :: proceed to the Conditional Comment.

rocknbil




msg:4360059
 4:43 pm on Sep 8, 2011 (gmt 0)

I think I see it. You have 0 auto on #contact-form, which is your container div, (confusing name though :-) ) but it is set to position:absolute without coordinates, which defaults to 0 0 (top left.) Then you have this:

form {
margin: 0;
position: absolute;
top: 373px;
right: 52px;
width: 451px;
height: 192px;
}

Absolute positioning takes the element out of the normal flow of the document **unless** the parent element has a positioning selector. So even if you remove position:absolute from #contact-form, it may still pop out of the container. Try adding

position: relative

to the selector for #contact-form. This should contain form inside #contact-form:

#contact-form {
width: 800px;
margin: 0 auto;
min-height: 600px;
padding:20px;
margin: 50px auto;
border: 6px none #3194ce;
-moz-border-radius:15px;
-webkit-border-radius:15px;
border-radius:15px;
/*position:absolute; <-- no */
position: relative;

background-image: url(img/bg.jpg);
background-repeat: no-repeat;
background-position: center center;

}

Coupee36




msg:4360062
 4:51 pm on Sep 8, 2011 (gmt 0)

Hey rock, i appreciate the suggestion. I havent had time to make the changes, due to work. But I hope i will be able to do so tonight, thanks again for the help! Glad to have you stay on top of my post. Person mark for you, sir! :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / CSS
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved