Conditional styles in a Navigation Bar - html

I'm currently attempting a do very specific kind of navigation bar for my website. The main 'feature' of it is that on hover over each menu item, the item itself grows, while both the item before and the item after moves x pixels away from the item.
The styling of these elements is what would perform these transformations, using the following:
.navitem{
display:inline;
transition: transform .75s ease-in-out;
}
.navitemRight {
transform: translate(10px);
}
.navitemLeft{
transform: translate(-10px);
}
.navitemCenter{
text-shadow:0px 0px 1px black;
transform: scale(1.20);
}
The problem is that there seems to be no way to apply these style whenever an item is hovered. It seems as though for each item, they should have these kind of stylings :
.item2 : hover ~ item 1{
blabla
}
.item2 : hover ~ item 3{
blabla
}
but they should generated somehow, since the navigation bar's items are dynamic.
I tried using React and really thought I was going to get away with it, where onMouseEnter and onMouseLeave changed the state of the component, and everytime, it would re-render the navbar, with the correct style, like so:
almost working but ugly when hover out
But it does not satisfy me because we only get the transform when hovering, and lose the transition whenever the state changes, which is why the change is so ugly when I hover out. For reference, here's an example of what it looks like when you have the same trasnform and transition:
simple transform
Anyways, I am sure there is a way to do it, using Javascript probably, maybe sass or Jquery, both of which im not really familiar with. If you have any idea, or maybe a reference to tools that could help me with that, it would be very apprecited!Thanks

You can apply conditional classes on each element, based on your component state. The conditional classes will be added/removed based on template string literals

Here's an example that does basically what you want, but through a different method. It's all in CSS so it will not have and rendering issues.
I've moved all .nav-links to the left 10px if they aren't being hovered (this is therefore their default state).
Once you hover on one this will increase in size by 1.2 as you would like.
All subsequent .nav-links after the hovered one (using the subsequent sibling selector ~) will move to the right by 10px to accomodate the increase in size.
You can fine tune this to your preferences, but at least it gives you a nice structure to work from and looks reasonably slick I think!
Demo
nav {
padding: 0px 24px;
}
.nav-link {
display: inline-block;
transition: transform .75s ease-in-out;
background: whitesmoke;
height:20px;
width:40px;
padding: 4px 8px;
text-align: center;
}
.nav-link:not(:hover) {
transform: translate(-10px);
}
.nav-link:hover {
transform: scale(1.2);
}
.nav-link:hover ~ .nav-link {
transform: translate(10px);
}
<nav>
<a class='nav-link'>Link</a>
<a class='nav-link'>Link</a>
<a class='nav-link'>Link</a>
<a class='nav-link'>Link</a>
</nav>

Related

Multiple :not pseudos not working as intended

A normal website menu with decently sized buttons comprised of centred listed text inside blocks with background-color.
I need the buttons to change their background-color when hovered over, when pressed, and when the user is on the respective page.
HTML of the menu:
<div class="box box-menu">
<ul class="nav">
<li class="button button-activated">Home</li>
<li class="button">Gallery</li>
<li class="button">Commission Us</li>
<li class="button">Official Staff</li>
<li class="button">FAQ / TOS</li>
<li class="button button-last">Contacts</li>
</ul>
and its stylesheet:
.box-menu{
position: relative;
float: left;
width: 200px;
}
.nav{
text-align: center;
}
.button{
height: 70px;
list-style-type: none;
background-color: #141414;
color: #e8a53c;
font-size: 20px;
line-height: 60px;
vertical-align: middle;
text-shadow: 2px 2px black;
margin-bottom: 10px;}
.button-last{
margin-bottom: 0;
}
.button:hover:not(.button-activated):not(.button:active) {
background-color: #141414;
color: #901313;}
.button:active{
background-color: #761111;
}
.button-activated{
background-color: #901313;
color: #e8a53c;
}
:active is for the button as its being clicked.
button-activated is the class for the currently selected page.
A little needlessly confusing, but whatever.
I want the Activated button to NOT change color when its being hovered over.
I want the rest of the buttons to change color when they're being hovered over.
When the user presses a button, I want its :active (being clicked on) background-color - NOT its :hover background-color (since the user has his mouse on top of the button as he's pressing it).
One way I thought of resolving it was to use :not pseudos, but whenever I write multiple of them in one line the whole thing stops working altogether... It should work like:
"Button should change bg-color on hover - UNLESS it's being clicked on, and UNLESS it's specified as the page you're on."
Am I doing something wrong? Do I need to write them separately, one :not at a time?
The problem is not that you have multiple :not(), but that your second one is invalid.
In current CSS Selectors Level 3 specification1, not() only accepts simple selectors, and .button:active is a complex selector so your not(.button:active) is invalid and the whole rule is discarded.
But in your case, you really don't need that complex selector here, the simple :active will do, which gives you .button:hover:not(.button-activated):not(:active).
Here is a much simpler example still exposing the issue:
.foo:not(.bar):not(.foo:hover) { /* does not work */
color: red;
}
.foo:not(.bar):not(:hover) { /* does work */
background-color: green;
}
<div class="foo">hover me to remove stylings</div>
1 - Though next version 'CSS Selectors Level 4' now makes not()'s param a selector-list, so if I got it right, we should soon be able to pass complex selectors here.
With multiple css classes it's better to list with comma separation, rather than chaining them, as you've done with your pseudo-classes here.
.button:hover:not(.button-activated),
.button:hover:not(.button:active) {
background-color: #141414;
color: #901313;
}
However, with more complex classes and code in general, descriptive is often more useful than efficiency. Separating these own might be the way to go and still be functional, after having plugged this into a CodePen:
.button:hover:not(.button-activated) {
background-color: #141414;
color: #901313;
}
.button:hover:not(.button:active) {
background-color: #141414;
color: #901313;}

Need CSS transition scale to push text out of the way responsively

I need the below code to push surrounding text out of the way.
The CSS transition currently expands and ignores the surrounding text going over it if the transition is to large. I need a good way of having the text around the transition to expand and move out of the way.
I know that it would be possible to use margin/padding and multiple a classes to get each one to push the right amount around but I was hoping for a better / more responsive option.
If any more information is needed please ask!
a:link {
color: #000;
text-decoration: none;
transition: all .2s ease-in-out;
display: inline-block;
}
a:hover {
color: #000;
text-decoration: none;
transform: scale(1.1);
padding: 5px;
}
<p>Check out our website and if you have a question just click the 'Contact Us' link and we will do our best to respond with an answer. Thank You.</p>

Navbar fade out issue

This is really a matter of me messing around in the CSS, and ending up unknowingly deleting or overriding something. When I hover over the nav link which drops down a list, it fades in. However, it doesn't fade out when it disappears after moving the mouse away.
Link to fiddle
<nav class="bg">
<ul class="width nav">
<li><span></span></li>
<li>Link</li>
<li><span></span></li>
<li>DropLink
<ul class="drop dr2">
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
</li>
<li><span></span></li>
</ul>
</nav>
CSS (sorry for the mess on this one. In the full CSS file, it's all combined to save on space):
*{margin:0;padding:0;font-size:small;font-family:Roboto;vertical-align:middle;}
.drop,nav{border:1px solid #BBB;}
body>.main,nav>ul{font-size:0;}
.width{margin:0 auto;width:84%;}
.width,.bg{min-width:1000px;}
.bg,.drop{background:linear-gradient(#444,#000,#444);}
nav{border-left:none;border-right:none;}
nav>ul:after{content:"";}
nav a{padding:8px;}
nav a:hover, nav a:active{color:#BBB;}
.drop{z-index:2;left:-9999px;top:-9999px;opacity:0;border-top:none;}
.dr1{transform:translateX(-28.5%);width:230%;}
.dr2{transform:translateX(-27%);width:216%;}
.drop li{white-space:nowrap;display:block;}
.nav li:hover .drop{left:0;top:100%;opacity:1;transition:opacity .5s,top 0s,left 0s;}
nav span{border-left:1px solid #000;border-right:1px solid #555;padding:7px 0;}
nav{font-weight:bold;}
nav>ul{text-align:justify;}
nav>ul,nav>ul:after{width:100%;}
a{text-decoration:none;}
a{color:#FFF;}
.nav li:hover .drop{text-align:center;}
nav a,nav li,nav>ul:after{display:inline-block;}
nav li,nav>ul{position:relative;}
.drop{position:absolute;}
While I'm asking, I might as well kill two birds with one stone and ask something else. If you zoom in on the bar where the borders are, you'll see that they don't actually have the same height as the navbar itself. If I increase the padding in the relevant area in my CSS, it becomes one pixel too long. If I don't, it's one pixel too short. It is never 100% the height of the bar. I asked this question not too recently, but after changing the entire navbar code, I've been unable to replicate the solution.
Your problem is that you are only setting the left and top rules on hover, but when you aren't hovering the dropdown completely leaves the view, so although the opacity if fading, you can't see it.
What you need to do is change two group selectors to have the same position whether hovering or not.
E.G.
.nav li:hover .drop {
left:0;
top:100%;
opacity:1;
transition:opacity .5s, top 0s, left 0s;
}
Should become two selectors:
.nav li .drop {
left: 0;
top: 100%;
pointer-events: none;
opacity: 0;
transition: opacity .5s, top 0s, left 0s;
}
.nav li:hover .drop {
pointer-events: auto;
opacity: 1;
}
In addition, your text-align property should be set even when not hovering, otherwise the text will shift left.
E.G.
Delete:
.nav li:hover .drop {
text-align:center;
}
Add:
.nav li .drop {
text-align:center;
}
If you make these corrections, you should get: jsfiddle.net/4PLbd/28
Note:
I don't want to be nitpicky, but your code is styled terrible and makes fixing things difficult, always try to make code readable. I did not clean up a lot of code here, although it should be.
You should probably consider re coding this, half of your CSS was unecessary and also had to be cleaned up. I also took out code that was not needed, I can show you how to do it better, for instance the spaces with span elements, not necessary. Also, good code structure is important, as it will help you find and locate problems. I cleaned this up as best as I could. Again, this gets your first question done.
Here is just a few CSS changes that I made. Check the Jsfiddle
.drop {
opacity: 0;
transition: all .5s ease;
}
ul.nav li:hover .drop {
opacity: 1;
transition: all .5s ease;
}

Creating CSS3 dropdown menus on a horizontal navbar with a fade-in effect

I've been trying to get this right for a couple of days now, and so far, I haven't really made progress on getting the dropdown working at all, though partly because the guides I've read all make use of symbols in the CSS that I'm unfamiliar with, such as the tilde and 'greather than' symbols. In any case, here is the base code that I have:
<nav class="bg">
<ul class="width">
<li>Link</li>
<li>
<ul>
<li>Link</li>
<li>Link</li>
</ul>
</li>
</ul>
</nav>
CSS:
.width{margin:0 auto;min-width:1000px;width:84%;}
nav ul{width:100%;text-align:justify;font-size:0;position:relative;}
nav ul:after{content:"";width:100%;display:inline-block;}
nav li{list-style:none;display:inline-block;}
nav a{display:inline-block;padding:10px;}
I think the next step is to put display:none; on the inner <ul> elements to hide them by default, but the next part is where I get lost. How do you use CSS to make one object do something when something else happens to another object? In this case, how would you use the CSS to make the dropdown list appear when the relevant link is hovered?
Here is a Demo
First of all you need to understand how CSS Selectors are working.
For the way to add a Dropdown, you go for the hover on the parent element and wrap that around the sub-menu. Than if the hover event is fired everything inside will be able to select true :hover
Like:
.nav li:hover .sub-menu {
left: 0;
top: 100%;
}
I also added the Fade-In effect. There for you work with css-transition.
But be careful, if your Sub-menu will be on/off with display:block and :none it wont work i guess.
U may play around with that in that fiddle to fiddle out what you can do and how your changes effect the output.
.nav .sub-menu {
position: absolute;
left: -1000px;
top: -1000px;
opacity: 0;
-o-transition: opacity .25s;
-ms-transition: opacity .25s;
-moz-transition: opacity .25s;
-webkit-transition: opacity .25s;
transition: opacity .5s;
}
.nav li:hover .sub-menu {
left: 0;
top: 100%;
opacity: 1;
}
You can even create a dropdown without using any selectors. But it is important to know about selectors. Go through this link to know more about selectors.
You can have a look at Pure CSS Dropdown Menu to check how to create dropdown menu with css.
You have yourself correctly pointed out the problem and that is :
How do you use CSS to make one object do something when something else
happens to another object?
I think this simplest demo, would shed some light on your above question.
Specifically have a deeper look at below CSS in the demo.
#circle:hover .popup{
display : block;
}
How do you use CSS to make one object do something when something else happens to another object? In this case, how would you use the CSS to make the dropdown list appear when the relevant link is hovered?
li > a ul { display: none; }
li > a:hover ul { display: block; }
You can also play with transitions to add fade-in effect.

Delaying :hover using CSS? [duplicate]

This question already has answers here:
How can I delay a :hover effect in CSS?
(3 answers)
Closed 7 years ago.
I have a menu in which onHover apears a infobox, telling what the button does. How can I apply a delay so that the box apears let's say one second after i put my mouse over the button?
HTML:
<td class="info"><a id="login-edit_account" href="../login-edit_account.php">Edit account<span><div id="pointer"></div><p style="font-size:11px">Edit user's information.</p></span></a></td>
CSS:
td.info {
position:relative; /*this is the key*/
z-index:24; background-color:#ccc;
color:#000;
text-decoration:none
}
td.info:hover {
z-index:25;
background-color:#fff
}
td.info span {
display: none;
transition: 0s display;
}
td.info:hover span { /*the span will display just on :hover state*/
display:block;
position:absolute;
top:42px; left:7px;
width:210px;
border:2px solid #0cf;
padding: 5px;
background-color:#fff; color:#000;
text-align: center;
-webkit-transition-delay:5s;
}
#pointer {
border:solid 10px transparent;
border-bottom-color:#0cf;
position:absolute;
margin:-27px 0px 0px 10px;
}
It's really pretty simple. Example:
a {
-webkit-transition: 1s 3s;
}
a:hover {
background-color: red;
}
When the user hovers the link, the browser waits 3 seconds. Only when those seconds have passed does the background transition to red (in this case with a 1s transition time).
Here's a fiddle: http://jsfiddle.net/joplomacedo/VP7hE/
Yes, you can use CSS3's transitions to delay the :hover effect.
CSS transitions, which are part of the CSS3 set of specifications,
provide a way to control animation speed when changing CSS properties.
Instead of having property changes take effect immediately, you can
cause the changes in a property to take place over a period of time.
For example, if you change the color of an element from white to
black, usually the change is instantaneous. With CSS transitions
enabled, changes occur at time intervals that follow an acceleration
curve, all of which can be customized.
In your case I believe you need to focus on the transition-delay property.
Here are a few useful links in regard to using transitions/example use cases:
https://developer.mozilla.org/en-US/docs/CSS/transition-delay
http://css-tricks.com/transition-delay-delays/
http://designshack.net/articles/css/create-stunning-effects-with-css-transition-delays/