Struggling to properly line up the baselines of navigation list items that are styled as circular elements using CSS

I have transformed my navigation menu into a series of CSS circles with text inside. The issue I am facing is that the text spills out unevenly based on the amount of content in each circle. To address this, I used a JavaScript solution to center-align the text horizontally within the circles. However, I now encounter a new problem where the baselines of the circles are not consistent due to varying text lengths. Is there a straightforward CSS fix for this, or will I need to calculate and adjust the heights of each list item using JavaScript?

.html

<ul class="list-inline nav-horizontal">
    <li role="presentation" class="active">
        <a href="#1" data-toggle="tab" class="stylish">#1</a>
    </li>
    <li role="presentation">
        <a href="#2" data-toggle="tab" class="stylish">#2</a>
    </li>
    <li role="presentation">
        <a href="#3" data-toggle="tab" class="stylish">#3</a>
    </li>
    <li role="presentation">
        <a href="#4" data-toggle="tab" class="stylish">#4</a>
    </li>
    <li role="presentation">
        <a href="#5" data-toggle="tab" class="stylish">#5</a>
    </li>
</ul>

.css

.list-inline {
    display: inline-block;
    padding: 6px; 
}

.stylish {
    display: block;
    width: 140px;
    height: 140px;
    border-radius: 50%;
    border: 8px solid #ED1B24;
    font-size: 20px;
    color: #BBAE92;
    text-align: center;
    text-decoration: none;
    background: none;
    line-height: 1.1em;
}

.stylish:hover, li.active .stylish {
    color: #fff;
    text-decoration: none;
    background: #ED1B24;
}

.js

$("a.stylish").contents().wrap("<span>");
$("a.stylish span").css({
    position : "relative",
    "top" : function() {
        return ($(this).parent().height() - $(this).height()) / 2
    }
});

Answer №1

Check out this code snippet! http://jsfiddle.net/hd8donbn/3/

.nav-horizontal li:before {
    content:'';
    display: inline-block;
    vertical-align: middle;
    height: 100%
}

.stylish {
    display: inline-block;
    text-decoration: none;
    color: #ED1B24;
    vertical-align: middle;
    max-width: 90%
}

If you're interested, I can provide a detailed explanation for each part of the code :D

Answer №2

After some experimentation, I discovered a straightforward CSS fix. By incorporating vertical-align:text-top; into the styling of the .list-inline li class, I was able to successfully resolve the problem.

Answer №3

Hey there, I took the liberty of crafting my own markup specifically tailored to your requirements instead of following what was outlined in your initial question. Check it out below:

XHTML

<ul>
    <li class="active"><a href="#">small</a></li>
    <li><a href="#">medium sized content</a></li>
    <li><a href="#">slightly larger content</a></li>
    <li><a href="#">quite a big chunk of content</a></li>
    <li><a href="#">extremely large block with a lengthy word as well</a></li>
</ul>


CSS

*{
        margin: 0;
        padding: 0;
    }
    ul {
        display: inline-block;
        list-style: none; /*remove bullet points for `li`*/
    }

    li {
        border-radius: 50%; /*create rounded edges;*/
        overflow: hidden; /*hide any text exceeding boundaries*/
        float: left; /*align items horizontally at their top positions*/

        border: 8px solid #ED1B24;
        display: block;
        height: 140px;
        text-align: center;
        width: 140px;
    }
    li a{
        padding: 0 5px; /*prevent border interference*/
        word-wrap: break-word; /*wrap long words if necessary*/
        text-decoration: none;
        color: #BBAE92;

        /*the next 4 properties vertically center a child element*/
        display: block;
        position: relative;
        top: 50%;
        transform: translateY(-50%);

    }
    li.active,
    li:hover{
        cursor: pointer; /*give a link-like appearance on hover*/
        background: #ED1B24;
    }
    li.active a,
    li:hover a{
        color: #fff; 
    }


Finally, here's some basic JavaScript

$('li').click(function(){
    $(this).child('a').click();
});

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

Tips for injecting animation into a DIV

I am trying to make a <div> element on my webpage expand to the full width and height of the screen. While I have managed to achieve this, I also want to implement an animation that will be displayed when the <div> enlarges to fit the screen si ...

What is the best way to utilize the outcome of the initial API request when making a second API call in AngularJS

Looking to utilize the response of a first API call in a second API call. The situation is such that I need to fetch data from the first API and use it in the second API call. I believe a synchronous API call would be necessary. While attempting to imple ...

Move the divs within the overflow container by sliding them, then take out the initial element and append it to the end

Currently, when I utilize .appendTo(".wrapper") as shown in the code below, it eliminates the animation effect. My goal is to have the div on the far left slide out of view, triggering an overflow hidden effect, and then be placed at the end of the slide c ...

Updating the total of textboxes using jQuery

I'm working on a jQuery function that totals the values of 7 textboxes and displays the result in a grand total textbox: $(document).ready(function() { $('.class2').keyup(function() { var sum = 0; $('.class2').each(funct ...

Leveraging AJAX with React Router

Currently tackling a project utilizing React Router, encountering some challenges with data flow. Each page triggers an AJAX call to fetch data for the component. These calls have been placed within componentDidMount: // The following code is in ES6 comp ...

I am having trouble getting the Express Router delete method to function properly when using mongoose with ES8 syntax

I was working on this code snippet : router.delete('/:id', (req, res) => { Input.findById(req.params.id) .then(Input => Input.remove().then(() => res.json({ success: true }))) .catch(err => res.status(404).json({ success: f ...

Creating an application for inputting data by utilizing angular material and javascript

I am looking to create an application using Angular Material Design, AngularJS (in HTML), and JavaScript. The application should take input such as name, place, phone number, and email, and once submitted, it should be stored in a table below. You can fin ...

Creating a responsive single webpage using HTML/CSS

How can I make some div or img responsive for mobile format? <meta name="viewport" content="width=device-width, initial-scale=1" /> <div class="container"> <h1 class="title"> ...

differences between using form's get method and sending an angular $http.get request

When trying to make a GET request to a specific URL from my Angular frontend to an ExpressJS backend, I encountered some interesting behavior. In the frontend code snippet below: <li> <a ng-click="givequiz()">GiveQuiz</a> </l ...

The child process is arriving empty, which is causing a requirement

Every time I try to use var childprocess = require('child_process'); I keep getting a blank result. When I attempt var childProcess1 = require('child_process').spawn; it returns as undefined, and, var childProcess2 = require(&a ...

Creating a function to limit the display value of Material UI Autocomplete similar to Material UI Multiple Select's renderValue

Incorporating Material UI Features Utilizing the Material UI Multiple Select, you have the ability to truncate the displayed value after selection instead of wrapping onto another line by setting the renderValue to .join for the selected options, enabling ...

"Is there a way to target an element within another element that has inline styles using

What is the best CSS strategy for targeting elements with inline styles? How can I focus on the first span but exclude the second using only CSS? If that's not achievable, is it possible to accomplish this using jQuery? http://jsfiddle.net/TYCNE/ & ...

Mobile Website Blog Page With Dual Sidebars Issue

I have created a blog page using bootstrap 4 that features both left and right sidebars along with the main content section. Everything looks great on Desktop view. However, when viewed on a mobile phone, the order changes to: left-sidebar>content> ...

The iframe failed to load on the external server

I'm working on a website where the admin can upload PDF files that will be displayed on an HTML page. I'm using an iframe to achieve this, and it works perfectly fine on my local server. However, when I tried uploading it to the remote server, it ...

Create a stylish div slider with interactive menu using Jquery

Currently, I am in need of creating a slider for a <div>. However, most plugins are too large for my needs, so I am attempting to develop my own jQuery script since I only require the basic functionality. Admittedly, I am a novice in jQuery and could ...

Exploring the Differences Between NPM Jquery on the Client Side and Server

I'm still getting the hang of node and npm, so this question is more theoretical in nature. Recently, I decided to incorporate jQuery into my website by running npm install jquery, which placed a node_modules directory in my webpage's root along ...

Illuminate the current page

I have been working on a website with around 20 pages that all have a side navigation bar. To make it easier to manage, I decided to centralize the links in a JavaScript (JS) file and include it on each HTML page. This way, any changes can be made quickly ...

Excluding specific e2e tests in Protractor: A guide

I have a collection of end-to-end tests for my AngularJS web application. Here is the configuration in my current protractor.config.js file: // __dirname fetches the path of this specific config file // assuming that the protractor.conf.js is located at t ...

Real estate for a targeted audience

1. Overview I have a list of selectors that should always apply certain properties. Some selectors require additional properties to be added. I'm struggling to find a way to achieve this without repeating code. 2. Minimal CSS Example (MCVE) 2.1 ...

Check if the element is not present in the array, then add it to the array

I am attempting to generate an array in JavaScript with unique elements extracted from a JSON feed. My thought process is possibly too influenced by PHP, where a simple script like the one below would suffice: $uniqueArray = array(); foreach($array as $ke ...