How can I dinamically change the nth-child() property in SASS? - css

Good evening,
I'm currently working on a designing project and looking to get something like this : Menu
To do so I used this code in SASS :
&:nth-child(1){
left: 10px;
}
&:nth-child(2){
left: 15px;
}
&:nth-child(3){
left: 30px;
}
&:nth-child(4){
left: 35px;
}
&:nth-child(5){
left: 50px;
}
&:nth-child(6){
left: 55px;
}
&:nth-child(7){
left: 70px;
}
&:nth-child(8){
left: 75px;
}
&:nth-child(9){
left: 50px;
}
&:nth-child(10){
left: 55px;
}
&:nth-child(11){
left: 30px;
}
&:nth-child(12){
left: 35px;
}
&:nth-child(13){
left: 10px;
}
&:nth-child(14){
left: 15px;
}
Right now, it is hard-coded, and the layout is not the same if the numbers of elements from the list is different.
So I'd like to do this dynamically, how can I do so?
I thank you in advance for your help,
Alex

Few exemple that might help you going:
How to create a simple loop with pattern
#for $i from 1 through 20 {
li:nth-child(#{$i}) {
left: 25px + $i * 10px; // could be anything, you need to find the pattern
}
}
Compiles to:
li:nth-child(1) {
left: 35px;
}
li:nth-child(2) {
left: 45px;
}
li:nth-child(3) {
left: 55px;
}
li:nth-child(4) {
left: 65px;
}
li:nth-child(5) {
left: 75px;
}
li:nth-child(6) {
left: 85px;
}
li:nth-child(7) {
left: 95px;
}
li:nth-child(8) {
left: 105px;
}
li:nth-child(9) {
left: 115px;
}
li:nth-child(10) {
left: 125px;
}
li:nth-child(11) {
left: 135px;
}
li:nth-child(12) {
left: 145px;
}
li:nth-child(13) {
left: 155px;
}
li:nth-child(14) {
left: 165px;
}
li:nth-child(15) {
left: 175px;
}
li:nth-child(16) {
left: 185px;
}
li:nth-child(17) {
left: 195px;
}
li:nth-child(18) {
left: 205px;
}
li:nth-child(19) {
left: 215px;
}
li:nth-child(20) {
left: 225px;
}
How to create a loop whith a more complex pattern
#for $i from 1 through 20 {
#if ($i < 11) {
li:nth-child(#{$i}) {
left: 25px + $i * 10px; // could be anything
}
}
#else {
li:nth-child(#{$i}) {
left: (25px + (10 * 10px)) - ((11 - $i) * -1) * 10px; // could be anything
}
}
}
Compiles to:
li:nth-child(1) {
left: 35px;
}
li:nth-child(2) {
left: 45px;
}
li:nth-child(3) {
left: 55px;
}
li:nth-child(4) {
left: 65px;
}
li:nth-child(5) {
left: 75px;
}
li:nth-child(6) {
left: 85px;
}
li:nth-child(7) {
left: 95px;
}
li:nth-child(8) {
left: 105px;
}
li:nth-child(9) {
left: 115px;
}
li:nth-child(10) {
left: 125px;
}
li:nth-child(11) {
left: 125px;
}
li:nth-child(12) {
left: 115px;
}
li:nth-child(13) {
left: 105px;
}
li:nth-child(14) {
left: 95px;
}
li:nth-child(15) {
left: 85px;
}
li:nth-child(16) {
left: 75px;
}
li:nth-child(17) {
left: 65px;
}
li:nth-child(18) {
left: 55px;
}
li:nth-child(19) {
left: 45px;
}
li:nth-child(20) {
left: 35px;
}
How to create a loop from a list of values
$fooList : 10px, 20px, 30px, 0px, 200px, 80px, -10px;
#for $i from 1 through length($fooList) {
li:nth-child(#{$i}) {
left: nth($fooList, $i);
}
}
Compiles to:
li:nth-child(1) {
left: 10px;
}
li:nth-child(2) {
left: 20px;
}
li:nth-child(3) {
left: 30px;
}
li:nth-child(4) {
left: 0px;
}
li:nth-child(5) {
left: 200px;
}
li:nth-child(6) {
left: 80px;
}
li:nth-child(7) {
left: -10px;
}

Related

How do I shorten this CSS which has CSS Pseudo-elements using SCSS?

How do I shorten this CSS which has CSS Pseudo-elements using SCSS?
.bar-color1 {
background: #28A745;
position: absolute;
top: 0; bottom: 0; left: 0;
&::after {
color: #28A745;
content: url("../../assets/images/arrow.png");;
display: inline-block !important;
width: 0;
height: 0;
position: absolute;
right: 6px;
top: -6px;
}
}
.bar-color2 {
background: #28A745;
position: absolute;
top: 0; bottom: 0; left: 0;
&::after {
content: "" !important;
}
}
As you can see both has these common styles
background: #28A745;
position: absolute;
top: 0; bottom: 0; left: 0;
I think it's better to have a parent class like bar like bellow:
.bar {
background: #28A745;
position: absolute;
top: 0; bottom: 0; left: 0;
&-color1::after {
color: #28A745;
content: url("../../assets/images/arrow.png");;
display: inline-block !important;
width: 0;
height: 0;
position: absolute;
right: 6px;
top: -6px;
}
&-color2::after {
content: "" !important;
}
}
You can use multiple selector:
.bar-color1, .bar-color2 {
background: #28A745;
position: absolute;
top: 0; bottom: 0; left: 0;
}
CSS
.bar-color1, .bar-color2 {
background: #28A745;
position: absolute;
top: 0; bottom: 0; left: 0;
}
.bar-color1 {
&::after {
color: #28A745;
content: url("../../assets/images/arrow.png");;
display: inline-block !important;
width: 0;
height: 0;
position: absolute;
right: 6px;
top: -6px;
}
}
.bar-color2 {
&::after {
content: "" !important;
}
}
Use mixins:
#mixin customStyle {
background: #28A745;
position: absolute;
top: 0; bottom: 0; left: 0;
}
Then use it like so:
.bar-color1 {
#include customStyle();
}
.bar-color2 {
#include customStyle();
}
More on mixins here.

CSS Hover effect doesn't work when implemented [duplicate]

This question already has answers here:
Why do the :before and :after pseudo-elements require a 'content' property?
(5 answers)
Align inline-block DIVs to top of container element
(5 answers)
Closed 2 years ago.
i have troubleshooting with hover effects.
I'm working on a welcome webpage.
First i made an item list with HOVER effects :
body {
background-color: #E0105F;
}
.welcome-menu-box{
position: absolute;
right: 10%;
bottom: 10%;
}
.welcome-menu {
display: inline-flex;
justify-content: center;
align-items: center;
position: relative;
margin: 10px 10px;
width: 80px;
height: 80px;
background: #0D1033;
border-radius: 100px;
font-family: 'Montserrat', sans-serif;
color: #fff;
text-align: center;
font-size: 10px;
font-weight: lighter;
letter-spacing: 2px;
transition: 1s box-shadow;
}
.welcome-menu:hover {
box-shadow: 0 5px 35px 0px rgba(0,0,0,.1);
}
.welcome-menu:hover::before, .welcome-menu:hover::after {
content: '';
position: absolute;
width: 80px;
height: 80px;
background: #10E091;
border-radius: 100px;
z-index: -1;
animation: 1s clockwise infinite;
}
.welcome-menu:hover:after {
background: #EADCB5;
animation: 2s counterclockwise infinite;
}
#keyframes clockwise {
0% {
top: -5px;
left: 0;
}
12% {
top: -2px;
left: 2px;
}
25% {
top: 0;
left: 5px;
}
37% {
top: 2px;
left: 2px;
}
50% {
top: 5px;
left: 0;
}
62% {
top: 2px;
left: -2px;
}
75% {
top: 0;
left: -5px;
}
87% {
top: -2px;
left: -2px;
}
100% {
top: -5px;
left: 0;
}
}
#keyframes counterclockwise {
0% {
top: -5px;
right: 0;
}
12% {
top: -2px;
right: 2px;
}
25% {
top: 0;
right: 5px;
}
37% {
top: 2px;
right: 2px;
}
50% {
top: 5px;
right: 0;
}
62% {
top: 2px;
right: -2px;
}
75% {
top: 0;
right: -5px;
}
87% {
top: -2px;
right: -2px;
}
100% {
top: -5px;
right: 0;
}
}
<container class="welcome-menu-box">
<div class="welcome-menu">ABC</div>
<div class="welcome-menu">ABCD ABCDEF</div>
<div class="welcome-menu">ABCDEFGHIJ</div>
<div class="welcome-menu">ABCDEFGHI</div>
</container>
And I tried to implement it in a specific DIV on a webpage :
/* Div responsive encadrée pleine page avec arrière plan couleur + image png centrée et répétée depuis le centre */
.welcome-menu-box{
position: absolute;
right: 10%;
bottom: 10%;
vertical-align: middle;
}
.welcome-menu {
display: inline-flex;
justify-content: center;
text-align: center;
align-items: center;
position: relative;
margin: 10px 10px;
width: 80px;
height: 80px;
background: #0D1033;
border-radius: 100px;
font-family: 'Montserrat', sans-serif;
color: #fff;
font-size: 10px;
font-weight: lighter;
letter-spacing: 1px;
transition: 1s box-shadow;
}
.welcome-menu:hover {
box-shadow: 0 5px 35px 0px rgba(0,0,0,.1);
}
.welcome-menu:hover::before, .welcome-menu:hover::after {
position: absolute;
width: 75px;
height: 75px;
background: #10E091;
border-radius: 100px;
z-index: -1;
animation: 1s clockwise infinite;
}
.welcome-menu:hover:after {
background: #EADCB5;
animation: 2s counterclockwise infinite;
z-index: -1;
}
#keyframes clockwise {
0% {
top: -5px;
left: 0;
}
12% {
top: -2px;
left: 2px;
}
25% {
top: 0;
left: 5px;
}
37% {
top: 2px;
left: 2px;
}
50% {
top: 5px;
left: 0;
}
62% {
top: 2px;
left: -2px;
}
75% {
top: 0;
left: -5px;
}
87% {
top: -2px;
left: -2px;
}
100% {
top: -5px;
left: 0;
}
}
#keyframes counterclockwise {
0% {
top: -5px;
right: 0;
}
12% {
top: -2px;
right: 2px;
}
25% {
top: 0;
right: 5px;
}
37% {
top: 2px;
right: 2px;
}
50% {
top: 5px;
right: 0;
}
62% {
top: 2px;
right: -2px;
}
75% {
top: 0;
right: -5px;
}
87% {
top: -2px;
right: -2px;
}
100% {
top: -5px;
right: 0;
}
}
.encadrement {
background: url('https://www.agence-mibe.com/CODEPEN/GROSMYO-fullresponsive.png') repeat center center fixed, #E0105F;
position: absolute;
top: 0;
left: 0;
border: 75px solid #fff;
width: 100%;
height: 100%;
box-sizing: border-box;
background-size: auto 70%;
}
#media screen and (max-width: 1200px) {
.encadrement {
border: 50px solid #fff;
}
#media screen and (max-width: 768px) {
.encadrement {
border: 25px solid #fff;
background-size: 70% auto;
}
<section class="encadrement">
<container class="welcome-menu-box">
<div class="welcome-menu">ABC</div>
<div class="welcome-menu">ABCD ABCDEFG</div>
<div class="welcome-menu">ABCDEFGHIJ</div>
<div class="welcome-menu">ABCDEFG</div>
</container>
</section>
I have two problems that I'm not able to solve :
• The second item which contains two world has a vertical misalignment due to the occupied space
• The hover effect doesn't work on the web page, but works when alone
If somebody could give me a hint it would be wonderful :-)

Mixin for data-overlay selector

Is there a way to convert the following CSS rules to a Mixin or make it a bit more SCSS?
[data-overlay]:before {
content: '';
position: absolute;
background: #000;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 0;
}
[data-overlay="0"]:before {
opacity: 0;
}
[data-overlay="1"]:before {
opacity: 0.1;
}
[data-overlay="2"]:before {
opacity: 0.2;
}
Sassmeister demo.
Sass for loop.
#mixin overlays($count: 0) {
[data-overlay]:before {
content: '';
position: absolute;
background: #000;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: 0;
}
#for $i from 0 through $count {
[data-overlay="#{$i}"]:before {
opacity: $i / 10;
}
}
}
#include overlays(4);

Menu at the end of the page but link from top to bottom

How can I achieve this?
It's a wordpress menu.
pic
Ok, so far I have this:
ul{
display: flex;
li{
width: 100%;
a{
position: absolute;
bottom: 0;
top: 0;
width: 2%;
span{
display: block;
position:absolute;
top: 50%;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
};
};
};
};
and it works, but now I need some kind of a javascript, so that the headings inside the span will be centered and every element will have the same amount of space from left and right.
pic2
got it done, with adding left: %; to each menu item element.
Thanks for help!
Made it like this:
fiddle: https://jsfiddle.net/uq3vLvpb/
The html:
<ul>
<li><a><span>one</span></a></li>
<li><a><span>two</span></a></li>
<li><a><span>three</span></a></li>
<li><a><span>four</span></a></li>
<li><a><span>five</span></a></li>
<li><a><span>six</span></a></li>
<li><a><span>seven</span></a></li>
<li><a><span>eight</span></a></li>
</ul>
The css:
ul {
li {
width: 100%;
a {
position: absolute;
bottom: 0;
top: 0;
width: 10.5%;
font-family: 'Lora', serif;
font-size: 26px;
font-weight: 400;
text-align: center;
color: #ffffff;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
text-decoration: none;
padding: 0 15px;
cursor: pointer;
span {
display: block;
position: absolute;
top: 65%;
left: 50%;
transform: translate(-50%, -35%);
-webkit-transform: translate(-50%, -35%);
-moz-transform: translate(-50%, -35%);
}
&:hover {
background-color: rgba(0, 0, 0, 0.4);
}
}
}
li:first-child {
a {
left: 9.5%;
}
}
li:nth-child(2) {
a {
left: 19.5%;
}
}
li:nth-child(3) {
a {
left: 29.6%;
}
}
li:nth-child(4) {
a {
left: 39.6%;
}
}
li:nth-child(5) {
a {
left: 49.7%;
}
}
li:nth-child(6) {
a {
left: 59.8%;
}
}
li:nth-child(7) {
a {
left: 69.9%;
}
}
li:nth-child(8) {
a {
left: 80%;
}
}
}

Z-index issue using transform scale

I have a codepen below. Basically, I'm trying to show a popup on hover of the highlighted circles (red), however some highlighted circles are showing up above some of the popups, even when the popups are always given a higher z-index all the time.
http://codepen.io/Wolfmans55/pen/jPwKqZ
This is the animation used for the popup, which I believe maybe the culprit.
#-webkit-keyframes popup {
0% {
transform: scale(2.5);
}
100% {
transform: scale(0);
}
}
Take out the z-index setting on .white-popup and set a higher z-index on .white-popup:hover .popup and .white:hover.
see jsfiddle
#import url(http://fonts.googleapis.com/css?family=Roboto);
body,
html {
height: 100%;
}
body {
background: #343837;
transition: opacity .25s;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
overflow: hidden;
}
.show-body {
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
}
.page {
background: #343837;
font-family: 'Roboto', sans-serif;
font-size: 100%;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
transition: all 1s;
}
.page-2 {
left: auto;
right: -100%;
background: blue;
}
.prev-page,
.next-page {
position: absolute;
top: 50%;
width: 50px;
height: 50px;
background: red;
}
.next-page {
right: 20px;
}
.prev-page {
left: 20px;
}
.page-wrapper {
position: absolute;
top: 50%;
margin-top: -303px;
width: 100%;
}
.header_section {
text-shadow: 1px 1px 1px #000;
font-size: 2em;
}
h1 {
margin: 0;
}
.blue_title {
color: #54c8e7;
font-weight: normal;
}
.primary_title {
font-size: 4em;
margin-top: -34px;
margin-bottom: 20px;
}
.sub_title {
position: relative;
font-size: 1em;
background: #343837;
padding: 0 4px;
display: inline-block;
}
.sub_title_divider {
border-top: 1px solid #54c8e7;;
position: absolute;
top: 20px;
left: 0;
width: 100%;
}
.city-name {
font-size: 2em;
}
.emphasis {
font-weight: bold;
}
.inlineblock {
display: inline-block;
}
.centertxt {
text-align: center;
}
.pos_rel {
position: relative;
}
.overflow_hidden {
overflow: hidden;
}
.vert_mid {
vertical-align: middle;
}
table {
cursor: pointer;
margin: 0 auto;
border-spacing: 1px;
}
td {
width: 10px;
height: 10px;
border-radius: 50%;
}
.white,
.white-popup {
background: #000;
position: relative;
-webkit-animation: in .25s;
}
.white-popup {
background: red;
}
/*.plan-to {
background: #018c9e;
z-index: 2;
}*/
.white-popup .popup {
cursor: auto;
-webkit-animation: popup .25s forwards;
z-index: 10;
}
.popup {
position: absolute;
width: 100px;
height: 100px;
font-size: 5px;
overflow: hidden;
background: #54c8e7;
z-index:10;
top: -45px;
left: -45px;
padding: 8px;
border: 1px solid #343837;
border-radius: 3px;
box-sizing: border-box;
}
.white-popup:hover .popup {
-webkit-animation: out .25s forwards;
z-index: 50;
}
.white:hover {
-webkit-animation: out .25s forwards;
z-index: 50;
}
#-webkit-keyframes in {
from {background:#fff; transform: scale(1);}
to {background: #54c8e7; transform: scale(2.5);}
}
#-webkit-keyframes out {
0% {background:#fff; transform: scale(1);}
100% {background: #54c8e7; transform: scale(2.5);}
}
#-webkit-keyframes popup {
0% {
transform: scale(2.5);
}
100% {
transform: scale(0);
}
}
.key {
height: 15px;
width: 15px;
border-radius: 50%;
border: 1px solid #ccc;
margin-right: 2px;
}
.key_1 {
background: #54c8e7;
}
.key_2 {
background: #018c9e;
}
.key_3 {
background: #fff;
}
.legend {
text-transform:uppercase;
font-size: 12px;
color: #fff;
margin-bottom: 30px;
}
.legend_item {
margin-left: 15px;
}
#media screen and (min-width: 768px) {
.page-wrapper {
margin-top: -388px;
}
td {
width: 15px;
height: 15px;
}
}
I added the following CSS:
td:hover {
z-index: 1;
}
And inside .white-popup selector, I removed:
z-index: 2;
line.
Seems to work for me.

Resources