0

I looked around the Web and couldn't find a solution to this. So I'm going to ask the question myself.

How would I center my text in HTML? I don't mean center as in "text-align:center;" I mean, center it entirely. Like make sure the text is directly in the middle of your webpage on one line. Thanks in advance.

HTML:

<!DOCTYPE html>
<html lang="en">

  <head>

<title>CSGOCarry.com | Win Big!</title>
<link rel="stylesheet" type="text/css" href="csgositecss.css">

  </head>

  <body>

    <div class="container">
      <div class="center">
        <h1>Welcome to CSGOCarry</h1>
      </div>
    </div>

  </body>

</html>

CSS:

body {
  background-image: url("csgocarryback.jpg");
  background-size: 100%;
}

h1 {
  color:white; 
  text-align:center; 
  font-size:50px
}
.container {
}
.center {
}

Note: I have tried making the text centered in the center block. But I'm having trouble doing so.

Tuan Ha
  • 620
  • 2
  • 8
  • 25
Matt Jones
  • 13
  • 1
  • 7

3 Answers3

2

try this

.container {
    left: 50%;
    position: absolute;
    top: 50%;
    transform: translate(-50%,-50%);
}

body {
  background-image: url("csgocarryback.jpg");
  background-size: 100%;
  background-color: #AAA;
}
h1 {
  color: white;
  text-align: center;
  font-size: 50px;
}
.container {
  left: 50%;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
}
<!DOCTYPE html>
<html lang="en">

<head>

  <title>CSGOCarry.com | Win Big!</title>
  <link rel="stylesheet" type="text/css" href="csgositecss.css">

</head>

<body>

  <div class="container">
    <div class="center">
      <h1>Welcome to CSGOCarry</h1>
    </div>
  </div>

</body>

</html>
Josh S.
  • 612
  • 3
  • 8
0

One solution is using flex on the parent element like so:

.container {
    display: flex;
    justify-content: center; /* Horizontally */
    align-items: center; /* Vertically */
}

.center {
    text-align: center;
}

This centers whatever is in the .container both vertically and horizontally.

Daniel Hallgren
  • 497
  • 7
  • 12
0

One solution is to put <center> immediately after <body>, and put end it immediately before ending body. Like this: HTML:

    <!DOCTYPE html>
<html lang="en">

  <head>

<title>CSGOCarry.com | Win Big!</title>
<link rel="stylesheet" type="text/css" href="csgositecss.css">

  </head>

  <body>
<center>
    <div class="container">
      <div class="center">
        <h1>Welcome to CSGOCarry</h1>
      </div>
    </div>
</center>
  </body>

</html>

CSS:

  body {
  background-image: url("csgocarryback.jpg");
  background-size: 100%;
}

h1 {
  color:white; 
  text-align:center; 
  font-size:50px
}
.container {
}
.center {
}

I have also created a jsfiddle for you: https://jsfiddle.net/8uafor26/