I've created simple responsive navigation menu with media queries. I faced the weird situation. After applying text-align:center to center text in blocks, the first li element is moved slightly to left, others are centered fine. I can't figure out what's the issue. I tried margin: 0 auto, it doesn't work for me.
This is what's happening:
Here's the code:
<header>
<div class="logo">Logo</div>
<div class="menu" role="button">
<span></span>
<span></span>
<span></span>
</div>
<nav class="navigation">
<ul>
<li>home</li>
<li>blog</li>
<li>team</li>
<li>contact</li>
</ul>
</nav>
</header>
CSS:
header{
background:grey;
.logo{
font-size:40px;
color:white;
background:blue;
width:200px;
display:inline-block;
}
.menu{
display:inline-block;
float:right;
}
nav{
display:inline-block;
ul{
margin:0;
padding:0;
li{
display:inline-block;
margin: 0 20px;
background:orange;
}
}
}
}
#media screen and(max-width:600px){
header{
.menu{
display:inline-block;
height: 40px;
width: 40px;
margin:10px;
span{
border-top: 5px solid #fff;
display: block;
margin: 5px 0;
}
}
.navigation{
display:block;
ul{
margin:0;
padding:0;
text-align: center;
li{
width:100%;
display:block;
background:orange;
margin:0;
}
}
}
}
}
Related
I am new to css so I thought I'd try to make a website with a navbar. I want to put my navigation bar on the same line as the header(logo). I have been trying to use float left and right to position the header and navbar on the same line but the navbar text never centers with the header.
I have tried to adjust the margin and padding to no avail. As well, tried to move the container class around. I inspected my website using the chrome dev tools and found is contained in a which doesn't show up in my text editor. P.s. I changed the links to #
body{
background-color:#fff;
color:#00f;
font-family:"Raleway", Helvetica, sans-serif;
margin:0;
}
.container{
width:80%;
margin: auto;
overflow: hidden;
}
.clr{
clear:none;
}
#logo{
border:3px solid #00f;
padding-left: 10px;
padding-right: 10px;
float:left
}
#navbar{
margin:0;
float:right;
}
#navbar ul{
padding:0;
list-style:none;
}
#navbar li{
display:inline-block;
}
#navbar a{
color:#00f;
text-decoration:none;
font-size:18px;
padding-right:15px;
}
<body>
<div id="main-header">
<div class="container">
<h1 id="logo">HM<h1>
<nav id="navbar">
<ul>
<li>Home</li>
<li>Projects</li>
<li>Github</li>
<li>Twitter</li>
</ul>
</nav>
<div class="clr"></div>
</div>
</div>
Use flexbox
add this style to your style
.container {
display:flex;
justify-content:space-between;
align-items:center;
}
body{
background-color:#fff;
color:#00f;
font-family:"Raleway", Helvetica, sans-serif;
margin:0;
}
.container {
display:flex;
justify-content:space-between;
align-items:center;
}
#logo{
border:3px solid;
padding:0 10px;
}
#navbar li{
display:inline-block;
}
#navbar a{
text-decoration:none;
font-size:18px;
padding-right:15px;
}
<div id="main-header">
<div class="container">
<h1 id="logo">HM</h1>
<nav id="navbar">
<ul>
<li>Home</li>
<li>Projects</li>
<li>Github</li>
<li>Twitter</li>
</ul>
</nav>
</div>
</div>
Add margin:0 to #nav-bar ul, and the issue will be solved.
I currently have a Header in my HTML which is a Unordered List with List Items display = inline. What I am try to accomplish is to spread each of the 5 items into an equaled space (20% Width for each) and step them centered in their respective spaces. I was able to accomplish this with a lot of ease on the Footer, but instead of List Items, I used Divs for each of the Options. Can anyone help me do the same with list? I can redo it as Divs, but I'd like to at least know how to make the list work.
How it should be
html, body {
margin:0;
padding:0;
height:100%;
}
a:link {
text-decoration:none;
}
#page {
position:relative;
background:#E9EAEE;
min-height:100%;
}
#header {
position:fixed;
left:0px;
top:0px;
z-index:1;
width:100%;
background-color:#3f5c99;
padding-top:10px;
}
#header .holder {
width:100%;
float:left;
}
#header a {
color:#ffffff;
font-size:11px;
font-weight: bold;
padding:0px 10px 0 0;
text-transform:uppercase;
}
#header li {
display:inline;
margin-left:-10px;
padding:0 4px 0 6px;
border-left: 1px solid #2F477A;
}
.logo {
border-radius:2px;
}
<body>
<div id="page">
<div id="header">
<div class="holder">
<ul>
<li style="border-left:0px;"><img class="logo" src="../img/logo.png"/></li>
<li>Comics</li>
<li><img src="../img/friendOff.png"/></li>
<li><img src="../img/mailChatOff.png"/></li>
<li><img src="../img/globeOff.png"/></li>
</ul>
</div>
</div>
...
</div>
</body>
I hope I was able to illustrate what I am trying to accomplish, but if it is still unclear please let me know. Thanks in advance for any help.
You can try like this ,I hope it will helps you.
html, body {
margin:0;
padding:0;
height:100%;
}
a:link {
text-decoration:none;
}
#page {
position:relative;
background:#E9EAEE;
min-height:100%;
}
#header {
position:fixed;
left:0px;
top:0px;
z-index:1;
width:100%;
background-color:#3f5c99;
}
#header .holder {
width:100%;
float:left;
}
#header ul {
display: table;
margin: 0;
padding: 0;
width: 100%;
}
#header a {
color: #ffffff;
display: inline-block;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
}
#header li {
display: table-cell;
padding: 5px 0;
position: relative;
text-align: center;
vertical-align: middle;
width: 20%;
}
#header li:after {
border-left: 1px solid #2f477a;
content: "";
height: 20px;
position: absolute;
right: 0;
top: 8px;
}
.logo {
border-radius:2px;
}
<body>
<div id="page">
<div id="header">
<div class="holder">
<ul>
<li style="border-left:0px;"><img class="logo" src="../img/logo.png"/></li>
<li>Comics</li>
<li><img src="../img/friendOff.png"/></li>
<li><img src="../img/mailChatOff.png"/></li>
<li><img src="../img/globeOff.png"/></li>
</ul>
</div>
</div>
...
</div>
</body>
ul {
display: flex;
}
#header li {
...
flex: 1;
display: flex;
align-items: center;
justify-content: center;
...
}
https://jsfiddle.net/234zzgn7/
Add this css. Ofcourse this is using flexbox which is something that I found to be extremely awesome. But you should investigate whether your users support it or not.
http://caniuse.com/#search=flexbox
Here try this. Changes are on #header li and #header a
html, body {
margin:0;
padding:0;
height:100%;
}
a:link {
text-decoration:none;
}
#page {
position:relative;
background:#E9EAEE;
min-height:100%;
}
#header {
position:fixed;
left:0px;
top:0px;
z-index:1;
width:100%;
background-color:#3f5c99;
padding-top:10px;
}
#header .holder {
width:100%;
float:left;
}
#header a {
color:#ffffff;
font-size:11px;
font-weight: bold;
padding:0px 10px 0 0;
text-transform:uppercase;
display:block;
width:100%;
box-sizing:border-box;
}
#header li {
display:inline-block;
box-sizing:border-box;
width:20%;
margin-left:-10px;
padding:0 4px 0 6px;
border-left: 1px solid #2F477A;
}
.logo {
border-radius:2px;
}
<body>
<div id="page">
<div id="header">
<div class="holder">
<ul>
<li style="border-left:0px;"><img class="logo" src="../img/logo.png"/></li>
<li>Comics</li>
<li><img src="../img/friendOff.png"/></li>
<li><img src="../img/mailChatOff.png"/></li>
<li><img src="../img/globeOff.png"/></li>
</ul>
</div>
</div>
...
</div>
</body>
Probably a really simple solution.
I have two container divs
.topwrap{
position:relative;
top:100px;
background:#00C;
}
and
.lowerwrap{
position:relative;
top:100px;
background:#00C;
}
but .lowerwrap for some unknown reason keeps staying above .topwrap. Within those two containers are images etc that are positioned correctly. Its just that I cant get .lowerwrap to go directly below .topwrap
Sorry if I havent explained it all properly.
Kind regards
(full code)
/*---------------------------- Header ----------------------------*/
.headerwrap{
position:relative;
}
.header{
position:relative;
width:100%;
z-index:1;
}
.header img{
display: block;
margin-left: auto;
margin-right: auto;
width:100%;
}
.logo{
position: absolute;
float:left;
width:15%;
top:5%;
left:43%;
z-index:10;
}
.logo img{
display: block;
margin-left: auto;
margin-right: auto;
}
/*---------------------------- Main Work ----------------------------*/
.menu{
width:100%;
top:0px;
z-index:200;
}
ul {
margin: 0 auto;
padding:0 0 5px 0;
list-style-type: none;
}
li{
display: inline;
list-style:none;
padding:1%;
transition: all 300ms;
}
li a{
color:#A11D22;
transition:300ms;
}
li a:hover {
color:#999;
}
.menutxt{
text-align: center;
font-family: 'Roboto', sans-serif;
font-size:2.5vw;
color:#A11D22;
font-weight:bold;
z-index:3000;
}
/*----------------------------Top ----------------------------*/
.topwrap{
position:relative;
background:#00C;
height:auto;
}
.face{
position:relative;
width:20%;
float:right;
right:15%;
background:#00C;
}
.face img{
display: block;
margin-left: auto;
margin-right: auto;
width:100%;
}
.speech{
position:relative;
width:50%;
float:right;
right:15%;
background:#00C;
}
.speech img{
display: block;
margin-left: auto;
margin-right: auto;
width:100%;
}
/*----------------------------Lower----------------------------*/
.lowerwrap{
position:relative;
background:#00C;
}
<!--===================================================Header===================================================!-->
<div class="headerwrap">
<div class="header">
<img src="images/header.png" />
<div class="logo">
<img src="images/logo.png" />
</div><!--close logo-->
<div class="menu">
<ul class="menutxt">
<li>HOME</li>
<li>PORTFOLIO</li>
<li>CONTACT</li>
</ul>
</div><!--close menu-->
</div><!--close header-->
</div><!--close headerwrap-->
<!--===================================================Top===================================================!-->
<div class="topwrap">
<div class="face">
<img src="images/face.png"/>
</div><!--close face-->
<div class="speech">
<img src="images/speech.png"/>
</div><!--close speech-->
</div><!--close topwrap-->
<!--===================================================Lower===================================================!-->
<div class="lowerwrap">
<p>dsadsadasd</p>
</div><!--close topwrap-->
</body>
</html>
I wouldn't use top, how about this:
.topwrap{
clear: both;
position:relative;
float: left;
background:#EEE;
margin-top: 100px;
}
.lowerwrap{
position:relative;
float: left;
background:#555;
display: block;
}
I created a simple fiddle here:
https://jsfiddle.net/6xj5snv2/
you have to use z-index to achieve that. keep in mind that the div with the higher z-index will be on top so i suggest to add z-index:2 for .topwrap and z-index:1 .lowerwrap you can read more here: http://www.w3schools.com/cssref/pr_pos_z-index.asp
HTML:
<div id="navbar">
<div id="container">
<ul style="height:40px">
<li><p id="navtext">Home</p></li>
</ul>
</div>
</div>
CSS:
ul {
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
li {
float:left;
}
#navtext {
color:#FFFFFF;
padding-bottom:-50px;
}
#navbar {
width:100%;
height:40px;
background-color:#3498db;
}
#container {
margin-right:15%;
margin-left:15%;
vertical-align: middle;
}
I would like to have the "Home" text centered within the <ul> tag that is 40px tall. I have tried looking around for an answer but nothing I try seems to work. I'm trying to make a navbar for my site.
Add in your css:
#container ul li {line-height:10px;}
http://jsfiddle.net/PWTcL/
Try:
li {
float:left;
height:100%;
display:table;
}
#navtext {
color:#FFFFFF;
padding-bottom:-50px;
display:table-cell;
vertical-align:middle;
}
Fiddle here.
If you only have a single line, use line-height: 40px, and remove margin on <p> :
ul {
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
li {
float:left;
line-height: 40px;
margin: 0 5px;
}
.navtext {
color:#FFFFFF;
margin: 0;
padding-bottom:-50px;
}
#navbar {
width:100%;
height:40px;
background-color:#3498db;
}
#container {
margin-right:15%;
margin-left:15%;
vertical-align: middle;
}
<div id="navbar">
<div id="container">
<ul style="height:40px">
<li><p class="navtext">Home</p></li>
<li><p class="navtext">Link</p></li>
<li><p class="navtext">Contact</p></li>
</ul>
</div>
</div>
<div id="header">
<div id="logo">
<h1>Look Around You</h1>
</div>
<div id="horNav">
<ul class="horNav">
<li class="horNav">HOME</li>
<li class="horNav">SUBMIT-CONTACT</li>
</ul>
<ul class="horNav-last">
<li class="horNav">TAGS</li>
</ul>
<ul class="advertisment">
<li>ABOUT</li>
<li>ADVERTISEMENT</li>
</ul>
</div>
</div>
css is:
#wrapper{
width:80%;
margin:0 auto;
min-width:920px;
}
#header, #logo{
width: 100%;
float:left;
}
#horNav{
background:black;
width: 80%;
color:white;
margin: 0 auto;
}
#horNav a{
background: black;
}
.horNav li{
float:left;
border-right:1px solid #828282;
}
.horNav-last li{
float:left;
}
.advertisment li{
float:right;
border-left:1px solid #828282;
}
#horNav a{
display:block;
padding:5px 10px;
color:white;
font-size: 13px;
}
#horNav a:hover{
background:#828282;
}
what happens is that unordered lists are colored black in separate corners and middle of #horNav is white as you can see it HERE but of course it needs to be all black. how to fix this?
#horNav {
overflow: hidden;
display: block;
width: 100%;
}
#horNav can be whatever width you want e.g. 80% as it is now, and if you want it to be centred you can reintroduce the margin: 0 auto;.