Conflicting names between a Less mixin and a CSS selector

After writing out this simplified Less script

.placeholder(@color: #333333) {
    &::-webkit-input-placeholder  { color: @color; }
}

input {
    .placeholder();
}

.placeholder {
    margin-top: 20px;
}

When I compile this code using my local compiler or the winless online less compiler, the generated output is as follows:

input {
  margin-top: 20px;
}
input::-webkit-input-placeholder {
  color: #333333;
}
.placeholder {
  margin-top: 20px;
}

Instead of the expected result which should be:

input::-webkit-input-placeholder {
  color: #333333;
}
.placeholder {
  margin-top: 20px;
}

Is this a bug in the compiler or am I overlooking something in my code?

Based on the outcome, it appears that having CSS selectors with the same name as mixins with default values may be causing the issue.

This problem arises when I try to compile Bootstrap along with my custom code. Although I can find workarounds for now, in the long run, managing mixins with default values could become cumbersome as the project expands to include more components from other sources.

Edit: Upon revisiting the documentation and the information provided on the first page of the docs, I realize that every element can be treated as a mixin in Less.

Answer №1

Within Less, every element is essentially considered a mixin, whether it is written with parentheses (with parameters) or without them (resembling a CSS class selector). The distinction lies in the fact that when parentheses are included, the properties within them are not displayed unless specifically called from within a selector block.

Citing the Less Website:

It is permissible to define multiple mixins with the same name and parameter count. Less will incorporate properties from all applicable mixins.

In scenarios where another mixin includes a default value for its sole parameter, both sets of properties may be applied when invoked without any parameters, making it inevitable.

Potential Resolution: A potential workaround involves encapsulating conflicting rules within a parent selector (such as body).

.placeholder(@color: #333333) {
    &::-webkit-input-placeholder  { color: @color; }
}

input {
    .placeholder();
}

body{
    .placeholder{
        margin-top: 20px;
    }
}

Compiled CSS:

input::-webkit-input-placeholder {
    color: #333333;
}
body .placeholder {
    margin-top: 20px;
}

Alternative Approach: Extracted from the resolution provided by seven-phases-max in the Less GitHub Issue thread.

One potential workaround for this specific scenario is to isolate conflicting classes within an unnamed scope so they do not clash with external names:

.placeholder(@color: #333333) {
    &::-webkit-input-placeholder  { color: @color; }
}

input {
    .placeholder();
}

& { // unnamed namespace
    .placeholder {
        background: #ffffff;
    }
} // ~ end of unnamed namespace

Note: The above content is directly copied from the GitHub thread without any alterations to preserve the accuracy of information.

Answer №2

@mixin custom-placeholder(@color: #555) {
    &::-webkit-input-placeholder  { color: @color; }
}

input {
    @include custom-placeholder();
}

.custom-placeholder-style {
    margin-top: 20px;
}

Looks good to me!

Answer №3

Let me make sure I got it - you're looking to increase the top margin of the placeholder by 20px? In that case, try using padding-top on the input element instead.

input {
    padding-top: 20px;
}

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

The CSS selector functions as expected when used in a web browser, however, it

While conducting test automation using Selenium, I typically rely on css selectors to find elements. However, I recently came across a peculiar issue. I observed that in certain cases, the css selector works perfectly when tested in the browser console. Fo ...

What is the best way to prevent table cell borders from encroaching on the padding area of the cell?

When dealing with table cell elements that have borders, it's common for them to not respect the vertical height of the cell containing them. Sometimes a child element's borders may overlap the padding or border of its containing cell. To prevent ...

Troubleshooting issue: Bootstrap 4 input group not properly showing invalid feedback text

I've been exploring the features of Bootstrap 4 - beta and I've noticed that when using .is-invalid with .input-group, it doesn't seem to be visible. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.cs ...

The sidebar made an unexpected leap from one side to the other, with no memory of ever tampering

I'm feeling a bit overwhelmed because I can't seem to figure this out. My goal is to create a basic page layout with a header, subheader, sidebar, content pane, and footer. Everything was going smoothly until I started writing a function in my J ...

What techniques can I use to achieve a seamless transition using Javascript and CSS?

I'm striving for a seamless CSS transition. The concept of transition had me puzzled. Even though I utilized the setTimeout method in JavaScript to make my CSS functional, it lacks that SMOOTH feel! Check out my code below. function slideChange( ...

Arranging the R shiny selectInput controls in the correct position

I am looking for help with adjusting the position of two selectInputs in my R Shiny script. Currently, the dropdown is not appearing clearly and I've tried using padding without success. I have attached a snapshot for reference. Any assistance would b ...

Tips on setting a background image to a div element

I'm facing an issue with a div setup like this: https://i.sstatic.net/4nuyO.png My goal is to remove a specific section of the circle shape displayed below: https://i.sstatic.net/09IWX.png The desired outcome is to achieve a final shape similar to ...

The navigation bar struggles to display list elements without proper line breaks

Recently, I've been immersed in a web development project for a company. For the navigation bar, I opted to use the FlexNav JQuery plugin. While referencing the example on , I encountered an issue when attempting to add more than 5 list elements; they ...

Tips for arranging a jQuery UI dialogHere are some suggestions for

http://jsfiddle.net/Qt7pQ/5/ Here is a demonstration of the concept I am working on. My query is regarding how to position a jQuery UI dialog under a radio button? <div id="rbl_1"><input type="radio" group="one" id="r1" value="Milk"> Milk< ...

Determining the precise mouse location within child elements regardless of zoom level, scrolling, or dimension changes

My goal is to accurately determine mouse coordinates within the page elements. To see the code in action, please visit: http://jsfiddle.net/t8w9k6gu/ $('#p1, #p2, #p3').on('click mousemove touchmove', function(ev){ var target = ...

What is the best method for incorporating dynamic page transitions when navigating between individual pages?

Let's say I have the following pages: red.html blue.html green.html My goal is to create links from red.html to blue.html and red.html to green.html. When attempting to use page anchors and iframes, I encountered an issue where scrolling from red.h ...

Firefox encounters a border-radius glitch

Having an issue with the border-radius function in Firefox. After applying it, I notice a strange space or border around the item. Does anyone know if this is a bug specific to Firefox or if there is a solution for it? Here is an example of the problem: J ...

Unable to apply .png image using the content property in a Symfony project

I have a png image located in src/AppBundle/Resources/public/img To set the png image using the css content property, I used this code: content: url(../../public/assets/img/optional-checked.png); However, I encountered an error: GET http://127.0.0.1:80 ...

Accessing an image within a label using Jquery

I'm struggling with accessing the img tag when a specific checkbox is selected in order to add a particular class. I need help figuring out how to target the img tag. <div class="row "> <div class="col-md-5 col-md-offset-1 col-centered ...

Place an image on top of adsense

Is there a way to overlay an image on top of Google AdSense ads in order to track user clicks? I am trying to implement an onclick method for when someone clicks on the ads. .ad-alert { width: 728px; height: 400px; background: #cccccc; mar ...

Cover any HTML element with a translucent overlay box

I have a unique problem with an HTML file that is out of my control when it comes to its content. My only option is to inject a CSS file and/or JavaScript (potentially using libraries like jQuery) into the mix. Within this HTML, there are elements that re ...

Limitations of jQuery: onClick function not transferable

I am struggling to achieve the following functionality for my navbar dropdown menus: When the screen size is greater than 992px, I want the navbar dropdowns to open on mouse enter. For screens smaller than 992px, I want the dropdowns to open on ...

Using the identical code, Wicked PDF generates distinct reports on separate computers

While utilizing Ruby on Rails to render a PDF using WickedPDF and sending it via email, I encountered an unexpected issue. The same code is present on two separate computers, both with up-to-date versions synced from GitHub. However, the generated PDF repo ...

Looping through images using JQuery

I'm struggling with an image animation and can't quite figure out how to make it work. <div id="img_loop"> <img src="img/img1.jpg" alt="image1" /> <img src="img/img2.jpg" alt="image2" class="hidden" /> <img src="im ...

Tips for creating a dynamic grid layout with a maximum width for desktop items

Imagine this scenario (feel free to copy and paste the code into a new document for better visualization): .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); grid-gap: 20px; } .item { background-color: #bfe0f5; ...