This question already has answers here:
Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?
(8 answers)
Closed 5 years ago.
So I have a situation, where I have a list of elements like these:
<li class="a">1</li>
<li class="a">2</li>
<li class="a">3</li>
<li class="b">4</li>
<li class="b">5</li>
<li class="a">6</li>
<li class="a">7</li>
Now what I want to achieve is to use CSS queries to apply red background to the first and last occurrence of a. So in the example above: 1, 3, 6, 7 will have the background color red.
How can this be achieved using css? I have tried using css siblings queries but I can't find a way to determine when the switch from a -> b happens.
.a:nth-child(1) {
background: red none repeat scroll 0 0;
}
.a:nth-child(3) {
background: red none repeat scroll 0 0;
}
.a:nth-child(6) {
background: red none repeat scroll 0 0;
}
.a:nth-child(7) {
background: red none repeat scroll 0 0;
}
<li class="a">1</li>
<li class="a">2</li>
<li class="a">3</li>
<li class="b">4</li>
<li class="b">5</li>
<li class="a">6</li>
<li class="a">7</li>
you just need to change no which one you want to be make red background
.a:nth-child(no will come here ) {
background: red none repeat scroll 0 0;
}
As i understand from your question you need to apply background color red on every occurance first and last child elements. for this please refer the following css code.
.a:nth-child(2n+1), .a:nth-last-child(2) {
background: red;}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am to create a navigation menu using an unordered list in html. I am now trying to style the list to appear on one line, and to have a background colour, but I cannot get the colour to work. I have tried the background: and background-color: to add a background colour to my unordered list, but it doesn't see to appear in my result.
/* styling nav list */
ul#navlist {
display: table;
width: 100%;
list-style: none;
}
#navlist li {
display: table-cell;
text-align: center;
}
li.nav {
background-color: hsla(232°, 38%, 15%, 0.2);
}
<header>
<div class="navlist">
<!-- Unordered list = navigation menu -->
<ul id="navlist">
<li class="nav">Home</li>
<li class="nav">About</li>
<li class="nav">Gallery</li>
<li class="nav">Reviews</li>
<li class="nav">Fun Facts</li>
<li class="nav">News</li>
<li class="nav">Merchandise</li>
</ul>
</div>
</header>
As a last resort, I tried to apply a background colour to each list item which didn't work.
Any ideas on how to fix would be appreciated.
The hsla function's first paramater should not have the degrees symbol (°) on it.
It should just be background-color: hsla(232, 38%, 15%, 0.2);
You can find examples here: https://www.w3schools.com/csSref/func_hsla.asp
This question already has an answer here:
Avoid an element to be split into two columns while using column-count
(1 answer)
Closed 3 years ago.
.main{
column-count: 2;
}
.main .list{
background: #ccc;
padding: 10px;
list-style-type: none;
}
.main .list:hover{
background: #11e;
}
<ul class="main">
<li class="list">list-item-1</li>
<li class="list">list-item-2</li>
<li class="list">list-item-3</li>
<li class="list">list-item-4</li>
<li class="list">list-item-5</li>
<li class="list">list-item-6</li>
<li class="list">list-item-7</li>
</ul>
When I hover on the list-item-4 I can see the hover color covers some section on the second column as well. How can fix it?
You can use break-inside: avoid; to prevent the experienced behaviour.
You do not need to use vendor prefixes as usage of break-inside is already supported by more than 99% of the browsers according to caniuse.com
Please have a look at my JSFiddle to see it working.
This question already has answers here:
CSS selector for first element with class
(23 answers)
Closed 6 years ago.
Maybe this question is ridiculous, but I want to know this fact. I have a li element with two classes (see below), but it is not working as I expected. Does anybody know the reason?
.red:first-child {
color:#F00;
}
.blue:first-child {
color:#00F; /* not working */
}
.red:last-child {
color:#F00; /* not working */
}
.blue:last-child {
color:#00F;
}
<ul>
<li class="red">one</li> <!-- This is the first child of red -->
<li class="red">two</li>
<li class="red">three</li>
<li class="blue">one</li> <!-- and this first child of blue -->
<li class="blue">two</li>
<li class="blue">three</li>
</ul>
As others have mentioned, :first-child is working as expected, as the first child of the parent.
The :first-child selector is used to select the specified selector, only if it is the first child of its parent.
Source: CSS :first-child Selector
You can reach the first .blue like this:
.red + .blue
or if you want to get all the .blue after .red
.red ~ .blue
You might want to use :first-of-type which selects the first of a type but then those .blue would have to be a different HTML element.
div.red:first-of-type {
color:#F00;
}
div.red:last-of-type {
color:#00F;
}
p.blue:first-of-type {
color:#F00;
}
p.blue:last-of-type {
color:#00F;
}
<div>
<div class="red">one</div>
<div class="red">two</div>
<div class="red">three</div>
<p class="blue">one</p>
<p class="blue">two</p>
<p class="blue">three</p>
</div>
Because It consider Li list. First you have to mention first-childs so take first command to show red. THen Take last code as blue last-child so take last code to show blue.
It not consider the red and blue class orders. It consider li list and periorities.
It doesn't work because what you said to the browser was (for the red case, the blue one is similar):
Locate all elements .red.
From that list of .red elements, pick the first one (first-child).
Apply the red color to it.
The browser works as expected and apply the red color to the first element of the list.
EDIT:
The blue case is due to the fact that CSS always looks for the most precise rule. If both rules has the same precision level, the last one wins, so in this case, the last .blue:last-child wins and the .blue:first-child is ignored.
This question already has answers here:
A space between inline-block list items [duplicate]
(8 answers)
Closed 7 years ago.
I'm trying to cover the top of my site with list items.
My list items are appearing like this:
And i don't know why this separation between them. They don't have any margin and padding.
Anyone knows why is this?
HTML:
<ul class="listras-lista">
<li class="listras-lista_item"></li>
<li class="listras-lista_item"></li>
<li class="listras-lista_item"></li>
<li class="listras-lista_item"></li>
<li class="listras-lista_item"></li>
<li class="listras-lista_item"></li>
</ul>
CSS:
.listras-lista {
list-style: none;
width: 100%;
}
.listras-lista_item {
display: inline-block;
background: #CC00CC;
width: 20%;
height: 5px;
padding: 0;
}
It's whitespace that causes the spaces. If you can, change the markup so that </li><li> are together
This is caused by display: inline-block;. Change it to float:left;.
On this website, the menu has the following structure:
<ul>
<li class="page_item page-item-2 current_page_item">
Home
</li>
<li class="page_item page-item-7">
Features
</li>
<li class="page_item page-item-18">
News & Reviews
</li>
<li class="page_item page-item-20">
Contact
</li>
</ul>
The current_page_item class changes the background of the currently selected menu item to black via the following rule:
.mainnav li.current_page_item a {
background: url("images/bg_nav_hover.png") no-repeat scroll right center transparent;
}
This works for the Home menu item, but for all others a small gap is left on the left-hand side of the selected menu item, highlighted by the yellow circle in the image below
I can't seem to figure out why this problem occurs for all menu items except Home.
Your <li> tags are display: inline-block; and there are spaces between the tags.
Because your <li> tags are inline-block, they respect whitespace in the HTML, just like inline elements and text.
In the end, the background image is covering the background of the element correctly. Your best option to get rid of the spacing is to remove all whitespace between </li> and the next <li> tags.
As mentioned by #ajp15243 below, you can omit the closing tag, or use some wacky tricks to get the HTML to swallow up the whitespace.
You should use display:table-cell for .mainnav_wrapper .mainnav ul li class.
then add a padding to center the menu items to your ul element:
.mainnav_wrapper ul {
padding-left:192px;//this is especially for this project
margin: 0 auto;
overflow: hidden;
padding: 0;
text-align: center;
width: 960px;
}
and you are ready to go...