Choosing an item prior to a break in the text line

I designed a navigation bar with the following CSS structure

    nav {
        text-align: center;
    }
    nav > ul {
        list-style: none;
        padding: 0;
        margin: 8px auto;
    }
    nav > ul > li {
        display: inline-block;
        padding: 10px;
        width: 200px;
        text-align: center;
        box-sizing: border-box;
        border-right: 1px solid;
    }
    nav > ul > li:last-child {
        border-right: 0px none;
    }
    nav a {
        font-size: 130%;
    }
<nav>
    <ul>
        <li><a href="url">link one</a>
        <li><a href="secondurl">link two</a>
        ...
    </ul>
</nav>

The li:last-child part helps enhance its appearance. However, I encounter an issue when the navigation bar collapses (like on smaller screens or browsers). I want to remove those borders before they break into a new line. Is there a way in plain CSS to achieve this? If not, what would be a JavaScript solution for this scenario?

I explored the MDN Selector list, but it seems like no pseudo-class selector specifically caters to this requirement. I thought about using media queries for different screen sizes to dynamically remove the border, but that approach seems more like a workaround. Am I overlooking a more HTML-centric solution to address this concern?

Answer №1

When referencing "when the navigation bar collapses," it is assumed that you are referring to when it transitions into a single column layout.

In this scenario, you can utilize media queries to adjust the CSS styles when the screen size reaches a specific breakpoint. For example, you can exclude borders by default and then introduce them once the screen width hits 400px (or another suitable size based on your site's padding and margins within containers) with the following code:

@media only screen and (min-width:400px){    
     nav > ul > li {
        border-right: 1px solid;
    }
    nav > ul > li:last-child {
        border-right: 0px none;
    }
}

Functional Demo (For optimal viewing, expand to full screen and resize your browser window):

nav {
        text-align: center;
    }
    nav > ul {
        list-style: none;
        padding: 0;
        margin: 8px auto;
    }
    nav > ul > li {
        display: inline-block;
        padding: 10px;
        width: 200px;
        text-align: center;
        box-sizing: border-box;
    }
    nav a {
        font-size: 130%;
    }
@media only screen and (min-width:400px){    
     nav > ul > li {
        border-right: 1px solid;
    }
    nav > ul > li:last-child {
        border-right: 0px none;
    }
}
<nav>
    <ul>
        <li><a href="url">link one</a>
        <li><a href="secondurl">link two</a>
        <li><a href="url">link one</a>
        <li><a href="secondurl">link two</a>
    </ul>
</nav>

UPDATE: After reviewing your edits and feedback, achieving this without using media queries in HTML and CSS alone may not be feasible. I have included a link to a relevant question above which discusses potential JavaScript solutions if you prefer an alternative approach.

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

Issue with URL routing causing page to display incorrectly

When using the normal Href, the page is rendered properly, but the URL is not hidden: <asp:Repeater ID="rCompany" runat="server" DataSourceID="sdsCompany" > <ItemTemplate> <div class="col4" id="trip_third"> <a href="discover_detail.as ...

What could be causing html-webpack-inline-source-plugin to prevent the page from refreshing after editing CSS files?

Currently, I am utilizing a plugin that embeds all CSS within the final HTML file. This method prevents the application from refreshing in development mode. Whenever I make edits to the CSS, the build process completes normally, but the styles do not updat ...

Animation controlled by a button

I'm working on a project that involves creating a cartoon version of a record player. I want to incorporate an animation where the play button on the side toggles the spinning motion of the record using webkit, while also starting the audio track. The ...

How to prevent the back button from functioning in Android devices

It's so frustrating always hitting a dead end and I really need a clear answer. How can I disable the hardware back button on my app? And where should I input the code to disable it? I simply want users of my app to navigate within the app without usi ...

Ensure that the Div element is able to wrap lines in the same way that the Span element

Is there a method to ensure that a div element wraps the lines similar to how a span does? JSFiddle for testing In this example, I utilized the span tag to create an appearance as if the text was highlighted by a yellow highlighter pen. Everything functi ...

Troubleshooting a Messaging Error between Background and Another Script

Attempting to transfer selected text from the current page to an HTML page using message passing: content script to background script, then background script to the HTML page. However, encountering errors if the HTML page is not already open, and even gett ...

displaying information in table cells with jquery

Is there a way to display a table inside a td element with an on-click function without showing all tables in the same column when the function is triggered? I want the table to be shown only for the specific td that was clicked. $(document).ready(funct ...

Grammarly is seamlessly inserting padding into my body element

After setting up my website on GitHub, I ran into an issue where the Grammarly Chrome extension was automatically adding a padding-top attribute to the body tag. <body data-new-gr-c-s-check-loaded="14.1028.0" data-gr-ext-installed="" ...

I am experiencing issues with my JavaScript not functioning properly in conjunction with my HTML and CSS. I am uncertain about the root cause of the problem (The console is displaying an error message:

I am facing challenges in creating a content slider and encountering issues with its functionality. Specifically, when testing locally, I noticed that the current-slide fades out and back in upon clicking the arrows left or right, but the slide content is ...

Make the width of a table column match the width of the header

I am currently using Primeng2 datatable and I am looking to adjust the column width to match the header size. This is my HTML code: <p-dataTable #dtselfcollectmonthly [exportFilename]="exportname" [rows]="10" [value]="rawdatatrucks"> <ng-con ...

Interacting with CSS buttons using Selenium

Currently, I am working on a test case in Java where I am using Selenium to record a series of events and then play them back on an application. Here is the code snippet I have: // *The app opens a new window* // Get handle of the main window Stri ...

I want to initiate a Python script by clicking a button on my HTML page with the help of Ajax

Here is my JavaScript code: function executePython() { alert("executed"); $.ajax({ url: "http://localhost:5000/app.py", type: "POST", ...

Java code to extract and delete a section of a website on a webpage

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.fwebview, container, false); webView = (WebView) view.findViewById(R.id.webView); String url = ge ...

how to enable drag and drop functionality based on names in angularjs

In my project using angular js, I have implemented a drag and drop feature that allows items to be dragged between cells in two directions. I now want to enhance this functionality by allowing members with 'a's (e.g. 1a, 2a, etc.) to be dragged ...

clicking on the anchor does not trigger the modal to open

Below is an anchor tag I have set up: <a href="#" data-toggle="modal" data-target="#modalTC">terms and conditions</a> Here is the structure of my modal: <!-- Modal --> <div class="modal fade" id="modalTC" role="dialog"> <di ...

Incorporate a 'Select All' functionality into ion-select by adding a dedicated button

Looking for a way to set custom buttons on ion-select through interfaceOptions in ionic 4? HTML <ion-item> <ion-label>Lines</ion-label> <ion-select multiple="true" [(ngModel)]="SelectedLines" [interfaceOptions]="customAlertOption ...

Having trouble with tabs in jQuery?

I'm having trouble setting up tabs in a reservation form with 3 tabs that include text boxes for user input. I can't seem to get it working properly and I'm not sure where I've gone wrong. Could it be due to the placement of the content ...

The navbar background spans the full width of the screen, with all content contained within minimum and maximum width parameters

Currently, I am working on a fluid website design where I have set specific min-width and max-width values. However, I want the navigation bar background to extend across the entire screen width without being limited by the max width parameter, while still ...

Checkbox malfunctioning when trying to add values after being checked

I have successfully completed a calculation system project using JavaScript. Everything works well, the calculations are accurate and taxes are included properly. However, I am facing an issue where the tax is not being included when I click on the checkbo ...

Utilizing the Replace function just once

I am currently using the Avada Theme on Wordpress and I am attempting to use jQuery to translate the social media privacy labels/content. Everything is working smoothly except for one issue. Below is the HTML code: function translate() { jQuery(".fus ...