Place CSS elements in relation to the underlying element

This is an example of my HTML structure:

<nav id="menu">
   <ul>
      <li><a href="">Products</a></li>
      <li><a href="">Offers</a></li>
      <li><a href="">References</a></li>
      <li><a href="">Directions</a></li>
      <li><a href="">About us</a></li>
      <li><a href="">Jobs</a></li>
      <li><a href="">Partners</a></li>
      <li><a href="">Contact</a></li>
   </ul>
</nav>

Here is the CSS styling for the menu:

#menu {
    position:relative;
    width:100%;
    height:35px;
    margin-bottom:10px;
    background-image: url("../img/menuBackground.png");
    background-repeat: no-repeat;
}
#menu ul {
    display: block;
    list-style-type: none;
    position: relative;
    top:5px;
    margin-left:20px;
    margin-right:20px;
}

#menu ul li {
    display: block;
    position: relative;
    float: left;
    margin-right: 16px;
}

There is a concern about the spacing around the text in the A elements overlapping the white stripe in the "menuBackground.png" image. The issue occurs with the Kontakt element, where the text overlaps the white stripe.

How can I ensure that there will be additional spacing added when an element overlaps the white stripe?

Thank you in advance for any suggestions or solutions to this problem.

Check out a JS FIDDLE EXAMPLE for reference...

Answer №1

What has been found to work is:

#menu {
    position:relative;
    width:100%;
    margin-bottom:10px;
    background-repeat: no-repeat;
    overflow: hidden;
}
#menu ul {
    display: block;
    list-style-type: none;
    position: relative;
    top:5px;
    margin-left:20px;
    margin-right:20px;
    background-color: #ff9025;
    overflow: hidden;
}

#menu ul li {
    display: block;
    position: relative;
    float: left;
    padding: 0 8px;
    height: 35px;
    background-color: #ffbf00;
}

#menu ul li:last-child {
    background-image: url(https://i.stack.imgur.com/WSJSV.png);
    background-position: 63% top;
    background-repeat: no-repeat;
    padding-right: 60px;
}

Although it's not the most elegant solution.

View JS Fiddle demo.

The approach involved moving the background-image from the ul (or maybe the nav, unclear) to the end of the li:last-child1, positioning it correctly to show the stripe. Additional padding was added to that element, and the ul was styled with the color of the right side of the image, while the li elements were given the left side color to create the illusion of the image. Although not perfect, this method seems to be functioning reliably. It is recommended to add and comment out more li elements for testing purposes.

Update: In light of the limited support for :last-child in some browsers like IE, jQuery can be used as your site already utilizes it:

$('#menu li:last').addClass('lastChild');

Updated JS Fiddle demo.


Further Update: Here is what I believe to be the final version, including a feature-detection to enable the jQuery option if :last-child support is lacking (although explicit testing for lack of :last-child support is challenging, assuming that a browser without support for document.querySelectorAll() likely lacks support for :last-child):

if (!document.querySelectorAll){
    $('#menu li:last').addClass('lastChild');
}

Final JS Fiddle demo.


  1. This issue primarily affects older browsers, particularly those before IE9, as versions prior to IE9 do not support the :last-child pseudo-selector. A review of compatibility tables on Quirksmode.org reveals the extent of the problem.

Answer №2

Adding space using a background image can be challenging as it requires precise customization. One option is to create a div that divides your navigation bar, but this may involve cutting your background image into separate parts for the navigation section and white stripe. Alternatively, adjusting margins and font size can also help achieve the desired spacing.

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 showing placeholder text on Safari

I'm having an issue with the placeholder attribute in Safari. It seems to be working fine in all other browsers, but not in Safari. <textarea class="concerncommentArea" placeholder="Place your comment here"></textarea> Does anyone know h ...

Conceal the div using a sliding left animation

After researching extensively, I have been unable to find a solution to my problem. In order to better illustrate my issue, here is the code for reference: jsfiddle I am looking to create a sliding effect for a calc div where it moves to the left, simila ...

How can CSS be used to change the text color of input placeholder in Edge Browser?

While using Edge Browser, I encountered an issue where I was unable to change the input placeholder color. The :-ms-input-placeholder selector was not working on Edge, although it functioned properly on IE 10 and IE 11. input:-ms-input-placeholder { ...

Setting the selected value of a COREUI multiselect widget

I received the following JSON response from an API. {"first_name":"Person 1","last_name":"Person 1","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="78081d0a0b17163 ...

Error in Google Translate API AttributeError

Despite changing the gtoken on googletrans stopped working with error 'NoneType' object has no attribute 'group', I am still encountering the "AttributeError: 'NoneType' object has no attribute 'group'" error. Howeve ...

What is the best way to position a title and subheading alongside an image?

I am currently working on developing a blog using php and html to enhance my coding skills. On my website, I have a special category where I display posts, the number of which can vary. Each post is composed of a title, a subheading, and a placeholder div ...

How to make sure that an element overflowing its container always starts from the top

My website has a section called <mat-drawer-container>, which contains a list of items called <mat-selection-list>. However, when the number of elements in the list exceeds the display height and scrolling is necessary, the scroll position star ...

Obtain the values of multiple attributes using jQuery

Here is some HTML code that I am working with: <img src="" data-src="https://example.com/image1.jpg" class="img"> <img src="" data-src="https://example.com/image2.jpg" class="img"> <img src="" data-src="https://example.com/image3.jpg" class ...

AngularJS: Utilizing bold text to enhance the separation of concerns between controllers and templates

In my AngularJS version 1 application with Ecmascript 6, I have a requirement to display a string where one part is in normal weight and the other part is bold. For instance, consider the following scenarios: Will start now Will start in 6 minutes Will ...

Unable to append HTML table row to designated table

I am trying to populate an HTML table with the JSON string data I receive. The object data seems correct, but for some reason, it is not getting appended to the table. Can you help me identify what's wrong with my jQuery append statement? functio ...

The navigation menu that usually appears on mobile devices is surfacing on desktop screens instead - update: images now

I have recently been working on my website juniorgoldreport.com, which was created using WordPress. The plugin I am using specifically for the mobile nav bar is The functionality and ease of use of this plugin are incredible. The setting to show the toolb ...

Issues with CSS3 height transitions in a specific scenario

Check out this simplified version of my code on CodePen: http://codepen.io/anon/pen/pjZXob I am attempting to create a smooth transition in the height of the .destinationsTopicContent div, from 0 to auto. However, it is nested within a div that has visibi ...

Encountering Problem Importing HTML Table Data into R

I am looking to import the data table located at the bottom of a specific webpage into R, either as a dataframe or table. The webpage in question is . Initially, I attempted to utilize the readHTMLTable function from the XML package: library(XML) url <- ...

Height of list items in image gallery does not automatically readjust

I'm facing an issue regarding the height of an li element. Check out this CodePen link for reference. <div class="row fp-gallery"> <ul class="large-block-grid-4 medium-block-grid-4 small-block-grid-2"> <li> <a href= ...

Aligning a collapsible feature while ensuring that the content descends

My dilemma involves a centered menu with a dropdown feature. The issue arises from the absolute positioning, causing the entire menu to shift upward when the dropdown is activated due to the increased height. I am struggling to find an effective solution ...

Limit text to two lines inside a cell in a table

Apologies for not providing any evidence of my own efforts. I'm looking to wrap text in a table cell, but limit it to just 2 lines. Expanding the width is not possible. Any suggestions on how to achieve this? ...

I prefer the jumbotron to be uninterrupted by the navbar

I'm facing an issue where my jumbotron is ending up under the navbar, but I want to position it just below with some padding in between. Here's the code snippet: Take a look at the current appearance here: This is how it looks like ...

Numerous Kendo windows are layered on top of each other, yet the text divisions within them remain distinct

I am currently working on a project that involves laying out multiple Kendo windows in rows. Specifically, I need to display 4 windows in each row and have them shift left when closed. My framework of choice is Bootstrap 3. Everything works as expected w ...

When working with NodeJS and an HTML form, I encountered an issue where the 'Endpoint'

Having trouble sending input data from a form to my nodejs endpoint. When I try printing the req.body, it shows up as undefined and I can't figure out why. Here is the relevant API code snippet: var bodyParser = require('body-parser') var e ...

Incorporating Copyleaks SDK into Angular: A Seamless Integration

Currently, I'm in the process of implementing the Copyleaks SDK with Angular to conduct plagiarism checks on two text area fields within an HTML form. Within the form, my goal is to incorporate two buttons: one to check for plagiarism on one text area ...