Items in a list should have a top border that spans the full width if they wrap onto a second line

Currently, I am facing a challenge with a menu containing list items. The designer's requirement is to insert a line above the items if they break into two lines on smaller screens. This line should extend across 100% of the ul width, even if the second row of list items is shorter. The expected outcome can be seen in this image - only with the addition of a grey line above the list items.

https://i.sstatic.net/1SBsV.png

I have been experimenting with it here by attempting to utilize a :pseudo element, but so far, I have not succeeded in achieving the desired result of having the grey line from the second row span 100% of the width.

Does anyone have suggestions or solutions?

Answer №1

One clever solution is to enlarge the pseudo-element significantly.

li:before {
  content: " ";
  position: absolute;
  top: 0;
  left: 0;
  width: 2000px;
  height: 1px;
  border-bottom: 2px solid #ccc;
  z-index: 1;
}

The designer aims to insert a line above the items when they wrap into two lines (on smaller screens).

Note: As CSS cannot identify line breaks, this effect would be applied universally unless enclosed in a media query.

ul {
  list-style: none;
  margin: 0;
  overflow: hidden;
}
li {
  display: inline-block;
  margin-right: 2px;
  padding: 5px;
  position: relative;
  z-index: 2;
}
li:before {
  content: " ";
  position: absolute;
  top: 0;
  left: 0;
  width: 2000px;
  height: 1px;
  border-bottom: 2px solid #ccc;
  z-index: 1;
}
div {
  padding: 5px;
  width: 75%;
  margin: auto;
  overflow: hidden;
}
<div>
  <ul>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
  </ul>
</div>

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

Methods for Obtaining the Base URL Together with href Links

Currently, I find myself at this location: localhost/magento/dresses/adidas-t-shirt.html In my file, I have written the following: <a href="my/hello/">Click Here</a> When I click on Click Here, I am directed to: localhost/magento/dresses/ ...

Using JavaScript to modify a section of an anchor link attribute

I am looking to dynamically update part of the URL when a specific radio button is selected. There are three purchase links, each representing a different amount. After choosing an animal, I need to select one of the three amounts to spend. How can I modi ...

Text is misaligned with the cursor position

I've created an HTML file and implemented a typewriter-like animation using typed.js. Here's the code snippet: var typed = new Typed('#typed', { stringsElement: '#typed-strings', smartBackspace:true, ...

Ordering the list based on the highest number of matching items

I have a unique array that contains between 3 to 5 arrays, each with objects. My objective is to organize these objects into lists, resulting in 3, 4, or 5 lists. The sorting method I am aiming for is unconventional - I want to align the items in each list ...

Expanding navigation bar onto a second line with the help of Bootstrap

Currently delving into Bootstrap for the first time, I am working on designing a webpage from scratch. The navbar component has been giving me some trouble. My vision for the navbar is as follows: On desktops and tablets, it should function like a traditi ...

I require the flexbox children to be arranged in a regular manner

How can we set flexbox children to default layout behaviors? Let's make them behave like the disobedient kids they are. .wrapper { This should be a row } .column { Make all heights equal display: flex; } .child { Back to regular display...wit ...

Guide on excluding the first row when referencing all rows with CSS

My div container is hosting a table and I have a requirement to set the height of all rows, except the first one, to a specific value. Additionally, I need to adjust the text size in each column excluding the first row: This snippet shows my current html: ...

Having trouble installing node-sass through npm

Currently, I am attempting to install the SASS compiler node-sass in order to compile SCSS code into CSS code. Despite following an online tutorial and replicating the same steps, I keep encountering errors. npm init --yes : this will generate a json file ...

CSS might not always work properly on all web browsers, but it functions perfectly on Eclipse

When developing JSF pages with stylesheets, everything seems to be working fine in the Eclipse preview function. However, when I test the pages on IE8, the styles do not seem to have any effect. I am utilizing composite views to define a general layout fo ...

Overriding PrimeNG styles with Bootstrap _reboot.scss

I'm facing an issue where Bootstrap is overriding my PrimeNG styles, particularly with its _reboot.scss file. I have included the relevant parts where I referenced it in both my styles.scss and angular.json files. Interestingly, I noticed that this is ...

Wordpress team exhibition

Does anyone know how to create a Team showcase slider in Wordpress? I have been using slick slider, but I'm looking for a way to add team members easily through a plugin rather than manually. I found an image of what I want it to look like. Can any ...

JSON to HTML Table Comparison and Notification

I have implemented an AJAX request to update an HTML Table with data from a JSON file. However, I am now looking for a way to notify users of any changes by alerting them about the newly added table items. Here is how the HTML table gets populated: I&apos ...

Imitate the actions of images with {width: 100%; height : auto} properties

I am interested in creating a unique layout that consists of a stripe composed of images with varying widths and heights. These images need to be proportional, scaled at the same height, and collectively have a width equal to the parent element. However, ...

Is there a way to incorporate animation into a :hover element?

Currently, I am trying to create a :hover effect that will change the background color of an element between five different colors when it is hovered over. Below is the code I have been working on: <div class="background_changer"> < ...

Is there a problem with the background size?

I am struggling with scaling a giant background image to be 100% of the browser size. The issue arises when the webpage height is greater than that of the browser. For example, if the browser is 1000x1000 and my website is 1000x1500, scrolling down causes ...

Updating contact list to a database table structure

Currently, my code displays the data in address books, but I would like it to be shown in a table instead. I attempted to use document.write to create the table, but I'm unsure how to populate the table with the data rather than the address book forma ...

Having issues with media queries functioning incorrectly in VS Code

`@media(min-width:300px)and(max-width:400px){ div{ background-color: pink; } }` While experimenting with responsive design techniques, I attempted to implement this code snippet. However, it did not work as expected in Visual Studio Code. ...

Leverage the power of Next.js to dynamically render a specific section of an HTML webpage

I want to incorporate Next.js into a specific section of my website, while the rest of the site is built using different frameworks that are not even based on React. I aim to use Next.js within a <div id="nextjs_div">...</div> element ...

Position the div to align with just one side of the table-cell

I am struggling with making the height of my code dependent on only the left column, while still allowing the right column to be scrollable if it contains more content than the left column. Despite my best efforts, I can't seem to figure out how to a ...

Using HTML and CSS to create a sticky overlay effect

I'm currently working on developing a calendar application but facing some challenges with the layout structure. The main aim is to create a scrollable div featuring timeline overlays. This is my progress so far: .calendar { position: absolu ...