16

I have some experience with Java , C , databases , networking etc..But anything related with Html I am a begginer.The only thing that I am looking for is to center two words in the middle of the page(This page will have only those two words).

                                WORD1
                          WORDWORDWORDWORD2

I have tried some WYSIWYG software like KompoZer, but when I looked to the source code, it had generated a horrible static code with a lot of <br> to achieve the vertically center of the page.Anybody could help me finding a good solution to this problem

Mark Bell
  • 28,985
  • 26
  • 118
  • 145
tt0686
  • 1,771
  • 6
  • 31
  • 60
  • Do you also need it to be vertically centred? – Simon Edström Mar 22 '12 at 15:51
  • define "in the middle of the page". Centered up and down (verticle)? Centered left and right (horizontal)? – DwB Mar 22 '12 at 15:52
  • Yes Simon, i am completely thankfull by all the quick answers but the problem is that all of them just solve the horizontal centered.What i need is both positions(horizontal and vertical).Both words must be on the center of the page – tt0686 Mar 22 '12 at 15:56

7 Answers7

30

Centering horizontally is easy - centering vertically is a bit tricky in css as it's not really supported (besides table cells <td>, which is bad style for layouting unless a table is really needed as - well - a table). But you can use semantically correct html tags and apply table display properties to it.

That's one possible solution - there are many approaches, here is a good article on that.

In your case something like that should be sufficient:

<!DOCTYPE html>
<html lang="de">
    <head>
        <title>Hello World</title>
        <style>

        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
            width: 100%;
        }

        body {
            display: table;
        }

        .my-block {
            text-align: center;
            display: table-cell;
            vertical-align: middle;
        }
        </style>
    </head>
    <body>
    <div class="my-block">
       WORD1<br />
       WORDWORDWORDWORD2
    </div>
    </body>
</html>
acme
  • 14,654
  • 7
  • 75
  • 109
  • You're welcome - (html purists would directly use the `` as text container instead of an additional div). – acme Mar 22 '12 at 16:47
2

You can put the text inside a <div> and align the text using CSS :

<div style="text-align:center;">
    WORD1<br />
    WORDWORDWORDWORD2
</div>

the <div> is a block element which means it will be stretched to 100% width and the text will be in in the center of the page

jsFiddle example

Teneff
  • 30,564
  • 13
  • 72
  • 103
  • As mentioned on another answer, for a beginner this is fine, but in general HTML should be as semantic as possible, and in line style violates that principal as well as separation of concerns. – Chris Sobolewski Mar 22 '12 at 16:05
1

These times full centering is best achieved using either flexbox or grid layout. So if we just want to center some element within its parent we do something like this:

HTML

<div class="parent">
   <div class="child">Some Stuff</div>
</div>

CSS

.parent {
    display: flex;
    justify-content: center;
    align-items: center;
    align-content: center;
}

/* Or this way if the page doesn't have to uncenter with changing screen size */
/* .parent {
       display: flex;
       place-items: center;
   }  */

.child {
    text-align: center;   /* If we want the child element's content horiz centered within its own container */
}
Trunk
  • 742
  • 9
  • 24
1

The "best practice" way to do it would be this:

Since you say you're new, I'm showing the whole document structure for you. Style should go in the head tag so that it is loaded first, and you should avoid inline style as much as possible.

<!DOCTYPE html>
<html>
    <head>
    <style>
    .center{
        text-align:center;
    }
    </style>
    </head>
    <body>
       <div class="center">
           <p>WORD1</p>
           <p>WORDWORDWORDWORD2</p>
        </div>
    </body>
</html>
Chris Sobolewski
  • 12,819
  • 12
  • 63
  • 96
0

Just do it like

<div style="text-align:center;">
   WORD1<br />
   WORDWORDWORDWORD2
</div>
irfanmcsd
  • 6,533
  • 7
  • 38
  • 53
  • 2
    For a beginner this is fine, but in general, HTML should be as semantic as possible, and in line style violates that principal as well as separation of concerns. – Chris Sobolewski Mar 22 '12 at 15:55
0

best way is to put it in some element like div. inside div you can easily center text with text-align: center; (assuming that you set some width to that div). Then you can center that div on page by adding auto margin style (margin: 0px auto;)

w3 manual about centering ;-)

Gatekeeper
  • 1,586
  • 3
  • 21
  • 33
-2

To center text you could use the <center> HTML tag:

<center>Blah</center>

or you could use CSS:

<style>
  .centered_text {
    text-align:middle;
  }
</style>

<a class='centered_text'>Blah</a>
Adjam
  • 598
  • 1
  • 8
  • 22