Deprecated: Return type of Lime\App::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/Lime/App.php on line 1357

Deprecated: Return type of Lime\App::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/Lime/App.php on line 1346

Deprecated: Return type of Lime\App::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/Lime/App.php on line 1342

Deprecated: Return type of Lime\App::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/Lime/App.php on line 1361

Deprecated: Creation of dynamic property MongoHybrid\MongoLite::$db is deprecated in /home/heraudcoyp/www/admin/lib/MongoHybrid/MongoLite.php on line 20

Deprecated: Return type of MongoLite\Cursor::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/MongoLite/Cursor.php on line 254

Deprecated: Return type of MongoLite\Cursor::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/MongoLite/Cursor.php on line 263

Deprecated: Return type of MongoLite\Cursor::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/MongoLite/Cursor.php on line 259

Deprecated: Return type of MongoLite\Cursor::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/MongoLite/Cursor.php on line 267

Deprecated: Return type of MongoLite\Cursor::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/heraudcoyp/www/admin/lib/MongoLite/Cursor.php on line 247
SVG en-ligne comme fond d'un élément avec CSS3 et Javascript – heraud.co
n°3
SVG en-ligne comme fond d'un élément avec CSS3 et Javascript
Web, Javascript
16 août 2016

Depuis CSS3 on peut définir une image SVG comme fond d'un élément sans URL, en passant directement l'encodage de l'image. On peut ainsi éviter de charger un fichier externe.

div {
  width: 50vw;
  height: 50vh;
  background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNCIgaGVpZ2h0PSI2IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxwYXRoIGQ9Ik0wIDMgTDMgMCBMNCAxIEwyIDMgTDQgNSBMMyA2IFoiIGZpbGw9InJnYmEoMjAwLDIwMCwyMDAsMC43NSkiLz48L3N2Zz4=");
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

L'image SVG est encodée en base64 pour l'insérer en ligne directement dans le CSS. Des outils tel que celui-ci permettent cet encodage.

On peut aussi utiliser Javascript pour cela à l'intérieur d'un script. A ce moment là on peut même éviter d'avoir un fichier CSS.

Ici, par exemple, la même image en SVG HTML5 que j'encode et assigne avec Javascript.

var svgMarkup = "<svg width="4" height="6" xmlns="http://www.w3.org/2000/svg"><path d="M0 3 L3 0 L4 1 L2 3 L4 5 L3 6 Z" fill="rgba(200,200,200,0.75)"/></svg>";

var svg64 = window.btoa(svgMarkup);

document.querySelector("div").style.backgroundImage = "url('data:image/svg+xml;base64,"+svg64+"')";

c. héraud-louisadat

architecte & développeur
67200 Strasbourg
heraud-arobase-laponies-point-fr

*1992, Strasbourg.

Diplômé d'état en 2016 de l'ENSA Paris-Malaquais et de la RWTH Aachen et habilité à maîtrise d'oeuvre en nom propre (HMONP) en 2019, j'ai travaillé pour diverses agences d'architecture à Paris et Berlin puis fait de la recherche en histoire de l'art sur l'architecture Heimatschutz. J'ai fondé l'agence Atkinson-Héraud Architectes en 2023 avec Isabelle Atkinson-Evans. Je développe également des applications pour le Web et la 3D.

Amis, famille et blogroll : Anaïs Héraud-Louisadat, Héraud–Baumann, Till Baumann, Antoine Le Dreff, Station Zéro, BALT.

alsace europe