Navigation bar made of images - html

I have five different images being used as buttons for my website's navigation. I want them to be inline horizontally and centred in the browser window. They looked fine until I added code to have text appear under each button when hovering over each image. The buttons are now all aligned vertically in the middle of the window.
In html file:
<div class="nav">
<div class="container">
<ul>
<div class="about">
<li><input type="image" src="image.png" id="aboutPage" onClick = 'aboutPage()'/></li>
<p class = "text1"> About </p>
</div>
<div class="resume">
<li><input type="image" src="image.png" id="resumePage" onClick = 'resumePage()'/></li>
<p class = "text2"> Resume </p>
</div>
<div class="home">
<li><input type="image" src="image.png" id="homePage" onClick = 'homePage()'/></li>
<p class = "text3"> Home </p>
</div>
<div class="portfolio">
<li><input type="image" src="image.png" id="portfolioPage" onClick = 'portfolioPage()'/></li>
<p class = "text4"> Portfolio </p>
</div>
<div class="contact">
<li><input type="image" src="image.png" id="contactPage" onClick = 'contactPage()'/></li>
<p class = "text5"> Contact </p>
</div>
</ul>
</div>
In CSS file:
.nav {
position: absolute;
bottom: 0%;
left: 50%;
transform: translate(-50%, -50%);
}
.nav .container {
font-size: 12px;
font-family: 'Shift', sans-serif;
color: #5a5a5a;
font-weight: lighter;
}
.nav .container ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.nav .container li {
display: inline;
}
The following is a sample of the hover code for each image:
.nav .container .about .text1 {
position:relative;
bottom:0px;
text-align: center;
visibility: hidden;
}
.nav .container .about:hover .text1{
visibility: visible;
}
Any help would be much appreciated! Thank you.

First of all, I have a few remarks when it comes to your markup. I know this is not code review, and not entirely relevant to the question, but I just can't help myself when I look at the HTML:
a div.nav just screams to me that you in fact want to use a nav
that div.container seems obsolote to me, and just adds markup. If you realy need the container class (for css or js reasons), why not add it to the ul in stead.
an ul can only have li elements as direct child, so those div elements should become a child of the li in stead of a parent.
why are you using (the very rare) input[type=image] elements, in stead of the usual <a><img></a>? It just seems strange.
using a different class for each text seems to serve no purpose. It just makes your code harder to maintain and your css a lot more verbose. I wonder if you need a class at all, but If you have a good reason, at least use the same class for all the text blocks.
Taking those remarks into account, my markup would look something like this:
<nav>
<ul class='container'>
<li>
<a href='#'>
<img src='' alt='do not forget your alt, especially for nav!' />
<p>text</p>
</a>
</li>
...
</ul>
</nav>
Then for your actual question, I am not entirely sure I understand what you are trying to achieve, but this is what I came up with. The css looks something like this.
nav {
position: absolute;
top: 50%;
left: 0;
transform: translate(0, -50%);
text-align: center;
}
nav ul {
font-size: 12px;
font-family:'Shift', sans-serif;
color: #5a5a5a;
font-weight: lighter;
list-style-type: none;
margin: 0;
padding: 0;
}
nav li {
margin: 12px;
display: inline-block;
}
nav p {
opacity: 0;
transition: opacity .5s;
}
nav a:hover p {
opacity: 1;
}
Note that I went for opacity in stead of visibility because that allows you to add a transition, which I find to give a much nicer experience. You should be able to use visibility as well though (or just remove the transition) for an instant state switch.
I hope this puts you on the right track. Let me know if I misunderstood anything, or if you want me to explain further.

Related

element:hover is not changing display to block

Unfortunately it wont display my drop-down nav block on hover, hope someone can help. I'm trying to change display:none to display: block on the class .moreMenu.
html
css
Here is my code...
HTML
<div class="navigationBar">
<div class = "navigationBarTitles">
HOME
VERBS
NOUNS
ADVERBS
ADJECTIVES
GRAMMAR
<a href="contact.html" class= "more">MORE
<div class="moreMenu">
TRIAL VIDEOS
ABOUT
CONTACT
GCSE EXAM PREP
PLANS & PRICING
GRAMMAR
</div>
</a>
</div>
CSS
.moreMenu {
display: none;
position: absolute;
width: 140px;
height: 180px;
background-color: white;
z-index: 2;
}
.more:hover .moreMenu {
display: block;
}
You have an error in your HTML: you can't nest interactive elements. Since the .moreMenu div contains <a> elements, and is itself contained in an <a> element, it won't work.
Solution: don't put the <div> inside the MORE link, but put them next to each other in a container.
.more {
display: inline;
}
.moreMenu {
display: none;
position: absolute;
width: 140px;
height: 180px;
background-color: white;
z-index: 2;
}
.more:hover .moreMenu {
display: block;
}
<header>
<div class="navigationBar">
<div class="navigationBarTitles">
HOME
VERBS
NOUNS
ADVERBS
ADJECTIVES
GRAMMAR
<div class="more">MORE
<div class="moreMenu">
TRIAL VIDEOS
ABOUT
CONTACT
GCSE EXAM PREP
PLANS & PRICING
GRAMMAR
</div>
</div>
</div>
</div>
</header>

How do I make an image clickable in a navigation bar?

I've currently created a navigation bar for my website for university assignment. I've implemented an image on their which is the universities logo which I'd like to link to the university homepage. However, when I try to use the anchor tag '' to make the image clickable to link to the homepage, it messes up for the style of my navigation bar and would like to know if there's a workaround. I know the issue is that the image will take on the styles of the anchor tags I have declared for the navigation bar. I'll include images of before and after creating the link and show the HTML and CSS of that section.
This is the navbar before making the image clickable.
This is the HTML for it:
<div class="top_nav">
<img class="logo" src="images/NTU_badge.png" alt="NTU Badge">
<a class="active" href="#Home"> Home </a>
Hackathon
Choose a Challenge
Digital Horizons
</div>
This is the CSS for it:
.top_nav {
overflow: hidden;
background-color: #2c3e50;
border-bottom: 20px solid #ed0162;
position: fixed;
top: 0;
width: 100%;
}
.top_nav a {
float: left;
color: white;
text-align: center;
padding: 30px 20px;
text-decoration: none;
font-size: 17px;
margin-bottom: 0;
font-family: monospace;
}
.top_nav a:hover {
background-color: #ed0162;
color: white;
}
.logo {
float: left;
margin: 15px;
}
This is the HTML and webpage after I try to make the image clickable:
There is now big spacing inbetween and the hover style now affects the image when I don't want it too.
The HTML code after:
<header>
<div class="top_nav">
<a href="https://www.ntu.ac.uk/">
<img class="logo" src="images/NTU_badge.png" alt="NTU Badge">
</a>
<a class="active" href="#Home"> Home </a>
Hackathon
Choose a Challenge
Digital Horizons
</div>
</header>
I've tried removing the 'logo' class from the image style but it doesn't really change it that much.
There are lots of ways to do this but flexbox makes it really easy. also will simplify your css. Just wrap the image in a anchor tag to make it clickable.
.top_nav {
display:flex;
justify-content:space-around;
align-items:center;
overflow: hidden;
background-color: #2c3e50;
font-size:2vw;
width: 100%;
}
.top_nav a {
width:10%;
color: white;
text-decoration: none;
font-family: monospace;
}
.top_nav a:hover {
background-color: #ed0162;
color: white;
}
img{
width:100%;
}
#short{
width:2.5%;
}
<div class="top_nav">
<a id='short' href='https:\\www.google.com'><img class="fa facebook" src="https://www.sustainablewestonma.org/wp-content/uploads/2019/09/facebook-square-brands-blue.png" scale="0"></a>
<a class="active" href="#Home"> Home </a>
Hackathon
Choose a Challenge
Digital Horizons
</div>
I find that it's generally pretty poor practice to use element names (such as a and div) in CSS selectors at all, for this exact reason.
Consider adding something like class="navigation" to each link in your navbar, and then change the .top_nav a selector to .top_nav .navigation. Then you could add a similar class to the logo <a>.
Not only does this make the CSS more specific, but much more readable when someone else (or you in six months' time) has a look at this without any other context of the rest of the page.
One way to solve this is to use the :first-of-type pseudo css selector. Something like that would be the correct way to handle it:
.top_nav a:first-of-type {padding: 0;}
EDIT
I'm sorry, on the original answer i miss the part to advice you wrapping your image with a element. So change this:
<img class="logo" src="images/NTU_badge.png" alt="NTU Badge">
To:
<img class="logo" src="images/NTU_badge.png" alt="NTU Badge" />

How to change body color when hovering over a div

I'm currently coding a very basic page for my friend and he said he wanted a box which would change color depending on which link he hovers over. I've tried a few things but none of it seem to work.
This is how the body looks:
body {
color: #fff;
background: #98adca;
text-align: center;
margin: 275px auto;
}
#box {
padding: 30px;
border: solid;
}
li {
list-style: none;
text-decoration: none;
}
a,
a:hover,
a:active,
a:visited {
color: #fff;
text-decoration: none;
}
.twt:hover {
background: #c3c0d1;
color: #fff;
}
<div id="box">
<h1>social media</h1>
<div class="twt">
<li>twitter
</li>
</div>
<div class="ig1">
<li>art instagram
</li>
</div>
<div class="ig2">
<li>regular instagram
</li>
</div>
<div class="fb">
<li>facebook
</li>
</div>
<div class="yt">
<li>youtube
</li>
</div>
</div>
But I don't get how I should write the CSS to make the box another color when just, for example, hovering over the YouTube link. In my current CSS only the background of the text is changed when hovering and not the entire box.
Try using jQuery with the "onmouseover" event:
HTML:
<div id="box">
<a onmouseover="colorChange()" onmouseout="revert()" href="#">Link</a>
</div>
Javascript:
function colorChange() {
$("#box").css("background-color", "red");
}
function revert() {
$("#box").css("background-color", "lightgrey");
}
Here is my pen: http://codepen.io/Hudson_Taylor11/pen/ozQogO
Hope this helps!
Use jQuery:
$(".twt").hover(
function() {
$("#box").css( "background-color", "#000" );
},
function() {
$("#box").css( "background-color", "#98adca" );
}
);
Let me know if you need help setting up jQuery.
From what I know. CSS doesn't be made to walk backward. All I can think about the way I can do is using jQuery to do that.
$(document).ready(function(){
$('.ig1 li a').hover(function(){
$('#box').css({'background-color': 'green'});
});
$('.ig2 li a').hover(function(){
$('#box').css({'background-color': 'blue'});
});
$('.yt li a').hover(function(){
$('#box').css({'background-color': 'red'});
});
$('.fb li a').hover(function(){
$('#box').css({'background-color': 'pink'});
});
});
body {
color: #fff;
background: #98adca;
text-align: center;
margin: 275px auto;
padding: 30px;
}
#box {
border: 3px solid #fff;
padding: 30px;
}
li {
list-style:none;
text-decoration:none;
}
a, a:hover, a:active, a:visited {
color: #fff;
text-decoration:none;
}
.twt:hover {
background: #c3c0d1;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="box">
<h1>social media</h1>
<div class="twt">
<li>twitter</li>
</div>
<div class="ig1">
<li>art instagram</li>
</div>
<div class="ig2">
<li>regular instagram</li>
</div>
<div class="fb">
<li>facebook</li>
</div>
<div class="yt">
<li>youtube</li>
</div>
</div>
</body>
Right, so I started thinking, you can do it with JS, but can you do it with pure CSS. Short answer - No. CSS does not allow child elements to access parent elements, because of security and other concerns. A simple Google search will show you all the things I read, there's no point of sharing docs here. But what if we trick the user, right, just hear me out. Instead of changing the colour of the parent, which is illegal, let's change the colour of a sibling - allowed by CSS LinkSo I unified your classes, for the links to share the same class (they still have separate IDs, chill). I then added a "pretend div" which will serve the purpose of the body. I stylised the "pretend", the unified div and added a "sibling on hover" CSS rule. Take a look:HTML`
<body>
<div class="box">
<h1>social media</h1>
<div class="link_divs" id="div_1">
<li>twitter</li>
</div>
<div class="link_divs" id="div_2">
<li>art instagram</li>
</div>
<div class="link_divs" id="div_3">
<li>regular instagram</li>
</div>
<div class="link_divs" id="div_4">
<li>facebook</li>
</div>
<div class="link_divs" id="div_5">
<li>youtube</li>
</div>
<div id="pretend_div">
</div>
</div>
</body>
And here's the CSS
body {
color: #fff;
background: #98adca;
text-align: center;
margin: 275px auto;
padding: 30px;
border: 3px solid #fff;
height: 100%;
}
li {
list-style:none;
text-decoration:none;
}
a, a:hover, a:active, a:visited {
color: #fff;
text-decoration:none;
}
/* IMPORTANT - This will be the new "body" */
#pretend_div{
position: absolute; /* REQUIRED */
width: 96%; /* Matching your body size */
height: 180px; /* Matching your body size */
border: 1px solid red; /* Differentiating made easy */
top:0; /* IMPORTANT - push the element to the top */
left: 0; /* IMPORTANT - push the element to the left */
margin: 275px auto; /* Grabbed the margin from your body */
padding: 30px; /* Grabbed the padding from your body */
z-index: -1; /* IMPORTANT - push the element to the back of stack */
}
/* IMPORTANT - generic link class */
.link_divs{
z-index: 0; /* IMPORTANT - set the links on-top of the pretend div */
position: relative; /* IMPORTANT - positioning */
}
/* What link you hover over ~ The pretend div */
#div_1:hover ~ #pretend_div{
background-color: #00A000; /* change bck colour */
}
#div_2:hover ~ #pretend_div{
background-color: orangered;
}
#div_3:hover ~ #pretend_div{
background-color: darkgoldenrod;
}
REMARKS I'm aware this is not the best solution, honestly - just use JS. But I wanted to try and make it happen with pure CSS. Now I tried to match the pretend div to your body as best I could, thus it looks, well, not as good as it could. I added some comments to help you understand what is happening with each line. The ones that use the "sibling style" CSS are marked by Important. Everything else is just matching your body style.JSFiddle Demo -> DEMO LINKHope that helps
instead of background try background-color

Why is the same html/css for my .subnav working on one page, but not another?

So I'm extremely new to this html and css thing and I've got a bit of a problem. I'm in the middle of cutting a site for the very first time, but I'm having issues with the positioning of my sub navigation.
My issue is that the Sub Nav on my FAQS Page is being pushed off center and towards the right in the browser. But then, the subnav on the other pages like 'support', which use exactly the same html structure and css style as the one on the FAQS page, are fine and perfectly centered.
When I 'inspect element' in the browser, it seems to be thinking there's an extra subnav button on the left hand side and that the FAQS subnav button is positioned in the middle of the subnav , if that makes sense?
There's a second FAQS Page in the folder which works...but that was made by literally copy and pasting the code from the support page and then cutting out the bits that weren't needed, then copying in the parts from the FAQS page that I needed onto this .html instead.
So I have a feeling it's just a typo or a syntax error or something realllllly simple. I just really want to know what I did so I can avoid it in the future and know how to fix it.
Here is the html for my sub nav ( & page title ):
<div class="grid grid-pad">
<div class="col-1-1">
<div class="subnav clearfix">
Support
FAQS <div class="triangle"> </div>
Swifd Forum
Video Tutorials
</div>
<div class="pagetitle">
<h1 class="pageheading"> FAQS </h1>
<div class="pageheadingborder"> </div>
<h2 class="subtitleFAQS"> Got questions that need answering? <strong> we've got you covered</strong></h2>
</div>
</div>
</div>
And Here is the CSS so far:
/*---------------- SUB NAV ---------------*/
.subnav {
margin: 0 0 60px 0px;
text-align: center;
}
.subnav a {
display:inline-block;
font-family:'Bebas Neue', 'Oswald', Arial, sans-serif;
color:#ffffff;
background-color:#000000;
line-height: 1.5;
font-weight:bold;
text-transform:uppercase;
font-size: 23px;
margin:5px;
width: 180px;
position: relative;
transition: all 200ms ease 0s;
}
a.b1.subactive {
background-color: #ee812c;
}
a.b1.subactive div.triangle {
width: 0;
height:0;
border-style: solid;
border-width: 15px 32.5px 0 32.5px;
border-color: #ee812c transparent transparent transparent;
position: absolute;
top:100%;
left: 50%;
margin-left: -30px;
z-index: 2;
}
.subnav a:active {
top: 2px;
}
I know it's a bit rusty, but any help would be greatly appreciated.
Here is an extremely basic model : https://jsfiddle.net/2Ly4v9qu/
Thankyou!

Using svg on webpage results in weird css rendering in webkit browsers

I'm running into a weird glitch which only seems to happen in chrome and safari. It's hard to explain why this happens with sample code, but I'll try to illustrate what I'm doing with code, while providing a link to the actual page below.
First of all, I have an unordered list displayed inline-block, so it can be justified just like text. Each list item contains an svg in an image tag and a paragraph with a short description, both wrapped in a single anchor tag. Nothing special i guess, but here's the catch: in chrome and safari the browser renders a 1px by approximately 15px blue/blackish line between the paragraph and the image, and I have no idea why this is happening. Here's the code:
<div class="wrapper">
<div class="justified-list home-icons">
<ul>
<li>
<a href="#">
<img src="http://voctel.wearebold.nl/wp-content/uploads/2015/02/company-building.svg" />
<br/>
<p>Description</p>
</a>
</li>
<li>
<a href="#">
<img src="http://voctel.wearebold.nl/wp-content/uploads/2015/02/company-building.svg" />
<br/>
<p>Description</p>
</a>
</li>
<li>
<a href="#">
<img src="http://voctel.wearebold.nl/wp-content/uploads/2015/02/company-building.svg" />
<br/>
<p>Description</p>
</a>
</li>
</ul>
<span class="stretcher"></span>
</div><!-- .justified-list -->
</div><!-- .wrapper -->
and here is the css (I'm using scss):
.wrapper {
width: 100%;
}
.justified-list {
width: 100%;
text-align: justify;
* {
display: inline;
}
li {
display: inline-block;
vertical-align: top;
}
.stretcher {
display: inline-block;
position: relative;
width: 100%;
height: 0;
}
}
Also, a codepen is provided here:
http://codepen.io/smelly586/pen/NPVVYd
If anyone has a clue on what's going on, or even better: has a possible fix for this, you have my gratitude.
Set your font-size on the element to 0. What you're seeing is the underline in the anchor element for whitespace in your HTML.
You could turn off the text-decoration: underline; that the browser renders by default for anchors, but let's assume that's not what you want to do.
Instead, the element with text will need to be reset to document root font-size (or whatever you want) using something like p { font-size: 1rem; }.
Example Codepen
So, accordingly, the SCSS/LESS would be:
.justified-list {
width: 100%;
text-align: justify;
* {
display: inline;
}
li {
display: inline-block;
vertical-align: top;
a {
font-size: 0;
p { font-size: 1rem; }
}
}
.stretcher {
display: inline-block;
position: relative;
width: 100%;
height: 0;
}
}