I really hope I'm not repeating an old question - I'm new to selectors so my terminology might be lacking.
I'm working on a tabbed single-page webpage based around this Default :target with CSS solution. I would like the current tab to have its link highlighted or altered in some way, to indicate the current location.
HTML:
<ul id="tabnav">
<li>Tab 1</li>
<li>Tab 2</li>
</ul>
<a id="tab1"></a>
<a id="tab2"></a>
<div class="tab tab1 default-tab">This is text you should see as you load the page</div>
<div class="tab tab2">This is text that will appear after you click on tab 2</div>
CSS:
.tab {
display:none
}
.default-tab {
display:block
}
:target ~ .default-tab {display:none}
#tab1:target ~ .tab1,
#tab2:target ~ .tab2 {
display: block
}
ul#tabnav a:hover {
background: red;
}
ul#tabnav a:target{
border-width: thick;
border-color: black;
}
It seems like my last element (a:target) doesn't do anything, even though clicking on tabs does bring me to new anchors and change the displayed content. Any suggestions?
:target is the selector for the element on the page that has the id or name of the anchor name in the URL. So ul#tabnav a:target won't match anything, but if you change the selector to a:target, it will style the 2 links you have in the middle of the page, <a id="tab1"></a><a id="tab2"></a>
To style the "active" link in your navigation you'll need to use javascript to add a class on click, then style that class.
var $links = $('#tabnav a');
$links.on('click',function(e) {
e.preventDefault();
$links.removeClass('active');
$(this).addClass('active');
})
.tab {display:none}
.default-tab {display:block}
:target ~ .default-tab {display:none}
#tab1:target ~ .tab1,
#tab2:target ~ .tab2 {
display: block
}
.active {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<ul id="tabnav">
<li>Tab 1</li>
<li>Tab 2</li>
</ul>
<a id="tab1"></a>
<a id="tab2"></a>
<div class="tab tab1 default-tab">
This is text you should see as you load the page
</div>
<div class="tab tab2">
This is text that will appear after you click on tab 2
</div>
You can use the bootstrap tabs it will solve you problem and works fine.
Please check the demo here
Hope this will solve you problem.
Happy Coding :)
Related
Currently I´m highlighting the active tab in my site with just a CSS class,
the problem is that I´m having more and more tabs and this list is getting bigger and every time I add a new tab I also need to add a new selector to the class
Is there a CSS only way to simplify this?
body.students li.students,
body.teachers li.teachers,
body.sports li.sports,
...,
... {
background-color: #000;
}
I´m adding the class to the body for each section
is there a way to do something like this with CSS?
body.[class] > li.[class] {
background-color: #000;
}
Basically I just want to add a property if both (body and li) have the same class
Example for students.html
<body class="students">
<ul>
<li class="students">students</li>
<li class="teachers">teachers</li>
</body>
In this example li.students is the one that will be highlighted
Example for teachers.html
<body class="teachers">
<ul>
<li class="students">students</li>
<li class="teachers">teachers</li>
</body>
In this example li.teachers is the one that will be highlighted
Thanks!
Change your HTML code - introduce a class "current_page" (or whatever you'd like to call it)
<body class="students">
<ul>
<li class="students current_page">students</li>
<li class="teachers">teachers</li>
</ul>
</body>
Or
<body class="teachers ">
<ul>
<li class="students">students</li>
<li class="teachers current_page">teachers</li>
</ul>
</body>
That way, you'll only ever need one selector:
li.current_page {
background-color: #000;
}
You could also do it in JavaScript if you don't like to change your HTML code:
var classname = document.querySelector("body").className;
var li = document.querySelector("li." + classname);
li.className = li.className + " current_page";
There is fiddle with my problem.
As I can understand -- link doesn't work, because when I click on the link, link disappear because :focus isn't active anymore. But I can't come up with solution.
I think it's very common problem, but I didn't found any information about this.
Thanks for any help.
CSS:
#search:focus + #results {
display: block;
}
#results {
display: none;
}
HTML:
<input id="search" type="text"/>
<ul id="results">
<li> First </li>
<li> Second </li>
<li> Third </li>
</ul>
Just add a hover method to #results:
#results:hover{display:block;}
http://jsfiddle.net/gc6L323f/3/
I would suggest in your case to include also :hover pseudo class and make the #results object visible on hover.
Like this :
#results:hover {
display: block;
}
You can check working demo.
Got help earlier on how to get this collapsing menu to work. But now all of a sudden the links in it won't work. they just contract the the menu again. Any ideas?
Thanks a lot!
CSS:
#list, .show {display: none; }
.hide:focus + .show {display: inline; }
.hide:focus { display: none; }
.hide:focus ~ #list { display:block; }
#media print { .hide, .show { display: none; } }
li.folding {list-style-type:none; margin-left:-20px;}
HTML:
<div>
[Link]
[Link]
<ol id="list">
<li class="folding">Item 1</li>
<li class="folding">Item 2</li>
<li class="folding">Item 3</li>
</ol>
</div>
Your code works fine as it is..
Working JSFiddle. ( IN FIREFOX ONLY ! )
The problem is more browser compatibility, run the JSFiddle in firefox and it will work as you expect, on chrome and safari it does not work.
The better solution for this (for all browsers), is to use Javascript/jQuery to hide and show the menu on click of a button, this will work across all mobiles, tables & browsers consistently with minimal code.
Update
Here is a quick example of getting this working using jQuery. You can see the code is very small, easy to read and extend, and will also work on ALL browsers!
VIEW THE JSFIDDLE
HTML:
<div>
[Link]
<ol id="list">
<li class="folding">Item 1</li>
<li class="folding">Item 2</li>
<li class="folding">Item 3</li>
</ol>
</div>
Javascript/jQuery:
// When the page is loaded and ready
$(function(){
// On click of `.toggler` (the <a> with class `.toggler`)
$('.toggler').click(function(){
// Toggle the list menu (toggle means if hidden show it, else hide it)
$('#list').fadeToggle(); // also try.. `slideToggle()`
});
});
The above HTML & Javascript does exactly the same thing, except it will work on all devices!
View the JSFiddle - Javascript/jQuery Version
Since clicking another link makes the first one loose focus, the items in the list go back to hiding.
Since the focus loss is always done before executing the click behaviour, the click does not reach the link since it is not under your mouse anymore.
I'm trying to adjust Bootstrap tabs to make them span the full width of their container. Here's my code (a minimal working example):
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Full Width Tabs using Bootstrap</title>
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
<style>
.full-width-tabs > ul.nav.nav-tabs {
display: table;
width: 100%;
table-layout: fixed; /* To make all "columns" equal width regardless of content */
}
.full-width-tabs > ul.nav.nav-tabs > li {
float: none;
display: table-cell;
}
.full-width-tabs > ul.nav.nav-tabs > li > a {
text-align: center;
}
</style>
</head>
<body>
<div class="tabbable full-width-tabs">
<ul class="nav nav-tabs">
<li class="active">Tab 1</li>
<li>Tab 2</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab-one">
I'm in Tab 1.
</div>
<div class="tab-pane" id="tab-two">
Howdy, I'm in Tab 2. Howdy, I'm in Tab 2. Howdy, I'm in Tab 2. Howdy, I'm in Tab 2.
</div>
</div>
</div> <!-- /tabbable -->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</body>
</html>
I get this (undesired) result:
However, I want the tab "headers" to span the entire width of the tab container - and distribute their individual width's evenly, something like this desired result:
How do I achieve that?
Update 1: Here's a JSFiddle: http://jsfiddle.net/agib/FZy4n/
Update 2: I already had a working widget using custom javascript. However, I'm looking for a solution that integrates seemlessly with Bootstrap and thus relies only on standard Bootstrap javascript.
Update 3: If I remove / comment out
/* table-layout: fixed; */
header widths are taking up all horizontal space as needed. However, their widths are resulting from the length of the header texts and thus not distributed evenly.
This is not what I want either:
Update 4: The upcoming Bootstrap 3 appears to have full-width tabs as a standard component using the class .nav-justified: Bootstrap 3 -> Navs -> Justified nav
Twitter Bootstrap 3 contains a class for this, the nav-justified class.
Use it like so:
<ul class="nav nav-tabs nav-justified">
<li>Foo</li>
<li>Bar</li>
</ul>
Maybe I've found your solution:
Create that class with css:
.take-all-space-you-can{
width:100%;
}
And then apply that class to your li tags into your ul. Doing these all the li takes the space that they can and automatically divide the space in the single row.
DEMO
You can use javascript and jquery.
Building on Nick Bull's answer above, you can dynamically determine the number of tabs on the page using Jquery.
Try this on your html page.
<script type="text/javascript">
$(document).ready(function() {
var numTabs = $('.nav-tabs').find('li').length;
var tabWidth = 100 / numTabs;
var tabPercent = tabWidth + "%";
$('.nav-tabs li').width(tabPercent);
});
</script>
Bootstrap 4
It's now very simple to get full-width Tabs using the nav-fill class. This works on all modern browsers including IE 11.
To get full width tabs, use (sized by tab content):
<ul id="myTabs" class="nav nav-tabs nav-fill">
<li class="nav-item">Home</li>
<li class="nav-item">Profile</li>
<li class="nav-item">Messages</li>
</ul>
Demo
To get full equal width tabs, use nav-justified:
<ul class="nav nav-tabs nav-justified">
<li class="nav-item">...</li>
</ul>
Try
.nav-tabs > li {
float:none;
display: table-cell;
width: 1%;
}
Simple:
.nav-tabs > li {
/* width = (100 / number of tabs). This example assumes 3 tabs. */
width:33.33333%;
}
Hope that helps!
Try this solution, just one line of code to achieve that.
.full-width-tabs > ul > li { width: 100%; }
It will make the tabs to span full width of their container.
Look at jsfiddle : http://jsfiddle.net/BhGKf/
Try this, for all '.nav-pills' and each 'li' element inside with double loop.
function(){
$('.nav.nav-tabs').each(function(){
var liGroup = $(this).children('li');
var liLength= liGroup.length;
liGroup.each(function(){
var liWidth = 100/liLength-1;
$(this).css({'min-width': liWidth+'%','margin-left':'0px','margin-right':'0px'});
});
});}
DEMO http://jsfiddle.net/
<style>
.nav-tabs .nav-item.show .nav-link, .nav-tabs .nav-link.active {
flex: auto;
}
.nav-tabs .nav-link {
flex: auto;
}
</style>
This is what i was looking for.
Is this menu:
<div class="navigation">
<ul id="nav-menu">
<li class="active">
<div class="sifr-r active"><span class="blue-small">01.</span><br />HOME</div>
<div class="blue-line"></div>
</li>
<li>
<div class="sifr-r"><span class="blue-small">02.</span><br />PROPERTY<br />MANAGEMENT</div>
<div class="blue-line"></div>
<ul>
<li>Rental returns</li>
<li class="last">Resources</li>
</ul>
</li>
</ul>
and sIFR:
sIFR.replace(conduititc_light, {
selector: '.sifr-r',
css: [
'a {color: #3c4a4b; text-decoration: none; margin-left: 4}',
'a .blue-small {color: #00bbd6; font-size: 8}',
'a:hover {color: #ffffff}',
'a:hover .blue-small {color: #00bbd6}',
'a.hover {color: #ffffff}'
],
wmode: 'transparent'
});
How save hover effect in sIFR while mouse point on li? Whithout sIFR it has been made with js (mootools):
var nav = $('nav-menu');
nav.getElements('.sifr-r').each(function(item) {
item.getParent().addEvents({
'mouseover': function() {
if (!item.getParent().hasClass('hover')) {
item.getParent().addClass('hover');
}
},
'mouseout': function() {
if (item.getParent().hasClass('hover')) {
item.removeClass('hover');
}
}
});
});
and CSS:
.navigation ul li.hover a.top-link {color: white}
You would need to create the hover effect in your sIFR flash file. sIFR replaces the HMTL element in the page with a Flash SWF. This SWF is not affected by CSS classes at all.
You need to replace the parent element of the <a>, such that the link itself is replaced, rather than the content of the <a> element.
That said, Flash CSS isn't very good with styling elements nested within links, and you're complicating with linebreaks and such. Perhaps a good idea not to use sIFR here.