Heading by pushing the containing div from top to bottom?

I am new to stackoverflow and web development. Trying to learn without any help. I am trying to create a CV of sorts as part of honing my skills. Here's what happened.

The first div below my containing div is the div with id header

. I fixed my containing div top with margin: 0 auto;

It works fine without text in my nested div

(header), but as soon as I write something in the header div

it pushes the header div

down and since header is the first element div

/ in the containing div div

it also discards that.

Here's my HTML :

body {
  margin: 0;
  font: normal 12px/18px'Helvetica', Arial, sans-serif;
  background: #44accf;
}
/* Positioning Rules */

#container {
  width: 900px;
  margin: 0 auto;
}
#header {
  background: #b7d84b;
  height: 50px;
  text-align: center;
  color: #ddd;
  line-height: 50px;
}
      

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">

<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Did We?</title>
  <link href="didwe.css" type="text/css" rel="stylesheet" />
</head>

<div id="container">
  <div id="header">
    <h1> Mansoor Zia </h1>	

  </div>
</div>
<!--container-->
</body>

</html>
      

Run code


I cannot attach an image here as I am a good new member.

+3


source to share


6 answers


Add h1{margin:0}

to your css and it will fix your problem. Just so you know, unless otherwise noted, all text elements have an edge that curls with positioning and size :)



+1


source


Each browser sets different default values ​​for different HTML elements. With CSS Reset, we can neutralize this difference to provide cross-browser styling.

use http://cssreset.com



/* v2.0 | 20110126
  http://meyerweb.com/eric/tools/css/reset/ 
  License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}


body {
    margin: 0;
    font: normal 12px/18px'Helvetica', Arial, sans-serif;
    background: #44accf;
}
/* Positioning Rules */
 #container {
    width: 900px;
    margin: 0 auto;
}
#header {
    background: #b7d84b;
    height: 50px;
    text-align: center;
    color: #ddd;
    line-height: 50px;
}

h1 {
    font-size: 20px;
}
      

<div id="container">
    <div id="header">
        	<h1> Mansoor Zia </h1>	
    </div>
</div>
      

Run code


+1


source


Here's a solution http://jsfiddle.net/hovru6qh/

* {
    padding: 0;
    margin: 0;
}

      

You need to reset all padding and fields

0


source


You forgot opening

<body>
      

Run code


-tag maybe so.

Also, instead of using

margin: 0 auto;
      

Run code


to make it stick to the top of the page, try using

top: *margin to top*(0 = sticks to top without margin)
      

Run code


0


source


Margins within block elements - by default - overflow vertically bidirectionally when an element with a difference is set display

to inline-block

or inline

( h1

elements are displayed in inline

by default).

To fix this without changing margin

your H1 text, but fixing the heading so it doesn't follow it (as it shouldn't), add overflow: auto

in your heading CSS.

The script is here for you:

Bidirectional field overflow error

Good luck!

0


source


Some of the other answers seem a bit deceiving, so I decided to create this one to describe the problem for you.

The problem is called margin unwinding , you can read a little about it here .

The top and bottom margins of the boxes are sometimes merged (collapsed) into one edge, the size of which is the largest of the margins merged into it, a behavior known as collapse.

The fix for this would be to remove the default field for the tag <h1>

, see example below.

body {
  margin: 0;
  font: normal 12px/18px'Helvetica', Arial, sans-serif;
  background: #44accf;
}
#container {
  width: 900px;
  margin: 0 auto;
}
#header {
  background: #b7d84b;
  height: 50px;
  text-align: center;
  color: #ddd;
  line-height: 50px;
}
h1 {
  margin: 0; /* Added this */
}
      

<div id="container">
  <div id="header">
    <h1> Mansoor Zia </h1>	
  </div>
</div>
      

Run code


0


source







All Articles