Center unordered lists with varying numbers of items

I am currently facing a challenge while attempting to create four lists with different numbers of items and align them properly on my webpage.

After applying inline-block to the ul class, I encountered issues with alignment as illustrated below:

https://i.sstatic.net/DPked.png

The problem persists even when I try to make all the lists the same size, resulting in the following outcome:

https://i.sstatic.net/MMLvE.png

Below is the snippet of my HTML code:

<body>

    <img src = "sample-img.png" class="center"/>
    <div class= "externalMenu">
        <ul class="exSection">
            <li> <a href= "#">Packaging </li>
            <li> <a href= "#">Packaging Org Chart </li>
        </ul>

        <ul class="exSection">
            <li> <a href= "#">FAQs </li>
            <li> <a href= "#">KB Articles </li>
            <li> <a href= "#">Customer Survey </li>
            <li> <a href= "#">EUCD Dashboard </li>
            <li> <a href= "#">RSM Dashboard </li>
        </ul>

        <ul class="exSection">
            <li> <a href= "#">SPT Maintenance Calendar </li>
        </ul>

        <ul class="exSection">
            <li> <a href= "#">myEars </li>
            <li> <a href= "#">SLM </li>
            <li> <a href= "#">RSM </li>
            <li> <a href= "#">Remedy </li>
            <li> <a href= "#">Export </li>
        </ul>
    </div>

</body>

My CSS stylesheet looks like this:

*{
margin: 0;
padding: 0;
}

.center{
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 30%;
}

.externalMenu{
 width:100%;

}

.exSection{ 
  background-color:yellow;
  list-style-type:none;
  display:inline-block;
  border: 1px solid #000;   
  width: 200px;
  height: 150px;
}

Answer №1

To make sure your content is aligned at the top within .exSection, include vertical-align: top;:

.exSection{ 
  background-color:yellow;
  list-style-type:none;
  display:inline-block;
  border: 1px solid #000;   
  width: 200px;
  height: 150px;
  vertical-align: top;
}

If you don't need a specific height for the container, feel free to remove height: 150px;.

Answer №2


//Please insert the code for displaying the external menu

externalMenu{
 width:100%;
 display: block;

}

//Modify the float property in exSection and set it to left. //Add some margin between each section to make them wider.

.exSection{ 

background-color:yellow;
  list-style-type:none;
  float: left;
  margin-left: 10px;
  border: 1px solid #000;   
  width: 200px;
  height: 150px;

}***

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

What could be causing my Next.js layout to re-render?

I currently have a basic root layout set up in Nextjs (app router) that displays a navigation bar and the child components underneath it. ROOT LAYOUT import "./globals.css"; import type { Metadata } from "next"; import { Inter } from & ...

Issue with Bootstrap Dropdown Select CSS not functioning properly when using !important

I'm having trouble customizing some text in a dropdown menu. Even when I use the !important rule to override the CSS, nothing changes. I'm not sure what I'm doing wrong. Here's the issue: Before making a selection, everything looks fin ...

completing data tables and presenting them to clients

I am currently working on a website for a Nutrition specialist who requires a monthly diet regimen table with five meals daily. The doctor will be responsible for completing these tables, which will then be presented to clients. It's important that th ...

Tips for embedding Javascript code within the window.write() function

I have a JavaScript function that opens a new window to display an image specified in the data variable. I want the new window to close when the user clicks anywhere on it. I've tried inserting some JavaScript code within window.write() but it doesn&a ...

Difficulty with slideToggle and other jQuery animations arises when selecting specific elements

When elements are selected from the page using the following code: $('.offers')[count - 1].slideToggle(500); The slideToggle function stops working, along with any other animations. However, this code snippet does work: $('.offers')[ ...

Can you please adjust the image to the right side?

I'm having trouble aligning the image to the left in my code. It always seems to leave a margin on the sides that I can't figure out how to get rid of. https://i.sstatic.net/kKwII.jpg This is the snippet of my code: <section class="conta ...

Tips for adjusting the width of the box-shadow in CSS

I am facing a challenge with my button that has a box-shadow effect on hover. Currently, I am unsure how to adjust the width of the box-shadow effect. Here is a snapshot of my button's current appearance: https://i.stack.imgur.com/Mg0df.png</p&g ...

"Encountering an Ajax error 419 when using radio buttons and a form

I'm attempting to use AJAX to send radio button values to a Laravel route when a button is clicked, but I keep receiving error code 419 even though I am sending the CSRF token. $(document).ready(function(){ $("#valor").on('click' ...

Place three images in the center of a div container

Recently, I've been working on some HTML code that utilizes the Angular Material library: <div layout="row" layout-wrap style="background: yellow; "> <div ng-repeat="pro in Products" > <md-card class="cardProduct" > ...

Directing BeautifulSoup to a specific <tr> class

I'm currently working on a project where I need BeautifulSoup to extract the numbers located in the "Units Sold" column: from bs4 import BeautifulSoup from urllib import urlopen html = urlopen('http://www.the-numbers.com/home-market/dvd-sales/2 ...

The floating label appears distorted when used with a datalist input in Bootstrap 5

How can I get the floating label to display correctly for datalists in Bootstrap 5? Currently, it looks like this... It's working fine without the form-floating class, but I want to use the floating form feature. <div class="form-floating" ...

Issue with Dependent Drop Down functionality

I am a beginner explorer diving into the world of PHP & MySQL, and I am currently working on creating a dependent drop-down list. The first drop-down seems to be working fine, but I am facing issues linking the second drop-down with the first one. Can anyo ...

Encountering an error stating 'Chart name not found' while attempting to utilize chart.js

For days now, I've been struggling with an annoying issue in chart js. I've tried every possible solution but have not been able to figure it out. The problem arises from using a TypeScript environment with ES modules. I attempted to import Char ...

Utilizing Binding Time in Angular for Enhanced Views

I am completely new to Angular JS. I have a simple question that I have been searching for answers on SO. My goal is to display the current time in real-time (refreshing every second as the clock ticks) on my view. Here's what I have tried so far: H ...

Trigger an event in jQuery when the focus moves away from a set of controls

Within a div, I have three textboxes and am looking for a way to trigger an event when focus leaves one of these inputs without transitioning to another one of the three. If the user is editing any of the three controls, the event should not be triggered. ...

Can a hyperlink exist without a specified href attribute?

One scenario could be when I receive this specific code from a third-party source <a class="cs_import">Add contacts from your Address Book</a> Curiously, the link "Add contacts from your Address Book" does not redirect to any page as expected ...

Prevent form from being resubmitted upon browser refresh

Currently, I have a form where users input data and it searches the database to display results. The issue arises when the user refreshes the page as it triggers an alert about resubmission. Although the data remains the same upon resubmitting, is there a ...

Can divs be arranged in a similar fashion as images?

My goal is to create a navigation bar (#nav) with each nav item being a div (#nav div). However, my current approach isn't working as expected. Here is the code I'm using: #nav { background: url("navbg.png"); /* navbg.png is 1x40 pixels */ ...

Is there a potential security threat in using the same names for your form fields as those in mysql?

Are there any considerations to take into account when deciding whether or not to name your form fields the same as the HTML fields? <input type="text" name="my_field_1" id="my_field_1" /> --> mysql row my_field_1 or <input type="text" name= ...

Guide to adding a Scrollable Navbar

Is the term "scroll bar" really accurate? I want to create a scrollable feature where users can easily select a city using their mouse wheel (or swipe on a smartphone) and then choose from possible cities within that country in a second window. Something s ...