Hover effects on class and id tied to each other? - html

I'm working on a custom navbar on a site I'm making but I've run into an issue. I've searched around and have found some similar threads, but nothing that has worked for me. I have a navbar with an image and some text underneath. What I'm trying to accomplish is while hovering over either the text or image, the hover effect for both occurs. The issue is that for the image, the hover effect is done thru an ID and the text hover effect is done thru a class. Here is my markup for one of the sets.
<div class="col-md-2 col-sm-1">
<a href="#Url.Action("Index","Home")" title="HOME" style="margin-left:10px;">
<div id="homenav"></div>
<div class="navtext">HOME</div>
</a>
</div>
And here is the CSS for the image:
#homenav {
margin: 0 auto;
display: block;
width: 47px;
height: 50px;
background: url('../Images/NAV_ICONS/NAV_HOME.png') no-repeat 0 0;
}
#homenav:hover {
background: url('../Images/NAV_ICONS/NAV_HOME_RED.png') no-repeat 0 0;
}
And the CSS for the text:
.navtext{
font-family: RobotoCondensed-Bold, 'Arial Narrow', Arial, sans-serif;
color: #00457c;
overflow: hidden;
white-space: nowrap;
}
.navtext:hover{
color: #ee2e24;
}
And just for clarity, I'm simply trying to make both turn the same color red. Any advice will be greatly appreciated.

Could this fit your needs ?
Put both div (#navbar and .navtext) inside a wrapper div, then put your hover styles in those selectors:
#wrapper:hover #homenav
and
#wrapper:hover .navtex
See fiddle here.
But the hover styles come for a hover over the whole wrapper div.

When using css :hover, the property changes will only affect the element being hovered. You would have to use Javascript/JQuery to do this, but it's pretty straightforward.
First, we need to change the :hover css elements into classes like so
.navtextHover{
color: #ee2e24 !important;
}
.homeNavHover {
background: url('http://lorempixel.com/g/400/200/') no-repeat 0 0 !important;
}
We also need to add a new class. This class will essentially mean that they are all tied together when hovered - ie, when one is hovered, all will be treated as hovered. So add that to the HTML (I called it syncedHover)
<div class="col-md-2 col-sm-1">
<a href="#" title="HOME" style="margin-left:10px;">
<div id="homenav" class="syncedHover"></div>
<div class="navtext syncedHover">HOME</div>
</a>
</div>
Then the javascript merely adds the classes on hover and removes them on exit
$(".syncedHover").hover(
function () {
$("#homenav").addClass("homeNavHover");
$(".navtext").addClass("navtextHover");
},
function () {
$("#homenav").removeClass("homeNavHover");
$(".navtext").removeClass("navtextHover");
}
);
This could be written better by passing data to the JQuery method on hover so you don't have a list of addClass methods, but this isn't too cumbersome for only two elements.
This JSFiddle shows a working example with how it can be done https://jsfiddle.net/bou7mqk3/1/

You just need to put a class on the link instead. As in, the parent a tag that the two elements are nested inside.
In this HTML the only change is the additional class on the link - 'your-class', and I removed the unnecessary syncedHover classes:
<div class="col-md-2 col-sm-1">
<a href="#" title="HOME" style="margin-left:10px;" class="your-class">
<div id="homenav"></div>
<div class="navtext">HOME</div>
</a>
</div>
And then update your CSS to this:
.your-class:hover #homenav{
background: url('../Images/NAV_ICONS/NAV_HOME_RED.png') no-repeat 0 0;
}
.your-class:hover .navtext{
color: #ee2e24;
}
No JavaScript needed at all!

Related

How to make a background in a container and put the text in the middle of the photo? HTML/ CSS

I am learning HTML/ CSS and JS. I am making my first website and I have a problem. How can I change the background in the container under the pictures, to my color. On the internet I only find bg-secondary etc and I need for example #82b5cf.
I also have a question, I want to put the text in the middle of the picture, now it is under the photo and I can't do anything with it, for a test I changed the font and there is no reaction. Thank you very much for your help :)
main {
.aboutus-card-title {
font-size: 30px;
}
}
<main>
<section id="UAV" class="...."> //#82b5cf
<div class="container ">
<div class="row gx-4 ">
<div class="col-sm ">
<img class="uav-photo" src="img/introduction_1.jpg" alt="An orange four-engine drone hovers in the clear blue sky.">
<p class="aboutus-card-title">Introduction</p>
</div>
<div class="col-sm">
<img class="uav-photo" src="img/UAV_features_2.jpg" alt="An orange four-engine drone hovers in the clear blue sky.">
<p class="aboutus-card-title">Elements</p>
</div>
</div>
</div>
</section>
</main>
For your first question, it sounds like you're wanting a fallback color behind your image. Here's an example how to do this from: https://css-tricks.com/css-basics-using-fallback-colors/
header {
background-color: black;
background-image: url(plants.jpg);
color: white;
}
Basically you first create your fallback background-color, then overwrite it with your background-image. If the background-image doesn't load, the background-color will stay.
For your second question about the text with the .aboutus-card-title class, you have your CSS selectors messed up. This is a good source for learning about selectors: https://css-tricks.com/how-css-selectors-work/. If you want to select that class within main your selector should look like this:
main .aboutus-card-title{
font-size: 30px;
}
In this case, you can probably leave off main and just have this:
.aboutus-card-title{
font-size: 30px;
}
The only reason you would want to use the main selector here is if you wanted to style the .aboutus-card-title class differently if it's within main compared to somewhere else.
These are two questions. I'd split them into two separate posts, makes it a bit easier to search.
For changing the background you can use:
background-color: #82b5cf;
or
background: #82b5cf;
For the centering of text on an image, you could use a combination of position, top, left and transform:
.centered-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

Styling hyperlinked images in css

I'm trying to apply anchor tags to an image I had already styled in css by pushing it to the right (using text-align: right on its containing div) and resizing it from 400px to 25px:
css:
.instagram-section {
text-align: right;
font-family: 'Life Savers', cursive;
font-size: 1em;
}
.ig-logo {
height: 25px;
}
But I'm finding that all this styling is lost on the image once it's surrounded in <a> tags:
html (before anchor tags applied to image):
<div class="instagram-section">
<strong>... my Instagram</strong>
<!-- <a href="https://www.instagram.com/myinstagram/"> -->
<img src="/img/icons/instagram/instagram-logo-400.png" class="ig-logo">
<!-- </a> -->
</div>
html (after anchor tags applied to image):
<div class="instagram-section">
<strong>... my Instagram</strong>
<a href="https://www.instagram.com/myinstagram/">
<img src="/img/icons/instagram/instagram-logo-400.png" class="ig-logo">
</a>
</div>
I'm a bit new to html/css and haven't been able to find much on the internet apart from using an image already the size I want. This is fine, but I also need the image to appear on the same line as the text and also aligned to the right.
Any pointers would be appreciated.
Playing around a bit, this seemed to work:
.instagram-section {
text-align: right;
font-family: 'Life Savers', cursive;
font-size: 1em;
}
.instagram-section a img.ig-logo{
height: 25px;
display: inline;
}
Don't know if it's the best method, so welcome better answers.

HTML/CSS removing little line by link when using embedded image

When I use an img tag inside of an a tag, these little lines at the bottom show up. I've tried several css properties to remove them and couldn't find one that did the trick.
Relevant html:
<a href='https://github.com/'>
<img class='ContactLink' src='Images/Icons/GitHub.png' alt='GitHub'>
</a>
<a href='https://twitter.com/'>
<img class='ContactLink' src='Images/Icons/Twitter.png' alt='Twitter'>
</a>
<img class='ContactLink' src='Images/Icons/Gmail.png' alt='Email'>
CSS:
.ContactLink{
height: 50px;
width: 50px;
border: 0;
}
///Add this code in CSS file
a {
text-decoration:none;
}
use normalize css or try this
a,a:focus,a:hover,a:active{
text-decoration:none;
outline-width: 0;
}
img {
display:inline-block;
}
I will suggest to check the image files using any tool like photoshop and see if there is any transparent pixel there for that icon. Also try to keep the dimensions of all the three icon exactly same. If required edit the image accordingly.

How to change background image change on hover

I would like to hover over div id-"RollOver1" and be able to change the background to a different image from the main one. Only pasted the HTML for the rollover div cant use jscript so is there a way in HTML or ....?
<div id="RollOver1" style="position:absolute;overflow:hidden;left:152px;top:397px;width:183px;height:183px;z-index:4">
<a href="./car.html">
<img class="hover" alt="" src="images/Enter_02.jpg" style="left: 0px; top: 0px; width: 183px; height: 183px; display: block;">
<span style="display: none;"><img alt="" src="images/index_01.jpg" style="left:0px;top:0px;width:183px;height:183px"></span>
</a>
</div>
You can do this with the following code:
#RollOver1 {
background:url(INITIAL_BACKGROUND);//here use the url of the background you want when is NOT on hover
}
#RollOver1:hover {
background:url(BACKGROUND_ON_HOVER);//here use the url of the bg you want when is on hover
}
You can use :hover pseudo class:
#RollOver1 {
background: url('img1.png');
}
#RollOver1:hover {
background: url('img2.png');
}
But you will usually see "glich" between changes of images, because second image will take some time to be loaded.
To avoid that, use image sprite. Put both images (normal and hover) to single image and than use css background-position
#RollOver1 {
background: url('sprite.png') no-repeat 0 0;
}
#RollOver1:hover {
background-position: -80px -90px;
}
It will be more efficient way to load small images (like buttons, icons and so on).
Check this link
Using JQuery you can try
$(document).on("mouseover", "#RollOver1", function(e) {
$(this).css("background", "url(sampleImage.png) no-repeat");
}
});
use the css pseudo class :hover
You can use below styles
.RollOver1:hover {
background-image: url('paper.gif');
}

CSS Background-Image not showing up

I am trying to setup background images using CSS but I can't seem to get the images to populate correctly.
Here is the CSS for what I want to do
a.fb {
background-image: url('img/Facebook.png');
}
a.fb:hover {
background-image: url('img/FacebookHover.png');
}
Here is the html code that I am using, I have tried a couple of different ways to populate the images with no luck
<div class="footer">
<a class="fb" href="http://www.facebook.com" target="_blank"></a>
</div>
Any help would be greatly appreciated
Okay added the following and still not go any other thoughts
a.fb {
display:block;
width: 33px;
height: 33px
background-image: url('img/Facebook.png');
}
EDIT: Yup got it working now forgot the ; after height, but no I get a white border around it and tried setting border: none; no luck
a.fb {
border: none;
display:block;
width: 33px;
height: 33px;
background-image: url('img/Facebook.png');
}
An anchor tag by default shows as an inline elements, so it depends on its content in order to get a height and width. To do what you want, you should add some styles: display:block; width: 20px; height: 20px.
You could also change the aproach completely and use html + mouseover and mouseout events:
<div class="footer">
<a class="fb" href="http://www.facebook.com" target="_blank">
<img src="http://www.facebook.com/images/fb_icon_325x325.png" alt="fb" name="fb" width="33px" height="33px" name="image_name" onmouseover="fb.src='http://goo.gl/cxiR7'; fb.width='38'; fb.height='38';" onmouseout="fb.src='http://www.facebook.com/images/fb_icon_325x325.png'; fb.width='33'; fb.height='33';" />
</a>
</div>
Here is a jsBin: http://jsbin.com/onehuw/1/edit
background-image only draws in the space that the element occupies. Your a tag has no content, and therefore it's width is 0. You'll not see any content (and background) until you give it at least some width (and height if needed).
You need to add padding to the <a> tag otherwise it has a width and height of 0 for example:
a.fb {
padding: 20px;
background-image: url('img/Facebook.png');
}
a.fb:hover {
background-image: url('img/FacebookHover.png');
}
You could also just set the width and height of the anchor