albert
albert

Reputation: 61

How to make css not apply to certain attributes?

I am working on a plugin for a wordpress page and in the theme it's going in, there is a style.css for that theme. In that style.css there are CSS attributes that apply to all img and p tags and that is messing up the appearance of my plugin. How can I make it so my plugin, which has its own stylesheet, ignore the style of the theme's css?

Upvotes: 0

Views: 100

Answers (3)

Refilon
Refilon

Reputation: 3489

Include your stylesheet after the style.css

So:

<link rel="stylesheet" type="text/css" href="/path/to/style.css">
<link rel="stylesheet" type="text/css" href="/path/to/yourStylesheet.css">

If this still is not working use !important, but try to avoid this.

!important is placed after the style rule, like so:

p {
    color: red !important;
}

You can also use more specific styles like @SarahBourt said.

#news-item > p {
    color: red;
}

Upvotes: 1

Deimyts
Deimyts

Reputation: 374

You can place styles in your stylesheet specifically to override the theme's styles.

Assuming your stylesheet is loading after the default theme stylesheet, then you can do the following.

In your web inspector, find the offending style in the theme's stylesheet. Copy it into your stylesheet, and replace every value with 'initial' to reset it to the defaults, or with your custom styles if that's more appropriate.

If your stylesheet is loading before the theme's styles, then you can still override the theme styles, even if they include !important. To override !important, create a more specific selector than the theme uses (Read about CSS specificity to figure out the best way of doing this), and add !important only to those which have !important in the theme style. If the style you're overriding doesn't use !important, just use the more specific style, as including too many !important tags can make it harder for you or someone else to modify your code later, as you're experiencing now.

In addition, you want to be sure that your overrides only get applied to your plugin, and not the rest of the site. So, wrap the plugin with a div or other element if it isn't already, and give the wrapper a unique class or id, e.g. class="my-plugin". Preface all of your overrides with this class to avoid breaking other areas of the site.

HTML:

<div class="my-plugin>
    <!--plugin HTML-->
</div>

CSS:

.my-plugin img {
   //override styles
}
.my-plugin p {
   //override styles
}

Overriding original styling like this can get messy, but sometimes it's the only way to get things done when you don't have access to the other .css files. Just do the minimum necessary to make your styles more specific and you should be okay.

Upvotes: 0

Abhinav
Abhinav

Reputation: 8168

Craete a new CSS file and include your stylesheet after the default bootstrap css file, your styles will override the bootstrap styles

If still you are getting some problems you can also use !important next to that style

!important will ensure that your style will be given first preference

Ex:

p{
display:inline-block !important;
}

Upvotes: 0

Related Questions