4

I have headings that have display:inline-block set, but one of them is really long and takes two lines. The issue is that when it breaks to the second line, it automatically makes it take the whole width of the parent container (even though it has plenty of space around it, and it is set to inline-block).

Anyone knows how to prevent this? Also, I can't really use a solution that only applies to this one if it will break other headings, because the same code is generating other 9 non-line-breaking headings using the same structure.

Thank you for your time.

CodePen: https://codepen.io/anon/pen/gGdYmB#anon-signup

<div id="parent1" class="parent">
  <h2>SHORT HEADING</h2>
  <h2>THE REALLY LONG HEADING THAT 
  <span>BREAKS TO A SECOND LINE</span></h2>
</div> 

<style>
.parent {
   width:50vw; 
   background-color:#046; 
   height:20vw; 
   text-align:center;
}
.parent h2 {
   display:inline-block;
   color:#fff;
   font-family:sans-serif;
   font-weight:900;
   font-size:2vw;
   background-color:#28a;
   padding:10px 0;
}
.parent h2 span {
    display:inline-block
}
</style>
JH_
  • 406
  • 1
  • 4
  • 15
cVergel
  • 181
  • 1
  • 3
  • 11
  • 1
  • Yes, that solves it, but the heading isn't supposed to always break. Only on media queries for widths below 590px and above 1200px. So inserting a
    would make it break on every single device, not just the ones with smaller or larger screens. I could remove it selectively with jQuery, but I am ideally looking for an html/css only solution.
    – cVergel Oct 13 '17 at 16:30
  • Also, do you know what difference it made in that case to use a
    instead of wrapping the second line in a ? I was trying to avoid
    on purpose, but it seems to be the solution.
    – cVergel Oct 13 '17 at 16:37
  • Nevermind. I found the solution by changing display to none in the respective media queries. Thank you very much, however I cannot upvote a comment. – cVergel Oct 13 '17 at 16:42

3 Answers3

1

By default, an inline-block will get pushed to the second line unless the entire element can fit on the first line. The whole group of words are a single element and are trying to be inserted into your header directly to the right of the word "That". Because the words as a group are bigger than what can fit on the first line, it puts them all on the next line, but only after expanding the parent (your h2) trying to accommodate it.

If you're ok with the text in span breaking to a new line: Have your span display as block instead of inline-block, or switch to div instead of span. This will ensure that the text goes to a new line without first trying to expand your parent horizontally. Since an inline-block in this situation is probably going to have a line break anyways, why not do it this way?

If you want to make sure that the span doesn't break: Then you can't style it as Inline-block or block. Inline-block won't break only as long as it's width is small enough. Don't set the display (in span) at all and your header will wrap the text and take up the entire width available. Then add a small left/right margin to your header, as the most wasted space you'll have is the width of your longest word being pushed to the next line.

Kyle R
  • 99
  • 2
  • 12
  • Thanks but this isn't the issue. The span is there to make sure the heading breaks exactly where I want it to break. I do want it to break. If I use block instead of inline-block, all my headings will break the layout design (instead of just this one). What I need to know is why an inline-block element, when it breaks to a second line, doesn't keep to the width of its text and instead becomes 100% of the parent's width. I need every heading, breaking or not, to have the width of their text, not the parent element. – cVergel Oct 13 '17 at 16:25
  • If the spans main purpose is to break the line, and you don't want the other ones to break, then just get rid of the span in the other ones? I did explain why the Inline-block becomes 100%. With a display:block span and a display:inline-block h2, all your headings will keep displaying the way they are unless they have a span, in which case they will still display the way they are, except the span text will just be on the next line. Is this not what you want? – Kyle R Oct 13 '17 at 18:18
0

You could set max-width: 80%; on your .parent h2 css (line 7) to get the desired result

J Livengood
  • 2,729
  • 1
  • 12
  • 26
0

You can set a max-width. See example below:

.parent {
  width:50vw;
  background-color:#046;
  height:20vw;
  text-align:center;
}
.parent h2 {
  display:inline-block;
  color:#fff;
  font-family:sans-serif;
  font-weight:900;
  font-size:2vw;
  background-color:#28a;
  padding:10px 0;
  max-width: 45vw; /* 5vw smaller then your parent width, which is 50vw */
}
.parent h2 span {display:inline-block}
<div id="parent1" class="parent">
  <h2>SHORT HEADING</h2>
  
  <h2>THE REALLY LONG HEADING THAT <span>BREAKS TO A SECOND LINE</span></h2>
</div>
Red
  • 6,599
  • 9
  • 43
  • 85
  • 1
    Thanks, but this doesn't address the issue. I can't set a max-width like that because it would make other headings that aren't supposed to break, break, making it worse. I need to know why the heading takes the container's width when breaking to a second line, when that isn't the usual behavior of a single-line heading that has inline-block set. What I need is the breaking heading to be the width of its text. – cVergel Oct 13 '17 at 16:22