How to properly position animated numbers in a stacked css flexbox? - html

I am trying to display animated text in a css flexbox. It should be displayed "30,538+" on first line and "Leggs Broken" on second line (dummy text). Both should be stacked in the center of the page (I have other text above and below it). The numbers are animated on hover with just CSS.
.legg-section {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
flex-direction: row-reverse;
width: 100%;
text-align: center;
background-color: #ffffff;
}
.legg-desc {
color: #ae95da;
padding-top: 50px;
align-items: center;
}
/* number animation */
.legg body {
background: #fff;
margin: 0;
padding: 0;
}
.legg ul {
padding: 10px;
margin: 0;
display: flex;
position: absolute;
top: 30%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
margin-top: 225px;
}
.legg ul li {
list-style: none;
color: #ae95da;
float: left;
font-size: 0.5em;
font-family: 'Poppins', sans-serif;
transition: 0.9s;
font-weight: 400;
}
.legg ul:hover li {
transform: rotateY(360deg);
}
.legg ul:hover li:nth-child(1) {
transition-delay: 0.9s;
}
<div class="legg-section">
<h1 class="legg">
<ul>
<li>3</li>
<li>0</li>
<li>,</li>
<li>5</li>
<li>3</li>
<li>8</li>
<li>+</li>
<div style="clear: both"></div>
</ul>
</h1>
<h2 class="legg-desc">Leggs Broken</h2>
</div>
The first problem I am having is that the numbers jump up or down when the screen is resized. I am having trouble pinning it to a specific spot on the page. Second thing is to have it expand and shrink properly on screen size (a little buggy now).
I spent many hours trying to fix and troubleshoot this but nothing worked. Any help or suggestions would be very much appreciated!

I've tried to keep the style as much the same as possible. Basically, any positioning and floating has been removed. The rest is pretty straightforward.
.legg-section {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
}
.legg-desc {
color: #ae95da;
}
ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
}
ul li {
color: #ae95da;
font-family: 'Poppins', sans-serif;
transition: 0.9s;
font-weight: 400;
}
/* number animation */
ul:hover li {
transform: rotateY(360deg);
}
ul:hover li:nth-child(1) {
transition-delay: 0.9s;
}
<div class="legg-section">
<ul>
<li>3</li>
<li>0</li>
<li>,</li>
<li>5</li>
<li>3</li>
<li>8</li>
<li>+</li>
</ul>
<h2 class="legg-desc">Leggs Broken</h2>
</div>

Related

Make an element move by hovering over another element

I would like to hide the nav element totally from the user. When the user hovers over an icon (in this case, an image), the nav element should slide in from the top of the screen. I thought about a countdown of 0.5 seconds before the animation starts, then 1 second when the nav element moves from its initial position (above the screen) to its final position (at the top of the screen). The nav bar should stay there until the user stops hovering over the icon or the nav bar.
The problem is that my nav element appears in its final position when the icon is hovered - it does not move as it should do. From tests I've done so far, it seems that I cannot move an element when hovering over another, but I hope I'm wrong and that it's possible - I explored an option but it ran the animation in a loop and I could only get it working once, nor moving from location A to location B as it was moving from A to B to A. I tried using margins, paddings, changing height, I browsed many topics from here and other websites, but I could not find a way, probably because my code is a mess. I am learning JS so I am open to all solutions.
Here is my code, as cleaned as possible:
<header>
<div class="thing">
<img src="img/mainicon.png" class="mainicon">
<nav>
<ul>
<li><a class="menu" href="#">Half-Life</a></li>
<li><a class="menu" href="#">Half-Life 2</a></li>
<li><a class="menu" href="#">Half-Life Alyx</a></li>
</ul>
</nav>
</div>
<div class="clean"></div>
</header>
.mainicon {
width: 8%;
margin-left: 2%;
margin-top: 2%;
float: left;
}
.mainicon:hover + nav {
display: flex;
margin-top: 0px;
}
nav:hover {
display: flex;
margin-top: 0px;
}
.clean{
clear: both;
content: ".";
display: block;
height: 0;
line-height: 0;
visibility: hidden;
}
nav {
height: 100px;
display: none;
justify-content: center;
align-items: center;
border: 4px solid #2CBCD6;
background-color: grey;
text-align: center;
}
ul {
padding: 0;
width: 100%;
}
li {
display: inline;
font-size: 25px;
}
a {
outline: none;
text-decoration: none;
display: inline-block;
width: 20%;
margin-right: 0.625%;
text-align: center;
line-height: 3;
color: black;
}
It's simpler than you think. Don't complicate it for yourself.
In short, all you need to do is set the margin-top to something negative to hide it and then margin-top to 0 when hovering over the image or the menu.
Here is a demonstration:
body {
margin: 0;
background-color: aqua;
}
.thing {
display: flex;
align-items: center;
}
.mainicon {
position: fixed;
margin-top: 0px;
top: 10;
left: 60px;
height: 40px;
width: auto;
object-fit: contain;
background-color: bisque;
}
.mainicon:hover + nav,
nav:hover {
margin-top: 0px;
}
nav {
background-color: grey;
display: flex;
height: 60px;
width: 100%;
justify-content: center;
align-items: center;
margin-top: -60px;
transition: all 0.3s ease-in-out;
}
ul {
padding: 0;
margin: 0;
display: flex;
}
li {
list-style-type: none;
width: 100px;
}
a {
outline: none;
text-decoration: none;
}
<header>
<div class="thing">
<img
src="https://cdn1.iconfinder.com/data/icons/heroicons-ui/24/menu-512.png"
class="mainicon"
/>
<nav>
<ul>
<li><a class="menu" href="#">Half-Life</a></li>
<li><a class="menu" href="#">Half-Life 2</a></li>
<li><a class="menu" href="#">Half-Life Alyx</a></li>
</ul>
</nav>
</div>
</header

CSS border bottom only on one side

I am trying to use the css border-bottom property with a circle in between. Something like this :
what I want
But, for the first and last circles I only want it to line to be inclusive within the borders but its extending to the ends like this.
result of what I tried with normal css
This is the css I used:
.horizontalLineComplete{
width: 100%;
border-bottom: 4px solid #26890D;
height:20px;
}
.horizontalLineCurrent{
width: 70%;
border-bottom: 4px solid #63666A;
height:20px;
}
I tried using the li:: before and ::after selector classes as well but that also hasn't worked it just shows up the lines between the circles but the colors I assign aren't working accurately. It takes black color by default like this: result for what I tried with selector classes
This is the css I gave:
li.circleComplete::before
{
content: "";
flex: 1 1;
border-bottom: 2px solid #26890D;
margin: auto;
}
li.circleComplete::after {
content: "";
flex: 1 1;
border-bottom: 2px solid #26890D;
margin: auto;
}
li.circleNext::before
{
content: "";
flex: 1 1;
border-bottom: 2px solid #63666A;
margin: auto;
}
li.circleNext::after {
content: "";
flex: 1 1;
border-bottom: 2px solid #63666A;
margin: auto;
}
Can someone help me out on how I can adjust this or let me know if I am making any mistakes in the code? I am using react and typescript for my front end with scss.
This is one of solution how to fix your code.
$('.active').html("&#10003");
$('#goNext').on('click', function() {
$('ul>li.active').removeClass('active').next('li').addClass('active');
$("ul>li.active").html("&#10003")
});
li {
width: 2em;
height: 2em;
text-align: center;
line-height: 2em;
border-radius: 1em;
background: #45ad66;
margin: 0 1em;
display: inline-block;
color: white;
position: relative;
}
li::before{
content: '';
position: absolute;
top: .9em;
left: -4em;
width: 4em;
height: .2em;
background: #45ad66;
z-index: -1;
transition: all 1s;
}
li:first-child::before {
display: none;
}
.active {
background: #3f995b;
transition: all 1s;
}
.active ~ li {
background: gray;
}
.active ~ li::before {
background: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
</ul>
<button id="goNext">
Go to next
</button>
UPDATED code
I find it easier to put the central line as a background image (via linear-gradient) on the ul element itself.
This snippet sets the ul to display inline-flex and gets the circles (the li elements) spaced out evenly with the first at the left side and the last at the right side by using the space-between justification property.
This way you don't have to do lots of positioning.
The tick is put on via a content in a pseudo element for each li as I assume it is just for decoration rather than as actual content.
ul {
margin: 0;
padding: 0;
list-style-type: none;
display: inline-flex;
width: 100%;
background-image: linear-gradient(#45ad66, #45ad66);
background-size: 100% 2px;
background-position: center center;
background-repeat: no-repeat;
justify-content: space-between;
}
li::after {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
content: '✔';
color: white;
font-size: 3vmin;
}
li {
display: inline-block;
position: relative;
background-color: #45ad66;
width: 6vmin;
height: 6vmin;
border-radius: 50%;
transition: all 1s;
}
li.active,
li:hover {
background-color: #3f995b;
}
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
Note: dimensions are in terms of vmin so that the whole thing is responsive but of course you are free to change those if required.
Just so you can see the 'active' effect transitioning I've put the active color as the color for a hover on an li element as well. Also moved the transition to the element itself so the color transitions both in and out.

Align list items by their bounding rect

I have a list that uses display: inline-flex. The font-sizes of different items in the list are different. I discover that the list items are aligned according to the position of the bottom of the text. This makes the items with the smaller text sizes to appear lower than the others.
However, I want to align the items by the base of their bounding rects.
I have have found a hack to resolve this: I place a span containing a dot either side of the text I want to show, I set the font-size of this dot to size of the largest font-size in the list, and I set the visibility of these spans to hidden.
Is there a non-hacky, pure CSS way to achieve this?
JSFiddle
body {
margin: 0;
}
ul {
padding: 0;
margin: 0;
}
li {
display: inline-flex;
border: 1px solid #000;
justify-content: center;
align-items: center;
width: 64px;
height: 64px;
font-size: 64px;
line-height: 64px;
}
li:last-child {
font-size: 16px;
}
span {
font-size: 64px;
visibility: hidden;
}
hr {
position: absolute;
top: 47px;
width: 204px;
}
<ul>
<li>1</li>
<li>2</li>
<li>THREE</li>
</ul>
<hr>
<ul>
<li>1</li>
<li>2</li>
<li><span>.</span>THREE<span>.</span></li>
</ul>
You can achieve the alignment you want by adding display: flex to the container, in this case the ul element.
fiddle
body {
margin: 0;
}
ul {
padding: 0;
margin: 0;
display: flex;
}
li {
display: inline-flex;
border: 1px solid #000;
justify-content: center;
align-items: center;
width: 64px;
height: 64px;
font-size: 64px;
line-height: 64px;
}
li:last-child {
font-size: 16px;
}
<ul>
<li>1</li>
<li>2</li>
<li>THREE</li>
</ul>

Flex nav not aligning

Okay so I'm in the process of learning flexbox but I cannot understand why my navigation title is above the links.
HTML:
<style>
#import url(https://fonts.googleapis.com/css?family=Oswald:400,700);
.box {
display: flex;
}
.item {
width: 100px;
height: 100px;
background-color: red;
margin: auto;
color: #fff;
}
nav {
font-family: "Oswald", sans-serif;
display: flex;
min-width: 100%;
background-color: #181818;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
max-width: 960px
}
nav a {
display: block;
padding: 1rem;
text-decoration: none;
color: #fff;
font-weight: 400;
transition: all 0.3s ease-in-out;
}
nav a:hover {
color: #343434;
}
.title {
margin: 0 35px 0 10px;
color: #1BC;
}
</style>
<nav>
<div class="container">
<a class="title">Architect</a>
<ul>
<li>Getting Started</li>
<li>Examples</li>
<li>Blog</li>
<li>Forum</li>
</ul>
</div>
</nav>
Container CSS:
.container{
width: 100%;
margin: 0 auto:
max-width: 1200px;
}
Code Pen Link: http://codepen.io/ZoidCraft/pen/XKMewy
I would like the title "Architect" to be align to the left of the links.
You set <a class="title">Architect</a> to display: block; in your css. Block level elements will take up their own line. display: flex; elements will also take up their own line.
To fix your problem you could first remove that display: block; from your nav a style. Then change your nav ul from display: flex; to display: inline-flex;. Now you just need to add some padding back to your nav since everything is display inline now, so add padding: 1em 0; to your nav
Here is an updated CodePen of what I am talking about.

Centering a child of an absolute position div tag

Trying to position a horizontal menu over an image.
I have it positioned over the image as of now, but I can't seem to center the li elements to the middle of the screen.
Here is the HTML:
<div id="banner">
<img src="security-people-interior.jpg"/>
</div>
<div id="menu-outer">
<div id="menu">
<ul id="horizontal-list">
<li>About</li>
<li>Products</li>
<li>Monitoring</li>
<li>Testimonials</li>
<li>FAQ</li>
<li>Contact</li>
</ul>
</div>
</div>
The CSS3:
#banner {
display: block;
margin-left: auto;
margin-right: auto;
}
img, #banner {
width: 100%;
}
#menu-outer {
background: url(images/bar-bg.jpg) repeat-x;
z-index: 100;
position: absolute;
top: 25%;
width: 100%;
}
#menu {
????
}
ul#horizontal-list {
/*list-style: none;*/;
}
ul#horizontal-list li {
display: inline;
}
#menu a {
text-decoration: none;
margin: 10px;
float: left;
color: black;
padding: 2px 5px;
text-align: center;
white-space: nowrap;
}
#menu a:hover {
color: #fff;
background: red;
}
The only thing I am able to use is float in #menu{} in the css, but obviously float doesn't have centering.
CSS3 isn't my particular favourite to deal with, so any help would be greatful thanks.
Here are two options:
Set the display of the #horizontal-list element to inline-block and then use text-align: center on the parent element in order to center it.
Example Here
#menu {
text-align: center;
}
#horizontal-list {
display: inline-block;
}
Alternatively, using flexboxes:
Example Here
#menu {
display: flex;
justify-content: center;
}