English 中文(简体)
• 如何在幼年正确通过CSS变量?
原标题:How to pass a CSS variable correctly in nth-child?
  • 时间:2022-03-24 09:34:39
  •  标签:
  • css

<><>Goal: 成功地将变量作为参数传给婴儿。 在下文中,第三行应转而绿化。

<<>Problem>: 目前,参数被忽略为一个变量。

Question: Is this possible at all? If yes, what do I have to change?

www.un.org/Depts/DGACM/index_spanish.htm 例:

:root {
  --n: 3;
}
div p:nth-child(var(--n)) {
  background: green;
  padding: 10px;
}
<div>
  <p>a</p>
  <p>b</p>
  <p>c</p>
</div>
最佳回答

可在<条码>后在<<上公布<>/>。 这也意味着,不幸的是,你无法在选择者或媒体查询中使用这些习俗。

That is also the reason why people really should stop calling them CSS "variables".

You can, however, manipulate styles using Javascript:

const dynamicStyles = document.getElementById( dynamicStyles );

const n = getComputedStyle(document.documentElement).getPropertyValue( --n );

dynamicStyles.textContent = `
div p:nth-child(${n}) {
  background: green;
  padding: 10px;
}
`;
:root {
  --n: 3;
}
<style id="dynamicStyles"></style>
<div>
  <p>a</p>
  <p>b</p>
  <p>c</p>
</div>
问题回答

理解为什么在特别安全局无法做到这一点,考虑这个例子。 第三个要素的颜色是什么?

<div>
  <p>a</p>
  <p>b</p>
  <p>c</p>
</div>
:root {
  --n: 3;
}
p {
  background: cyan;
}
p:nth-child(var(--n)) {
  background: green;
  :root:has(&) {
    --n: 1;
  }
}

If --n evaluates to 3 then the third p element color is green and --n evaluates to 1. Which means that the third p element color is cyan and --n evaluates to 3. And repeat.

你可以用纯粹的中央安全局来这样做,但你可以试图撰写一份联合文件,以便这样做。

这一职能:

getComputedStyle(document.documentElement)
    .getPropertyValue( --n );

页: 1

现在,既然你可以把 j放在你身上,你就必须在你的超文本档案中这样做。 因此,你的工作是围绕你履行这一职责的变量总结一个html式标签:

<script>
  document.getElementById( custom-container ); //Select the div container

  node.innerHTML( <style> #custom-container p:nth-child( ); //Wrap style around the variable

  getComputedStyle(document.documentElement)
    .getPropertyValue( --n ); //get your variable, in this case  3 

  node.innerHTML( ){background: green;padding: 10px;}</style> ); // Close the wrapping and define the style
</script>

<div id="custom-container"> <!-- Mark your div container with and ID -->
  <p>a</p>
  <p>b</p>
  <p>c</p>
</div>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签