Align H1 Header and Normal Text in Same Line

前端 未结 6 2096
灰色年华
灰色年华 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:43

    I think you should write like this :-

    HTML

    <div style="border-bottom:1px solid black; overflow:hidden;"> 
    <h1>Header</h1>
    <div class="right">Regular Text Goes Here</div>
    </div>
    

    CSS

    h1 {
    float:left;
      margin:0px;
      padding:0;
    }
    .right {
    float:right;
      margin:0px;
      padding:0px;
    }
    

    DEMO

    EVEN YOU CAN USE THIS METHOD ALSO WITH MINIMIZED MARKUP :- DEMO

    0 讨论(0)
  • 2021-02-06 23:52

    Original answer (still working just fine)

    See this CodePen.

    The idea is to make the <h1> inline to allow the second text to be at the same line.

    HTML:

    <header>
      <h1>Text</h1>
      <span>text2</span>
    </header>
    

    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;
    }
    
    0 讨论(0)
  • 2021-02-06 23:52

    I came up with a simple solution. My requirements are slightly different in that I want my status right aligned.

    HTML

    <div class="my-header">
        <h2>Title</h2>
        <span>Status</span>
    </div>
    <div style="clear:both"></div>
    

    CSS

    .my-header h2 { 
      display: inline;
    }
    .my-header span { 
      float: right;
    }
    

    Example

    Check out this plunk.

    0 讨论(0)
  • 2021-02-06 23:53

    Add this line border-bottom:1px solid #000

    <div style="border-bottom:1px solid #000;"> 
    <div align="left"><h1>Header</h1></div>
    <div align="right">Regular Text Goes Here</div>
    </div>
    

    DEMO

    Use class name instead of inline-style.

    0 讨论(0)
  • 2021-02-06 23:58

    Try

    <div style="float:left;"><h1>Header</h1></div>
    <div style="float:right;">Regular Text Goes Here</div>
    

    instead?

    0 讨论(0)
  • 2021-02-07 00:03

    There are two methods to accomplish H1 and TEXT inline. To clarify, TXT is in an element container. You suggest DIV, but any symantic element will do. Below, h1 and p illustrate a common use, while showing that you need not hide from element blocking, using DIV's (though divs are pandemic for many javascript coders).

    Method 1

    .inline { display: inline; float: left; padding-right: 2rem; }
    <h5 class="inline">Element a's link family...</h5>
    <p class="inline">
    

    Method 2

    h5 { display: inline-block; float: left; font-size: 1rem; line-height: 1rem; margin-right: 2rem; }
    h5>p { display: inline-block; float: right; }
    <h5>Title</h5>
    <p>Paragraph</p>
    
    0 讨论(0)
提交回复
热议问题