Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop CSS overriding HTML width attribute

Tags:

html

css

I have some old content containing tables with their width specified with an HTML width attribute like so: <table width="250">. This attribute is being overridden by a rule for the CSS selector table. Can I stop this overriding so the tables have the width specified in the HTML attributute (which is not always 250 pixels, this is just an example)? I can pick out the tables which shouldn't have their widths overridden with the selector #column1 table.

like image 569
tog22 Avatar asked Mar 19 '12 13:03

tog22


People also ask

How do I override auto width in CSS?

The max-width property in CSS is used to set the maximum width of a specified element. The max-width property overrides the width property, but min-width will always override max-width whether followed before or after width in your declaration.

How do you reset the min-width in CSS?

Conversation. CSS tip: To reset a min-height or min-width declaration, set it to "0", not "auto". For max-height/width, the initial value is "none".

What is CSS override?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.


3 Answers

you can't override the css with html but you can add the attribute style for example:

style = " width: 250px;"

edit: You can override it externally by putting !important on the end of the css rule which will cancel out any other css attributes in relation to what you're changing.

eg:

#column1 table {
  width: 250px !important;
}
like image 195
CKKiller Avatar answered Sep 19 '22 09:09

CKKiller


Try this:

#column1 table {
    width: auto;
}

Just so you know, css will always override old html inline attributes. This is usually a problem when using a wysiwyg editor.

I solved this problem for the old html generated by a wysiwyg editor in a cms with javascript and jquery. I iterate over the table(s) and read the width and height attribute which I then convert to inline css:

var $table = $('#column1 table');
$table.css({
    "width" : $table.attr('width'),
    "height" : $table.attr('height')
});
like image 35
T. Junghans Avatar answered Sep 17 '22 09:09

T. Junghans


I know this question was asked 2 years ago, but it might be helpful for others who have similar issues. You can use CSS attribute selector:

table:not([width]) {
    width: 250px;
}

Note that it won't work under IE 9 as far as i know.

like image 38
greomex Avatar answered Sep 17 '22 09:09

greomex