Font scaling based on width of container

后端 未结 30 3773
面向向阳花
面向向阳花 2020-11-21 04:35

I\'m having a hard time getting my head around font scaling.

I currently have a website with a body font-size of 100%. 100% of what though? This seems t

30条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 05:17

    Take look at my code. It makes the font size smaller to fit whatever there.

    But I think this doesn't lead to a good user experience

    var containerWidth = $("#ui-id-2").width();
    var items = $(".quickSearchAutocomplete .ui-menu-item");
    var fontSize = 16;
    
    items.each(function(){
        // Displaying a value depends sometimes on your case. You may make it block or inline-table instead of inline-block or whatever value that make the div take overflow width.
        $(this).css({"whiteSpace": "nowrap", "display": "inline-block"});
        while ($(this).width() > containerWidth){
             console.log("$(this).width()" + $(this).width() + "containerWidth" + containerWidth)
             $(this).css("font-size", fontSize -= 0.5);
        }
    });
    

提交回复
热议问题