Image Hover-HTML CSS - html

I am having trouble creating a different image to appear when you place your curser over the original image. Heres my code without the rest of my listed buttons:
<nav class="buttons">
<ul>
<li class="left">
<a class="home" href="www.google.com">
<img src="img/Home_2.png"></a></li>
</ul>
</nav>
Css:
.buttons img{width: 190px; margin:0px; padding:0px; margin:0 auto; margin-top:55px;}
.buttons ul{list-style-type: none; margin:0px; padding:0px}
.left{float:left}
.home:hover {background: url(../img/Home_crack.PNG)}
Any suggestions?
EDIT: Okay great suggestions, however when I hover over the "home button" now the stuff in the float tweeks out. Heres the code with the full float properties:
HTML:
<nav class="buttons">
<ul>
<li class="left">
<a class="home" href="www.google.com">
<img src="img/Home_2.png"></a></li>
<li class="left">
<a href="www.google.com" class="menu">
<img src="img/Menu_2.png"></a></li>
<li class="right">
<a href="www.google.com" class="about">
<img src="img/About_2.png"></a></li>
<li class="right">
<a href="www.google.com" class="contact">
<img src="img/Contact_2.png"></a></li>
</ul>
</nav>
CSS:
.buttons img{width: 190px; margin:0px; padding:0px; margin:0 auto; margin-top:55px;}
.buttons ul{list-style-type: none; margin:0px; padding:0px}
.left{float:left}
.home:hover img{display:none}
.home:hover {background: url(../img/Home_crack.PNG);}
.right{float:right}
Basically i wanted to separate 4 buttons.... one in the left float and one in the right float, then on the hover, the buttons would change to a different image....With the new img{display:none} the left float rapidly flashes.

If you want a different img on :hover, you can add it via the :after psuedo element with a background-image.
This approach works well for dynamic img dimensions.
jsFiddle here - different img will be displayed on :hover of a.
a:hover:after {
content: "\A";
position: absolute;
z-index: 99;
width: 100%;
height: 100%;
background: url('http://...');
top: 0px;
left: 0px;
}

You're changing the background of the <a> tag, which happens to be completely filled with the <img>. As such, the background is effectively invisible because the img is completely covering it. Try putting in a
.home:hover img { display: none }
rule as well to hide the img when you are hovered. That'll allow the background to "show".

Try adding the following line, this will hide the orriginal image when hovering the link:
.home:hover img {
display: none;
}
I think the other image is being showed, but the default image won't dissapear and is simply before the hover image.
Now, I do also recommend you to change the display style of the a element to block and give it a fixed size like so because the a element will get a size of 0 when hiding the image element:
.home {
display: block;
width: 100px;
height: 100px;
}
Finally I recommend you to have the default image as a background in the a element too, just to make sure it will appear exactly the same as the hover image. Now you are using an img element to show the default image, a different element to show the hover image. I recommend you to prevent this, and try to use one single element. Simply add this line to achieve this:
.home:hover {
background: url(../img/Home_2.png)
}
Hope this helps!

In addition to everyone else here saying to add this to the css:
.home:hover img { display: none }
You will also need to add a height and width to the a tag that you are putting a background on or the tag will have no content in it and it will not show at all.
.home:hover {
background: url(../img/Home_crack.PNG)
width: [enter the width of the bg image];
height: [enter the height of the bg image];
}

Related

Can I get <li>'s side by side without increasing size of the containing div?

What I'm trying to do
So, I have a <div>, in this <div> is a <ul> populated with <li>, each containing an <img>. I want these images to be lined up side by side so I can write some jquery to make them slide left and make it look like a teleprompter for images.
Problem:
I set the width of the <div> to 400px so that it fits one image as I want it to be centered in the middle of the website (I used margin: 0 auto; for this). I now cannot get the li elements to line up outside of the div to the right, they just appera below it (see image).
So..
Is there a way to get all my <li> elements sitting side by side left to right without increasing the size of the <div>? Or perhaps I can center it some other way and increase the width of the <div>... but then I wouldnt be able to use overflow:hidden to hide the <li> elements before they come on screen with jquery.
HTML
<div id="slider">
<ul class = "slides">
<li class="slide" id="1"><img src="images/Coding.png"></li>
<li class="slide" id="2"><img src="images/Javascript.png"></li>
<li class="slide" id="3"><img src="images/League_of_legends.png"></li>
<li class="slide" id="4"><img src="images/Coding.png"></li>
</ul>
</div>
CSS
#slider{
width:400px;
height:400px;
margin: 0 auto;
border-style:solid;
border-color:#000;
border-width:1px;
}
.slides{
list-style-type: none;
padding:0px;
margin:0px;
}
.slide{
list-style-type: none;
}
.slide img{
width:400px;
height:400px;
}
Image for example (ignore the grey bar, thats just a placeholder for the footer)
Edit: worth noting I tried floating the li's left but they still go beneath, presumably because they dont fit inside the containing div.
Position your ul absolute in the div. Then add display:inline-block to the images/lists. Add position:relative to your div and overflow:hidden
You may want to change the width of .slides a bit. You can use jquery to absolute position the ul differently and get it to slide
#slider{
position:relative;
overflow:hidden;
}
.slides{
position:absolute;
white-space: nowrap;
}
.slide,.slide img{
display:inline-block;
}
EDIT: changed width:...px to white-space: nowrap; as recommended by IMI
http://jsfiddle.net/nLb8ptmy/1/
Just use Flexbox? You can then use margin-left on either the flexbox or its container (or transform:translateX even) to make it scroll).
#slider > ul {
display:flex;
flex-flow:row wrap;
justify-content:center;
list-style:none;
}
<div id="slider">
<ul class = "slides">
<li class="slide" id="1"><img src="images/Coding.png"></li>
<li class="slide" id="2"><img src="images/Javascript.png"></li>
<li class="slide" id="3"><img src="images/League_of_legends.png"></li>
<li class="slide" id="4"><img src="images/Coding.png"></li>
</ul>
</div>
Some prefixing required depending on target browsers.
You can use li {display: inline;} to get it side by side.
http://jsfiddle.net/18yao91v/262/
li{
display: inline;
}
You can use white-space: nowrap; on the parent container to avoid wrapping. Set overflow: hidden to avoid the extra elements to be seen.
Remove the overflow property to check how it is aligned.
#slider {
width: 400px;
height: 400px;
margin: 0 auto;
border-style: solid;
border-color: #000;
border-width: 1px;
overflow: hidden; /* Avoiding other elements to be visible */
}
.slides {
list-style-type: none;
padding: 0px;
margin: 0px;
white-space: nowrap; /* Do not wrap to next line */
}
.slide {
list-style-type: none;
}
.slide img {
width: 400px;
height: 400px;
}
<div id="slider">
<ul class="slides">
<li class="slide" id="1">
<img src="http://placehold.it/400x400">
</li>
<li class="slide" id="2">
<img src="http://placehold.it/400x400">
</li>
<li class="slide" id="3">
<img src="http://placehold.it/400x400">
</li>
<li class="slide" id="4">
<img src="http://placehold.it/400x400">
</li>
</ul>
</div>
You need to display your li elements as blocks and float them left, so they will be listed horizontally.
#slider li{
display:block;
float: left
}
Ones this has been done, you need to hide the rest of the li elements other than the first element. (You can do this by adding a different class to first element).
Using jquery, you can switch that class between li elements and add some animation.

clickable <li> with background-image:

I have some images inside of an <li> that I am wanting to make a link. Only the bottom half of the image is clickable. I have tried different structures and looked into jquery solutions, but want to keep it pure css.
<div class="link">
<ul>
<li>
<a href="url">
<img src="image">
</a>
</li>
</ul>
</div>
.link {
white-space: nowrap;
text-align:center;
}
.link li {
max-width: 23.3%;
display: inline-block;
text-align: center;
vertical-align: middle;
}
.link img {
max-width:100%;
}
Add display:inline-block to the a link and make it wrap around the images, with height:100% and width:100%.
I found a deeper problem. My header was overlapping that area.

HTML position variable

I know this is easy with Javascript but... is there any way with just CSS?
Let's say we have two elements (green and red) within a parent one (beige). The red element should be always to the right of the green one, except if the green one (because of the content) is too big to fit the parent in which case the red one will be over the green one (the normal behaviour would be the red element staying to the right of the green one and therefore being hidden because of the overflow of the parent)
In other words: red.x = min(green.x + green.w, beige.x+beige.w-red.w)
For more info, here's the concrete HTML:
<div class="beige" style="width:250px"> <!-- parent with a given width (unknown until the page is rendered) & overflow hidden -->
<a class="green"> <!-- link with display:inline -->
content
<em class="red"></em> <!-- actually a button, 15 px width -->
</a>
</div>
EDIT: #kyledws answer is awesome but I'll update the question with more info (needed things) such as:
red is only displayed when green:hover (that's why it's inside green)
you don't know beige width in CSS (in the real world beige is inside a with defined width but not known until the page is rendered)
green content is a variable length text, and the reason of red being pushed
if the green content does not fit into the parent, it should show the ellipsis (text-overflow: ellipsis; overflow: hidden)
must work in IE8+
If you're able to wrap the content inside <a> in a span then try this.
HTML
<div class="beige">
<a class="green" href="#">
<span class="content">This is some text.</span><em class="red"></em>
</a>
</div>
CSS
.beige {
background-color: #EBDFA0;
height: 32px;
overflow: hidden;
border: 4px solid #EBDFA0;
white-space: nowrap;
width: 400px;
}
.green {
background-color: #4CA73D;
color: #222;
display: inline-block;
height: 100%;
text-decoration: underline;
vertical-align: middle;
}
.content {
display: inline-block;
height: 100%;
margin-left: 4px;
position: relative;
width: 100%;
max-width: 364px;
top: -50%;
}
.red {
border: 2px solid red;
display: inline-block;
height: 28px;
position: absolute;
width: 28px;
}
Essentially the <a class="green">, <span class="content"> and <em class="red"> need to be display: inline-block and the <span class="content"> has to be width: 100% with a max-width of the different between the <div class="beige"> and the <em class="red" minus any additional padding/margin/border etc. (So in this example, max-width: 364px) By setting a width on the span you force the em outside of it's container but by setting a max-width you stop the em from flowing outside of the main wrapper.
Here is a codepen.io link to an example.
(Note: Most of the CSS above is just to make the example look like your images.)
UPDATE:
To show or hide the <em class="red"> add the :hover pseudo-class to .beige and visibility or opacity to .red. (Use opacity if you want to use a transition.)
.red {
opacity: 0;
}
.beige:hover .red {
opacity: 1;
}
Because the width of <div class="beige"> is unknown you can't use CSS to set max-width on <span class="content">.
(The 100% in max-width: calc(100% - 28px) is width of <a class="green"> not <div class="beige">. I couldn't hack it with pseudo-elements, positioning, floats or different display types like flex either.)
The way around this is to fix the max-width of the <span class="content"> in CSS (as is shown above) or use Javascript to detect the width of <div class="beige"> and then set the max-width.
content.style.maxWidth = beige.clientWidth - red.clientWidth + "px";
I updated the example with visibility and Javascript versions.
Also, I added position: absolute to .red so <span class="content"> doesn't have empty space on the right.
So I found a way that you can handle this in just CSS. I have setup a jsbin with a example. I don't have a fancy slider so you will need to use the inspector tool to resize the width or do it manually.
Basically I set up a dive as a table. Because you can't drop to different rows the right most column is forced to collapse but leaves the block visible because it is position absolute. The size of the container is based on the block which has been changed to a inline block to maintain the coloration and push its parent to become larger. Excuse me not making the styles match exactly what you had int he graphics.
http://jsfiddle.net/93u5E/3/
HTML
<div class="beige">
<div class="table">
<!-- parent with a given width & overflow hidden -->
<ul>
<li><a class="green"> <!-- link with display:inline -->
content
</a>
</li>
<li><em class="red"></em>
<!-- actually a button, 15 px width -->
</li>
</ul>
</div>
</div>
CSS
.beige {
background-color:grey;
overflow:hidden;
white-space:no-wrap;
}
.table{
display:table;
}
ul {
display:table-row;
list-style-type:none;
}
li {
display:table-cell;
position:relative;
}
li:last-child{
width:30px;
}
.green {
display:inline-block;
background-color:green;
}
.red {
border:3px solid red;
display:inline-block;
width:20px;
position:absolute;
right:0px
}
li:hover + li .red{
height:20px;
}
*Updating to include hover

DIV to the right side of the page

I'm having a problem with placing the 'navigation' div (within 5 buttons) to the right side of the page in the '#header' div. The 'navigation' div is still next to the 'logo' div.
Can someone help me to get it to the right side of the page?
CSS code:
body {
background-color: #000000;
margin:0;
padding:0;
}
#header {
width: 100%;
height: 100px;
background-color: 222423;
margin-bottom: 5px
}
#logo {
float: left;
}
#navigation {
display: inline-block;
vertical-align: middle;
}
#content {
height: auto;
}
.knop {
margin-right: 7px;
margin-left: 20px;
vertical-align: middle
}
.plaatje {
position: fixed;
width: 628px;
height: 300px;
margin: -150px auto auto -319px;
top: 50%;
left: 50%;
text-align: center;
}
.DivHelper {
display: inline-block;
vertical-align: middle;
height:100%;
}
HTML code:
<html>
<head>
<link typte="text/css" rel="stylesheet" href="css/style.css" />
</head>
<body>
<div id="header">
<div id="logo">
<img src="images/logo.png" width="90px">
</div>
<div id="navigation">
<img class="knop" src="images/buttonhome.png">
<img class="knop" src="images/buttonoverons.png">
<img class="knop" src="images/buttonproduct.png">
<img class="knop" src="images/buttonmedia.png">
<img class="knop" src="images/buttoncontact.png">
</div>
<div class="DivHelper"></div>
</div>
<img class="plaatje" src="images/headimage.png" >
fkfddkfd
</div>
<div id="footer">
</div>
</body>
</html>
There are multiple approaches to this, and you might have to experiment what works for you.
First of all, there's the position property, if you wanted to place the navigation to the right you'd get:
#navigation
{
position: absolute; /*or fixed*/
right: 0px;
}
This approach is very situational and might break. In some cases even breaking the entire lay-out. Best practices dictate to use this one as little as possible, but sometimes there's no other choice.
The other way, which may or may not work, again, is to use the float property
#navigation
{
float: right;
}
Do like this (use float & dont forget the clear in content div) :
#navigation {
display: inline-block;
vertical-align: middle;
float: right;
}
#content {
clear:both;
height: auto;
}
#navigation {
display: inline-block;
vertical-align: middle;
float: right;
padding-right: 50px;
padding-top: 50px;
}
adjust padding right and top px if u want....
You need to use float in navigation div and some width.
#navigation {
display: inline-block;
vertical-align: middle;
float:right;
}
Update this class and check it should work
Youri,
There are a few ways to accomplish this effect, here is one.
Take a look at this:http://jsfiddle.net/legendarylion/8jKUP/1/
THE HTML
<body>
<div id="header">
<div id="logo">
<!--You may wish to eliminate the "width" property here and use the css to style the image... also, I'm assuming you're going to want to wrap this image in an anchor tag that points back to index.html (or default.html, whatever your homepage is...-->
<img class="example-logo" src="images/logo.png" width="90px">
</div>
<!--Your image code in your original source did not have anchor tags. If you want those to function as a nav, you might as well mark it up like I have it below, wrapping the image inside of a 'nav' element, ul, li, and anchor tag. Also see the CSS comments for ideas on sprite images and sticky menus-->
<nav>
<ul>
<li><!--add your image code back here-->Home
</li>
<li><!--add your image code back here-->Overons
</li>
<li><!--add your image code back here-->Product
</li>
<li><!--add your image code back here-->Media
</li>
<li><!--add your image code back here-->Contact
</li>
</ul>
</nav>
</div>
<div class="DivHelper"></div>
</div>
<div id="footer"></div>
</body>
</html>
THE CSS
/* Make the header relative so we that the 'asbolute' positioning uses it as a reference, also, let's give that header an outline so we can see what we're doing */
#header {
position:relative;
border:1px dashed green;
}
/* Make the nav position asboslute to place it to the right */
nav {
position:absolute;
top:0px;
right:0px;
border:1px dashed blue;
}
/*So what happened? The parent element "header" is referenced by "nav" and "nav" is positioned absolutely relative to that parent in the top right hand corner. Nav will stay there even if the parent container has more elements added to it.
Also, it's worth asking I think... Did you want the menu static, or fixed as the page scrolls? That might be worth looking into as well. Look to the trusty W3C to help you out there: http://www.w3.org/Style/Examples/007/menus.en.html*/
/* Style the Nav (You can add your images right back into the html if you prefer, though you may want to look up how to make a sprite image with the css background property and positioning so they load as one file call, but hey, first thing is first right? */
nav ul li {
list-style-type:none;
display:inline-block;
margin:0 10px;
}
nav ul li a {
text-decoration:none;
}
.example-logo {
height:50px;
width:50px;
background:blue;
}
What we're doing here is declaring a parent element relative, and the element you want styled in the top right corner absolute to that relation.
Also take a look in my comments in that code for some other ideas that I think might be helpful to you.
I used margin-left property like this:
#navigation {
display: inline-block;
vertical-align: middle;
margin-left: 70%;
}
The margin-left will create space out side of element. You can get the left side of element with enough space, then your element will be the right side of the page.
Reference:
https://www.w3schools.com/css/css_margin.asp

How to display two images on the same line (CSS)

I'm currently skinning site for a virtual airline and I need help as to how to get
two images to show up on the same line instead of one breaking onto the next line.
It should be displayed as:
LOGO ICON
But instead it turns into:
ICON
LOGO
Does anyone know how to fix this in the CSS?
Thanks!
Check this jsfiddle
You can make a div for each LOGO and ICON and float them.
<div class="head">
<div class="logo">LOGO</div>
<div class="logo">ICON</div>
</div>
and CSS:
.head { width:100%;}
.logo {float:left; padding:10px;}
First, I have to admit your HTML is screwed up - inline style declarations, incorrect image links, etc.
Replace the #top div with the following in your layout.tpl file:
<!-- Logo + Search + Navigation -->
<div id="top">
<a id="logo" href="<?php echo SITE_URL?>" target="_blank">
<img src="/lib/skins/klm/img/logo.png" alt="Home">
</a>
<img id="fb" src="http://fc04.deviantart.net/fs71/f/2012/295/0/a/facebook_icon_by_x_1337_x-d5ikwkm.png" alt="Facebook">
</div>
Replace the following CSS style declarations with this:
#fb {
float: left;
position: absolute;
display: inline;
width: 50px;
bottom: 0;
right: 0;
}
#logo {
bottom: 0;
display: inline;
left: 0;
position: absolute;
}
#top {
height: 58px;
margin: 0;
padding: 10px 0;
position: relative;
}
try using links instead of using image tags ,,
HTML:
<div class="container">
<a class="one"><a class="two"></a></a>
</div>
CSS:
.one {float:left; background-image: url(../img/logo.png);}
.two {float:right; background-image: url(../img/ico.png);}
or if you still want to use the image tag, you can also use this ..
HTML:
<div class="container">
<img class="one" alt scr="bla">
<img class="two" alt scr="bla">
</div>
CSS:
.container {display:table;}
.one, .two {display:table-column;} -or- .one, .two {display:table-cell;}
if you're going to change the container's size, sure it must fit both of the images.