http://jsfiddle.net/rctdzdsm/
I'm trying to align the menu to the logo.
Do I have to manually margin it till I align correctly?
Or is there a better/proper way of doing this?
Because manually margining till I align it correctly, feels kinda kinda dumb.
* {
margin: 0;
padding: 0;
}
header {
background-color: #e58b1a;
}
header .logo {
float: left;
width: 400px;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
position: relative;
top: 20px;
left: 50px;
}
.menu {
float: right;
font-family: arial;
}
.menu li {
/** Float to the left, helps it become horizontal */
float: left;
/** Marigin-right allows you to space your links */
margin-right: 30px;
}
<header class="show-when-loaded" style="height: 108px;">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<div class="menu">
<nav>
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</div>
</header>
This is not a very flexible way of doing this. What if the height of your header changes?
Than you would manually have to align your padding and margins. If you are looking for a more accurate way of doing this, take a look at the different possibilities of displaying elements and aligning them. Even if your menu lives inside an unordert -list. Think of your list being a table and the list items being the table cells. If your elements inherit the height from your header you could easily align them vertically in the middle. Here is a snippet to give you an idea of what i mean.
html, body {
margin: 0;
padding: 0;
}
header {
height: 108px;
background-color: orange;
}
header:after {
clear: both;
}
nav {
height: 100%;
}
nav ul {
margin: 0;
padding: 0;
float: left;
display: table;
height: inherit;
}
nav li {
display: table-cell;
height: inherit;
vertical-align: middle;
}
nav a {
text-decoration: none;
color: white;
font-family: 'Arial';
}
.logo a {
font-size: 50px;
padding: 0 0 0 20px;
}
nav .nav-bar {
float: right;
}
nav .nav-bar li {
padding-right: 20px;
}
<header>
<nav role='navigation'>
<ul class="logo">
<li>
Daz Me Lulz
</li>
</ul>
<ul class="nav-bar">
<li>Home</li>
<li>About</li>
<li>Clients</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
Have fun.
I removed the position: relative. because its not needed.
Added the some margins for being aligned better.
Tried to make the li elements wrap, ended up with a none wrapping solution instead.
Made the links text direction from right to left. So they stick to the right side of the screen, without breaking the flow, as float: right does.
html, body {
margin: 0;
padding: 0;
}
header {
white-space: nowrap;
background-color: #e58b1a;
min-width: 650px;
}
header .logo {
display: inline-block;
font-family: 'Arial';
color: white;
font-size: 50px;
padding: 20px 0px 20px 20px;
}
.menu {
display: inline-block;
direction: rtl;
font-family: arial;
min-width: 350px;
width: calc(100% - 350px);
margin-top: 40px;
}
.menu ul {
display: inline;
direction: rtl;
padding: 0;
}
.menu ul li {
display: inline;
list-style: none;
margin-right: 10px;
}
<header class="show-when-loaded">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<nav class="menu">
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</header>
Looks like CSS tables will help here.
header {
background-color: #e58b1a;
display: table;
vertical-align: middle;
width: 100%;
}
header .logo {
display: table-cell;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
vertical-align: middle;
}
.menu {
display: table-cell;
font-family: arial;
text-align: right;
vertical-align: middle;
}
* {
margin: 0;
padding: 0;
}
header {
background-color: #e58b1a;
display: table;
vertical-align: middle;
width: 100%;
}
header .logo {
display: table-cell;
height: 53px;
font-family: 'Arial';
color: white;
font-size: 50px;
vertical-align: middle;
}
.menu {
display: table-cell;
font-family: arial;
text-align: right;
vertical-align: middle;
}
.menu li {
display: inline-block;
}
<link rel="stylesheet" type="text/css" href="style.css">
<title>Daz Me Lulz - Providng Renewable Energy to the children of Latin America</title>
<body>
<header class="show-when-loaded" style="height: 108px;">
<div class="logo">
<span class="text"> Daz Me Lulz </span>
</div>
<div class="menu">
<nav>
<ul>
<li> About Us
</li>
<li> Projectos De Lulz
</li>
<li> Get Involved
</li>
</ul>
</nav>
</div>
</header>
</body>
Related
I have this side menu (image below) which needs to be a block/column but display: block doesn’t work for some reason!?
Here is the CSS and the HTML can you tell what's wrong.
Screenshot here
PS. I don't want to do it with the <br> tags, rather doing it through CSS.
body {
font-family: arial;
}
body h4 {
padding-top: 20px;
padding-left: 20px;
}
.category-tree {
background-color: lightgrey;
float: left;
width: 300px;
height: 1100px;
}
.left-menu-categories {
float: left;
}
body div nav ul li a {
margin-top: 0;
margin-bottom: 0;
}
.left-menu {
display: block;
}
<body>
<div class="category-tree">
<h3 class="cat-title">Categories</h3>
<nav class="left-menu">
<ul class="left-menu-categories">
<li class="">Title</li>
<li class="">Title</li>
<li class="">Title</li>
</ul>
</nav>
</div>
</body>
This question already has answers here:
How to disable margin-collapsing?
(12 answers)
Closed 5 years ago.
Here is the HTML code (the white gap started appearing as soon as I added h3 to the last div):
body {
margin: 0;
font-family: sans-serif;
font-weight: 400;
background-image: url("../images/rooms.jpg");
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #343434;
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 10px;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
nav li {
padding: 0;
display: inline-block;
margin-left: 60px;
padding-top: 19px;
position: relative;
}
nav a {
text-decoration: none;
color: white;
font-size: 13px;
text-transform: uppercase;
padding: 1em 0.5em;
}
nav a:hover {
color: yellow;
}
.welcome {
width: 100%;
height: 250px;
background: #406295;
}
.welcome h3 {
text-align: center;
}
<header>
<div class="container">
<img src="images/logo.png" alt="">
<nav>
<ul>
<li>Room Types</li>
<li>Services</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="welcome">
<h3>Welcome to</h3>
</div>
I am fairly new to web development and stackoverflow. So I am sorry for any inconveniences. Any help is appreciated. Thank you.
Set margin: 0px; on h3 tag to resolve this issue. Check updated Snippet below..
body{
margin:0;
font-family: sans-serif;
font-weight: 400;
background-image: url("../images/rooms.jpg");
}
.container{
width: 80%;
margin : 0 auto;
}
header{
background: #343434;
}
header::after{
content: '';
display: table;
clear:both;
}
.logo{
float: left;
padding:10px;
}
nav{
float:right;
}
nav ul{
margin: 0;
padding: 0;
list-style-type: none;
}
nav li{
padding: 0;
display: inline-block;
margin-left: 60px;
padding-top: 19px;
position: relative;
}
nav a{
text-decoration: none;
color:white;
font-size: 13px;
text-transform: uppercase;
padding: 1em 0.5em;
}
nav a:hover{
color:yellow;
}
.welcome{
width: 100%;
height: 250px;
background: #406295;
}
.welcome h3{
text-align: center;
margin: 0px;
}
<header>
<div class="container">
<img src="images/logo.png" alt="">
<nav>
<ul>
<li>Room Types</li>
<li>Services</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="welcome">
<h3>Welcome to</h3>
</div>
Just remove the margin from h3 like
.welcome h3 {
text-align: center;
margin:0;
}
body {
margin: 0;
font-family: sans-serif;
font-weight: 400;
background-image: url("../images/rooms.jpg");
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #343434;
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 10px;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
nav li {
padding: 0;
display: inline-block;
margin-left: 60px;
padding-top: 19px;
position: relative;
}
nav a {
text-decoration: none;
color: white;
font-size: 13px;
text-transform: uppercase;
padding: 1em 0.5em;
}
nav a:hover {
color: yellow;
}
.welcome {
width: 100%;
height: 250px;
background: #406295;
}
.welcome h3 {
text-align: center;
margin:0;
}
<header>
<div class="container">
<img src="images/logo.png" alt="">
<nav>
<ul>
<li>Room Types</li>
<li>Services</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="welcome">
<h3>Welcome to</h3>
</div>
This is due to collapsing margins
Remove the margin on the h3. Replace it with padding if you want to create space between the header and maintain the background colour.
body {
margin: 0;
font-family: sans-serif;
font-weight: 400;
background-image: url("../images/rooms.jpg");
}
.container {
width: 80%;
margin: 0 auto;
}
header {
background: #343434;
}
header::after {
content: '';
display: table;
clear: both;
}
.logo {
float: left;
padding: 10px;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style-type: none;
}
nav li {
padding: 0;
display: inline-block;
margin-left: 60px;
padding-top: 19px;
position: relative;
}
nav a {
text-decoration: none;
color: white;
font-size: 13px;
text-transform: uppercase;
padding: 1em 0.5em;
}
nav a:hover {
color: yellow;
}
.welcome {
width: 100%;
height: 250px;
background: #406295;
}
.welcome h3 {
text-align: center;
margin-top: 0;
}
<header>
<div class="container">
<img src="images/logo.png" alt="">
<nav>
<ul>
<li>Room Types</li>
<li>Services</li>
<li>About Us</li>
</ul>
</nav>
</div>
</header>
<div class="welcome">
<h3>Welcome to</h3>
</div>
You can try adding style="display: inline; margin:0px; padding:0px;" to your <h3> Tag.
Another way is to apply a rule of overflow: auto to the .welcome div... thus creating a new block formatting context and avoiding the collapsing margins.
Edit: Let's add a little more context. In the spec, you can read that adjoining margins will collapse under certain circumstances. In particular, the margins need to belong to block-level boxes participating in the same block formatting context.
Even though .welcome and h3 are block-level boxes in your example, neither automatically establishes a new block formatting context (meaning they participate in the same block formatting context, meaning their margins collapse). Looking at the spec again, we see that some of the ways to establish a new block formatting context is to have a float, an absolutely positioned element, or a block box with the property of overflow set to something else than visible.
That's why the suggestions regarding overflow: auto or floating one of the elements work. My understanding is that if we make .welcome establish a new block formatting context, the context it participates in is different from the one it establishes itself. Removing the margin (possibly replacing it with padding) is another way to get around the problem.
Either apply margin-top:0 for H3-Tag
or
apply a float:left for .welcome
Both will fix your issue
So I'm working on a navigation bar for my website and I'm trying to space everything centred evenly in the bar, but for some reason, I can't get things to work out evenly. Either margins don't work, or they do but not all the way, and it's getting quite frustrating. Here's my code:
body {
margin: 0 px;
font - family: Helvetica;
}
.navbar {
background - color: grey;
width: 100 % ;
height: 70 px;
text - align: center;
}
.items li {
display: inline - block;
padding - left: 50 px;
}
.items a {
text - decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
padding-top: -25px;
}
<body>
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>
</body>
To center vertically links with the logo, use vertical-align:middle;.
Also, If you set margin or padding don't set it to <a> because it's an item with inline properties but set it on li items which are inline-block.
In addition to that, it's strange to write li items without ul container.
body {
margin: 0px;
font-family: Helvetica;
}
.navbar {
background-color: grey;
width: 100% ;
height: 70px;
text-align: center;
}
.items img
{
vertical-align:middle;
}
.items li {
display: inline-block;
vertical-align:middle;
padding-left: 50px;
}
.items a {
text-decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
}
<body>
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>
</body>
Note:See it in full page!
body {
margin: 0px;
font-family: Helvetica;
}
.navbar {
background-color: grey;
width: 100%;
height: 70px;
text-align: center;
position: relative;
}
.items {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.items li {
display: inline-block;
padding-left: 50px;
}
.items a {
text-decoration: none;
color: #333;
font-weight: bold;
font-size: 20px;
display: block;
}
<div class="navbar">
<div class="items">
<img src="logo1.png" style="height:55px;padding-top:7.5px;">
<li>Apparel</li>
<li>Equipment & Accessories</li>
<li>Contact Us</li>
</div>
</div>
I have a HTML5 header setup. It uses <ul> and <li> elements for the links.
This is the HTML:
<header>
<nav>
<ul>
<li id="logoli"><img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li class="5px">Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
And the CSS:
header {
height: 100%;
width: 100%;
}
nav {
width: 100%;
height: 65px;
font-size: 14px;
font-weight: 700;
text-transform: uppercase;
padding-left: 10px;
background-color: rgba(0, 0, 0);
color: #0077C5;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
float: left;
}
li {
width: 100px;
line-height: 65px;
vertical-align: middle;
text-align: center;
margin: 0;
text-align: center;
display: inline;
list-style: none;
float: left;
}
#logo {
height: 50px;
}
#logoli {
padding-top: 7px;
width: 250px;
}
For some reason this happens: https://image.prntscr.com/image/w0LAMn5qRMq0OqIAfDTsHw.png
If you look at the nav bar you can easily see that the first two elements have a normal amount of spacing in-between. But the seccond and third have weirdly small spacing. I used inspect element on the <li>'s but they are all the same height. Can someone help me?
You need some HTML and CSS Fixes, Wrap the anchors in li elements and the issue was you have mentioned a width to the li, which causes this wierd spacing.
HTML
<header>
<nav>
<ul>
<li id="logoli">
<img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li class="5px">Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
CSS
header {
height: 100%;
width: 100%;
}
nav {
width: 100%;
height: 65px;
font-size: 14px;
font-weight: 700;
text-transform: uppercase;
padding-left: 10px;
background-color: rgba(0, 0, 0);
color: #0077C5;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
float: left;
}
li {
line-height: 65px;
vertical-align: middle;
text-align: center;
margin:0px 10px;
text-align: center;
display: inline;
list-style: none;
float: left;
}
#logo {
height: 50px;
}
#logoli {
padding-top: 7px;
width: 250px;
}
You should put the <a href>'s inside the <li> tags like this:
<header>
<nav>
<ul>
<li id="logoli"><img src="/assets/logo.png" id="logo"></li>
<li>Home</li>
<li>Roulette</li>
<li>Free Stuff</li>
</ul>
</nav>
</header>
Also - you have a class="5px" on one of the <li>'s, but that class isn't in your CSS. You should make your <li>'s a fixed width and center the text, or use a standard padding on the left and right of the <li> text to maintain a uniform spacing rather than injecting classes to push single elements around.
This is happening because you're explicitly defining a width on your list items. Remove the width and separate the list items with padding instead. Optionally, remove any padding on the last list item, since you won't need it.
li {
/* width: 100px; */
padding-right: 1em;
line-height: 65px;
vertical-align: middle;
text-align: center;
margin: 0;
text-align: center;
display: inline;
list-style: none;
float: left;
}
li:last-child {
padding-right: 0;
}
https://jsfiddle.net/eulloa/yx7vkt79/
Also, restructure your list items so that they are the parent element of your anchors, not the other way around.
<li>Home</li> <!-- structure your list items this way -->
<li>Home</li>
I have a title and a menu and I want them to be in opposite corners of my webpage, kinda like this: http://www.google.com/drive
My website is http://www.vincestechsupport.com/home.html
I really appreciate any advice you can give. Thanks guys. Here is the code:
#Title {
font-size: 35px;
color: red;
font-family: arial;
float: left;
display: inline;
width: 100%;
font-variant: small-caps;
}
#Title a {
text-decoration: none;
color: red;
}
#Menu a {
text-decoration: none;
color: red;
width: 500px;
padding-left: 20px;
padding-right: 20px;
font-family: arial;
font-variant: small-caps;
}
#Menu {
font-size: 30px;
float: right;
display: inline;
font-family: arial;
width: 100%
}
#Menu ul {
list-style-type: none;
padding-left: 5px;
padding-right: 5px;
}
#Menu li {
display: inline;
}
<div id="Menu">
<center>
<ul>
<li><b>Home</b>
</li>
<li>Services
</li>
<li>Experience
</li>
<li>Pricing
</li>
<li>Contact Me
</li>
</ul>
</center>
</div>
<div id="Title">
<h1>
Vince’s Tech Support
</h1>
</div>
<div id="Home">
<h1>Home</h1>
</div>
After floating you should clear.
1.First method is adding "clearfix" class to <div id="wrapper"> - <div id="wrapper" class="clearfix">
and adding this style:
.clearfix:after { content: "."; display: block; clear: both; visibility: hidden; line-height: 0; height: 0; }
2.Second method is adding a height to <div id="wrapper">
3.And third method is adding width:100%; overflow: hidden; to <div id="wrapper">
You should choose but the best method is the first one with clearfix.