I'm trying to get my header with ul in the center. Another page suggested having overflow:hidden but when I shrink the window, a second scroll bar still appears.
Every time I try something, the li isn't in line with my logo.
Also tried to have the class 'container' margin: auto but nothing happens.
body {
font: 15px/1.5 Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
background-color: #f4f4f4;
}
/*Global styling */
.container {
width: 100%;
overflow: visible;
}
ul {
margin: 0;
padding: 0;
}
/*Header*/
header {
background: #100806;
color: #f2f2f2;
min-height: 75px;
border-bottom: #ffffff 3px solid;
text-align: center;
}
header a {
color: #ffffff;
text-decoration: none;
text-transform: uppercase;
font-size: 15px;
}
header li {
display: inline-block;
padding: 0 30px 10px 0;
float: left;
}
header .center-logo img {
width: 100px;
padding-left: 30px;
padding-right: 30px;
}
header nav span {
float: left;
}
header nav span:first-child {
padding-right: 50px;
/* half the logo width */
padding-top: 20px;
}
header nav span:last-child {
padding-left: 50px;
padding-top: 20px;
/* half the logo width */
}
header .highlight,
header .current a {
color: #e8491d;
font-weight: bold;
}
header a:hover {
color: #666666;
font-weight: bold;
}
<header>
<div class="container">
<nav>
<ul>
<span>
<li class="current"> Home</li>
<li> Photography</li>
</span>
<div class="center-logo">
<li><img src="https://placehold.it/100x50"></li>
</div>
<span>
<li> Biography</li>
<li> Contact Us</li>
</span>
</ul>
</nav>
</div>
</header>
The Issue
float rules declared on the list-items (li)
Consider removing the float rules declared on list-items, this will negate any attempt at horizontally center aligning your menu items using typical methods other than flex-box.
The Fix
text-align: center (on containing element)
display: inline-block (on nested elements to center)
Since there is text-align: center rule already declared on a containing parent element (header in this case), and the list-items have already been declared as inline-block items, the list-item elements will center after removing the float rules declared on them.
Further Concerns:
The only direct descendant elements of an unordered (ul) or ordered (ol) list should be list-items (li) - any element other than this would be considered invalid markup.
Code Snippet Demonstration: (view "Full Page")
Summary:
Markup cleaned/simplified
Invalid list elements removed (direct descendants)
Styles improved (omitting redundant rules, vertically centering
navigation)
body {
font: 15px/1.5 Arial, Helvetica, sans-serif;
padding: 0;
margin: 0;
background-color: #f4f4f4;
}
/*Global styling */
.container {
width: 100%;
overflow: visible;
}
ul {
margin: 0;
padding: 0;
}
/*Header*/
header {
background: #100806;
color: #f2f2f2;
min-height: 75px;
border-bottom: #ffffff 3px solid;
text-align: center;
}
header a {
color: #ffffff;
text-decoration: none;
text-transform: uppercase;
font-size: 15px;
}
header li {
display: inline-block;
padding: 0 30px 0 0; /* remove the bottom padding */
/* float removed */
vertical-align: middle; /* additional */
}
/* Additional */
li.logo {
padding-left: 50px;
padding-right: 50px;
}
header nav > ul { /* using the "child combinator" here (>) so that this rule doesn't apply to any dropdown or sub-menus */
padding: 10px 0 10px 0;
}
/*
header .center-logo img {
width: 100px;
padding-left: 30px;
padding-right: 30px;
}
header nav span {
float: left;
}
header nav span:first-child {
padding-right: 50px;
padding-top: 20px;
}
header nav span:last-child {
padding-left: 50px;
padding-top: 20px;
}
*/
header .highlight,
header .current a {
color: #e8491d;
font-weight: bold;
}
header a:hover {
color: #666666;
font-weight: bold;
}
<header>
<nav>
<ul>
<li class="current"> Home</li>
<li> Photography</li>
<li class="logo"><img src="https://placehold.it/100x50"></li>
<li> Biography</li>
<li> Contact Us</li>
</ul>
</nav>
</header>
<!--
Change Log:
1) Invalid nested elements removed from list (span & div)
2) Superfluous ".container" containing element removed (as providing no tangible benefits)
-->
Practical Demonstrations for Further Reference:
Horizontal Alignment (Text Elements)
Horizontal Alignment (Arbitrary Elements)
You could use Flexbox.
header ul {
display:flex
}
header li {
flex: 1
}
Powerful tool flex.
See fiddle: https://jsfiddle.net/fo7v1253/1/
Related
I want the navigation links: "about", "resume", "projects", and "contact" to line up horizontally in the navigation bar.
Why does this only work with display: inline-block?
It is my understanding that inline-block boxes allows these elements to be side by side. I need it to be inline-block instead of just inline because I want to size it to the nav bar's exact height.
What am I doing wrong?
Here is the HTML and CSS for my nav:
/* ----------------------------NAVIGATION SECTION-------------------------------- */
.headerContainer {
background-color: #000;
text-align: center;
height:60px;
margin-left: 600px;
margin-right: 600px;
font-family: 'Monda', sans-serif;
text-transform: uppercase;
position: fixed;
}
nav {
padding-left: 1000px;
padding-right: 1000px;
}
nav li {
list-style: none;
display: inline-block;
background-color: #000;
height: 40px;
padding-top: 20px;
width: 120px;
}
nav li:hover {
background-color: #e1e1e1;
-webkit-text-stroke: 2px #000;
}
a:link {
color: #fff;
text-decoration: none;
margin-left:25px;
margin-right:25px;
}
a:visited {
color: #fff;
}
a:focus {
color: #fff;
}
a:hover {
}
a:active {
color: #fff;
}
<!------------------------------NAVIGATION SECTION---------------------------------->
<header class="headerContainer">
<nav>
<ul>
<!-- you put the end tag ">" at the beginning of next line to get rid of whitespace between the links -->
<li>About</li
><li>Resume</li
><li>Projects</li
><li>Contact</li>
</ul>
</nav>
</header>
You have a massive amount of padding inside the nav element.
nav {
padding-left: 1000px;
padding-right: 1000px;
}
This doesn't leave very much space for the content to render in.
The li elements are laid out side by side until they run out of space, at which point they word wrap.
If you zoom out, you'll see the fit in one line.
If you look at the live demo in your question, with the very narrow frame, you will see everything squashed to the side.
Don't set a huge padding on the nav element.
This question already has answers here:
Width 100% with white borders around it. WHy?
(2 answers)
Closed 5 years ago.
I faced an issue today when I created the navigation bar I found a space between the screen and the navigation bar,
here's what I'm talking about
I want the navigation bar with full width, no space at all, I tried using width width: 100% but it didn't work.
Here's the code :
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #141414;
height: 80px;
border-radius: 5px;
}
li {
float: right;
display:inline;
list-style-type:none;
}
li a {
font-family: Julius Sans One, Arial;
font-size: 19px;
display: block;
color: white;
text-align: center;
padding: 30px 20px;
text-decoration: none;
}
.logoimg {
height: auto;
margin-left: 150px;
margin-top: 5px;
}
.left {
float: left;
}
<div>
<ul>
<li class="left"><img class="logoimg" src="/images/logo.png"></li>
<li><a>Test 1</a></li>
<li><a>Test 2</a></li>
</ul>
</div>
From the look of it, your navigation bar is full-width. The additional whitespace you are seeing is actually coming from <body>, which adds a margin of 8px by default. You can override this to ensure that your content is flush against the edge of the page:
body {
margin: 0; /* Added */
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #141414;
height: 80px;
border-radius: 5px;
}
li {
float: right;
display:inline;
list-style-type:none;
}
li a {
font-family: Julius Sans One, Arial;
font-size: 19px;
display: block;
color: white;
text-align: center;
padding: 30px 20px;
text-decoration: none;
}
.logoimg {
height: auto;
margin-left: 150px;
margin-top: 5px;
}
.left {
float: left;
}
<div>
<ul>
<li class="left"><img class="logoimg" src="/images/logo.png"></li>
<li><a>Test 1</a></li>
<li><a>Test 2</a></li>
</ul>
</div>
It's important to note that the <body> tag is always present, even when not explicitly written. This can be seen in the snippets here -- note that the original snippet appears to be offset from the edge and the line, whereas this does not, and all I added was an override for body margin.
Hope this helps! :)
What you are experiencing is the default window/page Padding/Margin. You can set this default value to 0 in order to have your full width/height of the page:
body{
margin: 0;
padding: 0;
}
This should correct your problem.
when I try to add in h3 onto the website it just stay at the top I tried to use position, top,left in the CSS but it doesn't want to response to it. I also tried to use a gallery template from w3schools to see if it will let me move the gallery but still no luck. I have tried to research this but I had no luck at all. sorry for the long code.
<html>
<link rel="stylesheet" href="sheet.css">
<head>
<title> Jon Barton </title>
</head>
<header>
<ul class= "nav-men">
<li> Gallery </li>
<li>About me </li>
<li> Contract me</li>
</ul>
<h1><span> Software Developer </span> </h1>
<p class = "kicker" >Ideas // Desinger // Implement </p>
</header>
<body>
<h3> Work</h3>
<footer>
<div class = "footer">
<div class ="content-wrap">
<ul>
<li> </li>
<li> </li>
<li> </li>
</ul>
<p class = "copyright"> All content copyright 2016</p>
</div>
</div>
</html>
The css is a bit long sorry and some of it is just repeated.
#css
header {
background: url(.//277H.jpg) ;
text-align: center;
width: 100%;
height: auto;
position: absolute;
}
.nav-men li {
display:inline;
margin-left: 100px ;
}
.nav-men ul{
margin: 30px 30px 0px 0px;
padding: 0;
list-style-type: none;
text-align: center;
text-decoration: none;
}
.nav-men a {
color:white;
}
.nav-men a:hover, a:visited, a:link, a:active
{
text-decoration: none;
}
h1 {
font-size: 60px;
color: white;
letter-spacing: 0.05em;
text-transform: uppercase;
text-align: center;
font-weight: 700;
margin-bottom: 20px;
}
span {
display: inline-block;
padding: 0.2em 0.6em ;
border: white solid 10px;
}
.kicker {
text-align: center;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.5em;
color: white;
}
body {
margin: 0;
padding: 0;
height: 100%;
}
.alt-selction {
background: black;
}
.thumb-container {
max-width: 980px ;
margin: 0px auto ;
padding-bottom: 100px;
}
.thumb-unit {
display:block;
width: 25%;
height: 150px;
float:left ;
}
.footer {
margin: 30px;
background: url(../277H.jpg) ;
width: 100%;
height: 100px;
clear: both;
bottom: -53px;
left:-30;
right:0;
position: absolute;
background-size: cover;
float: left;
}
.content-wrap {
float: left;
}
li {
display:inline;
margin-left: 100px ;
}
ul{
margin: 30px 30px 0px 0px;
padding: 0;
list-style-type: none;
text-align: center;
text-decoration: none;
}
a {
color:white;
}
a:hover, a:visited, a:link, a:active {
text-decoration: none;
}
That's because the position of the <header> and footer is absolute.
position:absolute positioned an element relative to the nearest positioned ancestor instead of positioned relative to the viewport, like fixed.(from here)
Remove the position:absolute from the class header & .footer. Then it will let you put anything between them.
Use this tutorial to have a sticky footer:
[https://css-tricks.com/snippets/css/sticky-footer/]
After just add div at the top as a Header and you can add as many content between them after.
I would recommend first cleaning up a bit of your HTML to put your header and footer inside the body element, and closing your body element properly as otherwise you will most probably run into other problems.
See this fiddle for an example:
https://jsfiddle.net/jowxoaca/1/
Also, as jonju mentioned, removing the position:absolute for header and footer would fix your ordering issue.
I am trying to center the navigation bar in the middle of the div body. I want the navigation bar to go from one side of the div to the other but have the list in the ul to be center in the middle of the div if that makes sense. I can't seem to figure it out even after trying online examples. Thanks
body {
margin: 0px;
padding: 0px;
background-color: #505050 ;
}
#body {
width: 75%;
margin: 0 auto;
position: center;
background-color: #C0C0C0;
height: 100%;
}
.nav {
}
.nav ul {
background-color: #CCCCCC;
width: 100%;
padding: 0;
text-align: center;
}
.nav li {
list-style: none;
font-family: Arial Black;
padding: 0px;
height:40px;
width: 120px;
line-height: 40px;
border: none;
float: left;
font-size: 1.3em;
background-color: #CCCCCC;
display:inline;
}
.nav a {
display: block;
color: black;
text-decoration: none;
width: 60px;
}
<div id="body">
<h2>Hello World!</h2>
<div class="nav">
<ul>
<li><a href="#">Home<a></li>
<li><a href="#">About<a></li>
<li><a href="#">News<a></li>
<li><a href="#">Contact<a></li>
</ul>
</div>
</div>
i attach fix here http://jsfiddle.net/o4716uo9/
use inline-block for li
background property should be setted in ul element, not li, in your case. Delete the float in nav li. Also, the a element it isn't closed correctly. Main changes:
.nav ul {
background-color: #cccccc;
text-align: center;
}
.nav ul li {
display: inline-block;
min-width: 120px;
[...]
}
I'll recommend you to take a look at the bootstrap framework. It could be interesting for you.
There are a couple things you can change to correct the issue:
1) Your <a> elements have a width of 60px. You can remove this.
2) You .nav li has a width of 120px. I would change this to 25% (If there are only going to be four navigational items).
http://jsfiddle.net/xLnz90ek/
Is that any closer to the desired effect.
Is this what you’re trying to do?
* { margin:0; padding:0 }
html {
background-color: #505050;
font-size: 4vw;
}
header {
width: 75%;
margin: 0 auto;
background-color: #C0C0C0;
}
nav {
background-color: #CCCCCC;
display: flex;
padding: 0.2rem 0;
}
nav a {
flex: 1 0 auto;
font-family: Arial Black;
font-size: 1rem;
background-color: #CCCCCC;
color: black;
text-decoration: none;
text-align: center;
margin: 0 0.3rem;
}
<header>
<h2>Hello World!</h2>
<nav>
Home
About
News
Contact
</nav>
</header>
On some of my pages, the section is longer than my aside. I don't want to alter the length of my aside because that would make some pages unnecessarily long for the short content on the page.
My issue is that once my section gets past the length of my aside, the text goes all the way to the left below my aside. I want to keep it going straight down so it is all in line. Make sense?
I can't seem to find the right CSS to make this happen. Can anyone help?
aside {
float: left;
width: 155px;
height: 200px;
padding-right: 25px;
margin-top: 25px;
}
aside ul {
padding-left: 5px;
list-style: none;
/* Removes bullets from list items */
}
aside li {
margin-bottom: 0.50em;
border: 3px solid black;
border-radius: 20px;
background-color: yellow;
}
aside li a {
display: block;
/* Makes whole box clickable */
padding: .5em 1em;
/* Space around text */
text-decoration: none;
/* Removes underlines */
color: black;
}
aside a.current {
color: green;
}
/* Section */
#learnin {
color: purple;
font-variant: small-caps;
font-family: "Courier", monospace;
width: 700px;
}
section img {
float: right;
padding: 10px;
}
section li img {
float: right;
padding: 10px;
}
section {padding: 5px 20px;/* top right bottom left */}
section ul {padding-left: 0px;}
section p {padding-right: 10px;}
section h3 {
clear;
both;
}
section li {
margin-left: 12em;
}
section a {
font-weight: normal;
color: blue !important;
}
img.smiles {
clear: all;
float: none;
padding: 0px;
}
ul.nested {
clear: all;
margin-left: 3em;
}
<aside>
<nav>
<ul>
<li>Link 1
</li>
<li>Link 2
</li>
<li>Link 3
</li>
</ul>
</nav>
</aside>
<section>
<p>[text] ...
</p>
</section>