Skip to content

Instantly share code, notes, and snippets.

@umanda
Last active May 26, 2020 10:03
Show Gist options
  • Save umanda/420705a9ea794444a8d820a22d59e4b9 to your computer and use it in GitHub Desktop.
Save umanda/420705a9ea794444a8d820a22d59e4b9 to your computer and use it in GitHub Desktop.
Styling elements based on number of siblings
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<style>
ul {
padding: 0;
}
li {
float: left;
text-align: center;
color: #fff;
font-weight: bold;
background-color: mediumaquamarine;
list-style: none;
border: 2px solid #fff;
padding: 8px;
}
/* one item */
li:first-child:nth-last-child(1) {
/* -or- li:only-child { */
width: 100%;
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
}
/* two items */
li:first-child:nth-last-child(2),
li:first-child:nth-last-child(2) ~ li {
width: 50%;
background: linear-gradient(90deg, #00C9FF 0%, #92FE9D 100%);
}
/* three items */
li:first-child:nth-last-child(3),
li:first-child:nth-last-child(3) ~ li {
width: 33.3333%;
background: linear-gradient(90deg, #FC466B 0%, #3F5EFB 100%);
}
/* four items */
li:first-child:nth-last-child(4),
li:first-child:nth-last-child(4) ~ li {
width: 25%;
background: linear-gradient(90deg, #3F2B96 0%, #A8C0FF 100%);
}
/* five items */
li:first-child:nth-last-child(5),
li:first-child:nth-last-child(5) ~ li {
width: 20%;
background: linear-gradient(90deg, #FDBB2D 0%, #22C1C3 100%);
}
</style>
</head>
<body>
<ul>
<li>A</li>
</ul>
<ul>
<li>A</li>
<li>B</li>
</ul>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
</ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment