Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I override a child element's style settings?

Tags:

html

css

jqxgrid

I'm working with a javascript grid plugin (jqWidget's grid). It allows me to apply a custom style to a cell when defining its columns:

{ "text": i, "width": 1, "cellclassname": "custom-column" }

This is useful to me because I am trying to reduce the width of the columns to exactly 1em.

.custom-column {
     width: 1em;
}

Unfortunately they included an extra DIV without an id or class in each cell:

<div role="gridcell" style="left: 725px; z-index: 1171; width:25px;" class="jqx-grid-cell jqx-item custom-column">
   <div style="overflow: hidden; text-overflow: ellipsis; padding-bottom: 2px; text-align: left; margin-right: 2px; margin-left: 4px; margin-top: 4px;">M</div>
</div>

In the HTML above you can see my custom class custom-column but the inserted child DIV includes padding and margins which I believe is affecting the grid cell by making it much larger than 1em (it appears closer to 3em).

Is there anything I can add in my custom-column class that would override or counter the padding and margin settings in the child?

Update : More info after change

Based on comment and the first answer I changed my custom-class to look like this:

.custom-column > div {
   width: 1em;
   margin: 0;
   padding: 0;
}

As far as I can tell this made no difference. I'm including a screenshot of Chrome's debugging tool which I hope shows enough information to help.

enter image description here

like image 742
Sailing Judo Avatar asked Sep 20 '15 17:09

Sailing Judo


People also ask

How do I override a style in HTML?

An !Important declaration is a great way to override the styles you want. When an important rule is used on a style declaration, this declaration will override any other declarations. When two conflicting declarations with the !important rules are applied to the same element, the declaration with a greater specificity will be applied.

How to override inline styles with the !important rule?

When an important rule is used on a style declaration, this declaration will override any other declarations. When two conflicting declarations with the !important rules are applied to the same element, the declaration with a greater specificity will be applied. Let’s see how you can use the !important declaration to override inline styles.

How do I override a class with an ID selector?

An ID selector only takes precedence over a Class selector if they are both used in the same element. Let’s now see how we can make one class override another. If that class has a background-color of blue, and you want your <div> to have a red background instead, try to change the color from blue to red in the class itself.

How to override other styles in WordPress customizer?

The CSS entered and saved gets added to the document head which, like the art direction method we discussed, gives you the opportunity to override other styles on that particular page. WordPress 4.7 introduced a new feature in the WordPress Customizer that adds a CSS editor:


1 Answers

just add this CSS rule:

.custom-column > div{
    margin: 0 !important;
    padding: 0 !important;
}

If the div is not an immediate child, replace > with a blank space.

You will have to add the keyword !important after every rule because the styles are inside the div element.

Note: But adding too much of this rule is not a good practice.

like image 191
Kristijan Iliev Avatar answered Oct 10 '22 02:10

Kristijan Iliev