css3选择器怎么选择第3个,第6个,第9个,第12个……元素??

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了css3选择器怎么选择第3个,第6个,第9个,第12个……元素??相关的知识,希望对你有一定的参考价值。

代码如下:

li:nth-child(3n)


background:#ff0000;

CSS即层叠样式表(Cascading StyleSheet)。 在网页制作时采用层叠样式表技术,可以有效地对页面的布局、字体、颜色、背景和其它效果实现更加精确的控制。

CSS3是CSS技术的升级版本,CSS3语言开发是朝着模块化发展的。以前的规范作为一个模块实在是太庞大而且比较复杂,所以,把它分解为一些小的模块,更多新的模块也被加入进来。这些模块包括: 盒子模型、列表模块、超链接方式 、语言模块 、背景和边框 、文字特效 、多栏布局等。

参考技术A li:nth-child(3n+0)

background:#ff0000;
追问

为什么要加零呢?

追答

打多了 。。。

追问

恩恩,谢谢你啊,你是第一个被采纳的!!

本回答被提问者和网友采纳
参考技术B ul li:nth-child(3n+0)

/*下面放你的样式*/

CSS:第n个选择标题

我需要一些帮助来从标题中选择字母/单词(h1,h2,..)。

这是一个CSS Zen Garden,这意味着我只能使用CSS而不能编辑HTML。我需要使用css选择器。我尝试使用不同的::第n选择器,但我还没有找到解决方案。

如果我可以更改HTML,我会在标题中包含我需要的单词,并使其获得一种新颜色,但这里不可能......

我需要选择h3的第10个字母,并在部分序言中将颜色更改为红色,在解释部分我也需要更改它。

我尝试使用第n个选择器:: nth-letter,:: last-line,:: nth-word。 ::第一个字母在第三个标题中起作用,但其余的则不起作用。

/*eerste section*/

.preamble h3 {
    font-family: fraktur;
    font-size: 15em;
    color: #4A4A4A;
    margin: 5%;
}

.preamble h3::nth-letter (5)  {
  color:#C54B36;
}

.preamble {
    /* width: 80%; */
    display: grid;
    grid-template-columns: 1fr;
    margin: 50px auto;
    /* border: 2px solid #000000; */
    background-image: url('../images/foto1.png');
    background-repeat: no-repeat;
    /*background-size: contain;*/
    background-size: 40%;
    background-position: 93% 82%;
}

.preamble p {
    width: 47%;
    margin: 15px 40px;
}


/*einde eerste section*/


/*tweede section*/

.explanation h3 {
    font-family: baskerville;
    font-weight: 100;
    font-size: 10em;
    max-width: 42%;
    color: #4A4A4A;
    margin-right: 12%;
}

.explanation {
    /* width: 80%; */
    display: grid;
    grid-template-columns: 1fr;
    margin: 50px auto;
    /* border: 2px solid #000000; */
    background-image: url('../images/dame.png');
    background-repeat: no-repeat;
    background-size: contain;
    background-position: 13% 82%;
    justify-items: end;
}

.explanation p {
    width: 47%;
    margin: 15px 40px;
}


/*vierde section*/

.requirements h3 {
    font-family: fraktur;
    font-weight: 100;
    font-size: 15em;
    color: #4A4A4A;
    margin: 5%;
}

.requirements {
    width: 80%;
    display: grid;
    grid-template-columns: 1fr;
    margin: 50px auto; 
    background-image: url('../images/fin.png');
    background-repeat: no-repeat;
    background-size: 30%;
    background-position: center bottom;
    justify-items: end;*/
}

.requirements p {
    margin: 0 auto;
    column-count: 2;
    column-gap: 50px;
}

.requirements h3::first-letter {
  color:#C54B36;
}
/*einde vierde section*/
<section class="intro" id="zen-intro">
		<header role="banner">
			<h1>CSS Zen Garden</h1>
			<h2>The Beauty of <abbr title="Cascading Style Sheets">CSS</abbr> Design</h2>
		</header>

		<div class="summary" id="zen-summary" role="article">
			<p>A demonstration of what can be accomplished through <abbr title="Cascading Style Sheets">CSS</abbr>-based design. Select any style sheet from the list to load it into this page.</p>
			<p>Download the example <a href="/examples/index" title="This page's source HTML code, not to be modified.">html file</a> and <a href="/examples/style.css" title="This page's sample CSS, the file you may modify.">css file</a></p>
		</div>

		<div class="preamble" id="zen-preamble" role="article">
			<h3>The Road to Enlightenment</h3>
			<p>Littering a dark and dreary road lay the past relics of browser-specific tags, incompatible <abbr title="Document Object Model">DOM</abbr>s, broken <abbr title="Cascading Style Sheets">CSS</abbr> support, and abandoned browsers.</p>
			<p>We must clear the mind of the past. Web enlightenment has been achieved thanks to the tireless efforts of folk like the <abbr title="World Wide Web Consortium">W3C</abbr>, <abbr title="Web Standards Project">WaSP</abbr>, and the major browser creators.</p>
			<p>The CSS Zen Garden invites you to relax and meditate on the important lessons of the masters. Begin to see with clarity. Learn to use the time-honored techniques in new and invigorating fashion. Become one with the web.</p>
		</div>
	</section>

	<div class="main supporting" id="zen-supporting" role="main">
		<div class="explanation" id="zen-explanation" role="article">
			<h3>So What is This About?</h3>
			<p>There is a continuing need to show the power of <abbr title="Cascading Style Sheets">CSS</abbr>. The Zen Garden aims to excite, inspire, and encourage participation. To begin, view some of the existing designs in the list. Clicking on any one will load the style sheet into this very page. The <abbr title="HyperText Markup Language">HTML</abbr> remains the same, the only thing that has changed is the external <abbr title="Cascading Style Sheets">CSS</abbr> file. Yes, really.</p>
			<p><abbr title="Cascading Style Sheets">CSS</abbr> allows complete and total control over the style of a hypertext document. The only way this can be illustrated in a way that gets people excited is by demonstrating what it can truly be, once the reins are placed in the hands of those able to create beauty from structure. Designers and coders alike have contributed to the beauty of the web; we can always push it further.</p>
		</div>

		<div class="participation" id="zen-participation" role="article">
			<h3>Participation</h3>
			<p>Strong visual design has always been our focus. You are modifying this page, so strong <abbr title="Cascading Style Sheets">CSS</abbr> skills are necessary too, but the example files are commented well enough that even <abbr title="Cascading Style Sheets">CSS</abbr> novices can use them as starting points. Please see the <a href="http://www.mezzoblue.com/zengarden/resources/" title="A listing of CSS-related resources"><abbr title="Cascading Style Sheets">CSS</abbr> Resource Guide</a> for advanced tutorials and tips on working with <abbr title="Cascading Style Sheets">CSS</abbr>.</p>
			<p>You may modify the style sheet in any way you wish, but not the <abbr title="HyperText Markup Language">HTML</abbr>. This may seem daunting at first if you&#8217;ve never worked this way before, but follow the listed links to learn more, and use the sample files as a guide.</p>
			<p>Download the sample <a href="/examples/index" title="This page's source HTML code, not to be modified.">HTML</a> and <a href="/examples/style.css" title="This page's sample CSS, the file you may modify.">CSS</a> to work on a copy locally. Once you have completed your masterpiece (and please, don&#8217;t submit half-finished work) upload your <abbr title="Cascading Style Sheets">CSS</abbr> file to a web server under your control. <a href="http://www.mezzoblue.com/zengarden/submit/" title="Use the contact form to send us your CSS file">Send us a link</a> to an archive of that file and all associated assets, and if we choose to use it we will download it and place it on our server.</p>
		</div>

		<div class="benefits" id="zen-benefits" role="article">
			<h3>Benefits</h3>
			<p>Why participate? For recognition, inspiration, and a resource we can all refer to showing people how amazing <abbr title="Cascading Style Sheets">CSS</abbr> really can be. This site serves as equal parts inspiration for those working on the web today, learning tool for those who will be tomorrow, and gallery of future techniques we can all look forward to.</p>
		</div>

		<div class="requirements" id="zen-requirements" role="article">
			<h3>Requirements</h3>
			<p>Where possible, we would like to see mostly <abbr title="Cascading Style Sheets, levels 1 and 2">CSS 1 &amp; 2</abbr> usage. <abbr title="Cascading Style Sheets, levels 3 and 4">CSS 3 &amp; 4</abbr> should be limited to widely-supported elements only, or strong fallbacks should be provided. The CSS Zen Garden is about functional, practical <abbr title="Cascading Style Sheets">CSS</abbr> and not the latest bleeding-edge tricks viewable by 2% of the browsing public. The only real requirement we have is that your <abbr title="Cascading Style Sheets">CSS</abbr> validates.</p>
			<p>Luckily, designing this way shows how well various browsers have implemented <abbr title="Cascading Style Sheets">CSS</abbr> by now. When sticking to the guidelines you should see fairly consistent results across most modern browsers. Due to the sheer number of user agents on the web these days &#8212; especially when you factor in mobile &#8212; pixel-perfect layouts may not be possible across every platform. That&#8217;s okay, but do test in as many as you can. Your design should work in at least IE9+ and the latest Chrome, Firefox, iOS and Android browsers (run by over 90% of the population).</p>
			<p>We ask that you submit original artwork. Please respect copyright laws. Please keep objectionable material to a minimum, and try to incorporate unique and interesting visual themes to your work. We&#8217;re well past the point of needing another garden-related design.</p>
			<p>This is a learning exercise as well as a demonstration. You retain full copyright on your graphics (with limited exceptions, see <a href="http://www.mezzoblue.com/zengarden/submit/guidelines/">submission guidelines</a>), but we ask you release your <abbr title="Cascading Style Sheets">CSS</abbr> under a Creative Commons license identical to the <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/" title="View the Zen Garden's license information.">one on this site</a> so that others may learn from your work.</p>
			<p role="contentinfo">By <a href="http://www.mezzoblue.com/">Dave Shea</a>. Bandwidth graciously donated by <a href="http://www.mediatemple.net/">mediatemple</a>. Now available: <a href="http://www.amazon.com/exec/obidos/ASIN/0321303474/mezzoblue-20/">Zen Garden, the book</a>.</p>
		</div>

enter image description here

答案

看一看:

marking lines on the expected results

  1. 红线将图片分割成具有单色字体的部分。
  2. 蓝色水平线显示文本不居中 - 第一行只是缩进。

h3 {
  font-family: Diplomata;
  font-size: 40px;
  font-weight: normal;
  width: 600px;
  
  text-indent: 1.35em;
  background: linear-gradient(to right, red 1.35em, black 1.35em);
  -webkit-background-clip: text;
  color: white; /* make text readable if clipping is not supported */
  -webkit-text-fill-color: transparent;
}
<link href="https://fonts.googleapis.com/css?family=Diplomata" rel="stylesheet">
<h3>The Road to Enlightenment</h3>
另一答案

好的,不要这样做,但我已经做了一个快速的例子来陪伴我的评论。

我所做的是将3个伪元素中的实际标题分开,其中第2个仅包含我制作红色的字母E.原始文本应该以某种方式崩溃,我在这里选择0px font-size作为在大多数桌面浏览器中都能运行的快速黑客。

这当然是丑陋的,但它对可访问性/搜索引擎优化有效并且不是很糟糕,因为屏幕阅读器不会读取伪元素(或者不应该?),原始标题仍然存在。

.preamble::before {
  color: black;
  content: 'The Road to ';
  font-size: 20px;
}

h3 {
  color: transparent;
  font-size: 0;
  display: inline-block;
}

h3::before {
  color: red;
  content: 'E';
  font-size: 20px;
}

h3::after {
  color: black;
  content: 'nlightenment';
  font-size: 20px;
}
<div class="preamble" id="zen-preamble" role="article">
  <h3>The Road to Enlightenment</h3>
  <p>Littering a dark and dreary road lay the past relics of browser-specific tags, incompatible <abbr title="Document Object Model">DOM</abbr>s, broken <abbr title="Cascading Style Sheets">CSS</abbr> support, and abandoned browsers.</p>
  <p>We must clear the mind of the past. Web enlightenment has been achieved thanks to the tireless efforts of folk like the <abbr title="World Wide Web Consortium">W3C</abbr>, <abbr title="Web Standards Project">WaSP</abbr>, and the major browser creators.</p>
  <p>The CSS Zen Garden invites you to relax and meditate on the important lessons of the masters. Begin to see with clarity. Learn to use the time-honored techniques in new and invigorating fashion. Become one with the web.</p>
</div>
另一答案

:nth-letter - why is it not working?

看起来没有这样的事情:

:nth-letter(10);

编辑:

以下是有人编码的第n个“所有”JavaScript的示例:https://codepen.io/FWeinb/pen/djuIx

我希望在你的项目中你可以添加javascript!

以上是关于css3选择器怎么选择第3个,第6个,第9个,第12个……元素??的主要内容,如果未能解决你的问题,请参考以下文章

,一共10个div,如何用css3选择器选择第4到7之间的div

CSS3 nth-child()选择前几个元素

CSS3 nth-child()选择前几个元素

H5-伪类选择器-伪元素选择器

CSS3小结

算法-选择冒泡插入