I want to create a navbar with a ul element in which each item on the navbar is a textless, image-only link. So I set it up like so:
<ul>
<li></li>
</ul>
And then I realized I didn't really know how to get the images in there. I thought of two ideas, but they each have problems:
Put an img tag inside the anchor. This isn't good content/design separation, and also, if I want to make the image change when the user hovers over it (which I do), it gets a lot more complicated.
Use CSS background images, but now I have to give the anchor a width and height equal to the image's dimensions, forcing me to modify my code every time I change the images.
Image-only navbars are very common, so is there a standard way to do this?
I'm using it these way:
CSS
ul{
display:inline-block;
width:350px;
height:150px;
}
ul li{
display:block;
}
ul li a, ul li{
display:inline-block;
width:100%;
height:100%;
}
ul li.fb a{
background:url(http://placehold.it/350x150) no-repeat left center;
}
ul li.tw a{
background:url(http://placehold.it/350x150) no-repeat left center;
}
<ul>
<li class="fb"></li>
<li class="tw"></li>
</ul>
And you can put all these images in sprite, etc...
http://jsfiddle.net/sthAngels/842d4493/
Use both, use image with transparency and bacground-image, so you can change img on hover and transparent img will stretch <a> tag for you:
<a><img src="some-image.jpg" /></a>
and css something like this:
a img{
opacity:0;
}
a{
background-image:url(some-image.jpg);
}
a:hover{
background-image:url(some-other-image.jpg);
}
The most easy way to achieve this is to insert the on-hover-image (which will of course have the same dimensions as the normal image) in the anchor itself, and stack an element on top of it, which will have the 'normal-state' image as a background. Then on hover, hide the stacked element to show the on-hover-image. The anchor itself will now by itself have the images dimension, as will the stacked element.
html:
<ul>
<li><a href class="link1"><img src="link1-hover-image.png"><span></span></a></li>
<li><a href class="link2"><img src="link2-hover-image.png"><span></span></a></li>
<li><a href class="link3"><img src="link3-hover-image.png"><span></span></a></li>
</ul>
css:
a { position: relative; }
a img { vertical-align: bottom; } /* little hack for correct placement */
a span { display: block; position: absolute; width: 100%; height: 100%; top: 0; left: 0; }
a:hover span { opacity: 0; }
.link1 span { background-image: url(link-1-image.png); }
.link2 span { background-image: url(link-2-image.png); }
.link3 span { background-image: url(link-3-image.png); }
DEMO
But like everyone said; wherever possible just use plain css. Much faster, easier to maintain. So only use this if it's reaaally not possible to use css to style your buttons.
Related
I'm sort of new to HTML and currently, I am creating a custom home page for myself containing links to site I often visit.
When I hover over a picture it expands to show more specific links (i.e. subreddits).
However, the problem is that the "sub-link-icons" are not properly aligned with the expanding DIV It will show in front of the bigger picture when hovering over it.
What I am trying to do is have the sub-link-icons to be in sync with the expanding div.*
HTML:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<title>Homepage</title>
</head>
<body>
<div class="submenu" id="steam"><img src="steam.png"></div>
<div class="submenu" id="reddit">
<img src="reddit.png"/>
<ul>
<li><img src="reddit.png"/></li>
<li><img src="reddit.png"/></li>
<li><img src="reddit.png"/></li>
</ul>
</div>
<div class="submenu" id="youtube"><img src="youtube.png"/></div>
</body>
</html>
CSS:
body {
background-color: #330000;
color: white;
}
div img {
width:256px;
height:256px;
border-radius:5px;
}
li img {
width:75px;
height:75px;
border-radius:15px;
}
#youtube:hover {
border: #E6E6E6 solid 4px;
background-color: #E6E6E6;
}
#steam:hover {
border: #12151A solid 4px;
background-color: #12151A;
}
#g2a:hover {
border: #0F1F2E solid 4px;
background-color: #0F1F2E;
}
#reddit:hover {
border: #999999 solid 4px;
background-color: #999999;
}
ul{
position:absolute;
list-style-type: none;
display:none;
margin-left: 125px;
}
.submenu {
border-radius: 5px;
position:relative;
display: inline-block;
margin-left: 0px;
width:256px;
height:256px;
border:4px solid #330000;
text-align:center;
margin-left:5px;
margin-top:5px;
transition: width 1s;
z-index:0;
}
.submenu img {
float:left;
}
.submenu:hover {
width:350px;
transition: width 1s;
}
.submenu:hover img {
float:left;
z-index:2;
}
.submenu ul {
position: absolute;
}
.submenu:hover ul {
display:inline-block;
float:right;
margin-top:-10px;
margin-left:-45px;
position:absolute;
z-index: 1;
}
.submenu:hover ul li img {
float:left;
margin-left: -30px;
margin-top: 12.5px;
}
I've tried searching the web for help but couldn't quite manage it.
JSFIDDLE
Lets go through this step by step.
First issue: On hover, "sub-icon-links" are layered over your big pictures, instead of under it.
This IS fixable with z-index, but first you have to understand how z-index works.
Note: z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).
With z-index you can layer elements that are in the same HTML layer. Because it didn't work I assume you've tried to apply z-index on the sub-menu-links. This wouldn't work because the big picture is not on the same layer as them. If we take a look at your HTML structure you'll see:
<div class="submenu" id="reddit">
<img src="http://www.workatplay.com/files/styles/fpo_medium/public/article/logo/Reddit%20Small.jpg?itok=dclAuuiP"/>
<ul>
<li><img src="http://www.workatplay.com/files/styles/fpo_medium/public/article/logo/Reddit%20Small.jpg?itok=dclAuuiP"/></li>
<li><img src="http://www.workatplay.com/files/styles/fpo_medium/public/article/logo/Reddit%20Small.jpg?itok=dclAuuiP"/></li>
<li><img src="http://www.workatplay.com/files/styles/fpo_medium/public/article/logo/Reddit%20Small.jpg?itok=dclAuuiP"/></li>
</ul>
</div>
To use z-index in this case, you have to see at which points the images or their containers are on the same layer.
Your big image is contained within an anchor tag (a)
Your small images are contained within list items
These list items are contained within an unordered list
This unordered list and the anchor tag are on the same layer. Applying z-index to one of these will fix this issue.
Note: This works different when using things like "position: absolute" and "position: fixed" or any other attribute that changes the position of the element in the HTML stack.
JSFiddle: https://jsfiddle.net/eehdo8wa/5/
What I did:
Added "z-index: -1;" to ".submenu ul"
Removed "z-index: 1;" from ".submenu:hover ul"
Second issue: On hover, the "sub-icon-links" should expand at the same rate as the div expands
So, doing this should be very simple now the pictures are layered under the big picture correctly. Basically, when you think about it, all you should have to do is make the pictures stick to the right side of its parent, so when it expands, the pictures stick to the right side and slide along, taking them into the view.
JSFiddle: https://jsfiddle.net/eehdo8wa/6/
What I did:
I redid some of the CSS to make it so everything is already in the right position before sliding into the view. This is essentially what you want in these cases. In your original fiddle you had a LOT of styling on the hover portions, changing all kinds of styling and spacings, but was it really needed? In the end, no. Now it's all in position behind the big image, ready to slide right into the view.
I have an image 78x26 px that i want to use as buttons for my navigation bar.
What would be the most suitable way of doing this?
I want the image to keep its original size at the moment it doesnt
#navbar li {
background-image:url("../images/btns.png");
}
Im aware that i can do something like this but then again how do i place the text over the images
<li><img src="images/btns.png"></li>
You must also specify your width and height of the <li>. Please try this:
#navbar li {
background-image:url("../images/btns.png");
width: 78px;
height:26px;
}
<li> elements, I believe, are inline and therefore you must do something like this to make them appear side by side:
#navbar li {
background-image:url("../images/btns.png");
width: 78px;
height:26px;
float: left;
}
background-image alone does not scale the image. If you feel like it's not rendering at the original size, it's probably because it's repeating.
use this instead:
background:url("../images/btns.png") no-repeat;
And then
<li>Your text here</li>
You might now need to resize the li to fit your design
It could be better if you load your code online, like on codepen.io
I believe this to be the standards ways:
<nav class="navbar" role="navigation">
<ul class="main-nav">
<li class="menu-1">Menu-Link-text</li>
<li class="menu-2">Menu-Link-text</li>
....
</li>
</ul>
</nav>
.navbar {
overflow:hidden /* trick to force the navbar to wrap the floated <li>s */
}
.main-nav li {
float: left;
display: inline; /* bug fix for older IE */
}
.main-nav a {
display:block; /* bigger click area */
width: 78px;
height:26px;
text-indent: 100%; /* Text in the link for search engines and assistive technologies */
whitespace: nowrap; /* see above */
background: #yourFallBackBackgroundColor url("../images/btns.png") no-repeat 0 0; /* use a sprite with all images in one file and move them within background as required */
}
.menu-1 {
background-position:0 0;
}
.menu-2 {
background-position:-78px 0;
}
.menu-3 {
background-position:-156px 0;
}
I am trying to put a background to a empty anchor tag, but nothing will show up unless i place text there. What is the best approach to having a image in a list item and change it to another image on hover with css.
html
<ul>
<li><a class="nav1" href="http://localhost:8888/fiftyfity/?page_id=2"></a></li>
</ul>
css
ul li a.nav1{
background-image: url("../images/nav1.gif");
width:161px;
height: 49px;
}
ul li a.nav1:hover{
background-image: url("../images/navB1.gif");
width:100px;
height: 20px;
}
Anchor links are inline elements by default, so applying a width and height won't work. You can make them block elements with CSS:
ul li a.nav1 {
display: block;
}
More info: http://www.webdesignfromscratch.com/html-css/css-block-and-inline/
I needed to change the anchor tag into a block level element. It all works now
ul li a.nav1{
display:block;
background-image: url("../images/nav1.gif");
width:161px;
height: 49px;
}
ul li a.nav1:hover{
display:block;
background-image: url("../images/navB1.gif");
width:100px;
height: 20px;
}
I would like to do this so that the clickable region on the tag is size of the LI.
My html looks like:
<li>
Link
</li>
As others have said
li a { display: block; }
should achieve what you're after. But you must also remove any padding from the <li> and set it on the <a> instead. For example:
li { padding: 0; }
li a { display: block; padding: 1em; }
In CSS:
li a {
display: block;
}
Of course, you'll want to make your selector more specific than that.
<ul>
<li class="myClass">
Link
</li>
</ul>
li.myClass a {
display: block;
background-color: #fdd; /* Demo only */
}
http://jsfiddle.net/userdude/jmj2k/
This will make the entire area clickable.
li a { display: block; }
Try this css
li{
border:1px solid black;
height:40px;
}
li a{
border:1px solid red;
display:block;
height:100%;
}
li a{
display: inline-table;
height:95%;
width: 95%;
}
the 95 to anticipate any li margin or padding
If you currently have this same question you can simply add padding to the right place:
li {
//remove any padding or margin attributes from here
}
li a {
display: block;
padding: 20px; //or however big you want the clickable area to be
}
Anchor tags are by default inline elements, so you have to explicitly change them to display as block elements before you can mess with the padding or the margins.
Hope this helps!
Just another option I used is create a transparent png image in photoshop and put it inside the anchor tag, make its position absolute and increase its dimensions to fit that parent div you want and you could have a large clickable area.
<a href="test.html" />
<img id="cover_img" src="cover.png" />
</a>
#cover_img {
display: block;
height: 200px;
width: 193px;
position: absolute;
}
Might be useful in certain circumstances.
I'm not an advanced user of CSS, but I have a decent working knowledge i suppose. I'm tryin to make an unordered list that uses different icons for each list element. I would also like the background colour of the list element to change upon hover.
Is there a way to do this through CSS, or would you just include the icon image within the list element (like below)?
<li><img src="voters.jpg">Voters</li>
Using the list-style-image on the ul level makes all of the icons the same, and I haven't been able to figure out another way. Most examples I've found teach you how to use images in a list, but only if the bulleted images are the same. I'm definitely open to suggestions and improvements on the way I'm trying to do this.
thanks
<div class="content-nav">
<ul>
<li class="instructions">Instructions</li>
<li class="voters">Voters</li>
</ul>
</div>
.content-nav {
font-size:12px;
width:160px;
z-index:0;
}
.content-nav ul {
padding:0 20px;
margin:30px 0;
}
.content-nav li {
padding:5px;
margin:5px 5px;
}
.content-nav li a {
color:#666;
text-decoration:none;
}
.content-nav li.voters a {
background:#FFF;
color:#666;
text-decoration:none;
list-style-image:url(images/voters.jpg);
}
.content-nav li.voters a:hover {
background:#0CF;
color:#000;
}
.content-nav li.instructions a {
background:#FFF;
color:#666;
text-decoration:none;
list-style-image:url(images/voters.jpg);
}
.content-nav li.instructions a:hover {
background:#0CF;
color:#000;
}
You could add background images on each list element, and use padding to push the text away from it.
<ul>
<li class="li1">List 1</li>
<li class="li2">List 2</li>
</ul>
.li1 {
background:url('li1.gif') 50% 50% no-repeat no-repeat;
padding-left: 5px;
}
.li2 {
background:url('li2.gif') 50% 50% no-repeat no-repeat;
padding-left: 5px;
}
Just make sure the padding-left is the same size as the image (or a bit larger if you want spacing)
Using CSS3's :nth-child() selector, does not require additional markup on each <li> element:
Live Demo
HTML:
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
CSS:
ul li:nth-child(1) {
list-style-image: url('http://google-maps-icons.googlecode.com/files/teal01.png');
}
ul li:nth-child(2) {
list-style-image: url('http://google-maps-icons.googlecode.com/files/teal02.png');
}
ul li:nth-child(3) {
list-style-image: url('http://google-maps-icons.googlecode.com/files/teal03.png');
}
Browser Support: IE9+, FF3.5+, SA3.1+, OP9.5+, CH2+
I would suggest to use the icons as background-images for each list element. With this approach you can easily position the "bullet points" also (especially horizontal positioning).
You try to set the list-style-image property on an a element. Try setting it on the li element instead.
If you want to use different icons for each list, than give each list an unique name and use background-image and position it appropriately in CSS.
Just use the image in content property of your pseudo-element ::before:
li.item1::before {
content: url(/Images/icon/item1-icon.svg);
display: inline-block;
width: 1em;
margin-right: 6px;
margin-left: -1em;
}
You obviously need to have a class for each li with different image.
/* Cờ cho language switcher */
.widget_polylang ul {
list-style: none;
}
.lang-item-en:before {
background: url(/wp-content/uploads/2020/03/us.png) no-repeat 0;
padding-left: 20px;
content: "";
}
.lang-item-vi:before {
background: url(/wp-content/uploads/2020/03/vn.png) no-repeat 0;
padding-left: 20px;
content: "";
}
i code for Polylang's language switcher