Display an icon to act as a separator between icons in CSS styling

Is there a way to display a spacer line before and after icons (cross symbols) while excluding the spacer line before and after buttons carrying the word "Cancel"? How can this be achieved?

This is my CSS file:

.Container > *:first-child::before,
.Container > *::after 
{
    display: inline-block;
    content: url('../Content/Images/Line.png');
}
  1. All icons and buttons containing "Cancel" are enclosed within the container div.

  2. How can we prevent the spacer lines from appearing before and after buttons with the text "Cancel"?

I attempted the following code, but it was unsuccessful.

.Container > *:not(input[type="button"]):first-child::before,
.Container > *:not(input[type="button"])::after 
{
    display: inline-block;
    content: url('../Content/Images/Line.png');
}

Answer №1

Update:

Consider the following example markup:

<div class="container">
    <span>x</span>
    <span>x</span>
    <span>x</span>
    <input type="button" value="Cancel" />
    <input type="button" value="Cancel" />
    <span>x</span>
    <span>x</span>
    <span>x</span>
</div>

To achieve your desired outcome, you can utilize the below CSS:

CSS

.container > *:not([type="button"]):first-child::before,
.container > *:not([type="button"])::after 
{
    /*content: url('../Content/Images/Line.png');*/
    content: ''; /* not needed if using a line image */
    background: #555; /* not needed if using a line image */
    display: inline-block;
    width: 1px;
    height: 100%;
    vertical-align: middle;
    margin: 0 8px;
}

See Example Here

Note: Instead of relying on the * selector, consider targeting specific child elements or assigning a class to them for better clarity.


Why did your initial CSS code not work as expected?

The :not() pseudo-class only accepts a simple selector.

As per the specification:

A simple selector can be a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.

While the not pseudo-class can accept an attribute selector like: :not([type="button"]), combining it with an element selector (e.g., input) - such as :not(input[type="button"]) is incorrect, leading to undesired results.

This will work:

.Container > *:not([type="button"])::after 
{
    display: inline-block;
    content: url('../Content/Images/Line.png');
}

However, this won't:

.Container > *:not(input[type="button"])::after 
{
    display: inline-block;
    content: url('../Content/Images/Line.png');
}

Refer to this demonstration for clarification.

Answer №2

To exclusively style the lines before and after icons, avoid using wildcards like * and deselecting buttons. Instead, directly target the icons by selecting their specific class such as .class.

.Container > .icon:first-child::before,
.Container > .icon::after 
{
 display: inline-block;
 content: url('../Content/Images/Line.png');
}

If the icons are <img> elements, the corresponding CSS would be:

.Container > img:first-child::before,
.Container > img::after 
{
 display: inline-block;
 content: url('../Content/Images/Line.png');
}

This approach resolves the issue efficiently, although additional details would provide more accurate solutions.

Answer №3

To resolve the issue at hand, employing an adjacent sibling selector is crucial. This allows you to target elements based on their preceding siblings like so:

.sibling#one + .sibling#two {
    /* apply styles to every .sibling#two that comes after a .sibling#one */
}

For a visual demonstration, I have prepared a simple example here, using borders instead of images with lines and div's acting as buttons. Hopefully, this gives you some insight into the solution. Best of luck!

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

Resolving the Issue of Jerky Graphics During HTML5 Canvas Animation

When animating a layer in canvas, the visual quality becomes uneven if there are additional layers present. You can see an example of this by clicking "RUN" on this fiddle: http://jsfiddle.net/Q97Wn/ If I modify this line: opts.percentageIndicator.clearR ...

Creating an Image Slideshow on Your Website

I'm looking to create an image slideshow on my website that functions similarly to the one found at . However, I want the images to occupy the entire screen rather than having smaller images like the reference site. Can anyone offer guidance on how t ...

PHP POST data not displaying in output

Having an issue with displaying the chosen value from a database enum in a text field on my website. The enum table has options like Bachelor of Science, Master of Science, Bachelor of Education, and Master of Education. I want to show the selected option ...

How can I customize the color of the check mark symbol in a bootstrap checkbox?

Here is my customized HTML code using Bootstrap: <div class="col-lg-4 col-12 text-center"> <div style="width: 80%; margin: auto"> <ul class="list-group"> {% for sl in my_list %} <li ...

Tips for retaining the original text value even after clicking the submit button

import java.util.Map; import java.util.HashMap; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org ...

Angular router link circles back to its originator

I've been working on a basic login page that transitions to a homepage once the user successfully logs in. So far, I've focused solely on the HTML structure of the login component without implementing any code in the corresponding TypeScript file ...

The lobster custom font is malfunctioning in Internet Explorer

I am trying to use the unique font called lobster. After downloading the lobster.woff2 file, I stored it in the font folder. In my custom CSS, I added the following: @font-face { font-family: 'Lobster'; font-style: normal; font-weight: 40 ...

Using jQuery to encode an ampersand in a URL

I am facing an issue where some URLs with & in them need to be converted to HTML entities using jQuery. The original code looks like this: <div id="box"> <a href="http://domain.com/index.html&location=1">First URL</a> &l ...

Troubleshooting: Issue with fixed positioning in React (Next.js) causing problems with modal window functionality

I'm currently working on implementing a modal window in React, but I've encountered an issue where setting position: fixed results in the modal window's position being relative to the page rather than the browser window. For privacy reasons, ...

A Guide to Configuring jQuery UI Slider with Three Distinct Colors

I'm currently implementing the UI Slider on my website. However, I would like to customize the slider with three different colors: Handle Color Previous portion of Handle Next portion of Handle I want it to look something like this: Currently, I h ...

What could be causing my div to not appear when using jquery's show function?

I'm dealing with HTML code that looks like this: <div id="answerTypeSection" style="visibility: hidden"> <div class="row"> <div class="col-md-2">adfadfafasdfasdfas</div> </div> <label class="control-label"&g ...

CSS is being applied on Internet Explorer 11 only after a hard refresh with Ctrl+F5

While my website loads perfectly in Chrome and Firefox, I have been experiencing issues with Internet Explorer 11. The CSS is not fully applying until after pressing Ctrl+F5 multiple times. It's strange because some of the CSS is being applied but cer ...

Tips for retrieving data from Angular Material Table source

It's great to see everyone doing well! I'm facing an issue with rendering data to a material table, and I can't figure out why it's not working. When I try to assign the data to the table's datasource for rendering, the information ...

What are the steps to utilize vue.js for dynamically adjusting my sidebar based on a URL input?

Greetings to the amazing community of Vue.js enthusiasts, I am a novice looking to develop a single-page web application using vue.js. This application will consist of a fixed header and dynamic body content that changes based on user interactions. Here&a ...

Converting a JSON file into an HTML table using PHP

I need help figuring out how to display JSON data in an HTML table. I have a script that successfully outputs my JSON content, but I'm struggling to format it into a table. Below is the code I have so far: <?php $dir = "/Apache24/htdocs/reservat ...

Create HTML elements based on the information in a JSON object

My goal is to create span elements for each word in my subtitle text, which is stored in a JSON object. Here is the JSON data I am working with: var sub_info = [ {'start': 3.92, 'end': 6.84, 'words ...

What steps do I need to take in order to ensure that each webpage displays unique thumbnails?

As a newcomer to website development, I recently looked into implementing open graph on my site. However, I ran into an issue where I could only set one thumbnail for the entire website. This posed a problem as I wanted each navigation menu tab (Home, Abou ...

`Cannot Get jQuery ScrollTop Function to Work for 2nd Element`

Having trouble with an issue. Let me explain. I implemented a scrollTop Jquery on page load that scrolls down after a delay to prompt user action. However, I'm facing a problem where another scrollTop doesn't work after clicking buttons "#b3,#b3 ...

Is it possible to alter the name of a slot before displaying the element in the shadowDOM, depending on the slot utilized in the DOM?

In my project, I am working on implementing different features for both desktop and mobile devices. Some of these features can be replaced by slots. My goal is to have a slot that can be either replaced by a desktop slot called poster-foreground, or a mobi ...

Difficulty encountered while setting up jQuery slider

I am struggling to set up a jquery slider (flexslider) It seems like I am overlooking something very fundamental, but for some reason I just can't figure it out. Any assistance would be greatly appreciated. Please check out the link to the test site ...