CSS Unable to use hover effect on list ID item - css

I have a Navbar that has a <li> and one of those list items have a ID of Navbar-login-btn. I am able to modify just that ID in the css, however whenever I try to use the :hover function on it; it does not work.
Is there something I am doing wrong here, because I am not sure. Any help would be nice, thank you!
Navbar.jsx:
import '../App.css';
import myAvatar from '../images/avataaars.png'
function Navbar() {
return(
<div className='Navbar-container'>
<img src={myAvatar} className='Nav-logo'/>
<ul>
<li>Home</li>
<li>About</li>
<li>Skills</li>
<li>Projects</li>
<li id='Navbar-login-btn'>Login</li>
</ul>
</div>
)
}
export default Navbar;
App.css:
.Navbar-container{
margin: 0;
width: 100%;
display: flex;
align-items: center;
position: fixed;
background-color: #fff;
box-sizing: border-box;
padding: 10px 40px 10px 40px;
}
.Nav-logo {
width: 75px;
height: auto;
}
#Navbar-login-btn {
border: 2px solid rgb(101, 201, 255);
border-radius: 20px;
color: rgb(101, 201, 255);
cursor: pointer;
}
#Navbar-login-btn:hover {
color: #fff;
background-color: rgb(101, 201, 255);
}
.Navbar-container ul{
width: 100%;
display: flex;
justify-content: space-evenly;
font-family: Russo One;
list-style: none;
gap: 40px;
}
.Navbar-container li{
padding: 10px;
}

First, I recommend you use a kebab-case for the CSS property.
example - navbar-container. lowercase with a hyphen.
By the way, you don't have to use an ID to grab the element you want.
you can use last-child method.
In your case:
If the Login is the last Element in the list. use this:
// for the navbar
.navbar-container{
margin: 0;
width: 100%;
display: flex;
align-items: center;
position: fixed;
background-color: #fff;
box-sizing: border-box;
padding: 10px 40px 10px 40px;
}
// for ul in the navbar
.navbar-container ul{
width: 100%;
display: flex;
justify-content: space-evenly;
font-family: Russo One;
list-style: none;
gap: 40px;
}
// for li ul in the navbar
.navbar-container ul li{
padding: 10px;
}
// for last li ul in the navbar
.navbar-container ul li:last-child {
border: 2px solid orange;
}

You have same background-color on #Navbar-login-btn & #Navbar-login-btn:hover
Try replacing different color on one of them
Example:
#Navbar-login-btn:hover {
color: #fff;
background-color: rgb(15 98 143);//place this new color
}
Playcode: Sample result here

Related

React fixed navbar with flexbox

so I'm trying to create my blog using the react framework, but I'm facing an issue here.
I really have been trying to tweaks settings on the css, html or even try to switch to grid instead of flexbox but I can't figure out how to make the "fixed" navbar detected by the flexbox.
Currently, the navbar works fine I guess, but the content that is supposed to be on the right, is not taking the place it should, it's taking the entire screen instead of the rigth section next to the navbar.
Some help would be highly appreciated !
body {
overflow: hidden;
height: 100vh;
top: 0;
left: 0;
margin: 0;
}
/*left box -Navbar*/
.nav-tab-text{
font-size: 1.6em;
display: block;
padding: 00px 0px 50px 0px;
text-align: center;
list-style-type: none;
display: flex;
}
.nav-tab a {
display: block;
text-align: center;
padding: 15px 18px;
text-decoration: none;
font-size: 18px;
color: aliceblue;
}
.nav-tab {
background-color: blue;
height: 100vh;
width: 18%;
border: 3px solid red;
position: fixed;
}
/*Right box - Home content*/
.home-content-container {
width: 100%;
height: 100vh;
border: 5px solid green;
}
.home-content-title {
text-align: center;
font-size: 1.7em;
text-decoration: underline;
text-decoration-thickness: 3px;
}
.home-content-featured{
border: 3px solid purple;
width: 50%;
height: 50%;
align-self: center;
margin-top: 3%;
}
.test{
display: flex;
}
function Navbar() {
return (
<div className="flex-container">
{/*left box - Navbar*/}
<nav className="nav-tab">
Home
Articles
Archives
About
</nav>
{/*Right box - Home content*/}
<div className="home-content-container">
<div className="home-content-title">
<h3>Name</h3>
</div>
<div className="home-content-featured">
<p>1</p>
</div>
</div>
<div className="test">
<p>2</p>
</div>
</div>
);
}
export default Navbar;
import Navbar from "./components/Navbar";
function App() {
return (
<div>
<Navbar />
</div>
);
}
export default App;
body {
overflow: hidden;
top: 0;
left: 0;
margin: 0;
}
/*left box -Navbar*/
.flex-container{
display: flex;
flex-flow: row;
}
.nav-tab a {
display: block;
text-align: center;
padding: 15px 18px;
text-decoration: none;
font-size: 18px;
color: aliceblue;
}
.nav-tab {
background-color: blue;
height: 100vh;
width: 18%;
border: 3px solid red;
}
/*Right box - Home content*/
.home-content-container {
width: 100%;
height: 100vh;
border: 5px solid green;
display: flex;
flex-direction: column-reverse;
}
.home-content-title {
text-align: center;
font-size: 1.7em;
text-decoration: underline;
text-decoration-thickness: 3px;
}
.home-content-featured{
border: 3px solid purple;
width: 50%;
height: 50%;
margin-top: 3%;
align-self: center;
}

Hyperlink combined with pseudo code

In the code below, how can I have the same font color for the hyperlinks as for the other text? It now seems to morph with the color as defined in the pseudo code.
ul {
list-style: none;
margin: 0;
padding: 0;
}
a, a:link, a:visited, a:active {
text-decoration: none;
color: #484693;
}
nav {
color: #484693;
font-weight: bold;
width: calc(100% - 5vw);
display: flex;
align-items: center;
}
nav>ul {
display: flex;
align-items: center;
width: 100%;
justify-content: space-around;
}
nav>ul>li {
margin-right: 2em;
}
nav>ul>li.header-button {
position: relative;
padding: 1em;
}
nav>ul>li.header-button::before {
content: '';
width: 100%;
height: 70%;
background: rgba(255, 25, 255, 0.4);
border: thin solid rgba(255, 25, 255, 0.4);
border-radius: 20px;
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
}
<nav>
<div class="header-logo">
<img src="http://via.placeholder.com/50x50" alt="" />
</div>
<ul>
<li>Option1</li>
<li>Option2</li>
<li class="header-button">Option3</li>
<li class="header-button">Option4</li>
</ul>
</nav>
The issue you are having is the pseudo element you are using to create the background is coming on top of the anchor tag, since you have given absolute position to the pseudo element. You can overcome that by assigning position relative to the anchor tag and assign a z-index to it.
WORKING DEMO:
ul {
list-style: none;
margin: 0;
padding: 0;
}
a, a:link, a:visited, a:active {
text-decoration: none;
color: #484693;
}
nav {
color: #484693;
font-weight: bold;
width: calc(100% - 5vw);
display: flex;
align-items: center;
}
nav>ul {
display: flex;
align-items: center;
width: 100%;
justify-content: space-around;
}
nav>ul>li {
margin-right: 2em;
}
nav>ul>li.header-button {
position: relative;
padding: 1em;
}
nav>ul>li.header-button::before {
content: '';
width: 100%;
height: 70%;
background: rgba(255, 25, 255, 0.4);
border: thin solid rgba(255, 25, 255, 0.4);
border-radius: 20px;
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
}
nav>ul>li a {
position: relative;
z-index: 1;
}
<nav>
<div class="header-logo">
<img src="http://via.placeholder.com/50x50" alt="" />
</div>
<ul>
<li>Option1</li>
<li>Option2</li>
<li class="header-button">Option3</li>
<li class="header-button">Option4</li>
</ul>
</nav>
Just add this to your css, so your element would be under your text.
nav>ul>li.header-button::before {
z-index: -1;
}
Your :before is overdone and its semitransparent „back“ground overlaps the actual link color (since it's positioned absolute...).
You don't need that. You could fix by z-index, but better do that background directly and way more natural on your links. (Which you want to be clickable, right?). And probably you want to have an inner span for your non-links, to ensure same padding and space:
<nav>
<div class="header-logo">
<img src="http://via.placeholder.com/50x50" alt="" />
</div>
<ul>
<li><span>Option1</span></li>
<li><span>Option2</span></li>
<li class="header-button">Option3</li>
<li class="header-button">Option4</li>
</ul>
</nav>
css:
ul {
list-style: none;
margin: 0;
padding: 0;
}
a, a:link, a:visited, a:active {
text-decoration: none;
color: #484693;
}
nav {
color: #484693;
font-weight: bold;
width: calc(100% - 5vw);
display: flex;
align-items: center;
}
nav ul {
display: flex;
align-items: center;
width: 100%;
justify-content: space-around;
}
nav ul li {
margin-right: 2em;
}
nav ul li a, nav ul li span {
padding: 10px;
}
nav ul li a {
background: rgba(255, 25, 255, 0.4);
border: thin solid rgba(255, 25, 255, 0.4);
border-radius: 20px;
}
nav ul li.header-button {
position: relative;
padding: 1em;
}
Looks like this:
Btw, almost certainly you will be better of with display:table for your <ul>, display: table-cell for your li. Using flex is most certainly overdone...

Unexpected height while using flexbox model

I'm trying to make a side menu and i want to use flexbox model. But i have a problem that i haven't solved.
In my menu there are some list element (like dashboard, componenets, ...). This list elements are in <a> tag. When you look with inspector tool the height of <a> tag is normal but containing <li> tags height is strangely long. There is some padding in <li> but not that much. There is no reaseon for the <li> is long like this.
The reason that i use flexbox model in <a> is there is some other element is <a> to be positioned in my original project. But i didn't put them in this example.
The height of <a>: https://postimg.org/image/7cqo2pazf/
The height of <li>: https://postimg.org/image/kgw8feaqz/
Why do you think this strangely long height of <li> is happened?
.left-menu {
position: fixed;
top: 0px;
bottom: 0px;
left: 0px;
width: 220px;
background-color: #eee;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
.left-menu .menu-container>ul {
margin: 0px;
padding: 0px;
}
.left-menu .menu-container>ul>li {
padding: 2px 8px;
}
.left-menu .menu-container>ul>li>a {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
position: relative;
padding: 6px 8px;
text-decoration: none;
font-size: .875em;
border-radius: 4px;
font-weight: bold;
color: #111;
}
.left-menu .menu-container>ul>li>a:hover {
background: rgba(212, 212, 212, 0.5);
}
<div class="left-menu">
<div class="menu-container">
<ul>
<li>Dashboard</li>
<li>Components</li>
<li>Forms</li>
<li>Tables</li>
</ul>
</div>
</div>
Simply add display:block to your list.
.left-menu .menu-container>ul>li {
padding: 2px 8px;
display: block;
}
Have a look at this snippet
.left-menu {
position: fixed;
top: 0px;
bottom: 0px;
left: 0px;
width: 220px;
background-color: #eee;
font-family: 'Open Sans', sans-serif;
font-size: 16px;
}
.left-menu .menu-container>ul {
margin: 0px;
padding: 0px;
}
.left-menu .menu-container>ul>li {
padding: 2px 8px;
display:block;
}
.left-menu .menu-container>ul>li>a {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
position: relative;
padding: 6px 8px;
text-decoration: none;
font-size: .875em;
border-radius: 4px;
font-weight: bold;
color: #111;
}
.left-menu .menu-container>ul>li>a:hover {
background: rgba(212, 212, 212, 0.5);
}
<div class="left-menu">
<div class="menu-container">
<ul>
<li>Dashboard</li>
<li>Components</li>
<li>Forms</li>
<li>Tables</li>
</ul>
</div>
</div>

Flexbox children don't always expand as wide as they could/should

Two of the link buttons in my nav have long-ish names, and they're text-wrapping and becoming two lines tall even though (as far as I can tell) there's nothing stopping them or the nav from growing wider. Have I just missed something?
The code is quite long, so you'll probably be happier viewing the codepen than reading it here. It's so long because I'm afraid something I've overlooked in my global styling or my media query changes is to blame for this bug.
EDIT: This one was an easy answer. I misunderstood selector precedence. I thought media queries took precedence over everything (except inline). So I didn't realize the nav a rule in my media query was not overwriting the nav > section > a in my base CSS. A good lesson about always respecting CSS selector precedence.
HTML
<section>
<nav>
<a id="NSFW_deactivateFilter" onClick="worksafeOff()" href="#">Worksafe Mode: On</a>
<h2 onClick="showMenu()">Category Filters</h2>
<div></div>
<section id="nav_Orientation">
<a id="Horizontals" href="#">Horizontals</a>
<a id="Verticals" href="#">Verticals</a>
</section>
<div></div>
<section id="nav_Category">
<a id="Biltmore" href="#">Biltmore</a>
<a id="Commercial" href="#">Commercial / Product</a>
<a id="Fashion" href="#">Fashion & Glamour</a>
<a id="Invocation" href="#">Invocation</a>
<a id="NewOrleans" href="#">New Orleans</a>
</section>
</nav>
</section>
CSS
/** GLOBAL **/
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
color: inherit;
font-size: 16px;
font-style: inherit;
font-weight: 400;
line-height: 1em;
margin: 0 auto;
padding: 0;
position: relative;
text-align: center;
text-decoration: none;
z-index: 1;
}
a {
text-decoration: underline;
}
body {
background-color: #444;
color: #ccc;
font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;
font-style: normal;
}
body > section {
background-color: #333;
width: 100%;
max-width: 1440px;
}
/** NAV **/
nav {
align-items: center;
background-color: #222;
display: flex;
flex-flow: column nowrap;
margin-bottom: 10px;
width: 95%;
max-width: 480px;
}
nav a, nav > h2 {
border: 1px solid #666;
margin: 0 0 5px 0;
padding: 5px;
}
nav a {
text-decoration: none;
}
nav > a, nav > h2 {
width: 100%;
}
nav > a {
background-color: #666;
}
nav > div {
display: none;
}
nav > section {
align-items: center;
display: none;
flex-flow: row wrap;
justify-content: space-between;
margin: 0;
width: 95%;
}
nav > section.nav {
display: flex;
}
nav > section > a {
display: inline-block;
width: 48%;
}
/** MEDIA QUERIES **/
#media (min-width: 1000px) {
nav {
display: inline-flex;
flex-flow: row nowrap;
width: auto;
max-width: none;
}
nav a {
margin: 0 5px;
width: auto;
}
nav > h2 {
display: none;
}
nav > div {
background-color: #666;
display: block;
height: 20px;
margin: 0 5px;
width:1px;
}
nav > section {
display: flex;
flex-flow: row nowrap;
width: auto;
}
}
Remove width: 48%; rule from nav > section > a.
See codepen.

Varying lengh of link text in menu links makes links unequal height

I making menu links which have varying lengths of link text. This makes some links wrap onto 2 lines and others not.
In the image below the red area is a link and so clickable, but the grey area is not. My code is the first example and I need to create the 2nd:
<ul>
<li>
Link 1
</li>
<li>
Link 2 which has very very very long text
</li>
<li>
Link 3
</li>
</ul>
ul {
padding: 0;
margin: 0;
display: table;
width: 400px;
border: 1px solid black;
}
li {
list-style-type: none;
display: table-cell;
background: grey;
width: 33%;
}
a {
background: red;
width: 100%;
display: inline-block;
height: 100%;
}
http://codepen.io/anon/pen/Gcaem/
This FIDDLE works.
CSS
ul {
padding: 0;
margin: 0;
display: table;
width: 400px;
border: 0px solid black;
}
li {
list-style-type: none;
display: table-cell;
background-color: transparent;
margin-left: 5px;
border: 5px solid white;
width: 33%;
height: 75px;
text-align: center;
border: 1px solid gray;
}
a {
display: table;
background-color: red;
color: white;
width: 100%;
height: 100%;
text-align: center;
text-decoration: none;
}
a span {
display: table-cell;
height: 100%;
width: 100%;
text-align: center;
vertical-align: middle;
}
This is the result in firefox:

Resources