Here is my css and html
#header {
position: absolute;
left: 0;
right: 0;
top: 0;
background-image: url('/public/images/header.png');
background-repeat: repeat-x;
}
#nav {
background: transparent;
height: 2.5em;
left: -25px;
list-style: none;
margin: 1em 0 1em;
min-width: 100%;
min-height: 100%;
overflow: hidden;
padding: 60px 0 30px 0;
}
#nav a {
color: white;
display: block;
float: left;
height: 2.5em;
padding-left: 30px;
text-decoration: none;
padding-right: 30px;
text-shadow: 0.1em 0.1em #333;
}
#nav a:hover {
text-shadow: 0.1em 0.1em white;
background-color: white;
color: darkred;
padding-bottom: 5px;
text-shadow: 0.1em 0.1em lightgray;
}
#nav a:hover.active, #nav a.active {
background-color: white;
background-position: 0 -60px;
color: darkred;
text-shadow: 0.1em 0.1em lightgray;
padding-bottom: 5px;
}
#nav li {
float: left;
margin: 0 8px 0 0;
/* Spacing between tabs */;
}
#nav span {
cursor: pointer;
display: block;
float: left;
line-height: 1.5em;
padding: .5em 5px 0 0;
}
<div id="header">
<div id="navigation">
<ol id="nav">
<li><a id="overview" href="/overview"><span>Overview</span></a></li>
<li><a id="analysis" href="/overview" class="active"><span>Analysis</span></a></li>
<li><a id="dashboard" href="/dashboard"><span>My Dashboard</span></a></li>
<li><a id="preferences" href="/overview"><span>Preferences</span></a></li>
<li><a id="contact" href="/overview"><span>Contact</span></a></li>
<li><a id="logout" href="/overview"><span>Sign In</span></a></li>
</ol>
</div>
</div>
This looks fine on my screen but if I start resizing the window it makes the tabs jump around and go to the next line. I've found that on other sites with tabs this is not the case. What am I doing wrong here? Thanks.
These "other sites" probably have their <div id="header"> contained in another element, such as <div id="container">, with an explicit width set with CSS, such as width: 960px:
See: http://jsfiddle.net/mBJQN/
<div id="container">
YOUR HTML HERE
</div>
#container {
width: 960px;
position: relative
}
I added position: relative so that the position: absolute on #header will be relative to #container (see here).
There is another option here, but it's probably not the one you're after.
You can add white-space: nowrap to #header, and change from floats to display: inline-block. If this is the option you want, let me know, because it needs a little more work.
http://jsfiddle.net/mBJQN/1/
Related
I have a Navbar with logo and links spaced out appropriately, in the hero section I use an image with full width and height but if leaves a white space between the nav and hero section. I have searched for how to address this but cannot seem to figure it out.
How can I remove the space between the Nav and next section?
Example Image: https://ibb.co/7YcTg4p
*Solved - After adding overflow: auto; inside the #container-bg {} class the white space collapsed and now the nav follows the next section with any space issues.
<header>
<img
src="https://cdn.pixabay.com/photo/2017/09/26/21/45/spiral-
2790215__480.png"/>
<nav class="nav-container">
<ul>
<li>Home
</li>
<li>Portfolio</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
</header>
<div id="container-bg">
<div class="content-wrapper">
<h1>Sample text</h1>
<p>More sample text</p>
Contact
</div>
CSS
header img {
width: 40px;
position: relative;
left: 120px;
top: 15px;
}
.nav-container {
float: right;
}
.nav-container ul {
margin: 0;
}
.nav-container ul li {
display: inline-block;
}
.nav-container ul li a {
text-decoration: none;
padding-right: 60px;
position: relative;
font-size: large;
color: black;
top: 22px;
right: 120px;
margin: 0px 0px 0px 20px;
padding: 0px 4px 6px 4px;
}
#container-bg {
background: url(img/img-bg.jpg) no-repeat center center fixed;
background-size: cover;
background-attachment: fixed;
width: 100%;
height: 100%;
}
.content-wrapper {
margin: 0 auto;
width: 80%;
text-align: center;
position: relative;
top: 30%;
}
.content-wrapper a {
background-color: blue;
color: white;
border-radius: 15px;
text-decoration: none;
text-transform: uppercase;
border: 1px solid #ffffff;
padding: 12px 18px;
font-size: 22px;
cursor: pointer;
I think I did not understand the question but it may help
.content-wrapper {
margin: 0 auto;
width: 80%;
text-align: center;
position: relative;
top: 0%; // here is the trick
}
After adding overflow: auto; inside the #container-bg {} class the white space collapsed and now the nav follows the next section without any spacing or gap issues.
I have a nav bar at the top of each page on my site. Here are the HTML and CSS:
HTML
<ul>
<li><a class="active" href="index.html">Home</a></li>
<li>Shifts</li>
<li>Part Five</li>
<li>Background</li>
</ul>
CSS
ul {
z-index: 100;
position: fixed;
list-style-type: none;
width: 80em;
height: 3em;
margin-left: auto;
margin-right: auto;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
width: 20em;
margin-right: auto;
margin-left: auto;
text-decoration: none;
overflow: hidden;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #87372c;
box-shadow: 0px 0px 50px #87372c;
color: #000000;
font-weight: 800;
text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold, 0 0 40px gold;
}
I've also set up a CodePen here: https://codepen.io/njpbray/pen/Rerabv
The bar isn't centered at all and it has an extra hanging portion to the left that doesn't react to hovering.
I'm not sure what's going on here. I thought that making something of width 80em and settings left and right margins to auto would make it rest center. I also thought an 80em width bar with 4 subdivision of 20em would be equally divided, but there's that bit on the left.
I would prefer to keep the bar fixed at the top though if possible. Some pages are long, and I want the user to be able to access the nav bar anywhere on the page.
To centralise the links, remove the float: left from li and set it to display: inline-block. Then simply set text-align: center on the <ul>.
The above is enough to centralise the blocks, but your content is still offset to the right of the page. To correct this, significantly shrink your width values. em is based on the font size, which is a good policy for text, but a poor choice for width. You can use percentages for responsiveness, or alternatively go with a fixed px unit depending on how 'indented' you want the links to be. I've gone with 100px for the links in my example.
And as you're making use of a <ul> element, you'll also want to remove the default padding, by setting padding: 0 on ul. This removes the left 'offset' you're seeing.
Finally, don't forget to set margin: 0 on body to get rid of that 8px of whitespace around the edges.
This can all be seen in the following:
body {
margin: 0;
}
ul {
z-index: 100;
position: fixed;
list-style-type: none;
width: 100%;
height: 3em;
margin-left: auto;
margin-right: auto;
background-color: #333;
text-align: center;
padding: 0;
}
li {
display: inline-block;
}
li a {
display: block;
color: white;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
width: 100px;
margin-right: auto;
margin-left: auto;
text-decoration: none;
overflow: hidden;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #87372c;
box-shadow: 0px 0px 50px #87372c;
color: #000000;
font-weight: 800;
text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold, 0 0 40px gold;
}
<ul>
<li><a class="active" href="index.html">Home</a></li>
<li>Shifts</li>
<li>Part Five</li>
<li>Background</li>
</ul>
You have the position set to fixed in your code. What you want is to set it to center.
ul {
z-index: 100;
position: center;
list-style-type: none;
width: 80em;
height: 3em;
margin-left: auto;
margin-right: auto;
background-color: #333;
}
https://codepen.io/anon/pen/dgGMva
I think you might be interested in going through the CSS tutorials at w3shcools. You will benefit immensely from this:
https://www.w3schools.com/Css/
The better way to handle what you want to achieve is to wrap you top bar in an element which take the entire width 100% of the browser with it position property set to fixed and add the list as a sub element.
And the space at the left of the first link it's due to the fact that by default the body tag have some margin and padding. You must remove it. the ul element have also same space padding and margin
html, body {
margin: 0;
padding: 0;
}
header {
width: 100%;
position: fixed;
background-color: blue;
}
ul {
z-index: 100;
list-style-type: none;
width: 80em;
height: 3em;
margin: 0 auto;
padding: 0;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding-top: 1em;
padding-bottom: 1em;
width: 20em;
margin-right: auto;
margin-left: auto;
text-decoration: none;
overflow: hidden;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #87372c;
box-shadow: 0px 0px 50px #87372c;
color: #000000;
font-weight: 800;
text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold, 0 0 40px gold;
}
<header>
<ul>
<li><a class="" href="index.html">Home</a></li>
<li>Shifts</li>
<li>Part Five</li>
<li>Background</li>
</ul>
</header>
It's better with css3 and display:flex instead of float:left and inline-block
<header>
<ul>
<li><a class="active" href="index.html">Home</a></li>
<li>Shifts</li>
<li>Part Five</li>
<li>Background</li>
</ul>
</header>
<main>
</main>
body {
margin: 0;
padding: 0;
}
header {
display: flex;
justify-content: center;
}
ul {
background-color: #333;
display: flex;
list-style-image: none;
list-style-type: none;
margin: 0;
padding: 0;
position: fixed;
z-index: 100;
}
li a {
align-items: center;
color: white;
display: flex;
justify-content: center;
padding-top: 1em;
padding-bottom: 1em;
text-decoration: none;
width: 20em;
}
li a:hover:not(.active) {
background-color: #111;
}
.active {
background-color: #87372c;
box-shadow: 0px 0px 50px #87372c;
color: #000000;
font-weight: 800;
text-shadow: 0 0 10px gold, 0 0 20px gold, 0 0 30px gold, 0 0 40px gold;
}
main {
height: 1500px;
}
https://codepen.io/anon/pen/qJbZMv
As mentioned before: CSS3 and flex-box is supported by almost all relevant web-browsers. Use that instead. Here is a link for learning.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I'm trying to get the headerLinks div to wrap around both headerLink divs so that I can move the links and edit the margins of the links as a whole. Is this the best way to do it? And if so, how would I fix my code? Thank you in advance.
This is my code:
#header {
background-color: #EDEDED;
position: fixed;
width: 100%;
top: 0px;
box-shadow: rgb(0, 0, 0) 0px 0px 10px;
}
#headerItems {
list-style-type: none;
overflow: hidden;
margin: 10px;
padding: 0px;
}
#headerName {
float: left;
display: inline-block;
color: #3D3D3B;
font-size: 28px;
}
.headerLinks {
display: inline-block;
float: right;
}
.headerLink {
text-align: center;
margin: 5px;
float: right;
}
.headerLink a {
color: black;
padding: 5px;
background-color: #E1E1E1;
text-decoration: none;
font-size: 20px;
}
<div id="header">
<ul id="headerItems">
<li id="headerName">My name</li>
<div id="headerLinks">
<li class="headerLink">Link 1
</li>
<li class="headerLink">Link 2
</li>
</div>
</ul>
</div>
Only the links on the right should be in a ul.
And I'm pretty sure you don't want the links reversing the order,
So you will need to add float:right on the parent (headerLinks), and float left on the lis
* {box-sizing: border-box;}
body {margin: 0;}
#header {
background-color: #EDEDED;
position: fixed;
width: 100%;
top: 0px;
box-shadow: rgb(0, 0, 0) 0px 0px 10px;
padding: 10px;
}
#headerName {
float: left;
display: inline-block;
color: #3D3D3B;
font-size: 28px;
}
#headerLinks {
list-style-type: none;
overflow: hidden;
padding: 0;
margin: 0;
float: right;
}
.headerLink {
text-align: center;
margin: 5px;
float: left;
}
.headerLink a {
color: black;
padding: 5px;
background-color: #E1E1E1;
text-decoration: none;
font-size: 20px;
}
<div id="header">
<span id="headerName">My name</span>
<ul id="headerLinks">
<li class="headerLink">Link 1</li>
<li class="headerLink">Link 2</li>
</ul>
</div>
If you want to make a dropdown menu, you will nest another ul inside the li
I found this menu that displays border at bottom, i would like to learn how to do it, but i don't understand how its done, is there an easier way that the same thing would be achieved?
Here is the URL: https://codepen.io/atomas/pen/zBoEZe?editors=1100
HTML:
<ul>
<li class="elm selected">Home</li>
<li class="elm">Services</li>
<li class="elm">About</li>
<li class="elm bar">Contact</li>
</ul>
CSS:
$elementsNumber: 4;
$width: 1/$elementsNumber;
* {
box-sizing: border-box;
}
ul {
position: relative;
margin: 50px auto;
width: 80%;
padding: 0;
list-style: none;
color: #000;
overflow: auto;
overflow: hidden;
li {
float: left;
padding: 15px;
font-size: 18px;
font-family: Roboto;
font-weight: 700;
width: percentage($width);
text-align: center;
cursor: pointer;
border-bottom: 4px solid #555;
}
.bar:before {
overflow: hidden;
content: "";
position: absolute;
top: 54px;
bottom: 0;
transition: all 0.25s;
left: 0;
width: percentage($width);
height: 4px;
background: red;
}
}
#for $i from 1 through $elementsNumber {
li:nth-child( #{$i} ) {
&.selected~.bar:before,
&.elm:hover~.bar:before,
&.selected.bar:before,
&.elm.bar:hover:before
{
left: percentage( ( $i - 1 ) * $width );
}
}
}
* { box-sizing: border-box;}
ul {
padding: 0;
list-style: none;
color: #000;
}
li {
float: left;
padding: 15px;
font-size: 18px;
font-family: Roboto;
font-weight: 700;
text-align: center;
border-bottom: 4px solid #555;
transition: all 0.3s;
}
li:hover {
border-bottom: 4px solid red;
}
<ul>
<li class="elm selected">Home</li>
<li class="elm">Services</li>
<li class="elm">About</li>
<li class="elm bar">Contact</li>
</ul>
If you'd just want a border on the bottom of the menu you can just use border-bottom:
body, html {
margin: 0;
padding: 0;
}
header {
background-color: #eee;
}
ul {
list-style-type: none;
padding-left: 0;
margin: 0;
border-bottom: 5px solid #ccc;
}
ul>li {
display: inline-block;
padding: 0 15px 0 15px;
margin-left: -4px;
}
ul>li:first-child {
margin-left: 0;
}
ul>li>a {
display: block;
color: #333;
text-decoration: none;
padding: 5px 0 5px 0;
}
ul>li:hover {
border-bottom: 5px solid #555;
margin-bottom: -5px
}
<header>
<nav>
<ul>
<li class="active">Home
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
</nav>
</header>
That css does a lot more than just adding a border at the bottom.
The grey border you see at the bottom is displayed through:
li {
float: left;
padding: 15px;
font-size: 18px;
font-family: Roboto;
font-weight: 700;
width: percentage($width);
text-align: center;
cursor: pointer;
***border-bottom: 4px solid #555***;
}
However, the css also adds a component which highlights the specific li element which is being hovered over to make the border-bottom red.
The width property at the top just makes sure each li element gets equal space horizontally in the browser:
$elementsNumber: 4;
$width: 1/$elementsNumber;
In order to achieve the same red hover as your codepen illustrates, you will need to write some css such as li:hover etc. to mimic the same effect.
That css you have is definitely more complicated than it needs to be but works for the intended purpose. Taking a look at w3schools link here should help you understand the hover property and other useful properties in CSS.
Hope this helped!
I have a z-index issue on top-navigation with a menu and its sub-menu, i want menu to overlap sub-menu, i have set z-index of menu higher than sub-menu, but it is not working sub-menu is overlapping menu as default.
Please see and suggest any possible way to do it.
jsFiddle
HTML
<div id="login">
<ul>
<li id="overlap">Log In | Join
<ul class="tsm">
<li>Log In</li>
<li>Join</li>
</ul>
</li>
</ul>
</div>
CSS
#login {
margin: 0px auto;
width: 1000px;
height: 38px;
background: #343438;
}
#login ul {
float: right;
margin: 0px;
padding: 0px;
width: auto;
height: 38px;
}
#login ul li {
float: left;
width: auto;
height: 34px;
display: inline;
list-style-type: none;
}
#login ul a {
color: #FFF;
display: block;
width: auto;
height: 34px;
font: bold 16px/34px "Arial Narrow", Arial, sans-serif;
text-decoration: none;
text-align: center;
padding: 0px 15px;
text-shadow: 0px -1px #000;
}
#login .tsm {
padding: 20px;
background-color: #F2F2F4;
width: 230px;
height: auto;
text-align: left;
border: 4px solid #777;
position: absolute;
visibility: hidden;
top: 32px;
right: 0px;
z-index: 100;
}
#login .tsm li a {
width: 230px;
height: 30px;
margin-bottom: 1px;
text-align: left;
padding: 0px;
text-decoration: none;
color: #000;
text-shadow: 0px 1px #fff;
font: 15px/30px Arial, sans-serif;
border-bottom: 1px solid #DDDDDF;
}
#login ul #overlap {
position: relative;
border: 2px solid #900;
z-index: 1000;
}
#login ul li:hover .tsm {
visibility: visible;
}
#login ul ul li {
border: none;
}
The problem stems from the fact that you're trying to position the parent above its child, which causes problems - if the parent moves up a level, so do its children. Instead of z-indexing the parent list, you need to z-index the login link (a tag):
http://jsfiddle.net/SaNJA/
The code is rough, but it should get you started.
Make sure you add a background colour to the link, otherwise you'll still be able to see through it.