Modify the height of one or more <span> elements within a table cell

Is it possible in this scenario to automatically adjust the row height for

<span class="orange">ORANGE</span>
? And if there are two or more <span> elements, can the cell be split to accommodate varying heights?

(You can use: display: table, table-row, table-cell, or a traditional HTML table)

http://jsfiddle.net/EjqL8/

<html>
<head>
<style>
.table {
  display: table;
  background: #eee;
}
.row {
  display: table-row;
}
.cell {
  display: table-cell;
  border: 1px solid #000;
}

.cell span {
    display: block;
}

.cell span.red {
    background: red;
}
.cell span.yellow {
    background: yellow;
}
.cell span.green {
    background: green;
}
.cell span.orange {
    background: orange;
}
.cell span.grey {
    background: grey;
}

</style>
</head>
<body>
<div class="table">
    <div class="row">
        <div class="cell">Header</div>
        <div class="cell">Header</div>
        <div class="cell">Header</div>
        <div class="cell">Header</div>
    </div>
    <div class="row">
        <div class="cell">Row 1</div>
        <div class="cell">Row 1</div>
        <div class="cell">Row 1</div>
        <div class="cell">Row 1</div>
    </div>
    <div class="row">
        <div class="cell">
            <span class="red">RED</span>
            <span class="yellow">YELLOW</span>
            <span class="green">GREEN</span>
            <span class="orange">ORANGE</span>
            <span class="grey">GREY</span>
        </div>
        <div class="cell">Row 2</div>
        <div class="cell">Row 2</div>
        <div class="cell">
            <span class="orange">ORANGE</span>
        </div>
    </div>
 <div class="row">
        <div class="cell">
            <span class="red">RED</span>
            <span class="yellow">YELLOW</span>
            <span class="green">GREEN</span>
            <span class="orange">ORANGE</span>
            <span class="grey">GREY</span>
        </div>
        <div class="cell">Row 2</div>
        <div class="cell">Row 2</div>
        <div class="cell">
            <span class="green">GREEN</span>
            <span class="orange">ORANGE</span>
        </div>
    </div>  
</div>
</body>
</html>

Answer №1

Check out this solution: http://jsfiddle.net/EjqL8/4/

$(document).ready(function(){
    $(".row .cell:last-child").has("span").each(function(){
        var counter = $(this).find("span").length;
        var height = $(this).height() / counter;                
        $(this).find("span").css('height', height+'px');        
    });
});

Answer №2

Check out this code snippet on JSFiddle

$(".row .cell:last-child").has("span").each(function(){
    var counter = 0;
    $(this).find("span").each(function(){
        counter += 1;
    });

    var height = 100 / counter;

    $(this).find("span").css('height', height+'%');        
});

It is important to ensure that heights are set for your cells for this jQuery script to calculate the correct height for your spans. You can also calculate the height of your cells and set them accordingly for this mechanism to work effectively.

Answer №3

To achieve this, jQuery comes to the rescue. Simply follow these steps:

$('.toExpand').each(function(){
    var parentHeight = $(this).parent().height();
    $(this).height(parentHeight);    
});

This code snippet will expand the element to the full height of its parent container. You can further adjust by dividing the parentHeight variable.

If you want to see it in action, check out the code here: http://jsfiddle.net/QtBlueWaffle/EjqL8/5/

Answer №4

To demonstrate merging columns using the FIDDLE, I utilized the table tag.

Simply utilize the rowspan=n or colspan=n (where n is the number of columns) to combine the columns efficiently.

Answer №5

I hate to sound blunt, but you're basically trying to overhaul something that already exists.

If you want to incorporate a div height percentage, you'll need to establish the parent's height first. Otherwise, you might end up reverting to a fixed starting height, which doesn't seem like an ideal solution.

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

Change the navbar brand in Bootstrap to be hidden on small screens like mobile devices

I am facing an issue with the navbar on my website where it expands in height when viewed on mobile devices due to not fitting into a single line. I have observed that if I remove the brand, it fits perfectly. However, I want to keep the brand visible on l ...

Does the use of CSS positioning impact the performance of browser rendering?

Given two DIVs, A and B, where A contains B, and the following CSS styles: A { margin-left: -2000px; } B { margin-left: 2000px; } With these CSS styles applied, the position of B remains unchanged compared to its original position without any CSS. I am c ...

JavaScript multiplying an array in HTML

Snippet of HTML code <input name="productCode[]" value="" class="tInput" id="productCode" tabindex="1"/> </td> <input name="productDesc[]" value="" class="tInput" id="productDesc" readonly="readonly" /></td> <input name="pr ...

Revamped Panel: Unleash the Power of J

I am currently working on a web application that utilizes a gridview with an updatepanel. After updating the gridview, I implemented a jQuery function that is called at the end of GridView1_RowUpdating. ClientScript.RegisterClientScriptBlock(this.GetType( ...

Transforming a Joomla template into a standard HTML template

I have a question about Joomla. Although I already have a Joomla template that was not originally intended for use with Joomla, I would like to utilize it as an HTML template so I can easily modify and edit it without using Joomla. Therefore, I am looki ...

What could be causing the problem with my CSS background-image being referenced correctly?

Everything seems to be in order: background-image: url(./dir/img.jpg); However, I encounter a 404 error when attempting to reference an image using this approach: index.html <div style="--url: url(./dir/img.jpg);"></div> css backg ...

Activate an update of a jQuery color selector plugin when there is a change in the input field

Currently, I am utilizing the jQuery .click() method to update 15 text fields with basic color hex values like this: document.form1.top_menu_bgcolor.value = '#FFFFFF'; In addition, I have incorporated ExColor for jQuery from which showcases an ...

React DOM's offsetHeight prior to rendering

I am struggling to figure out how to position a React DOM element based on its offsetHeight. The issue is that I cannot determine the offsetHeight of an element that hasn't been created yet (so the height cannot be passed as a parameter to the render ...

Issues with Ajax response being added to the table

I am encountering a technical problem with my university project. The task assigned by the professor is as follows: We need to create a static table that lists 3 domain names in order to enhance the performance of the domain availability API. <table&g ...

The Art of Determining the Text's Baseline

My goal is to create a test that determines whether the text rendered inside an <input> has the same baseline as a label: To achieve this, I want to calculate the baseline of the rendered text in each element and compare their values. Is it possible ...

Achieving Height Diversity in Floating HTML Divs

I have a unique set of dynamically generated divs inside a container div with different heights. <div id="container"> <div id='d1'>Varying content...</div> <div id='d2'>Varying content...</div> < ...

What is the best way to stretch the background image across both the footer and navbar for my app?

Here is the code snippet I am currently working with: <template> <v-app> <AppBar></AppBar> <v-main> <router-view></router-view> </v-main> <Footer></Footer> ...

In jQuery, utilizing dynamic class names with variables

I have a series of images with unique classes such as: .1a .2a .3a .4a ..... I am looking to toggle some other classes named .1b .2b .3b .. and so on so that: '.1a' toggles to '1b' '.2a' toggles to &ap ...

Attempting to grasp the fundamentals of angular Routing, however, upon attempting to reload the page, nothing appears to be displayed

While working in the Bracket editor, I created a file structure with various files located under the 'scripts' tags within the Index.html file. The root folder is named 'projectAngular', inside which there are subfolders like 'appC ...

CSS text width going from left to right

I am attempting to create a text fade effect from left to right, inspired by the technique discussed in this answer. However, I would like the text to be concealed behind a transparent background div instead of the white background used in the example. I ...

Uploading files with AJAX in CodeIgniter using Dropzone and CSRF security measures

Utilizing the dropZone JS jQuery plugin in conjunction with CodeIgniter for uploading images has presented an issue. A reset button is available on the form to allow users to upload a new image, but after resetting the form and attempting to upload again, ...

Attempting to align the text perfectly while also adding a subtle drop shadow effect

Trying to center text with a drop shadow without using the CSS property. I'm utilizing span and :before for cross-browser compatibility. Here is what it currently looks like: The HTML: <h3 title="Say hello to Stack Overflow"><span>Say ...

What steps can I take to avoid scaffold.css from taking precedence over my custom CSS?

I'm having trouble with this. It seems like a small issue, but I'm still very new to learning about rails. Any help you can provide would be greatly appreciated! Solution: To resolve the problem, I accessed the file responsible for adding head ...

I require assistance in developing a mouseover image link that conceals the top image and unveils a bottom image that scrolls vertically in the exact same area as the top

I am trying to achieve a similar effect as shown on: this website I require assistance in creating a mouseover image link that hides the top image and reveals a bottom image that scrolls vertically within the same space as the top image. The long vertica ...

Customize TYPO3 templates by modifying the div id structure

I have a template in TYPO3 that I want to use for multiple pages. Within this template, there is a specific DIV element. I am wondering if it's possible to change the ID of the DIV based on the page UID. This DIV is the only one that changes its cont ...