Is the practice of using data:text/css considered legitimate?

Looking to insert 3 CSS rules into the head tag by using <link /> tags.

$('head').append('<link rel="stylesheet" href="data:text/css, .rule0 {} .rule1 {} .rule2 {}" />');

Would this syntax be considered valid?

I am aware that I can enclose my rules within <style>. For example:

$('head').append('<style>.rule0 {} .rule1 {} .rule2 {}</style>');

Answer №1

If you want to use data URIs for your CSS content, make sure to properly declare them. A working example is shown below:

// Your CSS code goes here
var css = '*{color:red;}';

// Dynamically create a <link> element
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'data:text/css;charset=UTF-8,' + encodeURIComponent(css);

// Append the <link> to the <head>
document.getElementsByTagName('head')[0].appendChild(link);

This will result in the text on the page turning red.

If your CSS is lengthy, consider base64 encoding it. In this case, your .href should be:

// Note: You'll need to use a base64_encode implementation for this,
// as JavaScript does not have one built-in.
link.href = 'data:text/css;base64,' + base64_encode(css);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhancing Jquery loupe with fade-in and fade-out effects

Hello, I am diving into the world of jQuery and stumbled upon the loupe plugin below. After successfully implementing it, I now wish to enhance it by incorporating a simple fade in and fade out effect to the script. However, I have tried a few approach ...

Customize your QTabBar icons with Qt Stylesheet: Adjusting the icon mode

I am currently working on customizing a QTabBar with a stylesheet in my tool. I use QIcon to create icons for the tabs, allowing me to set different modes such as normal, disabled, and selected. The challenge I am facing is trying to apply a global stylesh ...

Can the post() method in jQuery be used to invoke a webmethod in asp.net 3.5?

Check out this block of javascript: $.ajax({ type: "POST", url: "default.aspx/GetDate", contentType: "application/json; charset=utf-8", data: {}, dataType: "json", success: function(result) { ale ...

Artistic Canvas: Selected Image

I am trying to determine if the user has clicked on an image drawn in a canvas element. Despite clicking on the image, nothing seems to be happening. The alert function is not being triggered and the last condition in the code never evaluates to true. An ...

If the overflow-x property is set to hidden for the html and body elements, the links in the footer will become unclickable as they will

Situation: Consider the following scenario with a simplified HTML example: position the footer behind the content and make it stick to the bottom when reaching the end of the page, the footer should slide up from behind the content I achieved this layo ...

Managing Margins and Padding in Multiple Lines of Divs in CSS

Trying to understand the spacing issues that arise when tiling a series of divs within a parent div. Here is a link to the fiddle for reference: http://jsfiddle.net/SNSvU/4/. Below is the code snippet: <div id="prioritiesWrapper"> <div class="p ...

Can someone guide me on how to utilize the onkeyup event within a bootstrap select element?

This code uses Bootstrap to filter elements, but I want to implement an onkeyup event to trigger a function. Whenever I type in the search input, I want it to automatically call myFunction(). <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/ ...

Is it possible to create two header columns for the same column within a Material UI table design?

In my Material UI table, I am looking to create a unique header setup. The last column's header will actually share the same space as the previous one. Picture it like this: there are 4 headers displayed at the top, but only 3 distinct columns undern ...

the script is run only one time

I have developed a unique web component and incorporated it in two distinct sections like this. <div class="sub_container"> <label>Users in Debt</label> <input placeholder="Search by user name" class="input_style ...

Cart Quantity Can Only Be Updated Once Using Ajax

I am currently facing an issue with my page that allows users to increase and decrease the quantity of a product in their cart before proceeding to the checkout confirmation page. I am using Ajax for this functionality, where the backend manipulates the qu ...

Updating the image source using JQuery dynamically

Within my index.php file: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $('.on').click(function ...

What's the most efficient way to show the player's live game score on the screen as it updates, rather than waiting to display it in a final alert

I created a notification that appears at the end of my game, showing how many coins have been earned (200, 300, etc.). However, I would like the notification to display the coin count in real-time while playing the game, rather than only at the conclusion. ...

Checking with JQuery if the alt tag of an image is contained in a variable array, and then applying a class accordingly

My webpage consists of an html grid displaying various images. I am looking to utilize JQuery to dynamically add HTML to all images that contain alt tags matching items from a specified list or array. While there are numerous tutorials available on using ...

Steps to Create an HTML Text Box that cannot be clicked

Does anyone know of a way to prevent a text box from being clicked without disabling it or blocking mouse hover events? I can't disable the text box because that would interfere with my jQuery tool tips, and blocking mouse hover events is not an opti ...

Half-height rows and columns in Bootstrap 4

Can someone help me create blocks similar to the one shown in the image? The left block should be col-5, while the right block should be col-7. The right block needs to have two rows with a height that is 50% of the left block. Is there a way to achieve ...

Adding a class to a TD element only when there is text in that TD as well as in other TD elements

I am facing a challenge where I need to add a new class to a td element, specifically the first one in the code snippet below. However, this should only happen if that td contains a certain number (e.g., "2") and if the next td contains some specific text ...

Submitting a file using Ajax XMLHttpRequest

Currently, I am facing an issue while attempting to send a file using the following XMLHttpRequest code. var url= "http://localhost:80/...."; $(document).ready(function(){ document.getElementById('upload').addEventListener('cha ...

Tips for extracting information from a JSON array using AJAX

I have created a code to retrieve specific data fields such as FirstName, CompanyName, Designation, PersonalEmail, and PersonalWebsite from a database using PHP and MySQL. Here is the code snippet: while ($row = mysqli_fetch_array($q ...

What is the best way to align the navigation with a maximum of three elements (lists) in a row using responsive design?

I've been trying various methods but haven't been able to center it while keeping the padding intact. It's a mobile navigation bar. Here is what my code is producing currently: [1]: https://i.sstatic.net/sHEjj.png This is how I want it to ...

Chrome distorts the display:table when zoomed in

Consider this CSS snippet: .table { display:table; } .table-row { display: table-row; } .table-cell { display: table-cell; } Now, let's add some HTML to go along with it: <div class="table"> <div class="table-row"> ...