Zed-King Institute

    CSS ID Selector Example: Font Size, Alignment and Font

    mediumCSSHTML

    An id identifies ONE element on the page; CSS targets it with #.

    The question

    Create an <h2> with id "title". Using the id selector in CSS, set its font-size to 36px, text-align to center and font-family to Verdana.

    The code

    index.html

    <h2 id="title">My Web Page</h2>
    
    <style>
    #title {
      font-size: 36px;
      text-align: center;
      font-family: Verdana;
    }
    </style>

    Result

    How it works

    1. 1id="title" names the element — an id must be unique on the page.
    2. 2#title { … } styles that one element.
    3. 3font-size sets the text size, text-align its alignment, font-family its typeface.

    Common mistakes

    • Using the same id on two elements.
    • Writing .title (class) when the HTML uses id="title".

    Now solve a similar question yourself

    A new HTML / CSS / JS question every time, checked instantly.

    Practice questions

    More in CSS