Change
height using CSS

后端 未结 11 2042
别那么骄傲
别那么骄傲 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:39

    You can control the <br> height if you put it inside a height limited div. Try:

    <div style="height:2px;"><br></div>
    
    0 讨论(0)
  • 2021-01-30 08:39

    #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!

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

    Use the content property and style that content. Content behavior is then adjusted using pseudo elements. Pseudo elements ::before and ::after both work in Mac Safari 10.0.3.

    Here element br content is used as the element anchor for element br::after content. Element br is where br spacing can be styled. br::after is the place where br::after content can be displayed and styled. Looks pretty, but not a 2px <br>.

    br { content: ""; display: block; margin: 1rem 0; }
    br::after { content: "› "; /* content: " " space ignored */; float: left; margin-right: 0.5rem; }
    

    The br element line-height property is ignored. If negative values are applied to either or both selectors to give vertical 'lift' to br tags in display, then correct vertical spacing occurs, but display incrementally indents display content following each br tag. The indent is exactly equal to the amount that lift varies from actual typographic line-height. If you guess the right lift, there is no indent but a single pile-up line exactly equal to raw glyph height, jammed between previous and following lines.

    Further, a trailing br tag will cause the following html display tag to inherit the br:after content styling. Also, pseudo elements cause <br> <br> to be interpreted as a single <br>. While pseudo-class br:active causes each <br> to be interpreted separately. Finally, using br:active ignores pseudo element br:after and all br:active styling. So, all that's required is this:

    br:active { }
    

    which is no help for creating a 2px high <br> display. And here the pseudo class :active is ignored!

    br:active { content: ""; display: block; margin: 1.25em 0; }
    br { content: ""; display: block; margin: 1rem; }
    br::after { content: "› "; /* content: " " space ignored */; float: left; margin-right: 0.5rem; }
    

    This is a partial solution only. Pseudo class and pseudo element may provide solution, if tweaked. This may be part of CSS solution. (I only have Safari, try it in other browsers.)

    Learn web development: pseudo classes and pseudo elements

    Pay attention to global elements - BR at Mozilla.org

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

    This feels very hacky, but in chrome 41 on ubuntu I can make a <br> slightly stylable:

    br {
      content: "";
      margin: 2em;
      display: block;
      font-size: 24%;
    }
    

    I control the spacing with the font size.


    Update

    I made some test cases to see how the response changes as browsers update.

    *{outline: 1px solid hotpink;}
    div {
      display: inline-block;
      width: 10rem;
      margin-top: 0;
      vertical-align: top;
    }
    
    h2 {
      display: block;
      height: 3rem;
      margin-top:0;
    }
    
    .old br {
      content: "";
      margin: 2em;
      display: block;
      font-size: 24%;
      outline: red;
    }
    
    .just-font br {
      content: "";
      display: block;
      font-size: 200%;
    }
    .just-margin br {
      content: "";
      display: block;
      margin: 2em;
    }
    
    .brbr br {
      content: "";
      display: block;
      font-size: 100%;
      height: 1em;
      outline: red;
      display: block;
    }
    <div class="raw">
      <h2>Raw <code>br</code>rrrrs</h2>
      bla<BR><BR>bla<BR>bla<BR><BR>bla
    </div>
      
    <div class="old">
      <h2>margin & font size</h2>
      bla<BR><BR>bla<BR>bla<BR><BR>bla
    </div>
      
    <div class="just-font">
      <h2>only font size</h2>
      bla<BR><BR>bla<BR>bla<BR><BR>bla
    </div>
    
     <div class="just-margin">
      <h2>only margin</h2>
      bla<BR><BR>bla<BR>bla<BR><BR>bla
    </div>
       
    <div class="brbr">
      <h2><code>br</code>others vs only <code>br</code>s</h2>
      bla<BR><BR>bla<BR>bla<BR><BR>bla
    </div>

    They all have their own version of strange behaviour. Other than the browser default, only the last one respects the difference between one and two brs.

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

    As the 'margin' doesn't work in Chrome, that's why I used 'border' instead.

    br {
      display: block;
      content: "";
      border-bottom: 10px solid transparent; // Works in Chrome/Safari
    }
    @-moz-document url-prefix() {
      br {
        margin-bottom: 10px; // As 'border-bottom' doesn't work in firefox and 'margin-bottom' doesn't work in Chrome/Safari.
      }
    }
    
    0 讨论(0)
  • 2021-01-30 08:57

    Take a look at the line-height property. Trying to style the <br> tag is not the answer.

    Example:

    <p id="single-spaced">
        This<br>
        text<br>
        is<br>
        single-spaced.
    </p>
    <p id="double-spaced" style="line-height: 200%;">
        This<br>
        text<br>
        is<br>
        double-spaced.
    </p>
    
    0 讨论(0)
提交回复
热议问题