Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AI Help] Question: Can you use counter() with calc() ? #48

Open
1 of 4 tasks
LeCitronVert opened this issue Mar 27, 2024 · 1 comment
Open
1 of 4 tasks

[AI Help] Question: Can you use counter() with calc() ? #48

LeCitronVert opened this issue Mar 27, 2024 · 1 comment
Labels
error: factual Misinterpreting or providing incorrect information.

Comments

@LeCitronVert
Copy link

What is the issue with the answer?

The answer provided is incorrect, as counter() cannot be used inside calc() due to returning a string.
It seems to me like the AI did not check MDN pages about calc() and thus did not take return types into account.

How would you categorize this issue?

  • The answer is harmful / unsafe.
  • The answer is not correct.
  • The answer is not helpful.
  • The answer is outdated.

What did you ask?

  1. Can you use counter() with calc() ?

What did AI Help answer?

Yes, you can use the counter() function within the calc() function in CSS. This can be useful for dynamically calculating values based on counters. Here's an example:

/* Increment the margin-left of each list item based on the counter value */
li {
  margin-left: calc(1em + counter(listCounter) * 10px);
}

In this example, the margin-left of each li element is calculated based on the value of the listCounter counter. The calc() function allows you to perform mathematical calculations, and combining it with the counter() function provides a way to create dynamic styles based on counter values.

What MDN pages did AI Help consult?

@LeCitronVert LeCitronVert added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Mar 27, 2024
@caugner
Copy link
Contributor

caugner commented Apr 19, 2024

Hi @LeCitronVert, 👋

Thank you for reporting this issue, I can reproduce the issue with GPT-3.5 (free user).

Here's how the answer looks with GPT-4 (paid MDN Plus subscription) using the same context. GPT-4 has more base knowledge about calc(), leading to what I would argue is an overall better response:

Yes, you can use the counter() function within the calc() function in CSS, but with some limitations. The counter() function is used to retrieve the current value of a counter, which is typically used for numbering items in a list or for similar purposes. The calc() function allows you to perform calculations to determine CSS property values.

However, the key limitation is that the result of counter() is not a number but a string representing the counter's value. Since calc() performs mathematical operations, it expects its arguments to be values it can calculate with, such as lengths, numbers, or percentages.

To effectively use counter() within calc(), the counter value must be used in a context where it represents a dimension, number, or any other value type that calc() can operate on. This is typically not straightforward because counter() outputs are not directly manipulable as numeric values in CSS.

For example, you might want to adjust spacing or sizing based on the count of items, but you would need to handle the numeric part of the operation outside of the counter() function itself. A common approach to achieve dynamic styling based on counters is to use CSS custom properties (variables) in JavaScript to set numeric values based on counters, and then use those variables within calc().

Here's a conceptual example of how you might try to use a counter value indirectly with calc():

:root {
  /* Assuming you have a way to set --counter-value based on a counter() value in JavaScript */
  --counter-value: 1; /* Placeholder value */
}

.item {
  width: calc(var(--counter-value) * 100px);
}

In this example, --counter-value would be dynamically updated based on the actual counter value, and then used within calc(). Directly combining counter() and calc() without such an intermediary step is not feasible due to the type of value counter() returns.

For more information on calc() and counter(), you can refer to the MDN documentation:

Remember, the direct manipulation of counter() values within calc() as described above is a conceptual approach and not directly supported in CSS. The actual implementation would require a workaround, such as using JavaScript to manipulate CSS variables based on counter values.

And here's how the GPT-3.5 answer will look in the next version of AI Help, which will use a newer embedding model to determine relevant MDN content, and consults the following MDN pages:

Yes, you can use the counter() function in combination with the calc() function in CSS. The counter() function is typically used to retrieve the current value of a named counter, while the calc() function allows you to perform calculations when specifying CSS property values.

Here is an example of how you can use counter() with calc():

/* Using counter() with calc() */
div::before {
  content: counter(myCounter) calc(10px + 5%);
}

In this example, the content property of a pseudo-element (::before) is set to the value of the counter named myCounter concatenated with the result of a calculation (10px + 5%) using calc().

For more information on the counter() function, you can refer to the MDN documentation on counter().

And for details on the calc() function, you can check the MDN documentation on calc().

Let us know what you think about these answers. 🙏

@caugner caugner added error: factual Misinterpreting or providing incorrect information. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error: factual Misinterpreting or providing incorrect information.
Projects
None yet
Development

No branches or pull requests

2 participants