Change
height using CSS

后端 未结 11 2043
别那么骄傲
别那么骄傲 2021-01-30 08:20

I have seen a question here about the same, but I can\'t get any of the answers to work (at least on Chrome).

This question is only for
, I know

相关标签:
11条回答
  • 2021-01-30 08:59

    The BR is anything but 'extra-special': it is still a valid XML tag that you can give attributes to. For example, you don't have to encase it with a span to change the line-height, rather you can apply the line height directly to the element.

    You could do it with inline CSS:

    This is a small line
    <br />
    break. Whereas, this is a BIG line
    <br />
    <br style="line-height:40vh"/>
    break!

    Notice how two line breaks were used instead of one. This is because of how CSS inline elements work. Unfourtunately, the most awesome css feature possible (the lh unit) is still not there yet with any browser compatibility as of 2019. Thus, I have to use JavaScript for the demo below.

    addEventListener("load", function(document, getComputedStyle){"use strict";
      var allShowLineHeights = document.getElementsByClassName("show-lh");
      for (var i=0; i < allShowLineHeights.length; i=i+1|0) {
        allShowLineHeights[i].textContent = getComputedStyle(
          allShowLineHeights[i]
        ).lineHeight;
      }
    }.bind(null, document, getComputedStyle), {once: 1, passive: 1});
    .show-lh {padding: 0 .25em}
    .r {background: #f77}
    .g {background: #7f5}
    .b {background: #7cf}
    This is a small line
    <span class="show-lh r"></span><br /><span class="show-lh r"></span>
    break. Whereas, this is a BIG line
    <span class="show-lh g"></span><br /><span class="show-lh g"></span>
    <span class="show-lh b"></span><br style="line-height:40vh"/><span class="show-lh b"></span>
    break!

    You can even use any CSS selectors you want like ID's and classes.

    #biglinebreakid {
      line-height: 450%;
      // 9x the normal height of a line break!
    }
    .biglinebreakclass {
      line-height: 1em;
      // you could even use calc!
    }
    This is a small line
    <br />
    break. Whereas, this is a BIG line
    <br />
    <br id="biglinebreakid" />
    break! You can use any CSS selectors you want for things like this line
    <br />
    <br class="biglinebreakclass" />
    break!

    You can find our more about line-height at the W3C docs.

    Basically, BR tags are not some void in world of CSS styling: they still can be styled. However, I would recommend only using line-height to style BR tags. They were never intended to be anything more than a line-break, and as such they might not always work as expected when using them as something else. Observe how even after applying tons of visual effects, the line break is still invisible:

    #paddedlinebreak {
      display: block;
      width: 6em;
      height: 6em;
      background: orange;
      line-height: calc(6em + 100%);
      outline: 1px solid red;
      border: 1px solid green;
    }
    <div style="outline: 1px solid yellow;margin:1em;display:inline-block;overflow:visible">
    This is a padded line
    <br id="paddedlinebreak" />
    break.
    </div>

    A work-around for things such as margins and paddings is to instead style a span with a br in it like so.

    #paddedlinebreak {
      background: orange;
      line-height: calc(6em + 100%);
      padding: 3em;
    }
    <div style="outline: 1px solid yellow;margin:1em;display:inline-block;overflow:visible">
    This is a padded line
    <span id="paddedlinebreak"><br /></span>
    break.
    </div>

    Notice how the orange blob above is the span that contains the br.

    0 讨论(0)
  • 2021-01-30 08:59

    The line height of the <br> can be different from the line height of the rest of the text inside a <p>. You can control the line height of your <br> tags independently of the rest of the text by enclosing two of them in a <span> that is styled. Use the line-height css property, as others have suggested.

    <p class="normalLineHeight">
      Lots of text here which will display on several lines with normal line height if you put it in a narrow container...
      <span class="customLineHeight"><br><br></span>
      After a custom break, this text will again display on several lines with normal line height...
    </p>
    
    0 讨论(0)
  • 2021-01-30 08:59
    <font size="4"> <font color="#ffe680">something here</font><br>
    

    I was trying all these methods but most didn't work properly for me, eventually I accidentally did this and it works great, it works on chrome and safari (the only things I tested on). Replace the colour code thing with your background colour code and the text will be invisible. you can also adjust the font size to make the line break bigger or smaller depending on your desire. It is really simple.

    0 讨论(0)
  • 2021-01-30 09:02

    The line height of the br tag can be different from the line height of the rest of the text inside a paragraph text by setting font-size for br tag.

    Example: br { font-size: 200%; }

    0 讨论(0)
  • 2021-01-30 09:04

    You can't change the height of the br tag itself, as it's not an element that takes up space in the page. It's just an instruction to create a new line.

    You can change the line height using the line-height style. That will change the distance between the text blocks that you have separated by empty lines, but natually also the distance between lines in a text block.

    For completeness: Text blocks in HTML is usually done using the p tag around text blocks. That way you can control the line height inside the p tag, and also the spacing between the p tags.

    0 讨论(0)
提交回复
热议问题