Choose solely the initial and concluding hyperlink within the divs

The structure I am working with is as follows:

<div class="navigation_sub_item_background" id="sub_nav_2">
    <div class="navigation_sub_item" id="2_1">
        <a  class="navigation__sub__link" href="?p=2_1">
            <span>
                Test 3
            </span>
        </a>
    </div>
    <div class="navigation_sub_item" id="2_2">
        <a  class="navigation__sub__link" href="?p=2_2">
            <span>
                Test 4
            </span>
        </a>
    </div>
    <div class="navigation_sub_item" id="2_3">
        <a  class="navigation__sub__link" href="?p=2_3">
            <span>
                Test 5
            </span>
        </a>
    </div>
    <div class="navigation_sub_item" id="2_4">
        <a  class="navigation__sub__link" href="?p=2_4">
            <span>
                Test 6
            </span>
        </a>
    </div>
</div>

I am trying to select only the first link and the last link. I attempted using

div.navigation_sub_item > a.navigation__sub__link:first-of-type
, but it ended up selecting all of the links.

Does anyone have a suggestion?

Answer №1

How to Select the First and Last Links

If your links are enclosed within div elements, you will need to target the first and last div and then select the first or last a element inside them:

.navigation_sub_item:first-child a:first-of-type {
    /* Styles for the very first link. */
}

.navigation_sub_item:last-child a:last-of-type {
    /* Styles for the very last link. */
}

It's important to use first-of-type and last-of-type on the a elements to handle scenarios where more than one a exists in the div. If you only want to target the actual first and last child without considering other elements, replace with first-child and last-child.


Dealing with Single Element Scenarios

If there is only one .navigation__sub__link-element, how can I apply specific styling instead of the :last-child rule being triggered?

To address this situation, override the selectors by combining both first-child and last-child:

.navigation_sub_item:first-child:last-child a {
    /* Override previous selectors here. */
}

If you haven't used :first-of-type or :last-of-type on your a elements, you can also utilize the :only-child selector:

.navigation_sub_item:only-child a {
    /* Another way to override the initial selectors. */
}

Answer №2

Everything seems to be in order.

.main_navigation_sub_item:first-child .sub__link_nav {}
.main_navigation_sub_item:last-child .sub__link_nav {}

Answer №3

Take a look at this demo example showcasing the use of first-child and last-child selectors

.navigation_sub_item:first-child .navigation__sub__link,
 .navigation_sub_item:last-child .navigation__sub__link{
  // add your styles here 
 }
.navigation_sub_item:first-child .navigation__sub__link{
   color: red;
}

.navigation_sub_item:last-child .navigation__sub__link{
    color: green;
}
<div class="navigation_sub_item_background" id="sub_nav_2">
    <div class="navigation_sub_item" id="2_1">
        <a  class="navigation__sub__link" href="?p=2_1">
            <span>
                Test 3
            </span>
        </a>
    </div>
    <div class="navigation_sub_item" id="2_2">
        <a  class="navigation__sub__link" href="?p=2_2">
            <span>
                Test 4
            </span>
        </a>
    </div>
    <div class="navigation_sub_item" id="2_3">
        <a  class="navigation__sub__link" href="?p=2_3">
            <span>
                Test 5
            </span>
        </a>
    </div>
    <div class="navigation_sub_item" id="2_4">
        <a  class="navigation__sub__link" href="?p=2_4">
            <span>
                Test 6
            </span>
        </a>
    </div>
</div>

Answer №4

To target the first and last child elements, you can use this CSS code:

.navigation_sub_item:first-child .navigation__sub__link,
.navigation_sub_item:last-child .navigation__sub__link {
 /*Add your CSS styles here*/
} 

For more information on CSS3 first-child and last-child selectors, you can click on the provided links.

Answer №5

To achieve this effect, make sure to use the CSS pseudo-classes :first-child and :last-child

.navigation_sub_item:first-child a,
.navigation_sub_item:last-child a {
    color: red;
}

Check out the DEMO

Answer №6

Give this a try:

.navigation_sub_item:first-child .navigation__sub__link

and

.navigation_sub_item:last-child .navigation__sub__link

To address your query, consider the following:

.navigation_sub_item:first-child .navigation__sub__link{
  font-weight: bold
}
.navigation_sub_item:last-child .navigation__sub__link{
  color: blue
}
<div class="navigation_sub_item_background" id="sub_nav_2">
    <div class="navigation_sub_item" id="2_4">
        <a  class="navigation__sub__link" href="?p=2_4">Test 6</a>
    </div>
</div>

Answer №7

I personally find this method to be the most effective for me

.div-selecter a:last-of-type { color:red;}

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 is the proper location to place the CSS I wish to implement for a module?

As I embark on creating my inaugural DotNetNuke website, I find myself faced with the task of adding an HTML module to a page. To style the text within it, I have been advised to utilize CSS classes. I'm curious about where .css files should be place ...

Responsive CSS Dropdown Menu - Divided into Two Columns

After searching for solutions in the support resources and experimenting with various techniques, I am struggling to divide this CSS Dropdown menu into two columns. The nav list is long enough that it extends below the fold. Although the menu adapts respo ...

Deciding on the proper character formatting for each individual character within the RICHT TEXT EDITOR

After browsing numerous topics on Stackoverflow, I was able to develop my own compact rich text editor. However, one issue I encountered is that when the mouse cursor hovers over already bold or styled text, it's difficult for me to identify the styl ...

Adjust the x and y coordinates of the canvas renderer

Manipulating the dimensions of the canvas renderer in Three.js is straightforward: renderer = new THREE.CanvasRenderer(); renderer.setSize( 500, 300 ); However, adjusting the position of the object along the x and y axes seems more challenging. I've ...

Automotive Dealership API by Edmunds

I am having some trouble with the Edmunds API data. I can't seem to get the JSON to display when using the example code provided. It just shows up as a blank page. I am quite new to working with API's and would really appreciate some guidance on ...

Positioning the filters in jQuery Datatables

I'm currently working with jQuery datatables and I'm attempting to align the filter/search box on the same row as the header of the container holding the datatable. Attached is a screenshot for reference: https://i.stack.imgur.com/nzbIl.png He ...

Eliminate the div element using jQuery if it contains a line break tag but no text

I am faced with a situation on a page where some div elements contain content while others only have a BR tag. I am attempting to remove the div elements that contain only the BR tag. Below is the HTML format in question: Here is an example of one type: ...

Creating a split hero section view using a combination of absolute/relative CSS techniques, Tailwind, and React

I'm in the process of creating a website using Nextjs, React, and TailwindCSS, and I aim to design a Hero section that resembles the one on the following website. https://i.sstatic.net/tq3zW.png My goal is to: Have a text title and buttons on the l ...

Tips on properly styling HTML using the BEM naming convention

I'm trying to implement BEM naming in my project, but I'm having trouble with how to name elements within elements. BEM guidelines state that elements of elements should not exist. How should I approach naming them correctly? <div class="cont ...

Upload several files sequentially in a form on a website using Selenium with Python

I need to automate the process of inputting multiple files from a folder into a website and running an online job for each file. Currently, I can only input one file at a time using this code snippet. from selenium import webdriver url = "http://www.exam ...

Guide on integrating React.js into an HTML development webpage

I can't seem to get this code to work properly. I have saved it as an .html file on my computer, but it's not displaying anything when I try to open it in Google Chrome. <DOCTYPE! html> <html> <head> <script src= ...

Encountered an error when attempting to use 'appendChild' on 'Node': the first parameter is not the correct type. It was able to work with some elements, but not with others

I'm currently working on a script that utilizes fetch to retrieve an external HTML file. The goal is to perform some operations to create two HTMLCollections and then iterate over them to display a div containing one element from each collection. Here ...

Executing Array.prototype.filter() results in an empty array being returned

I have a list of jQuery elements that I collected using the .siblings() method, and now I am trying to filter them. Here is the HTML code: <div> <div> <label for="username">Username</label> <input class="form ...

In which part of the DOM tree are Event/Callbacks typically located?

As I work on creating a one-page web application, the task of dynamically constructing nested Backbone.View components arises. My typical approach involves building an empty jQuery object, populating it with div elements, binding events to it, and then re ...

Automatically Modify Uploaded Files on the Server

There is one thing that has caught my attention. I am interested in working with scripts that can automatically edit a file once it's been uploaded. For example, imagine having a form where someone uploads an HTML file. The script would then edit spe ...

"Enhance User Experience with a Bootstrap Dropdown Button Offering Block Level Function

I'm attempting to modify a bootstrap 3 dropdown button to fully utilize the width properties of .btn-block class. It appears that the dropdown button does not respond in the same way as regular buttons. Below is my current code: <div class="row"&g ...

My attempts to troubleshoot the JavaScript function have all been unsuccessful

I am completely new to JavaScript and feeling a bit embarrassed that I'm struggling with this. My goal is to build a website that takes first and last names as input and generates email addresses based on that information. Despite my attempts, moving ...

The transparency of my navbar renders it elegant, yet I desire to imbue the toggle background with a vibrant hue

I'm facing an issue with the transparent Navbar I built in Bootstrap 5. On mobile toggle view, the navigation links are getting lost within the text of the hero image. To resolve this, I want to keep the main navigation bar transparent and change the ...

JavaScript can dynamically attach EventListeners to elements, allowing for versatile and customized event

I am currently populating a table using data from an XML file. One of the columns in the table contains links to more details. Due to the unique requirements of my web page setup (Chrome extension), I need to dynamically add an event handler when the table ...

CSS styling for a background image in the header

Why isn't my background image showing up even though I've used a direct link to the image? What could be causing this issue? .header { background-image: url("https://www.africa.com/wp-content/uploads/2015/07/Kenya.jpg"); background-attac ...