I am building a menu. I have this code:
<nav>
<ul>
<li>Kontakt</li>
<li>Reference</li>
<li>Moje služby</li>
<li>Kdo jsem</li>
</ul>
</nav>
And CSS:
nav{
width: 100%;
height: 90px;
border-top: 3px solid red;
border-bottom: 1px solid gray;
background-color: white;
}
nav li{
float: right;
padding: 20px 35px 0 0px;
}
nav ul{
margin-right: 100px;
height: 90px;
list-style-type: none;
}
nav a{
text-decoration: none;
color: black;
font-size: 17px;
font-family: Montserrat;
font-weight: 700;
}
nav a:hover{
text-align: center;
color: 33adae;
What I am trying to do is to make the links clickable like blocks with the height of the whole navbar. The way I have done It so far, you can click only the text in the links.
Generally all that is needed is.
nav a{
display:block;
}
However for a fuller example it's generally easier to let the links determine the height of the header.
For centering, don't use floats, set the ul to text-align:center and the li to display:inline-block.
* {
margin: 0;
padding: 0;
;
box-sizing: border-box;
}
nav {
border-top: 3px solid red;
border-bottom: 1px solid gray;
background-color: white;
overflow: hidden;
/* clearfix */
}
nav li {
display: inline-block;
}
nav ul {
list-style-type: none;
text-align: center;
}
nav a {
display: block;
text-decoration: none;
color: black;
font-size: 17px;
font-family: Montserrat;
font-weight: 700;
height: 90px;
line-height: 90px;
padding: 0 25px;
}
nav a:hover {
text-align: center;
color: 33adae;
background: plum;
<nav>
<ul>
<li>Kontakt
</li>
<li>Reference
</li>
<li>Moje služby
</li>
<li>Kdo jsem
</li>
</ul>
</nav>
You could remove the padding from your lis and add it to your a tags. See example http://codepen.io/anon/pen/gaGxpb
Move your <li> padding to the <a> children, and give the links a height:
See codepen
NB: Added a border to the links so as you see the boundaries.
display the links as blocks display: block; and use the line-height to give them the height you want. Try this:
nav {
width: 100%;
height: 90px;
border-top: 3px solid red;
border-bottom: 1px solid gray;
background-color: white;
}
nav li {
float: right;
padding: 0px 35px 0 0px;
}
nav ul {
margin: 0 100px 0 0;
height: 90px;
list-style-type: none;
}
nav a {
display: block;
line-height: 90px;
text-decoration: none;
color: black;
font-size: 17px;
font-family: Montserrat;
font-weight: 700;
}
nav a:hover {
text-align: center;
color: 33adae;
<nav>
<ul>
<li>Kontakt</li>
<li>Reference</li>
<li>Moje služby</li>
<li>Kdo jsem</li>
</ul>
</nav>
Here is my version using height and with properties instead of padding, i used background colors so you can see how is working: http://codepen.io/aluknot/pen/wKrqaG
HTML:
<nav>
<ul>
<li>Kontakt</li>
<li>Reference</li>
<li>Moje služby</li>
<li>Kdo jsem</li>
</ul>
</nav>
CSS:
nav {
width: 100%;
height: 90px;
border-top: 3px solid red;
border-bottom: 1px solid gray;
background-color: white;
}
nav li{
float: right;
background: red;
text-align: center;
height: 100%;
width: 120px;
line-height: 90px
}
nav ul{
margin: 0;
padding-right: 100px;
height: 90px;
list-style-type: none;
background: green;
}
nav a{
text-decoration: none;
color: black;
font-size: 17px;
font-family: Montserrat;
font-weight: 700;
display: block;
width: 100%;
height: 100%;
background: blue;
}
nav a:hover {
background: black;
color: white;
}
Related
I am trying to make a dropdown menu, it's my first time doing it and I'm experimenting with it. The problem that I'm facing is this:
As you can see, the principal menus are showing in a list. I have tried displaying them as flex and other attempts, but it seems like the header is making a limitation to them and I don't know how to put them beside each other. (Clarification: 'Notificaciones' and 'Usuario' are main menus and 'Mi Perfil' is a submenu that comes from 'Usuario' (parent))
Here is my code:
* {
text-decoration: none;
list-style: none;
}
body {
margin: 0px;
padding: 0px;
font-family: roboto;
}
.header-text {
position: relative;
margin: 2px 6px 2px 4px;
cursor: pointer;
}
.header-icons {
width: 32px;
margin: 2px 0px 2px 0px;
cursor: pointer;
}
header {
display: flex;
justify-content: space-between;
background-color: rgb(20, 33, 61);
color: white;
height: 30px;
align-items: center;
padding: 6px;
position: static;
}
.nav li a {
padding: 4px;
background-color: red;
list-style: none;
display: inline-block;
float: right;
}
<header>
<div class="header-text">
<h1 class="titulo-logo">Lorem</h1>
</div>
<nav class="nav">
<ul>
<li>Notificaciones</li>
<li>
Usuario
<ul>
<li>Mi Perfil</li>
</ul>
</li>
</ul>
</nav>
</header>
Thank you so much in beforehand!
First the <li> should have display: inline-block for being arranged in a row. It has nothing to do with the header.
Second, the position of the sub menu (ul ul) needs to be absolute within a li with position: relative.
white-space: nowrap will make the element not wrap when the width is larger than the parent element's width.
* {
text-decoration: none;
list-style: none;
}
body {
margin: 0px;
padding: 0px;
font-family: roboto;
}
.header-text {
position: relative;
margin: 2px 6px 2px 4px;
cursor: pointer;
}
.header-icons {
width: 32px;
margin: 2px 0px 2px 0px;
cursor: pointer;
}
header {
display: flex;
justify-content: space-between;
background-color: rgb(20, 33, 61);
color: white;
height: 30px;
align-items: center;
padding: 6px;
position: static;
}
.nav li a {
padding: 4px;
background-color: red;
list-style: none;
display: inline-block;
float: right;
}
/* added css */
ul li{
display: inline-block;
position: relative;
white-space: nowrap
}
ul ul{
position: absolute;
right:0;
top:100%
}
<header>
<div class="header-text">
<h1 class="titulo-logo">Lorem</h1>
</div>
<nav class="nav">
<ul>
<li>Notificaciones</li>
<li>
Usuario
<ul>
<li>Mi Perfil</li>
</ul>
</li>
</ul>
</nav>
</header>
I am trying to place an unordered list underneath two headers. I currently have the two headers aligned right and floated right, and they seem to be in the right position. However, when I try to align and float the unordered list to the right or use margins, it goes to very strange positions on the page.
body {
background-color: forestgreen;
color: yellow;
}
ul {
list-style-type: none;
align: right;
float: right;
padding: 0px;
width: 200px;
background-color: greenyellow;
text-align: center;
border: 2px solid yellow;
}
li a {
display: block;
margin: 0;
padding: 8px 16px;
color: darkcyan;
text-decoration: none;
font-family: Helvetica;
}
li a.active {
background-color: yellow;
color: red;
}
li a:hover:not(.active) {
background-color: skyblue;
color: forestgreen;
text-decoration: underline wavy brown;
}
h1 {
float: right;
align: right;
font-family: Helvetica;
font-size: 50px;
padding-right: 10px;
}
h2 {
float: right;
align: right;
font-family: Helvetica;
font-size: 30px;
margin-left: 1200px;
padding-right: 30px;
}
<h1>Welcome to ENlightenment!</h1>
<h2>Can you feel the love?</h2>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>Programming</li>
<li>Japanese</li>
<li>Russian</li>
<li>Video Games</li>
<li>Music</li>
</ul>
And here is an image explaining what I am trying to do:
Webpage plan
The best way to do this would be Flex Box.
I edited some parts of your code to see. with the arrival of CSS3, people won't use float / position to do this kind of stuff.
<!DOCTYPE html>
<html>
<head>
<title>ENlightenment</title>
</head>
<body>
<style>
body {
background-color: forestgreen;
color: yellow;
}
ul {
list-style-type: none;
padding: 0px;
width: 200px;
background-color: greenyellow;
text-align: center;
border: 2px solid yellow;
}
li a {
display: block;
margin: 0;
padding: 8px 16px;
color: darkcyan;
text-decoration: none;
font-family: Helvetica;
}
li a.active {
background-color: yellow;
color: red;
}
li a:hover:not(.active) {
background-color: skyblue;
color: forestgreen;
text-decoration: underline wavy brown;
}
h1 {
/* float: right; */
font-family: Helvetica;
font-size: 50px;
padding-right: 10px;
}
h2 {
/* float: right; */
font-family: Helvetica;
font-size: 30px;
/* margin-left: 1200px; */
padding-right: 30px;
}
.navBar {
display: flex;
justify-content: space-between;
}
</style>
<h1>Welcome to ENlightenment!</h1>
<div class="navBar">
<h2>Can you feel the love?</h2>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>Programming</li>
<li>Japanese</li>
<li>Russian</li>
<li>Video Games</li>
<li>Music</li>
</ul>
</div>
</body>
</html>
I am trying to get my nav bar to display horizontally in a line at the top of my web page below my h1 tag. I tried all sorts of different methods to get it to line up horizontally but it won't change. My nav bar continues to display vertically centered. I want it to be a thin bar up at the top below the h1. As of right now, it displays with each li stacked on top of the other instead of side by side. Any help or ideas are appreciated.
#wrapper{
background-color: #FFFFFF;
color: #000066;
min-width: 700px;
max-width: 1024px;
margin-right: auto;
margin-left: auto;
padding-top: 0px;
}
h1 {
background-color: darkcyan;
color: #74ebd5;
background-position: center;
background-repeat: no-repeat;
text-align: center;
font-size: 3em;
line-height: 80%;
padding: 30px;
text-shadow: #CCCCCC;
margin-bottom: 0;
}
main {
margin-left: 180px;
padding-bottom: 100px;
}
ul {
list-style-type: none;
margin: 0;
padding: 10px;
overflow: hidden;
background-color: #333;
width: 100%;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
nav{ display:inline-block;
width: 100%;
font-weight: bold;
background-color: grey;
}
nav ul {
list-style: none;
width: 100%;
border: 1px solid ;
text-align: right;
display:inline-block;
}
nav a{
text-decoration: none;
border: 1px solid;
width: 100%
}
nav a:link{color:cyan;}
nav a:visited{color:#6699FF;}
nav a:hover{color: gold;}
<div id="wrapper">
<header>
<h1>Polar Bar</h1>
<nav>
<ul>
<li>Home</li>
<li>Menu</li>
<li>About Us</li>
<li>Contact</li>
<li>Social</li>
</ul>
</nav>
Make the li elements inline-block, and add box-sizing:border-box:
CSS: #wrapper {
background-color: #FFFFFF;
color: #000066;
width: 700px;
max-width: 1024px;
margin-right: auto;
margin-left: auto;
padding-top: 0px;
}
h1 {
background-color: darkcyan;
color: #74ebd5;
background-position: center;
background-repeat: no-repeat;
text-align: center;
font-size: 3em;
line-height: 80%;
padding: 30px;
text-shadow: #CCCCCC;
margin-bottom: 0;
}
main {
margin-left: 180px;
padding-bottom: 100px;
}
ul {
list-style-type: none;
margin: 0;
padding: 10px;
overflow: hidden;
background-color: #333;
width: 100%;
box-sizing: border-box;
}
li {
display: inline-block;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
box-sizing: border-box;
}
nav {
display: inline-block;
width: 100%;
font-weight: bold;
background-color: grey;
}
nav ul {
list-style: none;
width: 100%;
border: 1px solid;
text-align: right;
display: inline-block;
}
nav a {
text-decoration: none;
border: 1px solid;
width: 100%;
}
nav a:link {
color: cyan;
}
nav a:visited {
color: #6699FF;
}
nav a:hover {
color: gold;
}
<div id="wrapper">
<header>
<h1>Polar Bar</h1>
<nav>
<ul>
<li>Home</li>
<li>Menu</li>
<li>About Us</li>
<li>Contact</li>
<li>Social</li>
</ul>
</nav>
I don't understand your question. what did you want?.But Is this you want?
#wrapper{ background-color: #FFFFFF;
color: #000066;
min-width: 700px;
max-width: 1024px;
margin-right: auto;
margin-left: auto;
padding-top: 0px;
}
h1 {background-color: darkcyan;
color: #74ebd5;
background-position: center;
background-repeat: no-repeat;
text-align: center;
font-size: 3em;
line-height: 80%;
padding: 30px;
text-shadow: #CCCCCC;
margin-bottom: 0;}
main {margin-left: 180px;
padding-bottom: 100px;
}
ul {
list-style-type: none;
margin: 0;
padding: 10px;
overflow: hidden;
background-color: #333;
width: 98% !important;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 20px;
text-decoration: none;
}
nav{ display:inline-block;
width: 100%;
font-weight: bold;
background-color: grey;
}
nav ul {
list-style: none;
width: 100%;
border: 1px solid;
text-align: right;
display: inline-block;
}
nav > ul > li {
float: left;
display: inline-block;
}
nav a{text-decoration: none;
border: 1px solid;
}
nav a:link{color:cyan;
}
nav a:visited{color:#6699FF;}
nav a:hover{color: gold;}
<div id="wrapper">
<header>
<h1>Polar Bar</h1>
<nav>
<ul>
<li>Home</li>
<li>Menu</li>
<li>About Us</li>
<li>Contact</li>
<li>Social</li>
</ul>
</nav>
</header>
</div>
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 header on my website, with a list in it. The list is aligned properly on 100% size, but when I ctrl-scroll to expand, the text in the list goes out of the header area.
HTML
body {
margin: 0;
}
.header {
background-color: #606060;
text-align: center;
color: #ffffff;
font-family: 'Montserrat', sans-serif;
font-size: 125%;
height: 4.5%;
width: 100%;
line-height: 50%;
margin-left: auto;
margin-right: auto;
position: fixed;
overflow: hidden;
top: 0px;
}
#headerLinks {
list-style-type: none;
text-shadow: 3px 3px 3px #aaaaaa;
}
#headerLinks li {
display: inline-block;
padding-left: 2%;
padding-right: 2%;
text-align: center;
color: #ffffff;
}
#headerLinks a {
color: #ffffff;
text-decoration: none;
}
#headerLinks a:hover {
color: #cccccc;
}
.introContent {
background-color: #cccccc;
height: 40%;
width: 100%;
margin-top: 2%;
}
<div class="header">
<div id="headerLinks">
<ul>
<li>Home
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
</div>
</div>
I want the list text to remain inside the header at all times.
EDIT: If I remove the height, since there is a position:fixed; the other containers will get overlapped by the header on zooming.
In your .header class, remove the height attribute - the browser will set the height of that div based on the content inside it (in this case, your menu items).
body {
margin: 0;
}
.header {
background-color: #606060;
text-align: center;
color: #ffffff;
font-family: 'Montserrat', sans-serif;
font-size: 1.25em;
height: 2.5em;
width: 100%;
line-height: 50%;
margin-left: auto;
margin-right: auto;
position: fixed;
overflow: hidden;
top: 0px;
}
#headerLinks {
list-style-type: none;
text-shadow: 3px 3px 3px #aaaaaa;
}
#headerLinks li {
display: inline-block;
padding-left: 2%;
padding-right: 2%;
text-align: center;
color: #ffffff;
}
#headerLinks a {
color: #ffffff;
text-decoration: none;
}
#headerLinks a:hover {
color: #cccccc;
}
.introContent {
background-color: #cccccc;
height: 40%;
width: 100%;
margin-top: 2%;
}
<div class="header">
<div id="headerLinks">
<ul>
<li>Home
</li>
<li>About
</li>
<li>Contact
</li>
</ul>
</div>
</div>
If you want to be able to scale everything relatively when you zoom you should use em units instead of percentages.