How to change the attributes of a .txt file using CSS? - html

I've inserted a .txt file in an html page using the object tag. But the text don't keep the parent's attributes. Here's the code: I set color:blue but the text is black.
#DESC {
color:blue;}
<div id="DESC"> <object data="document.txt" type="text/plain"></object> </div>

Including a text file using an <object> element is much like including one using an <iframe>. You create a viewport within your document which contains an independent document, CSS rules will not be inherited into the sub-document.
In Firefox (and possibly other browsers, but I haven't tested) you can use JavaScript to access the contentDocument property of the object (or iframe) and from there access the body (some browsers render text files by generating an HTML document representation of them) and modify the style.
In general, however, you would be better off including the text as part of the main document and then styling it normally. This could be as part of a static file, some form of server side include or (for the least reliable and search engine friendly approach) using the JavaScript XMLHttpRequest object.

css is only compatible with html
so the answer to your question is - you can not change a text file appearance with css

Related

Include HTML markup from file with pure HTML and suppress the creation of another docment structure

I have a HTML file where I'd like to include the HTML markup from other files, for example:
sub-content-1.html:
<div>This is the <b>sub-content</b> that should be included</div>
I've tried the following solutions from this answer: HTML5 include file:
<object name="included" type="text/html" data="sub-content-1.html"></object>
and:
<embed type="text/html" src="sub-content-1.html">
The problem I have is that it not only includes the content of the html file, but also generates an additional HTML/HEAD/BODY-structure inside the object (embed) element and wraps the content within. Therefore, the included content is treated as a separate document and doesn't use my css styles. I didn't specify this html-structure in the file (only the content) and I don't want it to be treated as a separate document.
I tried specifying the MIME type as text/plain, hoping that it would load it as-is. But still, the html-structure is generated and the content gets wrapped within a <pre>-tag.
So what I want HTML to do is just to include the markup, without the extra document-structure being generated. How can this be done?
If you use jQuery, you can use the .load() function.
If not, you can use an XMLHttpRequest object and load the content into the DOM via AJAX.

Compute html element's style with css

I need to calculate the style of a html element without using browser, first I'll grab the page source and download it's css files. how can I compute the style of an html element using it's css files. is there any engine out there to first parse and then compute the style of a given html node with minimum effort ?
the following is a good example of css parser but it doesn't give you computed style for a given html node in page source dom tree
http://www.codeproject.com/KB/recipes/CSSParser.aspx
You could use PHP.
http://php.net/manual/en/function.file-get-contents.php
You would pass through the path to your CSS file and it would dump it to a string. Then you could use a variety of string editing functions to find the class you're searching for and pull all the text in the string from { to } and then save that to another string and export it wherever you'd like.

Are html tags allowed as value of title attribute on image

I already found this existing question
Is this possible in XHTML: tags in a title-attribute?
...but it seems to be about browser-rendered title attribute.
I'm using a jQuery plugin to render the title attribute, called Tooltip by Flowplayer.
I can already tell you that the rendering of HTML tags contained in title attributes with this plugin WORKS. It shows nicely in FF3, IE7, IE8 and recent versions of Safari, Chrome and Opera.
My question is: is it "legit"?
I only found that the value of title attribute should be "text". Does it mean plain text or "string"?
Considering that most browsers display the HTML correctly with the jQuery plugin applied, would you think it's fair to put those tags, EVEN if they're not "officially" authorized ??
My answer would be to check out directly with a validator (http://validator.w3.org/). If the page is not on the web, open it in your browser and take the source code generated by jQuery and paste it in the validator's Direct Input tab.
If you want to know what is legit and what isn't, W3.org is the reference.
The W3C say on http://www.w3.org/TR/2011/WD-html5-20110525/elements.html#element-definitions
3.2.4.1 Attributes
Except where otherwise specified, attributes on HTML elements may have
any string value, including the empty string. Except where explicitly
stated, there is no restriction on what text can be specified in such
attributes.
This is a string value it is not markup (elements with or without attributes and text nodes). Firefox 19.0.2 and Chrome 25.0.1364.160 with no plugin, both display the raw string without trying to render it as markup. This I believe is the correct behaviour as an attribute is a text string not markup. So if you want to display html code it is fine (but it should really be html encoded) if you want to get a browser to render the html code without your JQuery plugin it is not going to happen. So your plugin is asking your browser to do something unnatural. Isn't there a better way to achieve the same effect?

Difference between SRC and HREF

The SRC and HREF attributes are used to include some external entities like an image, a CSS file, a HTML file, any other web page or a JavaScript file.
Is there a clear differentiation between SRC and HREF? Where or when to use SRC or HREF? I think they can't be used interchangeably.
I'm giving below few examples where these attributes are used:
To refer a CSS file: href="cssfile.css" inside the link tag.
To refer a JS file: src="myscript.js" inside the script tag.
To refer an image file: src="mypic.jpg" inside an image tag.
To refer another webpage: href="http://www.webpage.com" inside an anchor tag.
NOTE: #John-Yin's answer is more appropriate considering the changes in the specs.
Yes. There is a differentiation between src and href and they can't be used interchangeably. We use src for replaced elements while href for establishing a relationship between the referencing document and an external resource.
href (Hypertext Reference) attribute specifies the location of a Web resource thus defining a link or relationship between the current element (in case of anchor a) or current document (in case of link) and the destination anchor or resource defined by this attribute. When we write:
<link href="style.css" rel="stylesheet" />
The browser understands that this resource is a stylesheet and the processing parsing of the page is not paused (rendering might be paused since the browser needs the style rules to paint and render the page). It is not similar to dumping the contents of the css file inside the style tag. (Hence it is advisable to use link rather than #import for attaching stylesheets to your html document.)
src (Source) attribute just embeds the resource in the current document at the location of the element's definition. For eg. When the browser finds
<script src="script.js"></script>
The loading and processing of the page is paused until this the browser fetches, compiles and executes the file. It is similar to dumping the contents of the js file inside the script tag. Similar is the case with img tag. It is an empty tag and the content, that should come inside it, is defined by the src attribute. The browser pauses the loading until it fetches and loads the image. [so is the case with iframe]
This is the reason why it is advisable to load all JavaScript files at the bottom (before the </body> tag)
update : Refer #John-Yin's answer for more info on how it is implemented as per HTML 5 specs.
apnerve's answer was correct before HTML 5 came out, now it's a little more complicated.
For example, the script element, according to the HTML 5 specification, has two global attributes which change how the src attribute functions: async and defer. These change how the script (embedded inline or imported from external file) should be executed.
This means there are three possible modes that can be selected using these attributes:
When the async attribute is present, then the script will be executed asynchronously, as soon as it is available.
When the async attribute is not present but the defer attribute is present, then the script is executed when the page has finished parsing.
When neither attribute is present, then the script is fetched and executed immediately, before the user agent continues parsing the page.
For details please see HTML 5 recommendation
I just wanted to update with a new answer for whoever occasionally visits this topic. Some of the answers should be checked and archived by stackoverflow and every one of us.
I think <src> adds some resources to the page and <href> is just for providing a link to a resource(without adding the resource itself to the page).
HREF: Is a REFerence to information for the current page ie css info for the page style or link to another page. Page Parsing is not stopped.
SRC: Is a reSOURCE to be added/loaded to the page as in images or javascript. Page Parsing may stop depending on the coded attribute. That is why it's better to add script just before the ending body tag so that page rendering is not held up.
Simple Definition
SRC : (Source). To specify the origin of (a communication); document:
HREF : (Hypertext Reference). A reference or link to another page, document...
SRC(Source) -- I want to load up this resource for myself.
For example:
Absolute URL with script element: <script src="http://googleapi.com/jquery/script.js"></script>
Relative URL with img element : <img src="mypic.jpg">
HREF(Hypertext REFerence) -- I want to refer to this resource for someone else.
For example:
Absolute URL with anchor element: Click here
Relative URL with link element: <link href="mystylesheet.css" type="text/css">
Courtesy
A simple definition
SRC: If a resource can be placed inside the body tag (for image, script, iframe, frame)
HREF: If a resource cannot be placed inside the body tag and can only be linked (for html, css)
You should remember when to use everyone and that is it
the href is used with links
<link rel="stylesheet" href="style.css" />
the src is used with scripts and images
<img src="the_image_link" />
<script type="text/javascript" src="" />
the url is used generally in CSS to include something, for exemple to add a background image
selector { background-image: url('the_image_link'); }
after going through the HTML 5.1 ducumentation (1 November 2016):
part 4 (The elements of HTML)
chapter 2 (Document metadata)
section 4 (The link element) states that:
The destination of the link(s) is given by the href attribute, which must be present and must contain a valid non-empty URL potentially surrounded by spaces. If the href attribute is absent, then the element does not define a link.
does not contain the src attribute ...
witch is logical because it is a link .
chapter 12 (Scripting)
section 1 (The script element) states that:
Classic scripts may either be embedded inline or may be imported from an external file using the src attribute, which if specified gives the URL of the external script resource to use. If src is specified, it must be a valid non-empty URL potentially surrounded by spaces. The contents of inline script elements, or the external script resource, must conform with the requirements of the JavaScript specification’s Script production for classic scripts.
it doesn't even mention the href attribute ...
this indicates that while using script tags always use the src attribute !!!
chapter 7 (Embedded content)
section 5 (The img element)
The image given by the src and srcset attributes, and any previous sibling source element's srcset attributes if the parent is a picture element, is the embedded content.
also doesn't mention the href attribute ...
this indicates that when using img tags the src attribute should be used aswell ...
Reference link to the W3C Recommendation
If you're talking HTML4, its list of attributes might help you with the subtleties. They're not interchangeable.
They are not interchangeable - each is defined on different elements, as can be seen here.
They do indeed have similar meanings, so this is an inconsistency. I would assume mostly due to the different tags being implemented by different vendors to begin with, then subsumed into the spec as is to avoid breaking backwards compatibility.
They don't have similar meanings. 'src' indicates a resource the browser should fetch as part of the current page. HREF indicatea a resource to be fetched if the user requests it.
From W3:
When the A element's href attribute is
set, the element defines a source
anchor for a link that may be
activated by the user to retrieve a
Web resource. The source anchor is the
location of the A instance and the
destination anchor is the Web
resource.
Source: http://www.w3.org/TR/html401/struct/links.html
This attribute specifies the location
of the image resource. Examples of
widely recognized image formats
include GIF, JPEG, and PNG.
Source: http://www.w3.org/TR/REC-html40/struct/objects.html
I agree what apnerve says on the distinction. But in case of css it looks odd. As css also gets downloaded to client by browser. It is not like anchor tag which points to any specific resource. So using href there seems odd to me. Even if its not loaded with the page still without that page cannot look complete and so its not just relationship but like resource which in turn refers to many other resource like images.
src is to used to add that resource to the page, whereas href is used to link to a particular resource from that page.
When you use in your webpage, the browser sees that its a style sheet and hence continues with the page rendering as the style sheet is downloaded in parellel.
When you use in your webpage, it tells the browser to insert the resource at the location. So now the browser has to fetch the js file and then loads it. Until the browser finishes the loading process, the page rendering process is halted. That is the reason why YUI recommends to load your JS files at the very bottom of your web page.

HTML File upload field style

I am trying to create a file upload field that has a little bit of style to it, but I seem to be having problems finding examples of this. I know part of the reason is that the field itself varies from browser to browser.
Any ideas how to do this? Or is there a way to do this without using a file element of a form that can be styled?
If what you mean is the text field for the file names, you can use the input[type=file] selector in the css files. For example :
input[type=file] { background-color: red; }
If what you mean is the file selection dialog box, I think it's browser/OS dependent and there's little (if any) you can do about it.
I have come up on this problem before. Unfortunately, file uploads are nearly impossible to style consistently across browsers. As of CSS 2, I think, the W3C standard specifically leaves behavior undefined--think of how many ways it would need to be implemented on different platforms. Firefox, for example, generates anonymous button and input elements inside the file upload element which only inherit some of the properties that you set on the upload element itself.
You can get some to work using, for example, Furuno's method, but know that the behavior will be spotty and differ widely across platforms/browsers.
Here's some links I found:
QuirksMode Article
One Extra Pixel Article (look for the file input styling section)
This would fit for your requirement.
If you are using jQuery, have a look at this plugin - https://github.com/ajaxray/bootstrap-file-field
This tiny plugin will display the file input field as a bootstrap button (with configurable classes) and will show selected file names (or selection errors) beautifully.
Additionally you can set various restrictions using simple data-attributes or JS settings.
e,g, data-file-types="image/jpeg,image/png" will restrict selecting file types except jpg and png images.