How can I ensure that two links are equal in height when placed in columns?

I am working with an unordered list on a webpage that contains

x <li><a href="#">Link text</a></li>
items.

These items are styled to have 50% width each using CSS, resulting in 2 items per line. However, since the link texts can vary in height, I am facing an issue where the links on the same line may have different heights. My question is: how can I use jQuery to set the same height for all links (styled with a border bottom) on the same line? It is important to note that not all links in the sections should have the same height, only those on a "per line" basis.

I hope this explanation is clear :)

Answer №1

If you aim to achieve a layout that features a grid design along with varying row heights, the only viable option in HTML is by utilizing the table element.

However, be prepared to compromise the semantic structure of your code in order to attain the desired visual appeal.

To implement this layout, consider creating a two-column table and transferring the content from your previous li elements into the respective td elements within the table. Utilize properties like vertical-align and CSS styles to customize the appearance based on your design preferences.

Answer №2

Here is the fiddle link: http://jsfiddle.net/aSJSm/1/

Below is the code snippet:

<html> :

<ul>
<li><a href="javascript:void(0)">Item1 bla bla bla</a></li>
<li><a href="javascript:void(0)">Item2</a></li>
<li><a href="javascript:void(0)">Item3</a></li>
<li><a href="javascript:void(0)">Item4 ala bala bala</a></li>
<li><a href="javascript:void(0)">Item5</a></li>
<li><a href="javascript:void(0)">Item6</a></li>
<li><a href="javascript:void(0)">Item7</a></li>
<li><a href="javascript:void(0)">Item8</a></li>
</ul>​

<css> :

ul{
 width: 200px;   
 float:none;
}
ul li a{
 width: 50%;   
 float: left;   
 border-bottom: 1px solid black;    
}​

<javascript> :

var setList = function(){
    var listItems = $('ul').children();
    $.each(listItems, function(index){
        if (index % 2 == 0){
            var leftElementHeight = $(this).children('a').height();
            var rightElementHeight = $(this).next().children('a').height();
            if (leftElementHeight < rightElementHeight)
                $(this).children('a').css("height", rightElementHeight);
            else
                $(this).next().children('a').css("height", leftElementHeight);
        }    
    });
}
setList();​

Remember to wrap it in $(document).ready(function(){}) and ensure you have the latest version of jQuery.

Answer №3

For those interested in achieving this functionality using jQuery, here is a method that can be employed:

​$('li:even').each(function(){ 

    var leftHeight = $(this).height();
    var rightHeight = $(this).next().height();

    var newHeight = (leftHeight <= rightHeight) ? rightHeight : leftHeight;

    $(this).next().andSelf().height(newHeight);

});​​​​​​​​​​

Check it out on JSFiddle: http://jsfiddle.net/svenhanssen/VEnXp/

Answer №4

Assign an identifier to 2 hyperlinks and adjust their height using the provided technique.

$("#identifier").css("height", '100px');

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

acquiring the jQuery $.load() function for AJAX request object

One example displayed how jQuery's $.ajax call can yield an ajax object that is capable of being terminated if necessary. Terminate Ajax requests with jQuery In my current project, we are utilizing $.load(), which provides the object pattern instead ...

Customize context menu events based on the exact section where the menu was activated

Concern: The context menu functions, but it retains click events if pulled up on multiple divs. Steps to Replicate: Right-click one section. Click outside the menu to close it (without choosing an option). Click another section and choose an option. Delet ...

What is the best way to remove the border from bootstrap 4 cards?

I am experiencing a recurring grey outline on the image section of my bootstrap card and I need help removing it. https://i.sstatic.net/sIpIR.png <!-- Card deck --> <div class="card-deck"> <!-- Ca ...

Keeping track of the toggle state using a cookie

While searching for a way to retain the toggle state, I stumbled upon js-cookie on GitHub. The documentation provides instructions on creating, reading, and deleting a cookie. However, an example would have been really helpful in enhancing my understanding ...

Creating an image in JavaScript from PNG data

Sorry if this is a basic question, but I'm still learning about working with images. I have a PHP script that generates a PNG image. The final two lines of the script are header('Content-Type: image/png'); imagepng($image); I am trying to ...

Discovering Unnecessary CSS & JS Code

Currently, I am tackling a freelance project focused on upgrading a website. The client is requesting specific features from their old site be replicated on the new one. However, the challenge lies in the fact that the old site's CSS and JS are consol ...

The responsive navigation bar yielded an unforeseen outcome

Looking to create a responsive navigation bar similar to the one shown here My code successfully shows and hides the navigation items, but it's not updating the burger icon No console errors or warnings are present This is my HTML: <nav> ...

Parent div is positioned absolutely with overflow set to hidden

I am facing a unique requirement where the parent div has an absolute position and the child div also has an absolute position. Inside the parent div, I have set overflow: hidden to hide the extra width of the child div. However, this setup is not working ...

The identifier element.id is not acknowledged as a valid variable name

Having trouble with this task. I have a series of thumbnail images displayed on the page. When a user clicks on one of these thumbnails, I want the image to appear larger in another frame (in svg format). In my html code, I have: <div class="inner ...

Exploring the possibilities of accessing a PHP array with jQuery

I am currently working on an application that is built using Symfony 2.3. Within a jQuery section of my Twig file, I need to access a PHP array but I am unsure of the correct approach. Here is what I have tried: In the controller: public function indexA ...

"Exploring the relationship between Javascript objects and the '

function createNewRobot(robotIdentifier) { this.id = robotIdentifier; this.components = new Array(); this.gatherComponents = function() { $.getJSON('specific/url', function(data) { for(index in data.responses) { this.part ...

How can I change the text color in a QTextBrowser using HTML in PyQt?

I've been attempting to customize the font color of HTML text within a created QTextBrowser. While I have successfully used basic HTML commands to adjust paragraphs and change font size, I am facing difficulties with setting font color. Below is the ...

Javascript problem with adding floats

I am encountering an issue with the total value not being calculated correctly. Here is the scenario: let a = 86.2500; let b = 32.3550; alert(a+b); //returns 118.60499999999999, expected 118.605 alert((a+b).toFixed(2)) //returns 118.60, expected 118.61 ...

Prevent double tap selection

At times, I enable the user to click on the <li> or <span class="icon"> elements, but if they happen to click twice, it causes a bit of chaos and spreads the blue selection all over :/ To address this issue, I have come up with two solutions: ...

Is it possible to add a computed value to the bar's end in Chart.JS?

I'm creating a bar graph that has two bars representing different weeks. Currently, it looks like this: https://i.stack.imgur.com/6Avni.png However, I want it to look like this: https://i.stack.imgur.com/TAVqe.png In order to achieve the desired r ...

What is the best way to format MarkDown content within a nextJs environment?

Markdown-it is successfully converting markdown to html in my Nextjs project. However, I am facing a styling issue with specific elements such as h2, h3, ul, and li. Here's the code snippet: <h1 className="text-3xl mb-3 leading-snug d ...

The functionality of the Kendo dropdown menu is experiencing issues on Internet Explorer 8

I've implemented the following code to connect a kendo dropdownlist: <input type="text" id="ddlCurrency" class="k-dropdown" name="currency" tabindex="3" style="width: 80px;" /> var ddlCurrency = $("#ddlCurrency").ken ...

Using WebApi 2.2 with the Chrome browser may cause a CORS 405 error to occur

Currently, I am working on 2 projects: A Website built with HTML and Durandal that makes a webapi post method call using $.ajax. This site is located at localhost:33432 and communicates with the second site. The ajax call doesn't contain anything ou ...

What is the best method to "deactivate" a radio button while ensuring that a screen reader can still read the text and notify the user that it is inactive?

My current situation involves needing to deactivate certain radio buttons, while still having the option to reactivate them later. When I use the disabled attribute, screen readers will overlook this field and miss key information. I am seeking an accessi ...

Preserving the selected options in a dynamically populated dropdown list after submitting a form using php

I am attempting to preserve form values even after submitting the form. document.getElementById('start_date').value = "<?php echo $_POST['start_date'];?>"; document.getElementById('end_date').value = "<?php echo $_P ...