0

I have created header in the top of page as give below. But why it keeps space from top page border even though I have kept margin 0?

FIDDLE LINK

<body><div class="page-header header" style="color:fff; font-size: large; font-family: Arial;
">
            <h1 class="page-header-text">Review Networks</h1>
        </div>
3
  • Having <body> is superfluous. It's already inside of a body element. Commented Apr 2, 2014 at 18:11
  • It's called margin-collapse. Commented Apr 2, 2014 at 18:11
  • possible duplicate of Basic HTML/CSS - margin & display behaviour Commented Apr 2, 2014 at 18:14

3 Answers 3

2

Add the property margin-top: 0; to your .page-header-text selector.

See this JSFiddle for a working code snippet of the vertical centering you want.

Sign up to request clarification or add additional context in comments.

5 Comments

I want it to align in center vertically not the horizontal. This does not help
@Programming_crazy I will edit my answer once more, then. It's already center-aligned vertically.
@Programming_crazy I've added a JSFiddle which should give you the result you want.
can you please tell me how can I increase header height? without using pixel? I did height:10%; but did not help
@Programming_crazy height:10%; will specify a total element height of 10% of the parent element. What you can do is change padding-top: 5px; to padding-top: 1%; to achieve roughly the same result.
2

I usually do that like this;

my first answer was : fiddle - http://jsfiddle.net/TPQnx/2/

As you don't want position:absolute;

new fiddle: http://jsfiddle.net/TPQnx/3/

Changes I've made:

added this to header class:

text-align:center;

and added;

h1 {
margin: 0 auto;
padding-top: 0;
}

Hope this helps.

3 Comments

Okay I've handled it. Editing the answer
thanks, but height:10%; why it does not increase header height? I dont want to use pixel for height
it's funny cause when I change the font-size to e.g 16px it changes the header height proportionally. I'm trying to solve it somehow
2

Add this to your CSS:

h1 {
    margin: 0 auto;
    padding-top: 0;
    text-align: center;
}

Browsers add their own default styling, which was causing the margin issue with the h1. Setting margin: 0 auto; overrides that default styling. Auto tells the browser to split the remaining space between the left and the right of the element, thus centering it.

5 Comments

thanks but why text does not align in center? jsfiddle.net/karimkhan/Uwucd/1
Programming_crazy, if you want the text to be centered, you just need to add 'auto' to the left and right margins. I updated my answer with this. Also be sure to include text-align: center;
@Hiran: I want it to align in center vertically not the horizontal. This does not hep
@HiramHibbard Could you add a little more description about why it works?
@Kevin sure. Updated my answer to give a few more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.