Responsive font according to text size

Asked

Viewed 998 times

5

How can I make a source responsive according to the size of the text? I have a website: radioturn.com.br

The name of the song is constantly updated, I would like to remove the mark and let it stand still, however, some songs are too big and do not fit in the father div.

How can I do that?

I don’t want to use text-overflow.

  • you are to use bootstrap or you are doing everything by hand ?

  • I’m doing the hand, with bootstrap is possible?

  • 3

    Possible duplicate of Font Responsiva

  • 1

    Obs.: the site is pretty cool.

  • Have you ever thought of cutting the text, including "..." at the end and adding the full name to the title property, is it usually done like this? I do not think it is possible in a simple way to adjust the font size according to the available space. But even if it is possible, I do not think it will look so good. It will seem that your site does not follow a pattern.

  • Are you talking about text-overflow: Ellipsis? I need the song name to be complete.

  • Do you want the text size to decrease if its number of characters is wide? Or cut as told by @mauhumor?

  • .player . status box . sound { font-family:Segoe UI Bold; font-size:40px; color:#FFFFFF; width:300px; text-Transform:uppercase; text-shadow: 0px 2px 0px rgba(0,0,0,0.2); }

  • @Paulosérgiofilho My internet was slow, I think I know what you’re talking about.

  • You could try to change the font px, by in or %. But this would only have effect if the screen size changes. I believe.

  • @Paulosérgiofilho I just left an answer, you can analyze and adapt in your project !

  • 1

    What you need is something like "overflow:resize-text;". But that doesn’t exist. Maybe you have to calculate in Javascript. As already answered (I have not tested).

  • Maybe this is it: http://stackoverflow.com/questions/13358181/resize-font-size-according-to-div-size

  • Doesn’t look like a duplicate of the one indicated at the closing. The indicated does not resolve what was requested, which is to adjust the font size according to the variable text size, to fit.

  • Use Font Awesome, it is with Bootstrap, will save you time and they have various functions.

Show 10 more comments

3 answers

9


I think you can recreate the element that makes up the name of the song, and get its width in pixels. If your width is larger than the width of your container, you can resolve to create a <marquee>.

To apply the recreation of the element simply remove one of the .som and then pull the new element into the same container.

Obs.: The span width is being broken by the container itself, preventing comparison of its width with that of its container. Some style would solve this.

Remember that you do not need to re-create the Marquee/span element.

Example of how to pre-store Marquee/span:

var span = document.createElement('span');

span.className = 'som';
/* Desaplica a largura fixa de 300px: */
span.width = 'auto';

var marquee = document.createElement('marquee');
marquee.className = 'som';

Example of how to update the song name:

/* Remove o marquee por precaução: */
marquee.remove();

var nomeDaMúsica = 'Nome da música';

var container = document.querySelector(
    '.player .caixa-status'
);

span.textContent = nomeDaMúsica;

// Para obter a largura renderizada do span vai ser necessário
// adicioná-lo ao container (obs.: se ele já estiver adicionado
// ele não duplica, de acordo com meu navegador):
container.appendChild(span);

var largura = som.offsetWidth;

if (largura > container.offWidth) {
    /* Transforma o elemento span em marquee: */

    span.remove();

    marquee.textContent = nomeDaMúsica;
    container.appendChild(marquee);
}
  • I don’t understand much how to do it, I’m still new in javascript.

  • @Paulosérgiofilho You would have to do this every time you update the name of the current song. You’ve seen Edit, too (there was a typo)?

  • Is returning the following error: Uncaught Typeerror: Cannot read Property 'appendchild' of null at (index):39

  • @Paulosérgiofilho You are doing this before the container is declared, as seen in the element <head>. Tried to declare the element <script> at the end of <body> or wait for the page to load to execute the code? For my tests, after the page loaded, the container was being successively picked up.

  • I tested, and created a Marquee along with span.

  • @Paulosérgiofilho I’m still trying to find the problem.

  • Okay, I’m standing by.

Show 2 more comments

3

I tried to analyze your case using the javascript. You can adapt it according to your project using other languages if possible.

  • First choose a limit to the size of song titles
  • Restore the song title size
  • If the size exceeds the limit, apply a specific font
  • If not, keep the standard source.

To test the script just change the Titulo da Musica In the code HTML ( the text in span )

function Adapfont(limit){
  var titulo_da_musica = document.getElementById("t").innerText ;
  
  if(titulo_da_musica.length > limit){
     document.getElementById("t").style.fontSize = "large";
  }else{
     document.getElementById("t").style.fontSize = "xx-large";
  }
}
var limit = 20 ;
Adapfont(limit);
<div>
    <span class="titulo-musica" id="t">Titulo da Musica</span>
</div>

SUGGESTION :

Another solution for your case would be to limit the number of characters in the song title.

3

Issue 1

Well, I misread the question and ended up answering about responsiveness according to the viewport. At the risk of a negative vote, I’ll leave you my answer, even wrong, because it might be useful.


You can achieve responsiveness in typography using CSS only.

First, you can turn to the unit in or to %:

body {
    /**
    Faz a fonte ficar 2 vezes maior que o normal
    **/
    font-size: 2em;
    /**
    Fonte 95% do tamanho do normal
    font-size: 95%;
    **/
}

Or even, combine these types of sources with media queries or still use the media queries with the font sizes for each screen size:

/**
    Tela com até 600px
**/
@media (max-width: 600px){
    body{
        font-size: 10px;
    }
}

/**
    Tela com até 959px, maior que 600px
**/
@media (max-width: 959px){
    body{
        font-size: 14px;
    }
}
/**
    Tela com 960px +
**/
@media (min-width: 960px){
    body{
        font-size: 14px;
    }
}

The best CSS solution

Use the unit vw

body{
    font-size: 8vw;  
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.