Text einpassen in Zeile

Wer kennt es nicht, mal wieder bricht die Überschrift um, aber eigentlich dürfte sie lieber kleiner sein als umbrechen, also einfach die Schriftgröße eins - zwei Pixel kleiner, aber nur an den Stellen wo es erforderlich ist!

Das folgende Beispiel zeigt eine Möglichkeit die das ganze automatisiert, wie üblich ohne großes Framework!

Lorem ipsum dolor sit amet, consetetur sadipscing elitr

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.

HTML:

fittoline.htm HTML (532 Bytes) 06.09.2021 14:33
<!DOCTYPE html >
<html>
  <head>
    <meta charset="utf-8" />
    <title>fittext</title>
    <meta name="author" content="Udo Schmal" />
    <style>
      .fittoline span { white-space: nowrap; }
    </style>
  </head>
  <body>
    <h1 class="fittoline">Lorem ipsum dolor sit amet, consetetur sadipscing elitr</h1>
    <p class="fittoline">Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua.</p>
    <script src="fittoline.js"></script>
  </body>
</html>

JavaScript:

fittoline.js JavaScript (1,61 kByte) 06.09.2021 14:32
// coding: utf-8
/*! Created by: Udo Schmal | https://www.gocher.me/ */
/* fit text to line by reduce font size */
(function() {
  'use strict';

  function fitToLine (el) {
    if (fitToLine.prototype._singletonInstance) {
      var self = fitToLine.prototype._singletonInstance;
    } else {
      var self = this;
      fitToLine.prototype._singletonInstance = this;
      this.arr = [];
      window.addEventListener('resize', function() {
        var i, len=self.arr.length;
        for(i=0; i<len; i++) {
          self.update(self.arr[i]);
        }
      });
    }
    self.init(el);
    self.arr.push(el);
  }
  fitToLine.prototype = {
    init: function (el) {
      // wrap a span around the content
      var span = document.createElement("span");
      while (el.hasChildNodes()) {
        span.appendChild(el.removeChild(el.firstChild));
      }
      el.appendChild(span);
      this.update(el);
    },
    update: function (el) {
      // get the max width and the max font-size
      var elWidth = (el.style.width ? parseFloat(el.style.width, 10) : el.offsetWidth);
      var fontSize = (el.style.fontSize ? parseFloat(el.style.fontSize, 10) : 50);
      var span = el.firstChild;
      while (span.offsetWidth < elWidth) {
        el.style.fontSize = ++fontSize + "px";
      }
      // reduce font-size until span width less then max width
      while (span.offsetWidth > elWidth) {
        el.style.fontSize = --fontSize + "px";
      }
    }
  };

  var i, tags = document.querySelectorAll('.fittoline');
  for(i=0; i<tags.length; i++) {
    new fitToLine(tags[i]);
  }
})();

teile es:

Copyright / License of sources

Copyright (c) 2007-2025, Udo Schmal <udo.schmal@t-online.de>

Permission to use, copy, modify, and/or distribute the software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Kontakt

Udo Schmal
Udo Schmal

Udo Schmal
Softwareentwickler
Ellerndiek 26
24837 Schleswig
Schleswig-Holstein
Germany




+49 4621 9785538
+49 1575 0663676
+49 4621 9785539

Google Maps Profile
Instagram Profile
vCard 2.1, vCard 3.0, vCard 4.0

Service Infos

CMS Info

Product Name:
UDOs Webserver
Version:
0.5.1.240
Description:
All in one Webserver
Copyright:
Udo Schmal
Compilation:
Wed, 23. Apr 2025 10:41:18

Development Info

Compiler:
Free Pascal FPC 3.3.1
compiled for:
OS:Linux, CPU:x86_64

System Info

OS:
Ubuntu 22.04.5 LTS (Jammy Jellyfish)

Hardware Info

Model:
Hewlett-Packard HP Pavilion dm4 Notebook PC
CPU Name:
Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz
CPU Type:
x86_64, 1 physical CPU(s), 2 Core(s), 4 logical CPU(s), max 3000.0000 MHz