This question already has answers here:
What's the difference between the HTML width / height attribute and the CSS width / height property on the img element?
(8 answers)
Differences between assigning attribute, style, and class in div
(3 answers)
Closed 7 years ago.
What is the difference between height="50" VS style="height:50px" ?
And height="50" VS style="height:50"?
I am always confused by this.
Presentation-related attributes such as height="50" where the original way to specify presentation details of HTML elements.
However, they have since been deprecated in favour of CSS, via the style, class and id attributes, which give a lot more flexibility that the original attributes (at the very least because CSS can be extended without touching the definition of HTML itself, but of course also because you get the "cascading" part, as well as multiple units, media-queries, and much more).
You should thus generally avoid such attributes in HTML.
The only exception is HTML in e-mail, as many clients support those attributes but not the CSS versions.
Note that you should generally avoid style attributes as well, in favour of separate CSS, and class and/or id attributes. This allows you to completely separate the HTML and CSS, and makes it easier to change the presentation of your page without touching the HTML (or the code that generates it).
Also, in CSS (and thus in style attributes), you must specify units (except for 0), so height: 50 is not valid, you should use height: 50px (or another unit).
Using style attribute you add rich CSS to the element. Some styling can not be added using HTML attributes. For example <div style="background-color: #ff00ff; float: right"> is impossible with plain HTML attributes.
Related
This question already has answers here:
Image width/height as an attribute or in CSS? [duplicate]
(12 answers)
Closed 4 years ago.
Fairly new at this, but what’s the benefit to defining image height/width in a linked Css over defining it inline attribute of the img tag? Everything I’m seeing points to defining the img size in Html as better since the browser will load the page faster with the proportions in mind without having to track them down elsewhere, but the whole point of separating the two is to keep things like sizes and colors outside the main html and clean up everything.
Thanks!
Using inline style is hardly ever a good approach.
Using classes allows other developers to modify your code much easier. Also managing properties via classes is a standard everyone uses. Nobody will look for them coded inline.
There are really many reasons why you should define your styles somewhere else than inline.
You should take a read here and search stackoverflow/google as this question has been asked many times before.
I would suggest:
1) Define the original size value in HTML om the img tag.
2) Add an "id" to your img tag in HTML
3) With the "id" tag, add it as reference in CSS and adjust the size based on %.
The benefit of doing it like this is that you will control the size from CSS but anyhow have a standard size defined in HTML.
This question already has answers here:
Should I specify height and width attributes for my IMGs in HTML?
(5 answers)
Closed 5 years ago.
I'm not just referring to image's so it's not a duplicate.
It could be Inline JavaScript, Div tags, the list goes on.
Just cause I mentioned width/height, doesn't automatically mean images.
And I was primarily asking about 'HTML,' Not CSS*
Maybe I should've been more specific in asking my question.
This Is My Question:
What's the rule of thumb when determining whether or not Width/Height goes inside a Style Tag, or whether you leave it by itself as an HTML Attribute inside HTML.
Input Variable
<element width="100px" height="100px" />
Style Tag
<element style="width:100px; height:100px;" />
Best practice is to use css for styling because
After dynamic deployment of html, its little hesitating to access
attribute of html.
Sometimes as an Frontend developer you just have
access to static code and in this way it is easy to manipulate the
css code as compare to update html code
They have the same effect.
<element width="100px" height="100px" /> has been used for a long time, same with the widht/height properties of say.. an HTML table.
There is no difference whether you specify it on the element itself or within the CSS, though I now prefer using CSS so I can keep the HTML clear and concise.
There is absolutely NO difference.
This question already has answers here:
How to apply CSS to iframe?
(28 answers)
Closed 6 years ago.
I tend to apply my own custom CSS documents to certain sites, since I prefer dark backgrounds with light text as opposed to the vice-versa standard, and very few sites have a "dark mode" or otherwise cater to that preference, this site itself being an excellent example.
However, I was recently stricken by something odd - an entire HTML document nested inside of another one. (I now understand this is achieved via use of an <iframe> and so it's nearly impossible to style without JS or something) I can only apply the custom stylesheet to the parent document, though.
So, long story short, I'm wondering what sort of selectors I would have to use to target elements of the nested document only - for example, selecting the <body> of the nested document. Would I refer to a body in a html in a !DOCTYPE that is also in a body? What about recursively nested documents?
Whether this nesting thing is poor practice or not does not immediately concern me -- there seems to be a valid use case for it and, regardless, I'm not building the site. What I DO care about is how to add styles to it externally.
Assuming you literally have a document that has been 'injected' into another document, you would simply target it with the expected identifiers:
To target elements unique to the sub-document:
body body [element] {
}
To target elements that exist within both documents, you would just use the standard:
[element] {
}
The above would apply the style to any desired element that is contained within either document.
Please be aware that you cannot style an iframe inside a document with CSS -- you'd either have to find a way to manipulate the iframe's CSS itself, or use JavaScript to target the desired elements with document.getElementById().
Hope this helps! :)
This question already has answers here:
What's the difference between HTML's and CSS's width attribute?
(7 answers)
Closed 5 years ago.
Is there a difference between
<table width="100%">
and
<table style="width: 100%">
I know that the img need
<img width="20px">
to precalculate the space it will use. But how is it with tables?
The first option is the old HTML strategy of setting properties of a table.
It is deprecated because of concerns about adding "visualization" properties to your HTML code, which should focus on content markup.
The second option is the (new) CSS solution but, please, do not declare it "inline" but in a separate CSS file targeting the table element with a class or ID name, otherwise advantages are effectively null; CSS philosophy is based on separation between data and styles, so if you declare it "inline"... there is implicitly no separation.
1st example is HTML width tag, which nowadays is basically used on newsletters.
2nd example is CSS inline, not a good approach, unless you want to override some class style and you don't want to add more classes.
3rd example is like 1st example, and not necessary needs the unit measure , take a look: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Img
1) If inside CSS file we specify the following style:
td
{ text-align:center; }
While in a Html file we have
<td align=”right” … >
then value set in CSS file will take precedence over an inline html attribute and thus elements contained inside <td> cell will be aligned to the center.
a) Is same true for all html attributes? Meaning if a CSS rule and an html attribute functionalities overlap , will the CSS rule always take precedence?
BTW – I know we should usually prefer using CSS rules vs html attributes
thanx
Which set of definitions, HTML attributes or CSS properties, take precedence?
The textbook answer:
CSS properties take precedence over HTML attributes. If both are specified, HTML attributes will be displayed in browsers without CSS support but won't have any effect in browsers with CSS support.
(Reference: http://www.hwg.org/resources/faqs/cssFAQ.html)
The real-world answer:
It depends, if you want to be certain for a specific attribute or set of attributes, you will have to create a unit test and apply those tests to the specific browser(s) that you want to verify for compliance with the "textbook" answer, or compliance to your specification for the specific project you are working on.
You already imply that you know certain HTML attributes are deprecated, so I will not belabor that point here.