Align H1 Header and Normal Text in Same Line

前端 未结 6 2102
灰色年华
灰色年华 2021-02-06 23:33

I\'m trying to have a H1 header and regular text on the same line, with a line under it, like so:

\"enter

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-06 23:52

    Original answer (still working just fine)

    See this CodePen.

    The idea is to make the

    inline to allow the second text to be at the same line.

    HTML:

    Text

    text2

    CSS:

    header { border-bottom: 1px solid #000; }
    header > h1 { display: inline-block; }
    header span { margin-left: 100px; }
    

    2020 Update

    See this alternative CodePen that makes use of flexbox.

    Instead of setting the h1 to an inline-block, you can make the header a flex container. A flex container will (by default) layout its children on a horizontal axis. Note that you also need align-items: center to keep the h1 and span on the same vertical axis.

    Also, note that you might want align-items: baseline if you want the texts to appear on the same baseline (like my original answer).

    header {
      display: flex;
      align-items: center;
    
      /* Remove the next line if you want the span to appear next to the h1 */
      justify-content: space-between;
    
      border-bottom: 1px solid #000;
      padding: 10px 30px;
    }
    

提交回复
热议问题