CSS link styling priority - html

I'm using CSS to style some of my link on my website I'm making for school, but I've ran into a problem. One bit is styled with CSS so its can be used as navigation menu. The other links are regular links with some styling.
The navigation menu has to be in the div tag for the regular link styling too, otherwise the background will not cover the whole page.
The problem is, since the div of the nav menu is placed within the styling of the other links, the effect of both styles get applied to the navigation menu.
Is there a way to give the styling of the navigation menu a higher priority than those of the other links, so that only the styling of the navigation menu will be applied?
Here's the CSS of the navigation menu:
/* styling voor navigation menu */
.nav ul {
list-style: none;
background-color: #525252;
text-align: center;
padding: 0;
margin-left: 400;
margin-right: 400;
border-radius: 10px;
border: 1px solid black ;
color: #fff;
}
.nav li {
font-family: 'Oswald', sans-serif;
font-size: 1.2em;
line-height: 30px;
height: 30px;
}
.nav a {
text-decoration: none;
color: #fff;
display: block;
transition: .3s background-color;
}
.nav a:hover {
background-color: #404040;
}
.nav a.active {
background-color: #404040;
color: #fff;
cursor: default;
}
.nav li {
width: 110px;
height: 40px;
line-height: 40px;
font-size: 1.2em;
}
.nav li {
display: inline-block;
margin-right: -4px;
}
/* extra class voor meescrollen menubalk */
.main-nav-scrolled {
position: fixed;
top: 0;
z-index: 9;
text-align: center;
width: 41%;
background: #858585
}
here's the CSS of the other links:
/* links voor onderste gedeelte pagina */
.bottom a:link {
text-decoration: none;
color: inherit;
}
.bottom a:visited {
text-decoration: none;
color: inherit;
}
.bottom a {
color: inherit;
text-decoration: none;
border-bottom: 1px solid transparent;
transition: all 0.3s;
}
.bottom a:hover {
color: inherit;
border-color: inherit;
}
And here is the important bit of HTML:
<div class="bottom">
<!-- navigation balk bovenin pagina -->
<div class="nav">
<ul>
<li class="Pokémon"><a class="active" href="SQLpokemondb.php">Pokémon</a></li>
<li class="Types">Types</li>
<li class="Abilities">Abilities</li>
<li class="Natures">Natures</li>
<li class="Stats">Stats</li>
</ul>
</div>
<?php
echo " <span style='color:$color'><a href='SQLdetailtypendb.php?id=" . $data['type_id'] . "'>" . $type1 . '</span></a>';
?>
</div>
Thanks in advance!
-Gijs

if you add styles to .bottom a that are not given to .bottom .nav a like background: green it will become a style to .bottom .nav a also. All styles need to be overwritten if you want custom styles only for .bottom .nav a
because .nav a is a descendant of .bottom , even if you write a very specific path eg .bottom .nav ul li a , it will inherit from .bottom a
the styles that are 'unique'
for example
snippet not good for you ( .nav li a inheriting background: green style from .bottom a because it doesn't have a background style for its self )
.bottom .nav a:hover { color:red;}
.bottom a:hover { color:yellow;background:green}
<div class="bottom">
<!-- navigation balk bovenin pagina -->
<div class="nav">
<ul>
<li class="Pokémon"><a class="active" href="SQLpokemondb.php">Pokémon</a></li>
<li class="Types">Types</li>
<li class="Abilities">Abilities</li>
<li class="Natures">Natures</li>
<li class="Stats">Stats</li>
</ul>
</div>
</div>
snippet good ( disable background:green from .bottom a by adding background:none or background: anythinghere image or color etc. for .bottom .nav li a )
.bottom .nav a:hover { color:red;background:black}
.bottom a:hover { color:yellow;background:green}
<div class="bottom">
<!-- navigation balk bovenin pagina -->
<div class="nav">
<ul>
<li class="Pokémon"><a class="active" href="SQLpokemondb.php">Pokémon</a></li>
<li class="Types">Types</li>
<li class="Abilities">Abilities</li>
<li class="Natures">Natures</li>
<li class="Stats">Stats</li>
</ul>
</div>
</div>

So what you're going to want to do first is add a new class to all the links in your nav, for example:
<li class="Types"><a class="new-class-here" href="Over ons.html">Types</a></li>
Then in your css for the nav you will use .nav a.new-class-here instead of .nav a, .nav a.new-class-here:hover instead of .nav a:hover, and so forth.
Then in your css for the other regular links you will want to use .bottom a:not(.new-class-here) instead of .bottom a. What this does is selects all the links in .bottom that do not have that new class we created. So now your nav styles will only apply to your nav links and your regular styles will only apply to the non-nav links.
You can learn more about :not() here if you want: https://developer.mozilla.org/en-US/docs/Web/CSS/:not

Related

Drop down menu doesn't work how I want it to

I am trying to make a menu which contains one menu-item with a dropdown, however I can't seem to get it quite right.
I started off with combining my existing menu bar to a piece of code for a dropdown, I have managed to get de drop down item in the menu bar, but I don't know how to get the menu bar back to the top again and it still has a blank on the left.
There is also an extra empty item in my drop down menu and I don't know how to get rid off it?
Can someone please help me I feel like I am overlooking something
.menu-bar {
position: fixed;
width: 100vw;
background-color: #134c95;
color: white;
display: flex;
flex-direction: row;
justify-content: center;
opacity: 0.8;
}
.menu-item {
padding: 10px;
cursor: default;
font-size: 15pt;
margin-left: 15px;
margin-right: 15px;
}
.menu-item:hover,
a:hover {
background-color: white;
cursor: pointer;
color: 234c95;
}
.menu-bar a {
color: white;
}
.menu-bar a:visited {
color: white;
}
.menu-bar a:link,
.menu-bar a:visited {
text-decoration: none;
}
/*
.menu-bar a:hover {
color: #134c95;
}
*/
/* Dropdown Button */
.dropbtn {
background-color: inherit;
color: white;
/* padding: 16px;*/
/* font-size: 16px;*/
border: none;
cursor: pointer;
}
/* The container <div> - needed to position the dropdown content */
/*
.dropdown {
position: fixed;
display: inline-block;
}
*/
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: white;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
/* z-index: 1;*/
}
/* Links inside the dropdown */
.dropdown-content a {
color: #234c95;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
background-color: 234c95;
color: white;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: white;
color: #234c95;
}
<div class="menu-bar">
<a href="instructiestest.html">
<div class="menu-item">Welkom</div>
</a>
<a href="instructie2.html">
<div class="menu-item">Aanmelden Portbase</div>
</a>
<a href="instructie3.html">
<div class="menu-item">De App</div>
</a>
<a href="testing.html">
<div class="dropdown">
<button class="dropbtn">
<div class="menu-item">Het team</div>
<div class="dropdown-content">
Google
Facebook
YouTube
</div>
</button>
</div>
</a>
</div>
Remove the a tag from your dropdown item, like below:
<div class="menu-bar">
<a href="instructiestest.html">
<div class="menu-item">Welkom</div>
</a>
<a href="instructie2.html">
<div class="menu-item">Aanmelden Portbase</div>
</a>
<a href="instructie3.html">
<div class="menu-item">De App</div>
</a>
<div class="dropdown">
<button class="dropbtn">
<div class="menu-item">Het team</div>
<div class="dropdown-content">
Google
Facebook
YouTube
</div>
</button>
</div>
</div>
Also, your .dropdown-content a:hover rule is missing a # in your background-color attribute:
.dropdown-content a:hover {
background-color: #234c95;
color: white;
}
Here is a working Fiddle
Something seems to have an extra margin or similar. Try to load the code, and check with the inspector. It'll show you the different spacing types in nice colours: https://developer.mozilla.org/en-US/docs/Tools/Page_Inspector/How_to/Examine_and_edit_the_box_model
In all honesty, you might be better re structuring this menu completely. I don't mean to be harsh but the code isn't structured well enough and is a little confusing to work with.
Ive just tweaked your code a little to make it easier to work with. I'm not exactly sure what it is your trying to achieve but here is something for you to take a look at:
CSS
#menu-bar {
position: fixed;
width: 100vw;
background-color: #134c95;
padding: 15px;
}
#menu-bar ul {
list-style: none;
padding: 0;
margin: 0;
font-size: 22px;
font-family:sans-serif;
}
#menu-bar ul li {
margin-bottom: 10px;
width: 100%;
}
#menu-bar ul li a {
display: block;
padding: 10px;
color: #fff;
text-decoration: none;
}
#menu-bar ul li a:hover {
color: #134c95;
background-color: #fff;
}
#menu-bar ul li ul {
display: none;
}
#menu-bar ul li:hover ul {
display: block;
}
HTML
<nav id="menu-bar">
<ul>
<li>Welkom</li>
<li>Aanmelden Portbase</li>
<li>De App</li>
<li class="">Het team
<ul>
Google
Facebook
YouTube
</ul>
</li>
</ul>
</nav>
As you can see this is much easier to understand and follow. The ID of the nav element is all you need to style the child elements.
Hope that helps as a starting point!

How do I turn off hover for this <li>?

I'm trying to turn off the hover for the current page in a navigation menu.
div.nav {
width: 100%;
padding: 6px;
height: 40px;
}
.nav li {
color: #FFFFFF;
display: inline;
list-style-type: none;
text-align: center;
text-transform: uppercase;
padding: 20px;
margin: 0px;
height: 40px;
}
li.current {
background-color: #424242
}
li.current:hover {
background-color: inherit;
}
.nav li:hover {
background-color: #737373;
<div class="nav">
<ul>
<li class="current">Home</li>
<li>About
</li>
<li>Contact
</li>
<li>Gallery
</li>
</ul>
</div>
Here is the jsfiddle:
https://jsfiddle.net/swordams/jk6z5aqj/
I want the li for home to stay dark and not change on hover. I've tried setting the hover background color to "inherit", but that doesn't work.
Thanks!
You can use the CSS :not() pseudo-class:
.nav li:hover:not(.current) {
background-color: #737373;
}
jsFiddle example
You can use the solution by j08691 but ultimately, the problem with your css is that .nav li:hover is more specific than li.current:hover. Tacking a .nav will do the trick.
.nav li.current:hover {
background-color: inherit;
}
just make the active/current li background color important
li.current {
background-color: #424242 !important;
}

How to style more than one ul

I'm using the CSS lines below for a menu on a webpage.
The problem is that I don't know how to make that code apply only to the menu, and not to other ul -unordered lists- on the page!
ul {
text-align: left;
display: inline;
margin: 0;
padding: 15px 4px 17px 0;
}
ul li {
font: bold 12px/18px sans-serif;
display: inline-block;
margin-right: -4px;
position: relative;
padding: 15px 20px;
background: #AAF7BB;
font-size: 110%;
}
ul li:hover {
background: #ffffff;
color: #000000;
}
ul li ul {
padding: 0;
position: absolute;
top: 48px;
left: 0;
width: 150px;
opacity: 0;
visibility: hidden;
}
ul li ul li {
background: #ffffff;
display: block;
color: #00ff00;
}
ul li ul li:hover { background: #ffffff; }
ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: none;
}
a:active {
text-decoration: none;
}
Please help.
Try marking your ul as a class and refer to that instead of just ul.
<ul class="menu">
and change all instances of ul in the css to .menu.
Refering to just "ul" will apply the change to all ul elements.
As mentioned you can do this using classes on your html. I would try and Google this a little more. There are tons of example for menus out there.
In the top three is this:
http://www.noupe.com/essentials/freebies-tools-templates/100-great-css-menu-tutorials.html
Something simple:
<ul class="menu">
<li class="menu-option">
Home
</li>
<li class="menu-option active">
About
</li>
<li class="menu-option">
Contact
</li>
<ul class="sub-menu">
<a>Something to click</a>
<li class="menu-option">
Contact
</li>
<li class="menu-option">
Contact
</li>
</ul>
</ul>
Your css would need to be updated similar to below
ul.menu:{
/*Your style for the menu*/
}
ul.menu li.menu-option:{
/*Your style for the menu's options*/
}
ul.menu ul.sub-menu:{
/*Your style for the menu's sub-menu's*/
}
Use the class or id attributes alongside your HTML elements. For example:
HTML
<ul class="class1">
...
</ul>
<ul class="class1">
...
</ul>
<ul id="class2">
...
</ul>
CSS
.class1{
implement css styling here
}
#class2{
implement css styling here
}
Here, the top 2 ul elements will be styled by the .class1 CSS styling, the final ul with the attribute #class2 will be styled by the corresponding CSS.
Note that . denotes classes in CSS and # denotes IDs
IDs are unique, classes are not unique. More information

HTML & CSS: Float Attribute Issue

I have an issue floating an ul next to some text.
Here is the image:
I'm thinking maybe I have to use the relative position attribute to display it the way I want it to? But, I know for a fact that the float property should be used, but it's not working for me. I've tried using another div(logoAndMainMenu-Wrapper) to create a wrapper for the text and ul, but the float property still isn't working....
Here's my HTML:
<div id="topbar">
<div class="fixedWidth1">
<div class="logoAndMainMenu-Wrapper">
<p>The Official Website of<br />
<span id="AndrewVuText">Andrew Vu</span></p>
<ul>
<li>HOME</li>
<li>ABOUT</li>
<li>PROJECTS</li>
<li>COLLABORATE</li>
<li>CONTACT</li>
</ul>
</div> <!-- Logo & Main Menu Wrapper DIV -->
</div> <!-- Fixed Width 1 DIV -->
</div> <!-- Top Bar DIV -->
And here's my CSS:
#AndrewText {
font-size: 2em;
}
.fixedWidth1 {
margin: 0 auto;
width: 1000px;
height: 150px;
background-color: green;
border: 2px solid black;
}
.logoAndMainMenu-Wrapper {
border: 2px solid yellow;
}
.fixedWidth1 ul {
list-style-type: none;
text-align: center;
margin: 0;
background-color: green;
border: 2px solid orange;
}
.fixedWidth1 ul li {
display: inline; /* LI are horizontal */
padding: 5px;
border-right: 2px solid yellow;
}
.fixedWidth1 ul li a {
text-decoration: none; /* No UNDERLINE */
}
/* Normal, unvisited link */
.fixedWidth1 ul li a:link {
color: #FFFFFF;
}
/* Visited link */
.fixedWidth1 ul li a:visited {
color: #FFFFFF;
}
/* Mouse over link */
.fixedWidth1 ul li a:hover {
color: yellow;
}
/* Selected link */
.fixedWidth1 ul li a:active {
color: #FFFFFF;
}
Any help will do. Thanks!!!
Hi now define your p tag float left .logoAndMainMenu-Wrapper define this overflow hidden and as like this
.logoAndMainMenu-Wrapper>p{
float:left;
}
.logoAndMainMenu-Wrapper{overflow:hidden;}
Demo

Red block being displayed behind my image link

I put a link in the image which leads back to the home page; however, I have my nav menu set up so they turn red with hovering and somehow it's making a red block behind my image when I hover over it. I'm using HTML5 and CSS3
HTML5
<header class ="main-header">
<a href="index.html">
<img src="Images/image.png" alt="image logo"></a>
<nav><ul>
<li class="active">HOME</li>
<li>NEWS</li>
<li>LOCATION</li>
</ul></nav>
</header>
CSS3
/* Define Hyperlink Info */
a {
color: #FFFFFF;
font-weight: bold;
text-decoration: none;
}
a:link, a:visited{
color: #FFFFFF;
text-decoration: none;
}
a:hover, a:active {
background-color: #C71F0E;
color: #FFFFFF;
text-decoration: none;
}
/* Defines navigation menu */
.main-header nav {
background-color: #354175;
height: 40px;
}
.main-header nav ul {
list-style: none;
margin: auto;
}
.main-header nav ul li {
display: inline;
float: left;
}
.main-header nav li a:hover, .main-header nav li.active {
background-color: #C71F0E;
color: #FFFFFF;
text-shadow: none;
}
.main-header nav ul li a {
border-radius: 15px;
color: #FFFFFF;
display: block;
height: 20px;
padding: 10px 25px;
}
If you look at the a:link, a:visited line, you'll notice that this targets ALL links, even your image link.
To do away with this, try giving your first link a class (like "logo"), and write specific styles for it after the a:link, a:visited line.