I am trying to make a navigation menu for a simple html/css site, it uses blocks and unorganized lists to add items to the navigation.
The problem is that I want my navigation to be centered, right now it floats from left to right, is there another way of aligning the listed div other than making it float right? I tried using left:20; but didn't work, here is the code.
As I say, just need that to center slightly to the right, I am making it float to the left so that it organizes the list properly, without it, it'd be a messy list, try it and you'll see what I mean... Thanks for help! :D
body {
font-family: Arial;
}
#nav { /*indexed so I can see it over a content div.*/
z-index:0;
}
ul {
margin: 0px;
padding: 0px;
list-style: none;
}
ul li {
float: left;
width: 8%;
height: 40px;
background-color: #000000;
opacity: 0.8;
line-height: 40px;
text-align: center;
font-size: 90%;
}
ul li a {
text-decoration: none;
color: white;
display: block;
}
ul li a:hover {
background-color: green;
}
ul li ul li{
display: none;
}
ul li:hover ul li {
display: block;
margin-left: 0%;
width: 100%;
background-color: #000000;
}
<div id="nav">
<ul>
<li>Attractions
<ul>
<li>Our Team</li>
<li>Camp Sites</li>
<li>Mission & Vision</li>
<li>Resources</li>
</ul>
</li>
<li>Plan Visit
<ul>
<li>Activities</li>
<li>Parks</li>
<li>Shops</li>
<li>Events</li>
</ul>
</li>
<li>Birthdays
<ul>
<li>Map</li>
<li>Directions</li>
</ul>
</li>
</ul>
</div>
This, I think, gives the nicest result:
#nav {
left:40%
position:relative;
}
When using float, be careful using it without a clearfix hack. A float could end up collapsing your entire site. Link for it here, also an explanation: https://www.w3schools.com/howto/howto_css_clearfix.asp
Related
I have created a menu bar which I tried to make beautiful and align the text right but the a tag overlaps the previous one what I've tried is prioritize one selector over another one. with z-index.
So basically I want the nav bar to look like my website but I want the buttons to be clickable and not overlapping each other for the website.
If you check an early build of my website on Desktop I think you can understand my problem better due to my horrible English writing skills.
Website
and some code
nav ul {
position: inherit;
width: auto;
background: none;
height: auto;
display: flex;
padding-top: 0;
}
nav ul li {
float: left;
align-items: right;
}
nav ul li a {
color: black;
background-color: inherit;
padding: 1em 2em;
text-align: right;
width: 100%;
margin: 0;
}
nav ul li a:hover {
background-color: inherit;
position: absolute;
}
<ul class="ShowDesk HideDesk menull" id="nav">
<li id="exit" class="exit-btn HideDesk"><img src="../images/cancel-button.svg" alt="toggle menu"></li>
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li>Login</li>
</ul>
Replace this:
<ul class="ShowDesk HideDesk menull" id="nav">
<li id="exit" class="exit-btn HideDesk"><img src="../images/cancel-button.svg" alt="toggle menu"></li>
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li>Login</li>
</ul>
by:
<ul class="ShowDesk HideDesk menull" id="nav">
<li id="exit" class="exit-btn HideDesk"><img src="../images/cancel-button.svg" alt="toggle menu"></li>
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li>Login</li>
</ul>
Basically what you need to do is to wrap the li tag or div tag whatsoever inside the anchor tag. By doing this the clickable buttons will automatically take the right position.
Hope you'll find this useful.
If I understood you well, you want to make it like this:
HTML
<body>
<nav>
Home
About
Contact
Login
</nav>
<div class="">
Rest of website
</div>
CSS
nav{
text-align: right;
}
nav a{
margin: 0 2%;
text-decoration: none;
color: black;
}
I'd be happy if it's helpful.
What I eventually did was put out the header section into a different container named navbar-container So I could style the navbar separately from the other containers. Making the width bigger without interfering with the other code. (I dont know in anyway if this is an correct way to code.) And I styled the navbar:
.navbar-container {
text-align: center;
padding: 0.8em 1.2em;
}
/* and removing the width: 100% from here; */
}
nav ul {
position: inherit;
width: auto;
background: none;
height: auto;
display: flex;
padding-top: 0;
}
nav ul li {
float: left;
align-items: right;
}
nav ul li a{
color: black;
background-color: inherit;
padding: 1em 2em;
text-align: right;
margin: 0;
}
nav ul li a:hover {
color: black;
background-color: inherit;
}
I'd want to make the header to my web page where the logo would be on the left hand side and the horizontal navigation bar on the right hand side.
The problem is that whenever I shrink the browser window the navigation bar goes below the border of the header container, thus violating the page's structure.
Like this:
Are there any solutions? I can only think of making the parent container Header accommodate its height to the child containers, but I don't know how to implement that. Or should I use the media queries somehow?
I wrote the following code.
header {
height: 100px;
background-color: gray;
background-image: url('img/background.gif');
}
#logo {
float: left;
}
nav {
float: right;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<header>
<img id="logo" src="images/logo.png">
<nav>
<ul>
<li>Home
</li>
<li>About
</li>
<li>Services
</li>
<li>Gallery
</li>
<li>Blog
</li>
<li>Contacts
</li>
</ul>
</nav>
</header>
You may use the flex model or min-height + overflow to deal with floatting elements. You can also use both together, float properties will be overidden by flex where supported. ,
header {
min-height: 100px;
background-color: gray;
background-image: url('img/background.gif');
display:flex;
flex-wrap:wrap;
justify-content:space-between;
align-items:center;
overflow:hidden; /* for float if flex not supported */
}
#logo {
float: left;
/* to deal with flex */
flex-shrink:0;
margin:auto;/* or 0 or auto auto auto 0 or ... */
}
nav {
float: right;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<header>
<img id="logo" src="http://dummyimage.com/140x45&text=logo.png">
<nav>
<ul>
<li>Home
</li>
<li>About
</li>
<li>Services
</li>
<li>Gallery
</li>
<li>Blog
</li>
<li>Contacts
</li>
</ul>
</nav>
</header>
Try having your li display "inline-block"? I think you'd have to take the block setting off "li a".
Then you may have to give a set width to the ul itself so that it does shrink with the page or use media queries.
I hope this helps :)
When I hover over one of my menu items, the menu moves to the left. Why is that?
HTML:
<nav>
<ul>
<li>Home</li>
<li>Geschiedenis</li>
<li>Team</li>
<li>Agenda
<li>Foto's</li>
<li>Vacatures</li>
<li>Contact</li>
</ul>
</nav>
CSS:
nav {
float: right;
border-radius: 15px;
}
nav ul {
list-style-type: none;
margin-top: 55px;
}
nav li {
float: left;
margin-right: 25px;
position: relative;
}
nav a {
text-decoration: none;
color: gray;
font-size: 20px;
}
nav li:hover a {
color: black;
font-size: 22px;
}
As i can see you need to increase the size of the menu link when use hover on it but if you increase font-size this will dislocate the position of the link and thats the reason you link was moving to left.
Use css transform:scale(1.1,1.1) property to increase the size without change in position.
nav {
float: right;
border-radius: 15px;
}
nav ul {
list-style-type: none;
margin-top: 55px;
}
nav li {
display: inline-block;
vertical-align: top;
margin-right: 25px;
position: relative;
}
nav a {
text-decoration: none;
font-size: 20px;
color: gray;
display:block;
}
nav li:hover a {
color: black;
transform:scale(1.1,1.1);
-moz-transform:scale(1.1,1.1);
-ms-transform:scale(1.1,1.1);
-o-transform:scale(1.1,1.1);
-webkit-transform:scale(1.1,1.1);
}
<nav>
<ul>
<li>Home</li>
<li>Geschiedenis</li>
<li>Team</li>
<li>Agenda
<li>Foto's</li>
<li>Vacatures</li>
<li>Contact</li>
</ul>
</nav>
transform property should be use with prefix to let it support in all browsers.
Please remove nav li:hover a { font-size:22px; } so that it does not move left on hover
Replace float with inline-block for <li>. And you are changing font-size on hover which is creating dancing effect on list items. Better keep same font-size for normal and hover states.
nav {
float: right;
border-radius: 15px;
}
nav ul {
list-style-type: none;
margin-top: 55px;
}
nav li {
display: inline-block;
vertical-align: top;
margin-right: 25px;
position: relative;
}
nav a {
text-decoration: none;
font-size: 20px;
color: gray;
}
nav li:hover a {
color: black;
}
<nav>
<ul>
<li>Home</li>
<li>Geschiedenis</li>
<li>Team</li>
<li>Agenda
<li>Foto's</li>
<li>Vacatures</li>
<li>Contact</li>
</ul>
</nav>
Try playing with flex in css3 to see if that works for growing the text. Also there is an HTML menu tag you may want to have a look at.
I know that when you put float: right to a li element it displays in a reversed order, but how can I fix the order so it displays "correctly" and on the right side of the website? Now it displays in the left side of the website. I've tried to read some old questions but didn't find anything that could help me, and also, how can I make the header display in the middle of the #333333 colored header without padding? Will auto element work?
.header {
background-color: #333333;
height: 50px;
width: 100%;
}
.upper_header ul {
margin: 0px;
float: right;
}
.upper_header li {
display: table-cell;
vertical-align: middle;
float: left;
}
.upper_header a {
text-decoration: none;
color: #fff;
}
<div class="header">
<div class="headerContainer">
<ul class="upper_header">
<li>Home
</li>
<li>About Us
</li>
<li>Team
</li>
<li>News
</li>
<li>Contact Us
</li>
</ul>
</div>
</div>
I've tried putting float: right to the ul element and float: left to the li element, then the order is correct but the position of it is in the left. (Sorry for putting two questions in one thread, didn't want to wait another 30 minutes to submit another question.)
You can set your LI's to display: inline-block then you dont need to use floats.
Inline-block elements then can be aligned using text-align
Note:
Inline-block can cause a space between elements, for more info about then please read this https://css-tricks.com/fighting-the-space-between-inline-block-elements/
.header {
background-color: #333333;
width: 100%;
text-align: right;
}
.upper_header {
width: 100%;
}
.upper_header li {
display: inline-block
}
.upper_header a {
padding: 5px 10px;
display: block;
text-decoration: none;
color: #fff;
}
<div class="header">
<div class="headerContainer">
<ul class="upper_header">
<li>Home
</li>
<li>About Us
</li>
<li>Team
</li>
<li>News
</li>
<li>Contact Us
</li>
</ul>
</div>
</div>
First - you have some mistakes in your code, in CSS you use .upper_header ul, but this is not correct syntax in your context. Right is ul.upper_header (your ul list is not under class upper_header, but on the same level), so it does not have effect for you.
If you don't need so much nested div and not so much classes, prevent using it. Example is below (this is solution with centered menu):
.header ul {
text-align: center;
list-style: none;
}
.header li {
display: inline-block;
}
http://jsfiddle.net/aqhesrjn/4/
Then you can easily play with text-align: right in ul element
.header ul {
text-align: right;
list-style: none;
}
.header li {
display: inline-block;
}
http://jsfiddle.net/aqhesrjn/3/
slightly modified CSS & you are using wrong selector(.upper_header ul)instead of ul.upper_header.
ul.upper_header ==> center to align center
ul.upper_header ==> right to align right
.header {
background-color: #333333;
height: 50px;
width: 100%;
}
ul.upper_header {
margin: 0px;
text-align: center;
}
.upper_header li {
display: inline-block;
}
.upper_header a {
text-decoration: none;
color: #fff;
}
<div class="header">
<div class="headerContainer">
<ul class="upper_header">
<li>Home
</li>
<li>About Us
</li>
<li>Team
</li>
<li>News
</li>
<li>Contact Us
</li>
</ul>
</div>
</div>
you can do this as well ,one of the many options available depending upon ofcourse what you are trying to get as an end result.and you were using that upper_header class in a wrong way,you dont even need that.
.header {
background-color: #333333;
height: 50px;
width: 100%;
}
.headerContainer{width:30%;
float:right;}
.headerContainer ul li {
display:inline;
}
.headerContainer ul li a {
text-decoration: none;
color: #fff;
}
I am trying to make the font size bigger for first two letters of nav links. I know for first letter it can be achieved using :first-letter. However I have tried it using span like below and it's increasing the font size but not getting aligned horizontally. About and Contact links are moving to top, both should be aligned with rest of the links (from bottom). I don't want to set line-height to the main container because the sub links are supposed to display below the main links. Any idea about this?
<li><span>01</span> Architectural Design</li>
CSS
ul li a span {
font-size: 25px;
}
.nav {
font-family: arial;
background: #dddddd;
overflow: hidden;
padding: 10px;
}
a {
color: #000;
text-decoration: none;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
font-size: 12px;
float: left;
margin-right: 30px;
}
ul li a span {
font-size: 25px;
}
<div class="nav">
<ul>
<li>About</li>
<li><span>01</span> Architectural Design</li>
<li><span>02</span> Media</li>
<li><span>03</span> Developments</li>
<li>Contact</li>
</ul>
</div>
you may try this... old school but useful
<div class="nav">
<ul>
<li><span> </span>About</li>
<li><span>01</span> Architectural Design</li>
<li><span>02</span> Media</li>
<li><span>03</span> Developments</li>
<li><span> </span>Contact</li>
</ul>
</div>
If you add vertical-align:top to the span, it will align the words after the span to the top of the span
Fiddle
Other properties include
middle
bottom
baseline
I would set the line-height of the li elements.
li{
font-size:12px;
float:left;
margin-right:30px;
line-height: 25px;
}
JSFiddle
I know you mentioned that you
don't want to set line-height to the main container
but will this work for you?
It can only be used with the pseudo elements :before that make your code much cleaner and easy to read.
.empty:before {
content: "";
font-size: 25px;
}
.nav {
font-family: arial;
background: #dddddd;
overflow: hidden;
padding: 10px;
}
a {
color: #000;
text-decoration: none;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
font-size: 12px;
float: left;
margin-right: 30px;
}
ul li a span {
font-size: 25px;
}
<div class="nav">
<ul>
<li><a class='empty' href="#">About</a></li>
<li><span>01</span> Architectural Design</li>
<li><span>02</span> Media</li>
<li><span>03</span> Developments</li>
<li><a class='empty' href="#">Contact</a></li>
</ul>
</div>