CodeGym /Courses /Frontend SELF EN /Text Alignment

Text Alignment

Frontend SELF EN
Level 15 , Lesson 1
Available

6.1 The text-align Property

Text alignment is basically one of the most important aspects of styling web pages, as it lets you control how text is positioned within containers. CSS gives us two main properties for text alignment: text-align and vertical-align. These properties let you manage horizontal and vertical alignment of text respectively.

The text-align property manages the horizontal alignment of text inside a block element. It decides how text is aligned concerning the element's borders.

Values

  • left: aligns text to the left (default for left-to-right text)
  • right: aligns text to the right (default for right-to-left text)
  • center: center aligns text
  • justify: aligns text to both the left and right edges, adding spaces between words to spread text evenly across the full width of the container
  • start: aligns text to the start (left or right edge based on text direction)
  • end: aligns text to the end (right or left edge based on text direction)

Usage Example:

CSS
    
      .left {
        text-align: left;
      }

      .right {
        text-align: right;
      }

      .center {
        text-align: center;
      }

      .justify {
        text-align: justify;
      }
    
  
HTML
    
      <body>
        <p class="left">This text is aligned to the left.</p>
        <p class="right">This text is aligned to the right.</p>
        <p class="center">This text is centered.</p>
        <p class="justify">This text is justified. It will stretch so the text occupies the full width of the container.</p>
      </body>
    
  

6.2 The vertical-align Property

The vertical-align property manages the vertical alignment of an element relative to the baseline of its parent. It's mostly used for aligning inline elements, like images, lines of text, or tables, vertically within a line of text or another container.

Values:

  • baseline: aligns to the parent's baseline (default value)
  • sub: aligns as a subscript
  • super: aligns as a superscript
  • text-top: aligns to the top of the parent's font
  • text-bottom: aligns to the bottom of the parent's font
  • middle: aligns to the middle of the parent
  • top: aligns to the top of the container
  • bottom: aligns to the bottom of the container
  • Percentage value: offsets the element by the specified percentage relative to the line-height

Usage Example:

CSS
    
      .container {
        height: 100px;
        border: 1px solid #000;
      }

      .baseline {
        vertical-align: baseline;
      }

      .sub {
        vertical-align: sub;
      }

      .super {
        vertical-align: super;
      }

      .text-top {
        vertical-align: text-top;
      }

      .text-bottom {
        vertical-align: text-bottom;
      }

      .middle {
        vertical-align: middle;
      }

      .top {
        vertical-align: top;
      }

      .bottom {
        vertical-align: bottom;
      }

      .percent-50 {
        vertical-align: 50%;
      }
    
  
HTML
    
      <body>
        <div class="container">
          <span class="baseline">baseline</span>
          <span class="sub">sub</span>
          <span class="super">super</span>
          <span class="text-top">text-top</span>
          <span class="text-bottom">text-bottom</span>
          <span class="middle">middle</span>
          <span class="top">top</span>
          <span class="bottom">bottom</span>
          <span class="percent-50">50%</span>
        </div>
      </body>
    
  
1
Task
Frontend SELF EN, level 15, lesson 1
Locked
Horizontal Alignment
Horizontal Alignment
1
Task
Frontend SELF EN, level 15, lesson 1
Locked
Vertical Alignment
Vertical Alignment
Comments (1)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Mirko Mirxas Level 48
15 December 2025
You can simply copy the code from this lecture and paste it into the challenges. Such a waste of time. Codegym Web development course is still a total failure :(