Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I associate css to html string, received from server?

I'm working on a metro app. Say I receive a string (html) and I load that string in a webview. How can I associate a .css to that html?

Update:

WebViewColumnRight as suggested in an answers is an android method; I'm working on a metro app, c#.

like image 796
Massimo Variolo Avatar asked Jan 20 '13 11:01

Massimo Variolo


People also ask

How do you link your CSS to HTML?

CSS can be added to HTML documents in 3 ways: Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section. External - by using a <link> element to link to an external CSS file.

How do I integrate a CSS file into my website?

There are three ways to add CSS to HTML. You can add inline CSS in a style attribute to style a single HTML element on the page. You can embed an internal stylesheet by adding CSS to the head section of your HTML doc. Or you can link to an external stylesheet that will contain all your CSS separate from your HTML.

How do I link a CSS to a div?

There is no way to "link a css file to a specific div". You can make the css in style. css apply only to a certain div using selectors, but your question, as stated, makes no sense.


1 Answers

As JanivZ said the one way is that your returned html string should contain the reference to the required CSS so that it can be loaded automatically.

or the other option you have is, you can use WebView.loadDataWithBaseURL

htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />" +    htmlData;
webView.loadDataWithBaseURL(baseUrl, htmlData, "text/html", "UTF-8", null);

After this WebView will be able to find your CSS file from the base URL directory. And if both HTML and CSS are coming from the same base URL then no need to specify a baseURL in webView.loadDataWithBaseURL

like image 120
Raman Avatar answered Sep 27 '22 21:09

Raman