Hide item after line break

I am trying to create a responsive footer, but this is probably interesting for other sensitive elements too.

Is it possible to hide an element if the line is split into its position?

<footer>
John Doe · Main Street 123 · Sometown · +12 3456 789
</footer>

I want for wide screens:

John Doe · Main Street 123 · Sometown · 012 3456 789

And for small screens, for example:

John Doe · Main Street 123 · Sometown
012 3456 789

or

John Doe · Main Street 123
Sometown · 012 3456 789

and etc.

Thus, the separation point disappears if there is a line break, because it is no longer needed and does not look beautiful at the end or at the beginning of the line.

Edit: Some possible markup

<footer>
John&nbsp;Doe<span class="hide-when-linebreak"> · </span>Main&nbsp;Street&nbsp;123<span class="hide-when-linebreak"> · </span>Sometown<span class="hide-when-linebreak"> · </span>+12&nbsp;3456&nbsp;789
</footer>

Did not find a solution for this, maybe there is an idea how to start?

thank

+4
source share
2 answers

JavaScript :

$(window).on('resize load', function() {
    var footer = 'John Doe · Main Street 123 · Sometown · +12 3456 789' +
                 ' · jd@example.com · www.example.com';
    footer = footer.trim().replace(/&/g, '&amp;') // encode HTML entities
                          .replace(/</, '&lt;')
                          .replace(/([^·])\s+/g, '$1&nbsp;'); // don't allow breaks here
    var $footer = $('footer');
    var html = ''; // actual content to be put in footer
    var height = 0; // actual height of the footer
    footer.split(/· /).forEach(function (part, i) {
        $footer.html(html + (i ? '· ': '') + part); // try, and see what we get
        // Depending on height increase, place a break or a non-breaking space
        $footer.html(html += (i ? ($footer.height() > height ? '<br>' : '·&nbsp;') : '')
                          + part);
        height = $footer.height();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<footer></footer>
+3

, , -.

: , StackOverflow ... : http://codepen.io/memoblue/pen/oLVEOX

div {
  display: inline;
}

@media (max-width: 600px) {
  div {
    display: block;
  }
  .sm-hidden {
    display: none;
  }
}
<footer>
  <div>John Doe · Main Street 123 <span class="sm-hidden">·</span></div><div> Sometown · +12 3456 789</div>
</footer>
0

Source: https://habr.com/ru/post/1651520/


All Articles