Centering a nav in html5 that's responsive and uses borders - html

I have found lots of ways to center a nav using tricks. Either by setting the line height and the height equal to each other or using vertical-align with display:table. It worked but my border always came at the bottom of the container rather than text.
*{
margin:0px;
padding:0px;
}
header{
display:block;
height:100px;
background-color:blue;
}
nav{
height:auto;
width:100%;
display:block;
}
nav a{
height:100%;
text-align:center;
display:inline-block;
text-decoration:none;
color:black;
margin-left:25px;
font-size: 25px;
}
nav a:hover{
border-bottom: 3px solid #F3008A;
}
}
<header>
<nav>
Link
Link
Link
Link
</nav>
</header>
To clarify the problem, I want to vertically center the text or the "a" tags within the header block. I would like to do this and also be able to put borders on the "a" tag where the border is close to the text.

You could use the table-cell display vertical-align approach.
* {
margin:0px;
padding:0px;
}
header {
display:table;
width: 100%;
height: 100px;
background-color:blue;
}
nav {
height:auto;
width:100%;
display:table-cell;
vertical-align: middle
}
nav a {
text-align:center;
display:inline-block;
text-decoration:none;
color:black;
margin-left:25px;
font-size: 25px;
position: relative;
border-bottom: 3px solid blue;
}
nav a:hover {
border-bottom: 3px solid #F3008A;
}
<header>
<nav> Link
Link
Link
Link
</nav>
</header>
http://jsfiddle.net/wdabedbv/

You need to remove display:inline-block; and give line-height. It will solved your issue.
Check below your updated code:
*{
margin:0px;
padding:0px;
}
header{
display:block;
height:100px;
background-color:blue;
}
nav{
height:auto;
width:100%;
}
nav a{
height:100%;
text-align:center;
line-height:100px;
text-decoration:none;
color:black;
margin-left:25px;
font-size: 25px;
}
nav a:hover{
border-bottom: 3px solid #F3008A;
}
}
<header>
<nav>
Link
Link
Link
Link
</nav>
</header>

Try to use :Before as i did since your Height of the nav is fixed it wont be a problem.
Fiddle Link Demo
* {
margin: 0px;
padding: 0px;
}
header {
display: block;
height: 100px;
background-color: blue;
}
nav {
height: 100%;
width: 100%;
display: table;
}
nav a {
height: 100%;
text-align: center;
display: inline-block;
text-decoration: none;
color: black;
margin-left: 25px;
font-size: 25px;
display: table-cell;
vertical-align: middle;
}
nav a:hover:before {
background-color: #F3008A;
postion: absolute;
width: 50px;
content: "";
height: 3px;
position: absolute;
z-index: 4;
top: 64px;
}
<header>
<nav>
Link
Link
Link
Link
</nav>
</header>

I would assert that it is best to use flexbox since it is modern and responsive. The following will work nicely in all relatively modern browsers.
nav {
display: -webkit-flex;
display: flex;
}
nav > div {
background-color: pink;
height: 40px;
-webkit-flex: 1;
flex: 1;
display: -webkit-flex;
display: flex;
-webkit-justify-contents: center;
justify-contents: center;
-webkit-align-items: center;
align-items: center;
}
nav > div:nth-child(odd) {
background-color: red;
}
nav > div > a {
text-align: center;
-webkit-flex: 1;
flex: 1;
}
<nav>
<div>hi</div>
<div>hi2</div>
<div>hi3</div>
</nav>
See this JSFiddle.
Here is a helpful guide with pictures.

Related

spacing at bottom of footer

So I have some wierd spacing at the bottom of the footer and im not sure whats causing it. the code is as follows:
scss:
html,
body {
margin:0;
padding:0;
height:100%;
}
#wrapper {
min-height:100%;
position:relative;
}
header {
padding:10px;
background:#EFDECD; //#5ee;
// text-align: center;
}
#content {
padding:10px;
// padding-bottom:80px; /* Height of the footer element */
}
footer {
display: block;
width:100%;
height:5em;
position:absolute;
bottom:0;
left:0;
background:#EFDECD; //#EFDECD
text-align: center;
overflow: hidden;
a{
padding: .75rem;
// position:relative;
top: 1.5em;
display: inline-block;
font-size: .72rem;
}
p{
font-size: .72rem;
}
}
video{
width: 100%;
height:auto;
}
.label{
margin-left: .5em;
margin-right: .5em;
}
table{
font-size: .75em;
}
.panel.callout a:not(.button):hover{
color: $anchor-font-color-hover;
}
.headtext{
display:inline-block;
font-size:3em;
margin-left: 1.5em;
text-align: center;
font-family:"Courier New";
font-weight: bold;
margin-left: 6em;
margin-right: 7em;
}
.img2{
// max-height: 20%;
max-width: 25%;
float:right;
display:inline-block;
}
.accorborder{
border: black dotted 1px;
}
h6{font-weight: bold;}
The site is in development and is at www.new.omegadesignla.com please inspect element to view and can also view source code there.
The problem comes from the <br> which is between <div id="wrapper">...</div> and <footer>...</footer>.
If you eliminate position:absolute from your footer rule, the white space below footer will disappear. Is there a specific reason you are using position:absolute?

<li> with 2 elements not align properly why?

Hi I am struggling to align my numbers under my images they don't want to be aligned. this is a list and in the each line there an image and a number.
.category_icon { height:100px; width:300px; position:absolute; left:350px; top:150px;
ul { list-style:none; height:70px; width:220px;
li { display: inline; margin-right: 4px; width:65px; height:90px;
img { width:65px; height:64px;}
span { font-family:Tahoma; font-weight:bold; font-size:12px; position:absolute; bottom:7px; width:64px; }
}
}
}
<div class="category_icon">
<ul>
<li><img src="<%=atm.buttonPath%>/family_category_btn.png" alt="" /><span>346</span></li>
<li><img src="<%=atm.buttonPath%>/camping_category_btn.png" alt="" /><span>12</span></li>
<li><img src="<%=atm.buttonPath%>/cooking_category_btn.png" alt="" /><span>546</span></li>
</ul>
Thanks you if someone can help me thanks.
solution with text below
solution with centralised text below
for images, you always have to change their diplay properties before you start playing around with them....add the same here and li to display: inline-block; that would help
To do :
li {
display: inline-block; /*changed */
margin-right: 4px;
width:65px;
height:90px;
text-align:center;
}
img {
display:block; /*added */
width:65px;
height:64px;
}
PS : I am not aware of LESS...so gave you a generalized solution
http://jsfiddle.net/Qm6Bm/
.category_icon {
height: 100px;
width: 300px;
position: absolute;
left: 350px;
top: 150px;
ul {
list-style: none;
height: 70px;
width: 220px;
li {
display: inline-block; /* CHANGED */
margin-right: 4px;
width: 65px;
height: 90px;
text-align: center; /* ADDED */
img {
display: block; /* ADDED */
width: 65px;
height: 64px;
margin-bottom: 8px; /* ADDED */
}
span {
font-family: Tahoma;
font-weight: bold;
font-size: 12px;
/* Removed positioning and width */
}
}
}
}
I made the li an inline-block and the img a block. This means the image will push the text (from the span) under it. Then, by putting text-align: center in the li, that text (from the span) is centred nicely.
I put margin-bottom: 8px on your img just like how you had the span's bottom: 8px :P

Block elements within div in CSS

I'm trying to make 2 divs appear on separate lines within an outside div. Right now I have display:inline-block set for both of them, but I'm not sure how to change this to make them appear on separate lines.
Here is what my code looks right now, I would like John Doe and 100 to appear on separate lines within the leader div:
http://jsfiddle.net/ZnuPR/
HTML
<ul>
<li class="leader">
<div class="ranking">1</div>
<div class="name">John Doe</div>
<div class="score">100</div>
</li>
</ul>
CSS
.leader {
border: 1px solid;
background-color: #E6E6E6;
display: inline-block;
width: 400px;
margin: 2px;
padding: 2px;
background-repeat: no-repeat;
height: 75px;
}
.ranking {
display: inline-block;
margin:2px;
padding:2px;
width:50px;
height:65px;
background-color:green;
color:white;
}
.name {
display: inline-block;
}
.score {
display: inline-block;
}
You could simply float .ranking and then leave .name and .score as display: block.
http://jsfiddle.net/ZnuPR/7/
.ranking {
/* ... */
float: left;
}
The fastest solution is to set the ranking to "float:left;" and the name and score to "display:block;". Block level elements span 100% by default which will make sure the 2 elements are on seperate lines.
.leader {
border: 1px solid;
background-color: #E6E6E6;
display: inline-block;
width: 400px;
margin: 2px;
padding: 2px;
background-repeat: no-repeat;
height: 75px;
}
.ranking {
float:left;
margin:2px;
padding:2px;
width:50px;
height:65px;
background-color:green;
color:white;
}
.name {
display: block;
}
.score {
display: block;
}
http://jsfiddle.net/ZnuPR/2/
I think this is what you mean:
http://jsfiddle.net/ZnuPR/6/
Don't use inline-block and remove the height from the container, it will automatically adjust to the height it needs to be.
http://jsfiddle.net/ZnuPR/8/
Added a .details wrapper and some floats.
.ranking {
float:left; /* Floating */
margin:2px;
padding:2px;
width:50px;
height:65px;
background-color:green;
color:white;
}
.details {
float:left; /* floating */
}
.name {
display: block; /* Changed to block */
}
.score {
display: inline-block;
}
<ul>
<li class="leader">
<div class="ranking">1</div>
<div class="details">
<div class="name">John Doe</div>
<div class="score">100</div>
</div><!-- end details wrapper-->
</li>
</ul>
I think this could be useful:
http://jsfiddle.net/ZnuPR/10/
.leader {
border: 1px solid;
background-color: #E6E6E6;
display: inline-block;
width: 400px;
margin: 2px;
padding: 2px;
background-repeat: no-repeat;
}
.ranking {
width: 100%;
margin:2px;
padding:2px;
width:50px;
height:65px;
background-color:green;
color:white;
}
.name {
width: 100%;
}
.score {
width: 100%;
}
This is what I did:
CSS
.leader {
border: 1px solid;
background-color: #E6E6E6;
display: inline-block;
width: 400px;
margin: 2px;
padding: 2px;
background-repeat: no-repeat;
}
.ranking {
display: inline-block;
margin:2px;
padding:2px;
width:50px;
height:65px;
background-color:green;
color:white;
}
I got rid of display: inline-block and height

How to make nav bar stay in the middle

I'm trying to centralize my nav bar, I've tried text align center, and margin auto, but it stays fixed to the left. I also tried to add a width, but still it stays fixed. Thanks in advance for your help. Please check out the JSFIDDLE. The HTML is as follows:
<section class="contain">
<div id="section-nav" class="section-nav">
<div class="top">
<ul>
<li class="logo">Magna Contra</li>
<li class="active">Festival: Paris</li>
<li>Festival: Paris</li>
<li>Festival: Paris</li>
<li>Festival: Paris</li>
</ul>
</div>
</section>
And the CSS:
ul
{
list-style-type:none;
margin:0;
padding:0;
}
li
{
display:inline;
padding:15px;
text-align: center;
margin: auto 0;
}
li a{
text-decoration: none;
color:#bbbbbb;
font-family: "proxima-nova",sans-serif;
text-transform: uppercase;
text-align: center;
font-size: 0.78em;
letter-spacing: .2em}
li a:hover{
color:white;
}
.logo a{
color:#bbb;
font-size: 0.78em;
text-decoration: none;
text-transform: uppercase;
}
.logo a:hover{
color:white;
}
.active a{
color:white;
}
.container {
display: table;
width:980px;
height: 100%;
}
.contain{
display: table;
width: 100%;
text-align: center;
margin: 0 auto;
}
.block {
display: table-row;
height: 1px;
}
.navigation {
display: inline-block;
padding: 10px;
width:100%;
margin: auto;
height: 150px;
}
.top {
background-color: #444;
width:100%;
display: inline-block;
padding: 10px;
text-align: left;
}
.navigation:nth-child(odd) {
background: #222;
}
.push {
height: auto;
}
.container {
margin: 0 auto;
text-align: center;
}
.block:nth-child(odd) {
background: #fff;
}
.search {
border:0px;
background-color:transparent;
color:white;
display: inline-block;
height:28px;
}
.section-nav a{-webkit-transition:400ms;-moz-transition:400ms;-o-transition:400ms;transition:400ms;color:#bbb;font-weight:700;outline:0;text-decoration:none}
.section-nav a.active,.section-nav a:hover{color:#fff;text-decoration:none}
The easiest option is to add text-align: center to the ul:
ul
{
list-style-type:none;
margin:0;
padding:0;
text-align: center;
}
I would also set the lis to display: inline-block to give you more control over their styling.
Your div "section-nav" is not closed. I would fix this first.
You have also applied text-align:left to your .top div, which is the main container for the navigation buttons.
In the given fiddle you have
.top {
text-align: left;
}
change it to
.top {
text-align: center
}
fidd ->http://jsfiddle.net/ztyUF/2/ Is this what u ve asked?
I used this when I had the same problem and have pretty much duplicated it for multiple sites. He explains it much better than I can.
http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support

Position span text in the middle (vertically and horizontally) of a li element

How can I position span text so that it is positioned in the middle (vertically and horizontally). I have made a jsfiddle example and this is my code:
<ul>
<li class="one"><span>One</span></li>
<li class="two"><span>Two</span></li>
</ul>
css:
ul {
height: 200px;
border: 1px solid #000;
width: 200px;
list-style: none;
margin: 0;
padding: 0;
position: relative;
}
li {
height: 30px;
width: 100%;
position: absolute;
background-color: blue;
}
.one {
top: 30px;
height: 50px;
}
.two {
top: 150px;
}
li span {
color: #fff;
vertical-align: middle;
height: 100%;
display: block;
}
The height of the li's can vary. The above is just a reference using fixed heights.
You need to set line-height at the same height as your li:
li span {
color: #fff;
vertical-align: middle;
height: 100%;
display: block;
line-height:30px;
}
li.one span{
line-height:50px;
}
Here's your demo: http://jsfiddle.net/r4Dr9/2/
You don't need to do tricks with margin or padding.
Give the <li> a display:table;, and the <span> a display:table-cell;.
DEMO
CSS to add
li {
text-align:center;
display:table;
}
li span {
display:table-cell;
vertical-align:middle;
}
Try using text-align: center; on the li span, and you'll have to set the top margin accordingly for each to get it down from the top.
A couple things. First, height:100%;, vertical-align:middle; does nothing. Also, with width:100%; and not specifying text-align, it'll always default to left. I'd personally use padding:
li span {
display:inline-block;
color: #fff;
text-align:center;
height: 100%;
}
.one span {
padding-top:10px;
}
.two span {
padding-top:3px;
}
I updated your jsFiddle as well.
Added style - 'text-align: center' on li element and it worked for me