0

Here is the HTML part:

<div class="container">
    <h2>Some text<span></span></h2>
</div>

CSS for the elements:

.container {
width: 533px;
}

.container h2 {
color: #ec1c24;
font-size: 48px;
text-transform: uppercase;
font-family: "Open Sans Extrabold";
letter-spacing: -3px;
}

.container h2 span {
background: none repeat scroll 0 0 #ec1c24;
float: right;
height: 34px;
margin-top: 16px;
width: 38%;
}

What I'm trying to do is to make the span adjust it's size to the text lenght. With this CSS if the text is too long, the span goes below but it should just shrink.

Here's an example:

http://jsfiddle.net/SCj5Z/

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
user1410644
  • 351
  • 4
  • 6
  • 15

5 Answers5

1

Using flex display you can achieve this. Container display should be flex and the span child attribute could be 1 (or basically any other number since we don't have any other flex child). with using flex we set the span size to fill the empty space and because h2 size is fix (which is size of the text) span will fill all empty space.

you can read more about flexbox here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Hope this help..

HTML:

<div class="container">
    <h2>Some text<span></span></h2>
</div>

CSS:

.container {
     width: 533px;
}

.container h2 {
    display: flex;
    align-items: center;
}
.container h2 span {
    content:"";
    flex: 1 1 auto;
    border-top: 3px solid #2B3F4F;
    margin-left: 10px;
    margin-right: 5px;
    margin-top: 4px;
}

http://jsfiddle.net/besatzardosht/SCj5Z/75/

Besat
  • 1,428
  • 13
  • 27
0

I think display: inline-block will do the work correctly.

Meowsome
  • 99
  • 1
  • 10
0

remove the width: 38% from the span's CSS and the width should just be auto and adjust automatically.

or display: inline-block and text-align: right the span

Adrian Roworth
  • 813
  • 1
  • 7
  • 16
0

adds three dots in the end of Some text. Not crossbrowser

       .container {                           
       width: 533px;                          
       }                                      

       .container h2 {                        
       color: #ec1c24;                        
       font-size: 48px;                       
       text-transform: uppercase;             
       overflow-x:hidden;                     
       white-space: nowrap;                   
       text-overflow: ellipsis;               
       font-family: "Open Sans Extrabold";    
       letter-spacing: -3px;                  
       }                                      

       .container h2 span {                   
       background: none repeat scroll 0 0 #ec1
       float: right;                          
       height: 34px;                          
       margin-top: 16px;                      
       width: 38%;                            
       }                                      


       <div class="container">                
           <h2>Some text<br><span></span></h2>
       </div>                                 
el Dude
  • 5,003
  • 5
  • 28
  • 40
0

This questions sounds similar to this SO question:

Fluid width div relative to sibling

One of the answers links to the following JSFiddle:

http://jsfiddle.net/pQc3d/1/

If you can afford to manually set the background color of the actual h2 text and include &nbsp; in the dummy span, the following fiddle might work for you:

http://jsfiddle.net/doubleswirve/3QFgk/

Hope that helps!

Community
  • 1
  • 1
doubleswirve
  • 1,388
  • 3
  • 16
  • 23