How can I format the initial letter in the primary link within the initial list using CSS?

It's no surprise that the CSS isn't cooperating, but I'm hoping you can grasp the concept. There are two lists and my aim is to style the first letter of the first a in the initial ul. In this instance, it would be the B in Beauty Salons. Is it possible to achieve this using CSS without altering the HTML code?

CSS:

.tab-pane .category-headings ul:first-of-type a:first-of-type::first-letter {
    margin-right: 1px;
    padding: 0px 5px;
    background-color: #666;
    color: #fff;
    font-weight: bold;
}

HTML:

<div class="tab-pane" id="b">
    <div class="container-fluid category-headings">
        <div class="row-fluid">
            <div class="span11 offset1">
                <div class="row-fluid">
                    <div class="span4">
                        <ul class="unstyled">
                            <li><a href="./beauty-salons-and-therapy/">Beauty Salons &amp; Therapy</a>
                            </li>
                            <li><a href="./blinds/">Blinds</a>
                            </li>
                        </ul>
                    </div>
                    <div class="span4">
                        <ul class="unstyled">
                            <li><a href="./book-binders/">Book Binders</a>
                            </li>
                            <li><a href="./bookkeeping-services/">Bookkeeping Services</a>
                            </li>
                        </ul>
                    </div>
                    <div class="span4">
                        <ul class="unstyled">
                            <li><a href="./builders/">Builders</a>
                            </li>
                            <li><a href="./building-plans/">Building Plans</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

FIDDLE: http://jsfiddle.net/a4644b8h/2/

Answer №1

To make it work, you need to change the <a> tag to display as a block element:

.tab-pane .category-headings ul:first-of-type li:first-of-type a:first-of-type::first-letter {
    margin-right: 1px;
    padding: 0px 5px;
    background-color: #666;
    color: #fff;
    font-weight: bold;
}

.tab-pane .category-headings ul:first-of-type li:first-of-type a:first-of-type {
    display: inline-block;
}

The reason for this is that the :first-letter selector will only affect block elements, not inline ones.

Check out this example on JSFiddle.

Answer №2

To start, it's important to make a few adjustments to the selectors being used. Instead of targeting ul:first-of-type, focus on selecting the initial div with the class="span4". Here's an example:

.span4:first-of-type

Similarly, avoid trying to select a:first-of-type, as this will choose the first a tag within each li element. Target the first li instead:

li:first-of-type

Following that, target the a tag within that initial li.

Putting everything together:

.tab-pane .category-headings .span4:first-of-type li:first-of-type a::first-letter {

}

Additionally, as mentioned by Alan, the parent of the ::first-letter pseudo-element should be a block-level element, so include:

.span4 a { /* ensure specificity for this selector */
    display: inline-block;
}

These adjustments should suffice. Refer to the JSFiddle link for an illustration.

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

Styling the navigation bar using CSS and positioning a <div> element underneath

Struggling to design a navbar using Bootstrap and have a div positioned underneath it that fills the remainder of the page? You're not alone! As someone new to Bootstrap and CSS, this can be tricky. Here's an example of how I created a navbar: ...

What are the steps to change table characteristics?

I currently have this segment of HTML code: <table cellspacing="0" cellpadding="0" width="100%"> After validating my HTML document, I discovered that these attributes are outdated. However, removing them would affect the alignment of my website. I ...

The troubleshooting issue with jQuery animate Scrolltop malfunctioning

My goal is to use jQuery to scroll the page to a specific div when a button is clicked. Despite not seeing any errors in the JavaScript console, the page does not actually scroll to the desired location. I have tried placing the jQuery js file before and a ...

Steps for breaking down a given string into divisions that correspond to an A4 sheet

Within my content, there are various styles applied. For example: This is a <b>bolded</b> word. I am seeking a solution to divide this long string into smaller sections that would fit on an A4 page accurately. The goal is to maintain the integ ...

Tell me about the CSS ruby-align characteristic

While browsing online, I stumbled upon a reference to the ruby-align property that piqued my interest. After searching on w3schools for some examples, I was surprised to find no mention of it anywhere. I remembered that Ruby is a newer tag in HTML5 and de ...

Tips for populating a row in the Bootstrap grid with the assistance of the mr or ml classes

I've come across a roadblock here. The issue arises when attempting to fill the Bootstrap row while also utilizing mr (or ml). Initially, I attempted to calculate the numbers so that they add up to 12. In my 3-column layout, I have a column of 5, fol ...

What causes a block element to not stretch to match the width of its parent when it is in a fixed position?

I am currently developing a fixed horizontal navigation bar that spans the entire width of the screen. However, when I attempt to make its position fixed, the unordered list element only occupies the width required to contain its content. Considering that ...

I'm having some trouble getting my HTML menu within the header tag to hover correctly in Angular. Can someone please provide

https://i.sstatic.net/SK6Dw.png My Angular application's CSS is causing the menu to not hover over all controls in the component. CSS nav { background-color: #4D678D; } nav ul { padding: 0; mar ...

I'm working in JavaFX and I have several line charts that I need to customize individually using CSS styling

Is there a way in JavaFX to style different charts individually with CSS without having the styles override all of them? I've noticed that when I change the CSS for one chart, it affects the others as well. How can I write CSS code that only applies t ...

Flickering problems occur when using Jquery to load MVC Views upon hovering

I am trying to create a hover effect that expands and changes the content of each div when hovered over, and reverts back to its original state on mouseout. To achieve this, I am using MVC partial views: (Subpages.cshtml) <div id="subpages" c ...

Use jQuery to retrieve HTML content excluding comments

Take a look at the code snippet below. HTML: <div> <p>sdfsdfsfsf</p> <!--<p>testing</p>--> </div> JQUERY $(document).ready(function(){ alert($("div").html()); }); OUTPUT <p>sdfsdfsfsf</p> & ...

Use Flexbox hover effect to display submenu text exclusively in the column being hovered

Having an issue with a 5 column flexbox. Each box should display a header text and supplementary text only when hovered over, returning to normal when unhovered. I was able to accomplish this in Codepen (view the rough model here). However, when trying to ...

The CSS property col visibility:collapse is ineffective on the Chrome browser

I am attempting to conceal some columns in my HTML code by using MDN colgroup and col elements, and manipulating the styles of the columns. The <td> containing the text 'visible' is visible in all browsers (which is good), but the one with ...

What's the best way to include CSS and Javascript in an HTML document?

I'm still getting the hang of CSS and JavaScript. I stumbled upon a cool countdown timer that I'd like to incorporate into my website, but I'm not quite sure how to place it where I envision it. Check out the timer here CSS: html,body{mar ...

Is there a way to adjust the font size of a select option?

Looking to customize the appearance of a select option dropdown list. Wondering if it's possible to change the font sizes of individual options rather than keeping them all the same? For instance, having the default: -- Select Country -- displayed a ...

Ensure the td element is positioned at the top alongside newly added elements

I recently created a table that looks like a calendar in this Plunker. However, I encountered an issue where the days within the table do not always start from the top left corner despite adding the following CSS: td { vertical-align: top; } I'm ...

Leveraging Selenium for assessing the visibility of elements in Print media applications

Is there a way to use Selenium to determine if specific elements on a page will be visible when printed based on CSS @media rules? While the isDisplayed method considers CSS, is there a way to specify a media type in Selenium? Alternatively, are there ot ...

Using div tags may cause rendering issues

I am trying to use multiple div tags with webkit borders, but for some reason only the one called 'Wrapper' is displaying properly. Here is my code: .wrapper { margin: 20px auto 20px auto; width: 800px; background: url('images/background_1. ...

Changing the background color of a PHP input based on the webpage being viewed - here's how!

I'm in the process of creating a website where each page will have its own unique background color. Additionally, I am using a PHP input for both the header and footer sections, which need to change their background colors based on the specific webpa ...

Scrollbar styling functions flawlessly on Chrome and Safari, but encounters issues on Mozilla Firefox

Currently, the scrollbar is functioning properly in Chrome and Safari, but in Mozilla, it is not behaving as expected. Below, you can find my code. Although using jquery or JavaScript would be a quick solution, I am exploring CSS options first. ::-webki ...