5

Sometimes in xhtml we write suppose <table border="1"> and then again in the css we write table{ border:2px solid black}. I am confused when to write which. When to use attributes and when to use the css. Sometimes they get confusing.

Stephen P
  • 14,422
  • 2
  • 43
  • 67
raiyan106
  • 123
  • 2
  • 9
  • 2
    Regarding the `border` attribute: `Do not use this attribute, as it has been deprecated: the element should be styled using CSS.` https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table#attr-border
    – showdev Jul 15 '14 at 20:49
  • Well this was just an example. Another one is cellspacing and cellpading. Sometimes it is declared as an attribute and sometimes in the css. It gets confusing. – raiyan106 Jul 15 '14 at 20:50
  • 2
    The same goes for `cellpadding` and `cellspacing`: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table#Attributes – showdev Jul 15 '14 at 20:55
  • Actually, `border="1"` on the table element is not deprecated or obsolete in HTML5. The [HTML5.1 draft](http://www.w3.org/html/wg/drafts/html/master/tabular-data.html#attr-table-border) says "The border attribute may be specified on a table element to explicitly indicate that the table element is not being used for layout purposes. If specified, the attribute's value must either be the empty string or the value "1". The attribute is used by certain user agents as an indication that borders should be drawn around cells of the table." – Alohci Jul 15 '14 at 20:58
  • @Alohci yes it is... http://www.w3schools.com/tags/att_table_border.asp – RandomUs1r Jul 15 '14 at 21:04
  • 3
    @RandomUs1r - That's why you should never pay attention to what w3schools says. It is frequently wrong. And not associated with the W3C in any way. – Alohci Jul 15 '14 at 21:07
  • 2
    @RandomUs1r ... never trust w3schools to get their facts straight. go to the doc itself. Even if the site has corrected some issues, it is still not as reliable as the documentation itself, and is in no way a valid argument against the documentation – Brett Weber Jul 15 '14 at 21:07
  • @Alohci point taken, I didn't know it had discrepancies with the spec, however that wouldn't stop me from using style="border: 1px" (don't use just '1') in my markup as a border has to do with display & not content (html). – RandomUs1r Jul 15 '14 at 21:38
  • possible duplicate : http://stackoverflow.com/questions/2414773/is-it-ok-to-use-cellpadding-2-cellspacing-2-in-table – Brett Weber Jul 15 '14 at 22:03

4 Answers4

6

If you're not sure which tool to use, think about what they actually are.

HTML is a markup language. It's used to describe the content on your web page so that it can be understood by whatever is browsing your site. It defines paragraphs, quotes, important pieces of content, navigation, etc.

CSS is a styling language. It describes how your content is presented. Use it for making elements look the way you want them to look, making your website into something visually appealing.

Borders are a part of the visual style of your content. You've put your content in a table because it makes sense, but if you want to add a border to the table, add in CSS rules to make it look the way you want.

You'll often find that older HTML code is written with more attributes that style the content, as well as <font> and <center> tags and other things that mix the content with the presentation. You may also find that some visual parts of your site aren't possible in pure CSS, and require extra markup to make it work; these are the kinds of tradeoffs you get to make as a web developer.

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
2

Although the border attribute is not deprecated whereas the align attribute is (to give light to the status of the attribute as of the HTML4 recommendation), it is commonly recommended to not place these attributes in your markup.

I cannot find a reference to these attributes when looking at XHTML table documentation, so I would assume these are not acceptable there.

In place of the attributes in question, using CSS eliminates mixing content and styling. XHTML seems to be completely focused on content as opposed to styling, so I would have to say that although it is acceptable, it is not the best route to use the attributes in place of CSS in an external stylesheet.

One case where I could see using attributes instead of css would be in designing a html email, as most email clients strip css unless it is declared inline using the style attribute. It may be cleaner to use the individual attributes than stuffing all of the rules into the style attribute in this case.


UPDATE

Due to information provided in a comment from another user, I see that the XHTML Spec i was looking at is outdated. Modern XHTML does recognize and support attributes such as border. The link provided is an explanation of the differences between html and xhtml as an introduction to the subject. Good stuff. The comment I am referencing is as follows :

When you discuss the XHTML documentation, you mean the aborted XHTML 2 specification. Modern XHTML is XHTML5, which has the same attribute and content model rules as regular HTML5, just a different syntax. The authors of XHTML 2 set out to remove all the crud that had built up on HTML over the years, and so took a very purist approach. The HTML5 authors took a much more pragmatic attitude, recognised the certain odd features, like border="1", were actually useful and permitted them for those narrow situations.

Brett Weber
  • 1,839
  • 18
  • 22
  • When you discuss the XHTML documentation, you mean the aborted XHTML 2 specification. Modern XHTML is [XHTML5](http://www.w3.org/html/wg/drafts/html/CR/introduction.html#html-vs-xhtml), which has the same attribute and content model rules as regular HTML5, just a different syntax. The authors of XHTML 2 set out to remove all the crud that had built up on HTML over the years, and so took a very purist approach. The HTML5 authors took a much more pragmatic attitude, recognised the certain odd features, like `border="1"`, were actually useful and permitted them for those narrow situations. – Alohci Jul 15 '14 at 23:38
  • @Alohci - Great Information! I didn't know any of that, thank you for sharing it! – Brett Weber Jul 16 '14 at 15:25
1

You should prefer styles to attributes, and in this particular case you should always use styles because, according to the W3C index of attributes, the border= attribute on <table> is deprecated. Cellspacing and cellpadding are not officially deprecated, but their use is discouraged, and they are listed as "obsolete features" in HTML5.

As much as you possibly can, keep all style/presentation out of the document - the document should be content and structure - then style it however you want using XSLT or CSS.

Stephen P
  • 14,422
  • 2
  • 43
  • 67
  • It has already been pointed out in the comments that this statement("the border= attribute on is deprecated. Cellspacing and cellpadding are also deprecated.") is invalid. If you can provide documentation to this that could override the docs straight from W3C, I will remove my downvote
    – Brett Weber Jul 15 '14 at 21:25
  • @Brett sure, thanks, I'll cite the source ... but there were no comments when I was composing this, at least before I hit 'Post' – Stephen P Jul 15 '14 at 21:43
  • It's no problem. I'm just trying to keep this question accurate because I had the exact same question not to long ago. Until I saw this, I was still confused about it. I agree that the attribute shouldn't be used, though. – Brett Weber Jul 15 '14 at 21:45
0

Table attributes are deprecated. You should always use css to style your tables. Wheter you create a separate stylesheet or you embedded it in your html file.

Any html element can be styled using css.

EDIT:

More information about deprecated attributes here: http://www.w3.org/TR/html4/index/attributes.html

Good Luck!

rob
  • 715
  • 2
  • 6
  • 20
  • It has already been pointed out in the comments that this statement("Table attributes are deprecated") is invalid. If you can provide documentation to this that could override the docs straight from W3C, I will remove my downvote – Brett Weber Jul 15 '14 at 21:24
  • This is a link to attributes. The row defining border, as related to table, does not show that the attribute is deprecated. The space that should signify this is blank, not containing the character "D", as defined in the legend, to represent a deprecated attribute. This attribute is not deprecated in this recommendation for HTML4, nor the draft for HTML5.1. Beyond this, it is not shown to be expected to phase out, either. I'm sorry, this has only solidified the comments on the question – Brett Weber Jul 15 '14 at 21:43
  • Not trying to a super stickler, just trying to keep this question clean since I have been confused about this as well. I do agree that the attribute should not be used, though. – Brett Weber Jul 15 '14 at 21:50