I have a header that I would like to put a logo in the middle of. I don't want to use bootstrap unless there is no other choice. The logo appears to show in the centre of the navigation bar as I would like, however, the links do not go around it properly. Here is an example of what I want the navbar to look like in terms of positioning: ibb.co/zsGG9FY
#firstpage {
width: 100%;
height: 100%;
}
#firstpage ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
position: -webkit-sticky;
/* Safari */
position: sticky;
top: 0;
}
#firstpage li {
float: left;
}
#firstpage li a {
display: block;
color: white;
text-align: center;
padding: 33px 19px;
text-decoration: none;
font-family: Futura;
font-size: 11px;
}
#firstpage li a:hover {
color: #00CFFF;
}
#firstpage .active {
color: #00CFFF;
}
#firstpage .midlogo {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#firstpage .midlogo img {
width: 100%;
height: 100%;
background-color: white;
<section id="firstpage">
<ul>
<li><a class="active" href="#firstpage">HOME</a></li>
<li>HOW IT WORKS</li>
<li>WHY CHOOSE US</li>
<li class="midlogo">
<img src="logo.png">
</li>
<li>GALLERY</li>
<li>SERVICES</li>
<li>CONTACT US</li>
</ul>
</section>
You have set float: left on <li> tag so it keeps on left position. This should be removed.
And using display: flex layout on ul tag, you can align all items on center.
Attached snippet shows what you need.
#firstpage {
width: 100%;
height: 100%;
display: block;
margin: 0 auto;
}
#firstpage ul {
display: flex;
justify-content: center;
align-items: center;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
position: -webkit-sticky;
/* Safari */
position: sticky;
top: 0;
}
#firstpage li a {
display: block;
color: white;
text-align: center;
padding: 33px 19px;
text-decoration: none;
font-family: Futura;
font-size: 11px;
}
#firstpage li a:hover {
color: #00CFFF;
}
#firstpage .active {
color: #00CFFF;
}
#firstpage .midlogo img {
width: 100%;
height: 100%;
background-color: white;
}
<section id="firstpage">
<ul>
<li><a class="active" href="#firstpage">HOME</a></li>
<li>HOW IT WORKS</li>
<li>WHY CHOOSE US</li>
<li class="midlogo">
<img src="logo.png">
</li>
<li>GALLERY</li>
<li>SERVICES</li>
<li>CONTACT US</li>
</ul>
</section>
My solution uses Flexbox. I've replaced the image with an element with a dashed border so that it's obvious where it is. I've also but a pink background behind each link to show that they are indeed all the same width.
Each normal link has a width of 100% to force them to take up all available space. The logo has flex-shrink:0 so that it retains it's given size.
#firstpage ul {
list-style-type: none;
display: flex;
align-items: center;
justify-content: space-between;
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
position: -webkit-sticky;
/* Safari */
position: sticky;
top: 0;
}
#firstpage li {
width: 100%;
}
#firstpage li:nth-child(odd) {
background: pink;
}
#firstpage li a {
display: block;
text-align: center;
color: white;
padding: 33px 19px;
text-decoration: none;
font-family: Futura;
font-size: 11px;
}
#firstpage li a:hover {
color: #00CFFF;
}
#firstpage .active {
color: #00CFFF;
}
#firstpage .midlogo {
border: white dashed 3px;
width: 50px;
flex-shrink: 0;
height: 50px;
}
<section id="firstpage">
<ul>
<li><a class="active" href="#firstpage">HOME</a></li>
<li>HOW IT WORKS</li>
<li>WHY CHOOSE US</li>
<li class="midlogo">
</li>
<li>GALLERY</li>
<li>SERVICES</li>
<li>CONTACT US</li>
</ul>
</section>
I've removed the classes containing float: left for li items and position: absolute for .midlogo item respectively.
All you need is flex property inside ul item to adjust its 1 dimensional layout.
#firstpage {
width: 100%;
height: 100%;
}
#firstpage ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
position: -webkit-sticky;
/* Safari */
position: sticky;
top: 0;
/************ ADD FLEX DISPLAY ************/
display: flex;
flex-flow: row nowrap;
justify-content: space-around;
}
#firstpage li a {
display: block;
color: white;
text-align: center;
padding: 33px 19px;
text-decoration: none;
font-family: Futura;
font-size: 11px;
}
#firstpage li a:hover {
color: #00CFFF;
}
#firstpage .active {
color: #00CFFF;
}
#firstpage .midlogo img {
width: 100%;
height: 100%;
background-color: white;
}
<section id="firstpage">
<ul>
<li><a class="active" href="#firstpage">HOME</a></li>
<li>HOW IT WORKS</li>
<li>WHY CHOOSE US</li>
<li class="midlogo">
<img src="logo.png">
</li>
<li>GALLERY</li>
<li>SERVICES</li>
<li>CONTACT US</li>
</ul>
</section>
You should read about the flex property- https://developer.mozilla.org/en-US/docs/Web/CSS/flex
Related
I work on a new website, and now I am styling the navbar
I have created a dropdown menu with hover but the menu does not take the list's height as I think so when I hover to the next list in the dropdown menu, the menu disappears.
* {
box-sizing: border-box
}
body,
html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
}
nav {
display: grid;
grid-template-columns: 5rem 1fr;
justify-items: center;
align-items: center;
background-color: #000000;
}
.logo h1 {
color: #FFF;
font-weight: 400;
text-transform: uppercase;
letter-spacing: 5px;
}
.menu {
display: block;
position: relative;
}
.menu li {
color: #d3d3d3;
font-weight: 300;
text-transform: uppercase;
list-style: none;
display: inline-block;
}
.menu ul {
width: 100%;
height: 0;
opacity: 0;
position: absolute;
left: 0;
top: 35px;
background: #00f;
transition: all .5s ease;
}
.menu li:nth-of-type(4) {
height: 150%;
}
.menu>li>a {
display: block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
color: #d3d3d3;
}
.menu ul li {
display: block;
}
.menu li:hover ul {
height: 150%;
opacity: 1;
transform: translateY(0);
overflow: hidden;
}
<nav>
<div class="logo">
<!-- Logo -->
<h1>Hexa</h1>
</div>
<ul class="menu">
<!-- Nav Menu -->
<li class="active">Home</li>
<li>About</li>
<li>Projects</li>
<li>
Parent Link
<ul>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
</ul>
</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</nav>
I tried to change the parent list height and the menu inside it but still now working, do you have any idea about this?
Thank you all for help :D
I just added the z-index property to the menu and it is fixed
I work on a new website, and now I am styling the navbar
I have created a dropdown menu with hover but the menu does not take the list's height as I think so when I hover to the next list in the dropdown menu, the menu disappears.
* {
box-sizing: border-box
}
body,
html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
}
nav {
display: grid;
grid-template-columns: 5rem 1fr;
justify-items: center;
align-items: center;
background-color: #000000;
}
.logo h1 {
color: #FFF;
font-weight: 400;
text-transform: uppercase;
letter-spacing: 5px;
}
.menu {
display: block;
position: relative;
}
.menu li {
color: #d3d3d3;
font-weight: 300;
text-transform: uppercase;
list-style: none;
display: inline-block;
}
.menu ul {
width: 100%;
height: 0;
opacity: 0;
position: absolute;
left: 0;
top: 35px;
background: #00f;
transition: all .5s ease;
}
.menu li:nth-of-type(4) {
height: 150%;
}
.menu>li>a {
display: block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
color: #d3d3d3;
}
.menu ul li {
display: block;
}
.menu li:nth-child(4):hover ul{
height: 150%;
padding: 15px 0px 15px 0px;
opacity: 1;
overflow: hidden;
}
.menu li:nth-child(4) ul{
width: 130px;
padding: 0px;
height: 0%;
opacity: 1;
transform: translateX(250px);
overflow: hidden;
}
.menu li:nth-child(4) ul li{
padding: 0px 12px 0px 12px;
}
.menu li:nth-child(4) ul li a{
color: #fff;
text-decoration: none;
}
<nav>
<div class="logo">
<!-- Logo -->
<h1>Hexa</h1>
</div>
<ul class="menu">
<!-- Nav Menu -->
<li class="active">Home</li>
<li>About</li>
<li>Projects</li>
<li>
Parent Link
<ul>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
</ul>
</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</nav>
I'm trying to mimic this website (but with my own images): https://www.w3schools.com/w3css/tryw3css_templates_band.htm#
Every time I click on more to try to go down to "merchandise", "Extras" or "media" it closes (unless I do it very quickly). How can I fix this?
I know there's probably a better way of doing the drop-down menu in HTML/CSS but this is the only way I know how.
nav {
background-color: black;
height: 3rem;
widows: 100%;
}
.navbar {
height: 100%;
display: flex;
flex-flow: row wrap;
border: 1px solid hotpink;
justify-content: flex-start;
align-items: center;
}
.navbar li {
border: 2px solid green;
list-style-type: none;
height: 100%;
}
.navbar li a {
border: 1px solid orange;
color: white;
text-decoration: none;
padding: 0 20px;
text-transform: uppercase;
display: block;
height: 100%;
line-height: 2rem;
}
.navbar li a:hover {
background-color: #CCCCCC;
color: black;
display: block;
height: 100%;
}
section#header-image {
background-image: url('images/sky.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
width: 100%;
height: 60vh;
display: flex;
flex-flow: column nowrap;
justify-content: flex-end;
align-items: center;
color: white;
}
.moreArrow{
height: 6px;
width: 8px;
margin-left: 5px;
}
ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
ul li:hover ul li a {
color: white;
background-color: black;
text-decoration: none;
padding: 0 20px;
height: 100%;
}
<header>
<nav>
<ul class="navbar">
<li>home</li>
<li>band</li>
<li>tour</li>
<li>contact</li>
<li>more<img src="images/arrow.png" alt="x" class="moreArrow">
<ul>
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
</li>
</ul>
</nav> <!--end of nav-->
<section id="header-image">
<div class="header-text">
<h2>London</h2>
<p>We killing it up in 'ere </p>
</div>
</section>
</header>
So, i made a lil change on your HTML, in order to persist ':hover' state you need to put anything that it has to stay visible inside the hovered item, in this case, a 'span' tag (i did changed from an 'a' tag because you cant nest 'a' tags)
The css is pretty simple from this point, you hide the sub menu and display on hover, anything that its inside the span tag belongs to ':hover' state as well
body {
margin: 0px;
}
nav {
width: 100vw;
height: auto;
border: 1px solid red;
}
nav ul.navbar {
display: flex;
margin: 0;
padding: 0;
}
nav ul.navbar > li {
width: auto;
display: inline-block;
vertical-align: top;
}
nav ul.navbar > li a {
display: inline-block;
text-decoration: none;
color: black;
border: 1px solid red;
padding: 10px 30px;
box-sizing: border-box;
width: auto;
}
nav ul.navbar > li span {
display: inline-block;
text-decoration: none;
color: black;
border: 1px solid red;
position: relative;
padding: 10px 30px;
}
nav ul.navbar > li span:hover ul {
display: inline-block;
}
nav ul.navbar > li span ul {
display: none;
margin: 0;
padding: 0;
position: absolute;
top: 38px;
left: 0px;
width: auto;
border: 1px solid blue;
}
nav ul.navbar > li span ul li {
display: inline-block;
list-style: none;
width: 100%;
}
nav ul.navbar > li span ul li a {
width: 100%;
display: inline-block;
}
<header>
<nav>
<ul class="navbar">
<li>home</li>
<li>band</li>
<li>tour</li>
<li>contact</li>
<li>
<span class="more">more
<ul rel=":D">
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
</span>
</li>
</ul>
</nav> <!--end of nav-->
</header>
Hope this helps
So I am trying to create a navbar for a school project, however I cannot get all the buttons to be evenly spaced across the whole navbar. I have been trying to do this for a while now, and the navbar code is all messy which doesn't help my situation. Any solutons?
The HTML code is:
<ul>
<li> <img src="Images/homepage/logo.png" width="20" height="20" href="#home"> </li>
<div class="other";>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</div>
</ul>
The CSS code is:
ul {
list-style-type: none;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
z-index: 1;
top: 0;
left: 0;
width: 100%;
text-align: center;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
z-index: 1;
width: 100px;
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover{
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color:white;
}
Please help, this has been confusing me for a while now and I would rather not write up a new navbar code unless absolutely necessary!
Use Flex
ul{ display:flex;}
li{flex:1;}
* {
margin: 0;
}
ul {
display: flex;
flex-direction: row;
list-style-type: none;
padding: 0;
background-color: #333;
position: fixed;
z-index: 1;
height: 50px;
line-height: 50px;
width: 100%;
}
li {
flex: 1;
}
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
z-index: 1;
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover {
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color: white;
}
img {
margin: -15px;
}
<ul>
<li>
<img src="https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/68dd54ca-60cf-4ef7-898b-26d7cbe48ec7/10-dithering-opt.jpg" width="40" height="40px" href="#home">
</li>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</div>
</ul>
You can only nest li elements within the ul element. Nesting div inside the ul is not semantically correct. Also, the incorrectly nested div element within ul has a semicolon after its class name: this is not legal.
The flex box module is the best option to use in order to achieve what you are after. You can read more about it here.
One way to get the results you are after is as follows:
ul,
li,
a,
img {
box-sizing: border-box;
padding: 0px;
margin: 0px;
border: 0px;
height: 50px; /* Adjust this value to whatever height you want your nav to be */
}
ul {
position: fixed;
top: 0px;
left: 0px;
right: 0px;
list-style-type: none;
background-color: #333;
text-align: center;
display: flex;
justify-content: space-around;
align-items: center;
}
ul li {
flex: 0 1 100%;
}
ul li a{
display: block;
color: white;
text-decoration: none;
line-height: 50px; /* This value should be the same as your nav's height is in order for text to stay centered. */
padding: 0px 1em;
}
ul li a img {
height: 100%;
width: auto;
padding: 5px; /*You can adjust this value to whatever you want. */
}
li a:hover:not(.active) {
background-color: #111;
}
li a:hover{
text-decoration: none;
color: white;
}
.active {
background-color: #222;
color:white;
}
<ul>
<li> <img src="https://www.w3.org/html/logo/downloads/HTML5_1Color_White.png" href="#home"> </li>
<li>Films</li>
<li>Contact</li>
<li>About</li>
<li>FAQ</li>
</ul>
I hope this is helpful.
I want my text in the middle of the navigation bar. I tried vertical-align:center or middle but that didn't work.
Here is what I have:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
height: 60px;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
Set the line-height property equal to the height of your element :
li a {
height: 60px;
line-height: 60px;
}
In order to vertically centralise the text, you need set a line-height equal to the height of the element. In this case, it's 60px:
li a {
height: 60px;
line-height: 60px;
}
Hope this helps! :)
Use Flexbox (display: flex) to center the text. It's more flexible than line-height (height can be a percentage) and has good browser support. See caniuse.com.
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: flex;
justify-content: center;
align-items: center;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
height: 60px;
}
li a:hover {
background-color: #111;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
.nav{
border:1px solid #ccc;
border-width:1px 0;
list-style:none;
margin:0;
padding:0;
text-align:center;
background-color: #333;
}
.nav li{
display:inline;
}
.nav a{
display:inline-block;
padding:10px;
color: white;
text-align: center;
text-decoration: none;
height: 22px;
}
li a:hover {
background-color: #111;
}
<ul class="nav">
<li><a class="active" href="#home">Home</a></li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
HTML
Home
News
Photos
Videos
Schedule
Links
<ul id="HeaderMenu">
<li>Home</li>
<li>News</li>
<li>Photos</li>
<li>Videos</li>
<li>Schedule</li>
<li>Links</li>
<li>Contact</li>
</ul>
<ul id="footerMenu">
<li>Home</li>
<li>News</li>
<li>Photos</li>
<li>Videos</li>
<li>Schedule</li>
<li>Links</li>
<li>Contact</li>
</ul>
And address them separately in CSS as this :
body{
background: #cccccc url("http://www.paradise.lk/images/pattern.png") no-repeat;
width: 100%;
height:auto;
}
h1 {
text-align: center;
}
p.text{
text-align: center;
}
#HeaderMenu li {
float: left;
}
#HeaderMenu li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#HeaderMenu li a:hover:not(.active) {
background-color: red;
color:black;
}
#footerMenu li {
float: left;
}
#footerMenu li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#footerMenu li a:hover:not(.active) {
background-color: #2d6a05;
color:black;
}
ul.HeaderMenu {
list-style-type: none;
margin: -1px;
overflow: hidden;
background-color: #156611;
}
ul.footerMenu {
list-style-type: none;
margin: -1px;
overflow: hidden;
background-color: #156611;
}
#logo img {
height: 90px;
width: 212px;
}
.box {
background-color: #4b4244;
width: 300px;
padding: 25px;
margin: 25px;
}
.box1 {
background-color:white;
width: 300px;
padding: 25px;
margin: 25px;
}
Now do your styles separately for header and footer menus.
body{
background: #cccccc url("http://hdnaturewall.com/wallpaper/2015/10/sky-blue-wallpaper-background-18-desktop-background.jpg") no-repeat 0px -501px;
}