I want to create a tabbed view of pages similar to that of the profile page of stack overflow, as can be seen in the image below.
I have been able to create a tabbed interface, but I am unable to remove the border below the tab, because the border has been actually given to the div below. If I give the border to the tab, then I can't extend the border over the area where there is not a tab.
Here is the html that I am using
<div id="centerDiv">
<div id="centeredMenu">
<ul class="tabs">
<li><a class="active" href="#">Questions</a></li>
<li>Blogs</li>
<li>Posts</li>
</ul>
</div>
</div>
<div style="clear:both;"></div>
<div class="favContentBox">
<!-- The content goes here -->
</div>
<div class="favContentBox">
<!-- The content goes here -->
</div>
<div class="favContentBox">
<!-- The content goes here -->
</div>
I have as many favContentBox as there are the ul li elements. And the javascript is
$(document).ready(function(){
var currentTab = 0;
function openTab(clickedTab) {
var thisTab = $(".tabs a").index(clickedTab);
$(".tabs li a").removeClass("active");
$(".tabs li a:eq("+thisTab+")").addClass("active");
$(".favContentBox").hide();
$(".favContentBox:eq("+thisTab+")").show();
currentTab = thisTab;
}
$(".tabs li a").click(function() {
openTab($(this));
return false;
});
$(".tabs li a:eq("+currentTab+")").click()
});
And the css goes like this
.favContentBox
{
border:1px solid #808080;
padding-left:20px;
padding-right:20px;
min-height: 500px;
}
.tabs
{
margin:0 0 0 0;
padding:0 0 0 0;
left:50%;
text-align:center;
clear:left;
position:relative;
float:left;
}
.tabs li
{
list-style: none;
float: left;
right:50%;
display:block;
position:relative;
}
.tabs li a
{
display: block;
color:black;
font-weight: bold;
text-align: center;
text-decoration: none;
width:100px;
padding: 5px 0 5px 0;
border-left: 1px solid #808080;
border-top: 1px solid #808080;
border-right: 1px solid #808080;
margin-left:20px;
background-color:#F0F0F0;
}
Add a white bottom border to the tab and make the tab one pixel smaller than its container (to account for the top border). Here's an all CSS solution for the tab hovering.
Demo: http://jsfiddle.net/ThinkingStiff/sCgMg/
HTML:
<ul id="tabs"><!--
--><li class="tab">summary</li><!--
--><li class="tab selected">answers</li><!--
--><li class="tab">questions</li><!--
--><li class="tab">tags</li><!--
--></ul>
CSS:
#tabs {
border-bottom: 1px solid #666666;
font: bold 15px/15px Helvetica, Tahoma, Arial;
height: 30px;
margin: 0;
padding: 0;
padding-left: 10px;
}
.tab {
color: #777;
cursor: pointer;
display: inline-block;
height: 23px;
font-size: 13px;
line-height: 13px;
margin-left: 2px;
margin-right: 2px;
padding-top: 8px;
text-align: center;
vertical-align: top;
width: 80px;
}
.tab:hover {
border: 1px solid #666666;
border-bottom: 1px solid white;
height: 22px;
margin-top: 5px;
padding-top: 2px;
width: 78px;
}
.selected {
border: 1px solid #666666;
border-bottom: 1px solid white;
color: black;
font-size: 15px;
line-height: 15px;
padding-top: 6px;
width: 78px;
}
.selected:hover {
height: 23px;
margin-top: 0;
padding-top: 6px;
width: 78px;
}
Script:
$( ".tab" ).click( function () {
$( ".selected" ).removeClass( "selected" );
$( this ).addClass( "selected" );
} );
Output:
(Don't just copy and paste! Read the explaination below first!)
HTML:
<ul>
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
<div class="content-boxes">
<div class="content1">Content 1</div>
<div class="content2">Content 2</div>
<div class="content3">Content 3</div>
</div>
CSS:
ul li {
/* I've left out all the floating and other obvious stuff,
since you didn't ask for that */
position: relative;
top: 1px;
background-color: #fff;
border: solid 1px #808080;
border-bottom: none;
z-index: 2;
}
.content-boxes>div {
position: relative;
z-index: 1;
}
The essence of this code is:
Moving the tabs down by 1 pixel while leaving the content boxes' positions as they are with `position: relative; top: 1px;`
Giving the tabs a background color to cover up the content boxes' borders
z-index the tabs on top of the content boxes with `position: relative; z-index: 1/2;` (z-index only works on positioned elements)
I didn't test the code, so you will have to work out the details on your own. For example, this code would push all tabs down instead of just the active one. But I think you get the basic approach.
Hope this helps.
Related
I want to have the entire tab menu to be clickable. In order to do that, I need the "a href" to fill the entire tab but doing so I lose the exact size and shape I want. I'm trying to accomplish by adding the :not(#sub_menu ul il) to the main ul, il{} css styles but it throws the portions out of whack.
So how can I accomplish this in HTML & CSS? Also, the bottom border is still viewable, so how can I fix this as well? Thank you
Adding :not(#sub_menu ul li) to the main p, il settings.
Then increasing the padding by 5px to "10px 20px" in #sub_nav ul li #selected.
Home Page
<body>
<main>
<h3>Home</h3>
<nav id="sub_nav">
<ul>
<li id="selected">Home</li>
<li>Away</li>
</ul>
</nav>
</body>
Away Page
<body>
<main>
<h3>Away</h3>
<nav id="sub_nav">
<ul>
<li>Home</li>
<li id="selected">Away</li>
</ul>
</nav>
</body>
CSS
#sub_nav {
float: left;
width: 100%;
font-weight: bold;
padding-bottom: 5px;
border-bottom: 1px solid;
}
#sub_nav ul {
list-style: none;
padding: 0;
margin: 0;
}
#sub_nav ul li {
display: inline;
border: 1px solid black;
}
#sub_nav ul li a {
padding: 5px 15px;
color: inherit;
text-decoration: none;
border-width: 1px 1px 0px 1px;
}
#sub_nav ul li#selected {
border-bottom: 1px solid white;
}
main {
margin-bottom: 1px;
padding: 1px;
height: 4000px;
width: 80%;
border: 1px solid black;
float: left;
}
main ul {
margin: 10px;
padding: 10px;
}
main p, li {
margin: 5px;
padding: 5px;
}
I have a list ul with items li. Under the list there is a box surrounded with border. The top border of the box is also the bottom border of the list items.
What I want to do know is to remove the bottom border of the active tab. That means removing the top border of the content box along the active tab. Is this possible or do I need to use a different approach?
li {
display: inline-block;
padding-top: 0;
padding: 15px;
border-right: 1px solid #e6e6e6;
cursor: pointer;
border-top: 1px solid #e6e6e6;
font-family: 'Cera';
font-size: 13px;
}
ul {
list-style-type: none;
margin: 0 auto;
border-left: 1px solid #e6e6e6;
padding-left: 0px;
}
.content-box {
display: block;
min-height: 100px;
margin: 0 auto;
border: 1px solid #e6e6e6;
overflow: hidden;
padding-bottom: 10px;
}
.active {
position: relative;
background-color: #f8f8f8;
top: -3px;
}
<ul id="menu">
<li class="active" data-nav="1">Prerender</li>
<li data-nav="2">Prefetch</li>
<li data-nav="3">Preconnect</li>
<li data-nav="4">DNS-prefetch</li>
</ul>
<div class="content-box box1 expanded">
<h3 id="isPrerender"> Prerendered page:</h3>
<ul class="results" id='pagetitle1'></ul>
</div>
Here's how I'd like it to look:
I suggest that you use negative margin to overlap elements.
Use a margin-top:-1px to overlap the top border of the lower box with the bottom edge of the top boxes. This allows the background-color of the active top box to cover the top border of the lower box.
Use margin-left:-1px on all top boxes except the first one to overlap the borders on their left and right sides. Otherwise, with a border on only one side, the active box will be missing a piece of border where it rises above the others.
I've removed the white space between <li> elements because, since they are display:inline-block, that space is rendered as gaps between the boxes.
I'm using additional padding to raise the active top box, instead of using negative top. This keeps the text inside the active box at the same height as the other boxes.
I've aligned the top boxes with vertical-align:bottom to keep them flush against the bottom box.
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline-block;
padding: 15px;
margin-left: -1px;
border-style: solid;
border-color: #e6e6e6;
border-width: 1px 1px 0 1px;
cursor: pointer;
font-family: 'Cera';
font-size: 13px;
vertical-align: bottom;
}
li:first-child {
margin-left: 0;
}
.content-box {
min-height: 100px;
border: 1px solid #e6e6e6;
margin-top: -1px;
padding: 10px;
}
.active {
background-color: #f8f8f8;
padding-top: 18px; /* 15 + 3 */
}
<ul id="menu">
<li data-nav="1">Prerender
</li><li class="active" data-nav="2">Prefetch
</li><li data-nav="3">Preconnect
</li><li data-nav="4">DNS-prefetch</li>
</ul>
<div class="content-box box1 expanded">
<h3 id="isPrerender">Prefetched page:</h3>
<ul class="results" id='pagetitle1'></ul>
</div>
If your idea is to slide down the tab to hide the border , then you should reset vetical-align on li (and eventually mind the white-space) , then increase the padding of 1px (for a one px border) and low it down of that extra pixel(s) like you tried.
li {
display: inline-block;
padding-top: 0;
padding: 15px;
border-right: 1px solid #e6e6e6;
cursor: pointer;
border-top: 1px solid #e6e6e6;
font-family: 'Cera';
font-size: 13px;
vertical-align: bottom;
}
ul {
list-style-type: none;
margin: 0 auto;
border-left: 1px solid #e6e6e6;
padding-left: 0px;
}
.content-box {
display: block;
min-height: 100px;
margin: 0 auto;
border: 1px solid #e6e6e6;
overflow: hidden;
padding-bottom: 10px;
}
.active {
position: relative;
background-color: #f8f8f8;
padding-bottom: 16px;/* increase height of 1 px here, can be any value you want */
top: 1px;/* low it done at least the border's thickness to hide */
}
body {
margin: 1em;
}
<ul id="menu">
<li class="active" data-nav="1">Prerender</li><!-- kill that white space via comments
--><li data-nav="2">Prefetch</li><!--
--><li data-nav="3">Preconnect</li><!--
--><li data-nav="4">DNS-prefetch</li>
</ul>
<div class="content-box box1 expanded">
<h3 id="isPrerender"> Prerendered page:</h3>
<ul class="results" id='pagetitle1'></ul>
</div>
Here's my code :
ul {
width: 100%;
position: relative;
left:10%;
margin: 0;
padding-left: 100px;
}
li {
float:left;
box-align:right;
}
li a {
display: block;
width: 265px;
background-color: #800000;
color: #fff;
text-align: center;
font-weight: bold;
text-transform: uppercase;
padding: 5px;
border-right: 1px solid;
border-left: 1px solid;
}
#list {
background-color: #800000;
padding: 0.02px 0;
margin-bottom: 10px;
overflow: hidden;
width:70%;
text-align:center;
clear:left;
}
<ul>
<center>
<li>Introduction</li>
<li>Biography</li>
<li>Brand New</li>
<li>Pictures</li>
</center>
</ul>
So I've tried playing with the float, width, text-align, box-align, and position all to no avail. I'm just trying to make a simple navigation list with boxes that stretch across the width of my page. I noticed that when I acquired a page of content the list wraps. So I believe it might have something to do with my percentages. I'm not sure though. Any help would be appreciated.
Been trying to learn about CSS and the use of hover psuedo classes over elements. Whenever I select a tab in the navigation bar, the background colour changes colour ( which is what I want). However, when I move off the anchor text the text changes back to white.
Ideally what I'm wanting is for the navigation tab to turn the background colour white, and the text black together simultaneously when the mouse hovers over. Here's the code:
<head>
<link rel="stylesheet" href="css.css" />
</head>
<body>
<div id="container">
<div id="title">
<h1>Record Store</h1>
</div>
<div id="navigation">
<ul class="navbar">
<li>Home
</li>
<li>Vinyl Stock
</li>
<li>Online Offers
</li>
<li>Collectors News
</li>
<li>Contact
</li>
</ul>
</div>
</div>
</body>
and CSS code:
* {
border: 0;
padding: 0;
margin:0;
}
#container {
margin: auto;
width: 800px;
border: 1px solid black;
min-height: 600px;
z-index: -9;
}
#title {
margin:auto;
/*border: 1px solid black;*/
height: 30%;
}
#navigation {
border-top: 1px solid black;
border-bottom: 1px solid black;
height: auto;
overflow: auto;
}
.navbar {
}
.navbar ul {
}
.navbar li {
font: bold 12px/1.2em Arial, Verdana, Helvetica;
height: auto;
list-style: none;
text-align: center;
width: 20%;
float:left;
background-color: blue;
padding: 1% 0px;
}
.navbar a {
border-right: 1px solid #1F5065;
color: white;
display: block;
text-decoration: none;
}
.navbar a:hover {
color: black;
}
.navbar li:hover {
background-color: white;
}
Can someone have a look and point out where I'm going wrong?
You just have to be more conscientious about defining your selectors. You're doing one thing when somebody hover over an li and something else when they hover over the a. What you want is to change both elements when hovering over a common element.
To solve the problem, remove:
.navbar a:hover {
color: black;
}
And replace it with:
.navbar li:hover a {
color: black;
}
jsFiddle
The first selector says "Get all a:hover's that are children in .navbar," The second selector says "Get all a's that are children in li:hover's that are children of .navbar."
I am having a simple layout with a fixed left navigation and a centered page, now the issue in on low resolutions the fixed navigation is comping on the content area which I want to prevent, but I am not able to do so.
Demo
Any idea how I can keep my page centered and even the fixed with div just adjacent to it without overlapping my elements when screen resolution is low
What I want is like this no matter whatever resolution it is in, the page should be centered but the navigation should sit right besides the page and shouldn't overlap page
CSS
.page_wrapper {
min-width: 750px;
max-width: 750px;
margin: 20px auto;
border: 1px solid #ff0000;
}
.content_wrapper {
margin: auto;
max-width: 700px;
margin-left: 120px;
}
p,
ul {
margin: 0;
padding: 0;
}
p {
margin-bottom: 20px;
}
#nav {
left: 300px;
list-style: none;
position: fixed;
top: 20px;
}
#nav li {
margin-bottom: 2px;
}
#nav a {
background: #ededed;
color: #666;
display: block;
font-size: 11px;
padding: 5px 10px;
text-decoration: none;
text-transform: uppercase;
}
#nav a:hover {
background: #dedede;
}
#nav .current a {
background: #666;
color: #ededed;
}
.current {
background: red;
}
.section {
border-bottom: 5px solid #ccc;
padding: 20px;
}
.section p:last-child {
margin-bottom: 0;
}
From what I can tell, your "left" property on #nav is causing it to always position always 300px from the left margin. Removing that keeps the left nav on the left (instead of 300px from the left).
Instead of:
#nav {
left: 300px;
list-style: none;
position: fixed;
top: 20px;
}
try
#nav {
list-style: none;
position: fixed;
top: 20px;
}
See W3 Schools Left Property for more info.
In response to your comment "that will make position navigation to flow on the extreme left of the page" :
Add a margin-left:20px; property
this can be done by
#nav {
padding-left:20px;
padding-top:30px;
list-style: none;
position: fixed;
top: 20px;
}
Live Fiddle
I've made an example fiddle for you, what you just need is a wrapper div and a content div which is floated to right
Demo
I've changed some of the container div layout and basically you can wrap up the contents in your container
HTML
<div class="main_container">
<nav class="content_navigation">
<ul id="nav">
<li class="current">Section 1</li>
<li>Section 2</li>
<li>Section 3</li>
<li>Section 4</li>
<li>Section 5</li>
</ul>
</nav>
<div class="right_content">
<div class="section" id="section-1">
<strong>Section 1</strong>
</div>
<div class="section" id="section-2">
<strong>Section 2</strong>
</div>
<div class="section" id="section-3">
<strong>Section 3</strong>
</div>
<div class="section" id="section-4">
<strong>Section 4</strong>
</div>
<div class="section" id="section-5">
<strong>Section 5</strong>
</div>
</div>
</div>
CSS
html, body {
height: 100%;
width: 100%;
}
.main_container {
width: 900px;
min-width: 900px;
margin: 0 auto;
background: #ffffff;
}
.content_navigation {
width: 205px;
position: fixed;
margin-top: 120px;
}
.right_content {
float: right;
width: 675px;
border-left: 1px solid #252525;
margin-top: 25px;
}
#nav {
list-style: none;
}
#nav li {
margin-bottom: 2px;
}
#nav a {
background: #ededed;
color: #666;
display: block;
font-size: 11px;
padding: 5px 10px;
text-decoration: none;
text-transform: uppercase;
}
#nav a:hover {
background: #dedede;
}
#nav .current a {
background: #666;
color: #ededed;
}
.current {
background: red;
}
.section {
border-bottom: 5px solid #ccc;
padding: 20px;
}
.section p:last-child {
margin-bottom: 0;
}
use left: 50% with negative left-margin to position the #nav from the middle
jsfiddle
#nav {
left: 50%;
margin-left:-350px;
...
}
I tried to make something resembling what you have in the diagram, making #nav absolutely positioned wrt .left_navigation. Also removed the margin on .content_wrapper since it didn't seem to serve a purpose.
.content_wrapper {
/*margin-left: 120px;*/
}
#nav {
left:-77px;
width:76px;
list-style: none;
position: absolute;
top: -1px;
}
.left_navigation{
position:relative;
}
DEMO