What is the best way to add unique styles to multiple tags with the same class designation?

Looking for suggestions on styling two tags with the same class differently?

I have 2 tables:

<table id="tab1" class=".ui-jqgrid .ui-jqgrid-hbox">
<table id="tab2" class=".ui-jqgrid .ui-jqgrid-hbox">

The first table is a jqgrid on the page, and the second table tag contains a jqgrid within a pop-up. I want to style the top rows (labels/columns) of both jqgrids differently. However, when I apply the following style:

.ui-jqgrid .ui-jqgrid-htable th div {
    height: auto;
    overflow: hidden;
    padding-right: 4px;
    padding-top: 2px;
    position: relative;
    vertical-align: text-top;
    white-space: normal !important;
}

It affects both tables. I need it to affect only the first table and apply a similar style to the second table but with additional properties. Can anyone help me achieve this? Thank you!

Answer №1

Keeping it simple, why not just utilize the ids? Here's a possible solution:

.ui-jqgrid.ui-jqgrid-htable th div {
    /* styling for both tables */
}
#tab1 th div {
    /* specific to table 1 */
}
#tab2 th div {
    /* specific to table 2 */
}

Could I be misunderstanding the question at hand?

It's worth noting that I removed the space between the two class selectors (.ui-jqgrid.ui-jqgrid-htable). The way it was initially presented may have implied that .ui-jqgrid-htable needed to be a child of .ui-jqgrid in your HTML snippet, which is not accurate.

Answer №2

If this suits your needs

HTML Code Snippet

<table id="tab1" class="ui-jqgrid ui-jqgrid-htable">
<table id="tab2" class="ui-jqgrid ui-jqgrid-htable anotherClass">

CSS Code Snippet

.ui-jqgrid.ui-jqgrid-htable th{
    height: auto;
    overflow: hidden;
    padding-right: 4px;
    padding-top: 2px;
    position: relative;
    vertical-align: text-top;
    white-space: normal !important;
}

.ui-jqgrid.ui-jqgrid-htable.anotherClass th{
    /* applying different styles */
}

Ensure to eliminate the dots from the class names in

class=".ui-jqgrid .ui-jqgrid-hbox"
. You can see the font color difference in the second table in the demo link provided below.

View DEMO here.

Answer №3

#section1{
  margin: 10px;
}
#section2{
  margin: 15px;
}

I prefer to utilize the id selectors in my CSS code for better organization.

Answer №4

When discussing jqGrid, it is essential to note that we begin by specifying the <table> element like this:

<table id="list"></table>

This will then be transformed dynamically into a more complex structure consisting of various divs and tables, resembling the following:

To customize the formatting of column headers in the grid, you can define CSS using

.ui-jqgrid .ui-jqgrid-htable th div {...}
. If you have multiple grids such as

<table id="grid1"></table>
<table id="grid2"></table>

and wish to apply CSS specifically to the column headers of only one grid, you can target the id of the outer div "gbox" or "gview", which is constructed based on the id of the <table> element. For instance, to apply the necessary CSS to just the grid #grid1, you can use

#gview_grid1 .ui-jqgrid-labels th div {
    height: auto;
    overflow: hidden;
    padding-right: 4px;
    padding-top: 2px;
    position: relative;
    vertical-align: text-top;
    white-space: normal !important;
}

I chose to use the class ui-jqgrid-labels of the <tr> instead of ui-jqgrid-htable to ensure that the CSS applies solely to the column headers, even if you are utilizing the search toolbar in the grid.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Conflicting jQuery slide effects and jQuery UI positioning causing issues

As you hover over an element, the position function adjusts its left attribute to the correct value. However, when you move the mouse away, it resets back to 0, causing the element to appear below the first one in the list. This issue is noticeable when y ...

Error message in ThreeJS KTX2Loader: "Failed to load incompatible compressed texture format in .uploadTexture()"

While troubleshooting unfamiliar code, I encountered the following 3 errors related to loading KTX2 textures: THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture() WebGL warning: compressedTexSubImage: format must ...

Disconnect event happening

I am currently studying nodejs using a French tutorial on creating a chat application with nodejs https://www.youtube.com/watch?v=8jkkd2Ohte8 However, I am facing an issue when trying to track the users who leave the chat room. This is my Server.js code: ...

Utilizing a jQuery plugin or function with customizable options when adding elements dynamically

jQuery offers a useful feature where you can use $(document).on(...) to apply an event to newly added elements in the HTML, such as after an Ajax request. Can you provide an example of how to use this with a custom function/plugin like hoverpulse? Anothe ...

To ensure that quotes are correctly handled in a string being passed to a JavaScript function within an `onclick`

I am facing an issue with a cycle in a jspx file that I have. The cycle looks like this: <c:forEach var="var" items="${dataFile.list.rows}"> <li> <div> <a href="#" onClick="myFunct('${var.url}','escape(${var ...

Developing an interface that processes input text and implements it to perform various functions

UPDATE: Issue Resolved I've been working on creating an API to enable reading text from a word document and having the bot in botpress respond with a specific section of that text. I'm facing some confusion regarding the following aspects: ...

Attempting to send data as arguments in an axios request

I have been working on setting up a forum and I want to implement a feature where clicking on a post title will take the user to a page I've already created that displays the title and body of the post. I utilized MUI for building the page, but I enco ...

What are the steps for sharing a json file?

{ "entry": { "city_id": "1234", "city_name": "California" } } Here is the JSON data that needs to be submitted from an HTML form to a content management system API. The form contains two text boxes for capturing city_id and city_name en ...

A guide to efficiently fetch JSON data synchronously using AngularJS

Trying to fetch data from a JSON file using AngularJS results in an error due to the asynchronous nature of the call. The code moves on to the next step before receiving the data, causing issues. The $http.get method was used. $http.get('job.json&apo ...

My attempts to use media queries are being ignored by Internet Explorer

While other browsers recognize and function properly, it seems like IE is not recognizing my media queries. Could there be something wrong with my code? I've created a similar design before without any issues, so why is this one causing problems? Her ...

The table columns are expanding in width with each click of the "show column" button

https://i.sstatic.net/aZirB.png https://i.sstatic.net/ZC3IP.png every time I click on the button to show or hide certain columns in the table, the width of the columns keeps increasing gradually. Is there a solution for this issue? I am using DataTables wi ...

The height attribute is not functioning properly in Mozilla Firefox

I am facing an issue on my website where I set the height of the body tag to 1460px. It works perfectly on Chrome and IE, but there are problems on Firefox. See my code below: body { background-image: url('../images/background.jpg&apos ...

jQuery's display function is being obstructed by a lengthy operation

$('#someid').show(); someLongRunningFunction(); This snippet indicates that the element with id $('#someid') will be displayed only after the function someLongRunningFunction completes its execution. Is there a possible way to modify t ...

How can I best incorporate a symbol sketching tool into an HTML5 Canvas?

I am currently in the process of converting a symbol drawing library from Java to Javascript with the intention of having the symbols displayed on an HTML5 Canvas object. These symbols are characterized by coordinates and various properties such as shape ( ...

Searching strings with regex

I am working with a specific string of text that I want to replace within a given div. Here's how I know to do it: myelement.replace(/foo/g, 'bar'); But what if I need to use my own string instead of the fixed text "foo"? myelement.replac ...

Get the QR code canvas image with a customized background by downloading it

I am having trouble with downloading a QR code canvas image with a background. The download works fine, but my device is unable to scan the code properly due to an incomplete border. I need to add a white background behind it to make it larger. Current im ...

Display intricate header and preview in a printed datatable

Hey there, I've been using the Datatable plugin and it's really great. However, I've come across a problem with complex headers like this: <thead> <tr><td>some text</td></tr> <tr><td>some te ...

Connecting Filters Vue.js

I am in need of chaining filters. My goal is to implement the following filters: - A search filter - A checkbox styled price filter with pre-defined values such as <$10, $20-$50, $50+ - A checkbox styled filter for selecting 'topics' ...

Retrieve the values stored in hidden input fields

I have a form generated by a PHP loop that displays one row from a database. My goal is to fetch the value of hidden fields in the form when the "update_qty" input field is changed using an onchange event. I need to capture the updated quantity and all the ...

400 (Invalid Request) Error - Uploading Files with Spring MVC Using AJAX through jQuery

Seeking help with AJAX file upload using Spring MVC as I am encountering a 400 bad request error. Despite researching various solutions, none have provided the correct answer so far. Any assistance would be greatly appreciated. Here is the relevant code: ...