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#.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With