Html 如何覆盖 td align="center"?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3491593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:08:55  来源:igfitidea点击:

How to override td align="center"?

htmlcsshtml-table

提问by bjornl

As gathered from Why align="center" not overriding on {text-align:right;}this article, CSS should take prescience over old-style layout attributes. I am working on a project where I have to inject content in a cell that has the format:

正如从为什么 align="center" 不覆盖 {text-align:right;}这篇文章中收集到的那样,CSS 应该对旧式布局属性具有先见之明。我正在做一个项目,我必须在具有以下格式的单元格中注入内容:

<td align="center">...</td>

<td align="center">...</td>

I do not want my content to be centered, trying to use text-align="left" does not override the td-tag, as suggested in the article. What can I do to override the TD align attibute?

我不希望我的内容居中,尝试使用 text-align="left" 不会覆盖 td-tag,如文章中所建议的。我该怎么做才能覆盖 TD 对齐属性?

回答by jerjer

If you are allowed to change the content of the td, you can then wrap the td values with another tag with text-align:left;

如果允许您更改 td 的内容,则可以使用另一个带有 text-align:left 的标记将 td 值包装起来;

Resulting Tag would be:

结果标签将是:

<td align="center">
  <div class="left">content here</div>
</td>

Here is the new CSS:

这是新的 CSS:

td div.left { text-align:left; }

If you cannot modify the cell value, another work around is to modify the align attribute using javascript, In jquery:

如果您无法修改单元格值,另一种解决方法是使用 javascript 修改 align 属性,在 jquery 中:

$(document).ready(function(){
   $('td [align=center]').attr('align','left');
   // $('td [align=center]').attr('align','left').css('text-align','left'); //or still not working
});

回答by wilson

td { text-align: left; }

Tested in IE8 & FF3.6.8

在 IE8 和 FF3.6.8 中测试

Demo: http://jsfiddle.net/s8qhJ/

演示:http: //jsfiddle.net/s8qhJ/

回答by Kyle

<td align="center">...</td>

CSS:

CSS:

td
{
text-align: left;
}

Or add a class to it:

或者给它添加一个类:

<td align="center" class="mytd">...</td>

CSS:

CSS:

td.mytd
{
text-align: left;
}

回答by mattsven

For anyone specifically looking to override all elements that specify [align], this'll do:

对于特别希望覆盖指定 [align] 的所有元素的任何人,这将执行以下操作:

body [align] { text-align: center; }