Ready to get started?Download WordPress


[resolved] Only some of the options in external CSS work (7 posts)

  1. aurimus
    Posted 3 years ago #

    Hey there! Maybe somebody could help me with this

    I wrote a plugin http://wordpress.org/extend/plugins/google-calendar-weekly-timetable/. It displays a table with Google Calendar events. I did it by using code of other plugin.

    I can't understand why only some of external css options for the table work.

    The CSS is included with:
    wp_enqueue_style('gcwt_styles', WP_PLUGIN_URL . '/' . GCWT_PLUGIN_NAME . '/css/gcwt-style.css');

    The table is formed automatically (as it depends on how many events there are). Something like this:

    $markup = '<table <strong>style="border: 10px solid #333;"</strong> class="pretty-table"><tr><th scope="col"></th>';
    		for($i=0;$i < $number_of_days; $i++){
    			$markup .= '<th scope="col">'.$week_days_names[$i].'</th>';

    (the bolded part is what I have to add to make it work, however it is all already in CSS. After the table is output, it looks like this:

    <div class="entry-content">
    						<div><table style="width:200px;border: 10px solid #333;" class="pretty-table"><tr><th scope="col"></th><th scope="col">Monday</th><th scope="col">Tuesday</th><th scope="col">Wednessday</th><th scope="col">Thursday</th><th scope="col">Friday</th><th scope="col">Saturday</th></tr><tr>
    			<th scope="row">18:00</th><td></td><td style="background:blue">Beginners</td><td></td><td style="background:blue">Beginners</td><td></td><td></td></tr></table></div>
    <p> </p>
    <p>[google-calendar-events id="1"]</p>
    											</div><!-- .entry-content -->

    Whole CSS (notice the bold):

    /* TABLE GRID */

    width: 200px; //this option doesn't work unless I include it with style="width:200px"
    padding: 0;
    margin: 0;
    border-collapse: collapse;
    border: 1px solid #333; // this option doesn't work unless I include it with style="border: 1px solid #333"
    font-family: "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    font-size: 0.9em;
    color: #000;
    background-color: #bcc0e4; //This option works

    table.pretty-table caption

    caption-side: bottom;
    font-size: 0.9em;
    font-style: italic;
    text-align: right;
    padding: 0.5em 0;

    table.pretty-table th, .pretty-table td

    border: 1px dotted #666;
    padding: 0.5em;
    text-align: left;
    color: #FFF;

    table.pretty-table th[scope=col]

    color: #000;
    background-color: #6E6E6E;
    text-transform: uppercase;
    font-size: 0.9em;
    border-bottom: 2px solid #333;
    border-right: 2px solid #333;

    table.pretty-table th+th[scope=col]

    color: #fff;
    background-color: #6E6E6E;
    border-right: 1px dotted #666;

    table.pretty-table th[scope=row]

    color: #FFFFFF;
    background-color: #424242;
    border-right: 2px solid #333;

    table.pretty-table tr.alt th, table.pretty-table tr.alt td

    color: #2a4763;

    table.pretty-table tr:hover th[scope=row], table.pretty-table tr:hover td

    background-color: #22aa00;
    color: #000;

  2. stvwlf
    Posted 3 years ago #

    Most likely your CSS is not specific enough

    In other words, if there exists in the theme's stylesheet something like:
    .entry-content table { width: 450px; }

    and your css is
    table { width: 750px; }

    the CSS from the stylesheet will be applied because it is more specific than your CSS.

    The best way to debug this type of problem is with Firefox's Firebug extension. It will show you exactly what CSS is being applied to a page element, and the line number and theme file in which that CSS originates. Then you will see where you need to make your CSS more specific.

    The reason the styling works when you add it in "style=" attributes is because in almost all cases anything applied inline overrides anything located in a stylesheet. After you move it to a stylesheet it is subject to the rules of the cascade, which is what you are experiencing.

  3. aurimus
    Posted 3 years ago #

    Hey thanks for your answer,

    What do you mean by "more specific"? It is declared first/last? the two examples look alike to me

  4. stvwlf
    Posted 3 years ago #

    Many styling rules can be applied to a single page element. CSS is a system that determines which styling element winds up being applied.

    If this is at the top of a stylesheet
    table { width: 500px; }

    and this is at the bottom of a stylesheet
    table { width: 750px; }

    then all tables will be 750 pixels wide because when all other factors are equal, the last assignment to an element "wins"

    .entry-content table { width: 450px; }
    is more specific than
    table { width: 750px; }

    so if
    .entry-content table { width: 450px; } is at the top of the stylesheet and
    table { width: 750px; }
    is at the bottom of the stylesheet, all tables that have a parent element assigned to class entry-content will be 450 pixels wide.

    there can be quite a difference between
    .entry-content table

    When you create this
    <table style="width:800px">,
    that is even more specific since it is directly assigned to the page element inline. Under the rules of CSS inline is one of the highest ranking assignments in terms of resolving conflicting assignments, in determining which styling assignment "wins", that is, is applied by the browser to that page element.

    Most likely your CSS is not as specific as other assignments in the theme's stylesheet and thus is being overridden. That would be why its not being applied.

    I highly recommend learning to use Firebug. It will help you resolve this kind of issue faster than almost anything else. It shows you all the CSS that is assigned to an element, and the order in which it is being applied. It also shows as crossed out any CSS that is overriden by a more specific or higher ranked CSS assignment.

  5. Mark / t31os
    Posted 3 years ago #

    Use the !important declartion with each of your styles to ensure they win out over other competing style definitions.


    .somelcass #someid { color:#000!important; }


  6. aurimus
    Posted 3 years ago #

    Hey, Mark, Steve, Thank You!

    I solved the issue without !important. I assigned id to the table and then used it in CSS. I found more about CSS Specificity. It is calculated in a particular way and determines what CSS properties are taken in case of conflict:http://www.w3.org/TR/CSS2/cascade.html#specificity

    PS. Would be great to know feedback about the plugin. I have like 300downloads in a week. Does that mean somebody actually needs this kind of plugin? (I'm just thinking on whether I should continue developing after there's no use for myself)

  7. stvwlf
    Posted 3 years ago #


    I think there will be interest in your plugin.

    I have a suggestion about customized stylesheets. When WordPress updates a plugin it deletes all the existing plugin files before loading the revised plugin files. That means a customized stylesheet will also be deleted.

    You can code the plugin to first look for its stylesheet in the active theme folder. Then if its not found there load the stylesheet from the plugin folder. That way a customized stylesheet is not overwritten when the user updates the plugin.

    A popular plugin that works this way is wp-page-navi
    You can use some of its code as the basis of your version of that feature.

Topic Closed

This topic has been closed to new replies.

About this Topic