0

I'm trying to get the h1 text on the top center of the page, but when I add a border the width of the border extends the entire top part of the page. When I decrease of the width of the element using CSS the border pushes the text towards the left.

Before changing width

enter image description here

After changing the width

enter image description here

Demo :

JSfiddle

Community
  • 1
  • 1
  • The border doesn't push the text to the left. The text is pushed to the left because you reduce the width and the element is aligned to the left. – Oriol Jan 05 '15 at 01:26

4 Answers4

0

You can use margin: 0 auto in your css like this

h1{
    text-align: center;
    border: 3px solid green;
    width: 50%;
    margin:0 auto;
}

Hope this helps

akaBase
  • 2,178
  • 1
  • 18
  • 31
0

Remove float property and add margin: 0 auto to center h1 as it is a block element.

JSFiddle

Community
  • 1
  • 1
potashin
  • 44,205
  • 11
  • 83
  • 107
0

Try pasting this in the css,

h1{
text-align: center;
border: 3px solid green;
width: 50%;
margin:0 auto;
}

The changes made are: removed "float:center;" and added "margin:0 auto;"

Hope this helps.

0

The issue is with the float, there is no float for center, just left and right. Because you have declared a width you can use margin: 0 auto which will center your h1. Keep in mind that if you remove the width the margin: 0 auto will not work.

h1{
    text-align: center;
    border: 3px solid green;
    width: 50%;
    margin:0 auto;
}
RyanIndustries8
  • 157
  • 3
  • 16