简体   繁体   English

:在扩展标记以填充div的剩余宽度之后

[英]:after tag that expands to fill remaining width of div

I have aa h1 element with an :after tag that needs to dynamically expand to fill the remaining width of the containing div. 我有一个带有:after标签的h1元素,需要动态扩展以填充包含div的剩余宽度。 Basically it needs to have a width of calc( h1_width - 100%), where the h1 width isn't set or hardcoded. 基本上它需要有一个calc的宽度( h1_width - 100%),其中h1宽度未设置或硬编码。 Can this be done via SASS or will I need to use a JS option to manipulate the DOM? 这可以通过SASS完成,还是需要使用JS选项来操作DOM? JSfiddle . JSfiddle

HTML HTML

<div class="text-box__wrap">
  <h1>Hello</h1>
</div>

CSS CSS

  .text-box__wrap {
    width: 100%;
    margin-top: 2em auto 0;
    padding: 0 20px;
  }

  .text-box__wrap h1 {
    margin-bottom: 4%;
    letter-spacing: 1.5px;
    text-transform: uppercase;
    font-size: 12px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    &:after {
      display: inline-block;
      /* Needs to be dynamic based on difference of h1 width and remaining div width */
      width: 50px;
      height: 1px;
      margin-left: 10px;
      content: "";
      vertical-align: 30%;
      background-color: black;
    }
  }

You can use display:flex on the h1 to make the :after take the remaining space: 您可以在h1上使用display:flex来创建:after获取剩余空间之后:

 .text-box__wrap { width: 100%; margin-top: 2em auto 0; padding: 0 20px; box-sizing: border-box; } .text-box__wrap h1 { letter-spacing: 1.5px; text-transform: uppercase; font-size: 12px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; display: flex; align-items: center; } .text-box__wrap h1:after { display: inline-block; height: 1px; flex: 1 1 auto; margin-left: 10px; content: ""; background-color: black; } 
 <div class="text-box__wrap"> <h1>Hello</h1> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM