Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Override Style Info from a CSS Class in the Body of a Page?

Tags:

html

css

So I'm working on a project that accepts HTMLs as inputs and returns them as outputs. All of the HTMLs I get as inputs have all of their text in divs and style sheets that dictate the style for each div based on the class attribute.

To better visualize things, and to see how my project is coming along, I would love to output the input HTMLs color coded to specifications I give them. It's really easy for me to modify the body of the HTML, but difficult to deal with the style sheet. All I'm looking for is something simple to override the color property of the style sheet. It can be hacky, as this is just internal code for temporary use. I just want something simple that works. Is there an easy way to override aspects of CSS classes in the body of a file?

[EDIT] I want to provide an example to better explain what I'm looking for. An example of the style sheets I have at the top of my page (that I want to override) is:

.style21{vertical-align:top;font-size:13px;font-family:Helvetica;color:#000000;}

An example of a div whose color I'd like to change is:

<div style="position:absolute;top:432;left:422;color:#ff0000;"><span class="style21">relating to</span></div>

My problem is that I can't override the color specified in the css. As you can see in the above example, I'm trying to do it in the specific style within the div, but that isn't working. [/EDIT]

like image 419
Eli Avatar asked Mar 21 '11 18:03

Eli


People also ask

How do you override a CSS body?

You can never override the style of < html >. And neither you can go out of it's(< html >) scope. In this case whatever you do, you are bound to leave 20px on left side as you've styled the base part which is < html > and which can't be overridden.

What is CSS style overriding?

CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. Overriding: Overriding in CSS means that you are providing any style property to an element for which you have already provided a style.


2 Answers

Either use the style attribute to add CSS inline on your divs, e.g.:

<div style="color:red"> ... </div> 

... or create your own style sheet and reference it after the existing stylesheet then your style sheet should take precedence.

... or add a <style> element in the <head> of your HTML with the CSS you need, this will take precedence over an external style sheet.

You can also add !important after your style values to override other styles on the same element.

Update

Use one of my suggestions above and target the span of class style21, rather than the containing div. The style you are applying on the containing div will not be inherited by the span as it's color is set in the style sheet.

like image 98
Graham Avatar answered Nov 07 '22 20:11

Graham


  • Id's are prior to classnames.
  • Tag attribue 'style=' is prior to CSS selectors.
  • !important word is prior to first two rules.
  • More specific CSS selectors are prior to less specific. More specific will be applied.

for example:

  • .divclass .spanclass is more specific than .spanclass
  • .divclass.divclass is more specific than .divclass
  • #divId .spanclass has ID that's why it is more specific than .divClass .spanClass
  • <div id="someDiv" style="color:red;"> has attribute and beats #someDiv{color:blue}
  • style: #someDiv{color:blue!important} will be applied over attribute style="color:red"
like image 41
Johnner Avatar answered Nov 07 '22 20:11

Johnner