Ways to ensure that list items have uniform height

I currently have a group of elements arranged horizontally using the display:inline-block property.

However, one element is larger in height than the rest and extends beyond its boundaries. How can I adjust this element to match the height of the others while keeping its margins or paddings intact?

I'm unsure about the best approach for this issue, but I believe there are two possible solutions:

  • Is there a way to reduce the size of the larger element so it aligns with the others?

  • Can the other elements be resized to match the larger element?

Here's the codepen link for reference: http://codepen.io/anon/pen/domBYj

  <ul class="topbar-menu">
    <li><a> 1000 points </a></li>
    <li><a class="action-button green"> Get Points </a></li>
    <li><a> Level 10 </a></li>
    <li><a> Buddies </a></li>
  </ul>

This represents the menu structure:

.topbar-menu {
    display: table;
    margin: 1em auto;
}

.topbar-menu > li {
    background: #333;
    display: inline-block;
    position: relative;
}

.topbar-menu a {
    color: #eee;
    display: block;
    padding: 1em 2em;
    text-decoration: none;
}

And here is the larger element:

.topbar-menu .action-button {
    position: relative;
    margin: 0px 10px 5px 0px;
    border-radius: 8px;
}
.green {
    background-color: #82BF56;
    border-bottom: 5px solid #669644;
    text-shadow: 0px -2px #669644;
}

Answer №1

Initially, I recommend implementing the following block of code:

* {
    box-sizing:border-box;
}

This adjustment ensures that any height and width declarations will not include padding or border in their overall dimensions. In other words, it will adjust the height/width first and then incorporate the padding/border afterwards. For example, if you specify width:200px, it will remain 200px regardless of the padding or border added.

You should also make sure to update your existing code:

/* Topbar Menu */
* {
  box-sizing:border-box;
}
ul.topbar-menu {
    list-style: none;
    margin: 0;
    padding: 0;
}

.topbar-menu {
    display: table;
    margin: 1em auto;
}

.topbar-menu > li {
    background: #333;
    display: inline-block;
    position: relative;
}

.topbar-menu a {
    color: #eee;
    display: block;
    padding: 1em 2em;
    text-decoration: none;
}

/* Buttons */

.topbar-menu .action-button {
    position: relative;
    border-radius: 8px;
}
.green {
    background-color: #82BF56;
    border-bottom: 5px solid #669644;
    text-shadow: 0px -2px #669644;
}
.topbar-menu a.green {
    padding-bottom: calc(1em - 5px); /* Adjust padding for specific item with extra border */
}

.topbar-menu a.green:hover,
.topbar-menu a.green:focus{
    background: #82BF56;
}

.action-button:active {
    transform: translate(0px, 5px);
    border-bottom: 0px solid;
}
<div>
  <ul class="topbar-menu">
    <li><a> 1000 points </a></li>
    <li><a class="action-button green"> Get Points </a></li>
    <li><a> Level 10 </a></li>
    <li><a> Buddies </a></li>
  </ul>
</div>  

Answer №2

The size difference can be attributed to the impact of the border-bottom property in the CSS for the .green class, which affects the overall height due to the added margin from the .action-button:

.green {
  background-color: #82BF56;
  border-bottom: 5px solid #669644;
  text-shadow: 0px -2px #669644;
}

To address this issue, you have the option to set a fixed height for all <li> elements to eliminate the excess padding:

.topbar-menu > li {
  background: #333;
  display: inline-block;
  position: relative;
  height:50px;
}

Alternatively, you can remove the bottom margin of the button by adjusting its styling properties:

.topbar-menu .action-button {
  position: relative;
  max-height: 10px;
  margin: 0px 10px 5px 0px;
  border-radius: 8px;
}

Answer №3

To make the necessary changes to your CSS, adjust the bottom margin to 5px:

.navigation-bar .menu-item {
    position: relative;
    max-height: 12px;
    margin: 0px 10px 5px 0px;
    border-radius: 8px;
}

Answer №4

.topbar-menu .action-button {
    position: relative;
    max-height: 3px;
    margin: 0px 10px 10px 0px;
    border-radius: 8px;
}
.action-button:active {
    transform: translate(0px, 5px);
    border-bottom: 5px solid black;
}

When you apply the transform, it shifts the element downwards along with any surrounding white space. However, by adding a border to the .action-button, this downward shift can be counteracted.

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

Implementing a powerful multi-role authorization system in React

I am currently developing an application with multiple user roles (admin, user, manager). I am trying to restrict access to the admin route from both managers and general users, and also render the UI based on the user's role. I have attempted this bu ...

The CSS dropdown menu ends up being positioned underneath the slideshow

Currently, I am working on creating a CSS dropdown menu using CSS3. However, I have encountered an issue where the dropdown menus are going under the slideshow on the page when hovered, making the dropdown items invisible. I am unsure whether I should fix ...

Issue with retrieving data using $_GET in jQuery select2

I am facing a unique situation. Here is the code snippet for select2: $("#tags").select2({ multiple: true, placeholder: "Please enter tags", tokenSeparators: [',', ' '], initSelection : function (element, callback) { ...

Overflow of nested item within flex container

I'm trying to make the content under "foo" fully scrollable, but I've been unsuccessful using flexbox. I attempted it with min-height, yet it didn't work out. Check out this JSFiddle for reference $('.ui.dropdown') .dropd ...

Is it possible to embed HTML code within JavaScript strings?

Is my approach to inserting HTML into a JavaScript string correct? In particular, I want to insert an HTML link into a div tag using JavaScript. Here is the HTML: <div id="mydivtag"></div> And here is the JavaScript code: document.g ...

The HTTP.GET method seems to be malfunctioning

I'm having some trouble loading an image from a HTTP.GET request into my application. The picture just won't seem to display correctly. Any suggestions on what might be causing this issue? Below is the code I am using: HTML: <ion-view view- ...

Returning to the previous page is done by navigating back using the previous URL in Next.js

Recently, I encountered a situation where I had implemented filters on a webpage. Upon applying the filters, the URL of the page would change and additional query strings would be appended to the end. This caused an issue when navigating back using the b ...

Utilizing Ember to transmit models to Bootstrap Popovers

Seeking assistance from anyone familiar with utilizing Bootstrap for Ember components to help resolve an issue. I am trying to understand how to pass a model to the component when using {{bs-bind-popover}} <div {{bs-bind-popover templPop}}>Show pop ...

The initial dispatch of a React hook triggers subsequent dispatches to fire multiple times

Whenever I include more than one dispatch in useEffect(), it causes all subsequent actions to be triggered twice. Please see the example below. Below is the complete code. Feel free to remove comments in any order you prefer. PS: I am not sure how to mak ...

Typescript MUI Autocomplete: Can you specify the parameter type of the PaperComponents function?

If you use MUI's Autocomplete, there is a property called PaperCompomponent that allows you to pass your own react component. This property is a function with properties as a parameter, which can then be used to pass on to your custom component. In T ...

What are the steps to creating a unique event within an AngularJs service?

In the midst of my AngularJs project, I find myself faced with a dilemma. I have a service designed to manage button events, but I want another service to handle these events indirectly without directly interacting with the buttons themselves. So, my goal ...

Use the CSS class

To create tables with 3 rows and 2 columns using divs, I have utilized the ng-repeat directive. Within this setup, there are two CSS classes - red and green, that need to be applied in the following manner: - Red class for the 1st column of the 1st row - ...

Obtaining the ID of a newly created node from the server in jQuery, JSTree, and ASP.net

My JSTree setup is ready for creating and renaming a new node: .bind("create_node.jstree", function (NODE, REF_NODE) { $.ajax({ async: false, cache: false, type: 'POST', ...

"Combine elements into groups and calculate their sum using the 'groupBy

Below is an example of an array: let transactions = [{ date: 2019, amount: 3000 }, { date: 2019, amount: 5500 }, { date: 2020, amount: 2300 } ] I am looking to calculate the total amount for each year, resulting in the fo ...

The event listener for the custom cursor in Nuxt.js is failing to work properly when the route

I am currently in the process of constructing a new website for our studio, but am encountering difficulties with getting the custom cursor to function correctly. I implemented a custom cursor using gsap, and it worked perfectly; however, once I navigate t ...

A guide on displaying an Angular Component within a Thymeleaf page

Currently, I am faced with the task of integrating Angular and Thymeleaf. This is necessary because I have developed a dynamic graph using Angular and now need to incorporate it into an existing project that utilizes Thymeleaf. Are there any simplified m ...

Obtain the contenteditable span's value

I'm facing an issue with a content editable span - I want to extract its value when the post button is clicked, but it's not working as expected. Please note that I am unable to convert the span to a textbox or div, I specifically need the value ...

Lately, I've been coming across mentions of "myApp.controllers" and "myApp.modules" in Angular JS. Can anyone explain the significance of these terms?

Recently, I've come across code that looks like this: angular.module('app.controllers') This pattern has appeared in a few tutorials I've read. However, the purpose of the .controllers within the module name is unclear to me. I'v ...

Position the div at the bottom of the menu container, ensuring it is not displayed on mobile devices

When I click the button on my mobile sidenav menu, it appears and disappears. I'm trying to figure out how to position <div class="mtsMenu_footer"> at the bottom of the menu. I attempted to use position: absolute; and bottom: 0; in the ...

It seems that Firefox is ignoring the word-wrap style when the class of a child element is changed

Take a look at this: var iconIndex = 0; var icons = ['check', 'chain-broken', 'flag-o', 'ban', 'bell-o']; $('button:eq(0)').click(function() { iconIndex = (iconIndex + 1) % icons ...