Using CSS3, you can apply multiple backgrounds to an unordered list with one set

Currently, I am in the process of working on a unique Wordpress template. One of the challenges I am facing is figuring out how to incorporate three background images for each <li> element in the dynamically generated navigation menu. Here are two examples for reference:


Here is an example of CSS code:

.menu-item li {
  background-image: url(images/menu-left.png), url(images/menu-right.png), url(images/menu-center.png);
  background-position: left, right, center;
  background-repeat: no-repeat, no-repeat, no-repeat;
}

And another example of CSS for styling:

.menu-item li {
  background-image: url(images/menu-left.png), url(images/menu-right.png), url(images/menu-center.png);
  background-position: left, right, center;
  background-repeat: no-repeat, no-repeat, repeat-x;
}

For the HTML structure:

<ul class="menu-item">
  <li>ItemNumber1</li>
  <li>ItemNumber2</li>
  <li>ItemNumber3</li>
</ul>

I aim to have the outer background images remain without repeating while the middle background image repeats along the x-axis, but only within the boundaries of the other background images. When utilizing "repeat-x," as demonstrated in the second example, the middle image spans across the entire <li>. Since the menu text is generated dynamically, using <div> elements might not be feasible. Therefore, I believe I need to work solely with a <ul> structure without additional <div> elements. Any suggestions or assistance would be greatly appreciated.

Answer №1

hello there, an effective approach is to utilize the after-before pseudo class to achieve your desired outcome.

Take a look at the CSS implementation below :-

CSS

.menu-item li:after {
    background: url("images/menu-center.png") repeat-x scroll 0 0 transparent;
    bottom: 0;
    content: " ";
    left: 20px;
    position: absolute;
    right: 20px;
    top: 0;
    z-index: -1;
}
.menu-item li {
    background-image: url("images/menu-left.png"), url("images/menu-right.png");
    background-position: left center, right center;
    background-repeat: no-repeat, no-repeat;
    display: inline-block;
    height: 56px;
    line-height: 56px;
    min-width: 60px;
    padding: 0 20px;
    position: relative;
    text-align: center;
    z-index: 1;
}

Check out the result of this styling in action

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

Troubleshooting Issue: ASP.NET UpdatePanel Not Responding to jQuery

I am having difficulties invoking jQuery functions within an "asp:UpdatePanel". Despite the code provided below, my attempts to add a class to the div element ".popup-body" are unsuccessful. Interestingly, the "alert();" function works without any issues. ...

Preventing Form Submission from Redirecting to the Top in an HTML Document with PHP

Recently, I integrated a php form into my html code and changed the file from index.html to index.php. The contact form is functioning perfectly and sending all information as expected. However, after submitting the form, users are receiving the message "T ...

Customizing the active link color in Bootstrap dropdown menus

Is there a way to customize the color and background of an active link in a boostrap's dropdown menu? Despite trying to override bootstrap's @dropdownLinkColorActive and @dropdownLinkBackgroundActive variables, the changes don't seem to tak ...

The function Jquery .stop does not exist

I am encountering an issue with the magicline.stop function while attempting to implement an underline sliding effect for my navbar. I have tried to troubleshoot the problem but have been unsuccessful so far. Below is the code snippet: <nav class=" ...

Grids designed in the style of Pinterest

Seeking advice on aligning divs in a Pinterest-style layout. Currently, my setup looks like this: https://i.sstatic.net/erlho.png But I want it to look more like this: https://i.sstatic.net/K9FnD.png Would greatly appreciate any tips or suggestions on ho ...

Methods for adjusting columns and rows in HTML5 tables

As I was preparing to create a compatibility guide, I noticed that the HTML code consisted of a table. While tables are effective for displaying data, they can be cumbersome to edit frequently. What tools are available to quickly and cleanly edit an exist ...

PHP query returned no matches

<?php include 'db.php'; $GLOBALS["conn"] = connect(); if (isset($_POST['submit'])) { $ime = $_POST['ime']; $rm = $_POST['rm']; $minpl = $_POST['minpl']; $maxpl = ...

Do not manipulate HTML tags using sed command

There are numerous guides available demonstrating how to utilize sed for modifying/removing HTML tags, and most of them typically suggest something along the lines of... sed 's/<[^>]\+>//g' However, what I am seeking is the opposi ...

What is the best way to incorporate multiple countdown timers on a single HTML page?

I am in the process of developing an online auction site and I need to include the end time and date for each auction. To achieve this, I have created a countdown.js file with the following code: // set the date we're counting down to var target_dat ...

multiple divisions in the center

I am struggling with centering 2 divs, each containing 3 nested divs wrapped around a big main div. The code I have written is as follows: <div stye="margin-left:auto; margin-right:auto; width: 1210px;"> <div id="left" style=" float:left; width ...

Unable to assign attribute following discovery

Can the attribute of an anchor element that is found using find() be set? I attempted this: $(this).children('a').setAttribute("href","a link"); Although it does locate the anchor element, why am I receiving an error when trying to use setAttr ...

What is the best way to ensure that the button's left margin matches the left margin of the table at all times?

https://i.sstatic.net/qJwvE.png Hello there, I am currently working with HTML/CSS and I have managed to center a table on my page using the CSS class .tablecenter { margin-left:auto; margin-right:auto; } Following the table, I have placed a button: ...

Seeking to have text spill over into a different container

Novice in CSS seeks advice. I've nailed the layout of the home page for my website with two divs</p> <p><div> <div> <pre class="snippet-code-css lang-css"><code>#desktop-navbar { text-transform: uppercase; ...

Items outside the container element

I recently noticed an issue with my website, which is built using Bootstrap. The problem arises when users scroll down the page and encounter the fixed navigation menu. It appears that the menu items and logo are no longer contained within the designated ...

In terms of professional design, which is the better choice - Bootstrap or Foundation?

Greetings! I am in the process of creating a website with a professional design. I'm trying to decide between using Bootstrap 3 or Foundation 5 for my CSS framework. Which one do you recommend for designing purposes? Is there another CSS framework t ...

Define the width and height of images specifically for screens with a maximum device width using @media queries

Having some trouble with the sizing of images on mobile devices. Using the code below, but for some reason, the height remains at 13em on smartphones, despite setting it differently for other devices. Any ideas why this might be happening? @media only sc ...

What method can I use to ensure that the sidebar stays fixed at a particular div as the user continues to scroll down the

Is there a way to automatically fix the sidebar once the user scrolls down and hits the top of the .Section2? Currently, I have to manually enter a threshold number which can be problematic due to varying positions across browsers and systems. Fiddle htt ...

Image missing aria-label attribute

I am facing an issue with getting the aria-label to display on my image. Despite checking my code, I cannot figure out why it is not working. Any ideas on what might be missing? Here is the code from my NextJS app: The svg in my public folder. <?xml v ...

Implementing a document update event using jQuery

On my WordPress site, I am using a responsive lightbox plugin. When an image is clicked, it opens a popup box with the ID fullResImage. Now, I would like to incorporate the Pinch Zoomer function to it. Do I need to bind a function for this, or is there a s ...

The minification of HTML scripts may cause problems with the <script> tag

Greetings! I have a PHP script that is used to minify the page by removing any comments or spaces. Here is the script: <?php function sanitize_output($buffer) { $search = array( '/\>[^\S ]+/s', '/[^&b ...