h1 and nav on the same line - html

I searched Stack overflow and google and tried all the suggestions to getting my h1 and nav on the same line. I tried inline, inline-block, setting the header itself to 100%. It's just not aligning. On top of that my li posted backwards when I set it to float left so the home that was on the top of the list is now on the outer end instead of the beginning. here's my code
.header{
background-color: #00001a;
height: 40px;
width: 100%;
}
ul{
list-style-type: none;
}
.header h1{
float: left;
color: #ffffff;
font-size: 15px;
display: inline-block;
}
.nav li{
float: right;
display: inline-block;
color: #ffffff;
}
<div class="header">
<div class="nav">
<h1>EaTogeter</h1>
<ul>
<li>home</li>
<li>About</li>
<li>Couples</li>
<li>family</li>
</ul>
</div>
</div>
<div class="Maincontent">
<div class="container">
<h2>Try It</h2
<p>Today's Try It Recipe!<p>
</div>
</div>

display: flex; justify-content: space-between; will put them on the same line and separate them with the available space.
.header {
background-color: #00001a;
padding: 0 1em;
}
.nav {
display: flex;
justify-content: space-between;
align-items: center;
}
ul {
list-style-type: none;
}
.header h1 {
color: #ffffff;
font-size: 15px;
}
.nav li {
color: #ffffff;
display: inline-block;
}
<div class="header">
<div class="nav">
<h1>EaTogeter</h1>
<ul>
<li>home</li>
<li>About</li>
<li>Couples</li>
<li>family</li>
</ul>
</div>
</div>

Put the heading and the navigation in their own containers. Float one left, the other right, and make sure to clear them afterwards.
header {
background-color: #00001a;
padding: 0px 10px;
box-sizing: border-box;
}
h1 {
color: white;
margin: 5px 0;
padding: 0;
}
.left {
float: left;
}
.right {
float: right;
}
.clear {
clear: both;
}
ul {
list-style-type: none;
}
ul li {
display: inline-block;
color: white;
margin-left: 20px;
}
<header>
<div class="left">
<h1>
EaTogether
</h1>
</div>
<div class="right">
<ul>
<li>Home</li>
<li>About</li>
<li>Couples</li>
<li>Family</li>
</ul>
</div>
<div class="clear"></div>
</header>
Note: I've changed "Togeter" to "Together", assuming it was a typo.

I am not sure if you want this thing but I just gave a try,
For this, set float:right to ul element and not on li elements.
Since you want to align h1 and li content set line-height to 0.5 for ul element
please check this fiddle: https://jsfiddle.net/hz0104mp/
<div class="header">
<div class="nav">
<h1>EaTogeter</h1>
<ul>
<li>home</li>
<li>About</li>
<li>Couples</li>
<li>family</li>
</ul>
</div>
</div>
<div class="Maincontent">
<div class="container">
<h2>Try It</h2>
<p>Today's Try It Recipe!<p>
</div>
</div>
.header{
background-color: #00001a;
height: 40px;
width: 100%;
}
ul{
list-style-type: none;
}
.header h1{
color: #ffffff;
font-size: 15px;
display: inline-block;
}
.nav ul{
float:right;
line-height:0.5;
}
.nav li{
display: inline-block;
color: #ffffff;
}

I like the flexbox method mentioned by #Michael Coker but here's a solution using floats as the OP attempted to do.
You were on the right track but might have been applying some of your CSS to the wrong elements for the wrong reasons.
On top of that my li posted backwards when I set it to float left so the home that was on the top of the list is now on the outer end instead of the beginning.
The reasons for this are not obvious until you break things down. The reason this happens is because float: right is applied to each element separately and in the order they appear in the markup, not as a whole. The browser will float Home as far to the right as it can. After that, it will move About as far to the right as it can. Rinse and repeat for any other li.
I rectified this by floating the ul instead of individual li and setting those to display: inline;. Floating the li to the left would also work.
header {
padding: 0 0.5rem;
height: 40px;
color: #ffffff;
background-color: #00001a;
}
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
header h1 {
margin: 0;
font-size: 15px;
display: inline-block;
}
header h1,
.nav li {
line-height: 40px;
}
.nav {
float: right;
}
.nav li {
padding: 0 0 0 0.25rem;
display: inline;
}
<header>
<h1>Eat Together</h1>
<ul class="nav">
<li>Home</li>
<li>About</li>
<li>Couples</li>
<li>Family</li>
</ul>
</header>
<main>
<h2>Try It</h2>
<p>Today's Try It Recipe!<p>
</main>
Please note that I took a few liberties with your markup to help provide an example of how it can be more semantic and achieved with less markup (along with a few choice styles to make it a little more "pretty").

Related

How do I make the spaces between my links in my navbar wider?

How do I make the spaces between my links in my navbar wider? I am trying to make a website for my production company.
I want Home to be on far left, Portfolio to be on the left, Contact on the right, and About on far right.
body {
margin: 0;
}
.header {
Color: #FFFFFF;
background-color: #000000;
padding: 50px;
text-align: center;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: rgb(0, 0, 0);
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: justify;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<div class="header">
<h1>Sphinx Productions</h1>
</div>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Contact</li>
<li>About</li>
<ul style="font-size:20px">
</ul>
You need to use display: flex and justify-content: space-between. This page is required reading regarding flex boxes.
body {
margin: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: rgb(0, 0, 0);
display: flex;
justify-content: space-between;
}
li {
}
li a {
display: block;
color: white;
text-align: justify;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
<div class="header">
<h1>Sphinx Productions</h1>
</div>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Contact</li>
<li>About</li>
</ul>
</body>
Just give padding in li in style.css
Like
li {
padding : 10px;
}
I would use CSS grid (flexbox is also an option), which will allow you to organize your links into 4 columns.
Create a div and set its display to grid:
HTML
<div class="wrapper">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
<div class="five">Five</div>
<div class="six">Six</div>
</div>
CSS
.wrapper {
display: grid;
grid-template-columns: 20% 30% 30% 20%;
}
Change the grid-template-columns as needed to get the right spacing. You can adjust column-gaps and more, just look up the documentation.
body {
margin: 0;
}
.header {
Color: #FFFFFF;
background-color: #000000;
padding: 50px;
text-align: center;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: rgb(0, 0, 0);
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: justify;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111111;
}
li:nth-child(3),
li:nth-child(4){
float: right;
}
<div class="header">
<h1>Sphinx Productions</h1>
</div>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Contact</li>
<li>About</li>
</ul>
What is nth-child()?
nth-child() is a pseudo-class css selector that selects a child and styles it using the css you put in.
For example:
<body>
<p>This is the first child of the body tag</p>
<p>This is the second child</p>
<p>This is the third child(selected)</p>
<p>This is the fourth and last child</p>
</body>
p:nth-child(3){
-----
}
The nth-child() takes one argument and that is what child will be selected.
What about float?
This is a property that will move elements to the position you input. It will stay structured with the rest of the elements.
for example:
<p>first and second child will be selected</p>
<p>----me----</p>
<p> not me </p>
p:nth-child(1),
p:nth-child(2){
float: right;
}
They will go to the right, but it won't overlap or do anything crazy.

Vertical centering in a div

I am having a problem making my logo and links align vertically. I have tried to achieve this using inline block and vertical-align set to middle but it didn't work.
I know I can achieve this using flex box. But I don't want to use flex box. And if I must use flex, is flex box better in achieving proper alignment?
Please any help will be appreciated.
.header {
padding: 20px;
background: #000;
color: #fff;
}
.logo,
ul {
display: inline-block;
vertical-align: middle;
}
ul {
list-style: none;
float: right;
}
ul li {
display: inline-block;
}
<div class="header">
<div class="logo">XCode</div>
<ul>
<li>Account settings</li>
<li>Profile</li>
<li>Logout</li>
</ul>
</div>
In many browsers, the ul element comes with default top and bottom margins.
Chrome:
Those margins are throwing off the vertical centering. Just remove them.
.header {
padding: 20px;
background: #000;
color: #fff;
}
.logo, ul {
display: inline-block;
vertical-align: middle;
}
ul {
margin: 0; /* NEW */
list-style: none;
float: right;
}
ul li {
display: inline-block;
}
<div class="header">
<div class="logo">XCode</div>
<ul>
<li>Account settings</li>
<li>Profile</li>
<li>Logout</li>
</ul>
</div>
In some browsers, the <ul> element has margin by default.
Try
ul {
list-style: none;
float: right;
margin: 0;
}
to remove the default margins.
ul {
list-style: none;
float: right;
margin:0 auto;
}
margin:0 auto; will help you to set the ul element in vertically align.
.header {
padding: 20px;
background: #000;
color: #fff;
}
.logo,
ul {
display: inline-block;
vertical-align: middle;
}
ul {
list-style: none;
float: right;
margin:0 auto;
}
ul li {
display: inline-block;
}
<div class="header">
<div class="logo">XCode</div>
<ul>
<li>Account settings</li>
<li>Profile</li>
<li>Logout</li>
</ul>
</div>
You might check out Bulma, specifically their Level attribute. This uses flexbox in the background, but you don't have to learn it- just the easy-to-use classes!
<!-- Main container -->
<nav class="level">
<!-- Left side -->
<div class="level-left">
<div class="level-item">
<p class="subtitle is-5">Content</p>
</div>
</div>
<!-- Right side -->
<div class="level-right">
<p class="level-item">More Content</p>
<p class="level-item"><a class="button is-success">New</a></p>
</div>
</nav>
margin: 0; will help you.
ul {
list-style: none;
float: right;
margin: 0;
}
https://jsfiddle.net/n0o915w3/1/

How to align heading <h1> in the center of the page

I want to center <h1> or <div class="heading"> on the page. The only solution I have found is
body { text-align: center; }
but I can't figure it out why this code doesn't work. Display: inline-block is used because I want the border to wrap around my .
body {
margin: 0;
}
.navbar {
text-align: right;
background: black;
color: white;
padding: 10px;
}
ul {
list-style-type: none;
padding: 5px;
}
li {
display: inline-block;
}
.heading {
border: 2px solid black;
display: inline-block;
text-align: center;
}
<header>
<nav class="navbar">
<ul>
<li>home</li>
<li>about</li>
<li>contact</li>
</ul>
</nav>
<div class="heading">
<h1>heading</h1>
</div>
</header>
Add this:
.heading {
text-align: center;
}
...and delete display: inline-block from .heading. Instead, add this
.heading h1 {
display: inline-block;
}
.heading is the container of your h1. Both are by default 100% wide. This centers the inline-block h1 inside the full-width .heading
The secret you are looking for is to use a block-level element, and also set a margin: 0 auto. This tells the block to centralise, much like a standard text-align: center.
.header {
display: block;
margin: 0 auto;
}
By default, block-level elements occupy 100% of the width of their container, so you might also want to specify a width for the header. Alternatively, you can have the header automatically adjust to the size of the text by adding a container div that is set as in inline-block, and moving the border to there:
body {
margin: 0;
}
.navbar {
text-align: right;
background: black;
color: white;
padding: 10px;
}
ul {
list-style-type: none;
padding: 5px;
}
li {
display: inline-block;
}
.heading {
display: block;
margin: 0 auto;
text-align: center;
}
.heading-wrapper {
display: inline-block;
border: 2px solid black;
padding: 0 10px;
}
<header>
<nav class="navbar">
<ul>
<li>home</li>
<li>about</li>
<li>contact</li>
</ul>
</nav>
<div class="heading">
<div class="heading-wrapper">
<h1>heading</h1>
</div>
</div>
</header>
This way, the header will stay centralised, and have the border automatically expand correctly to accommodate the header, no matter how much text there is.
Hope this helps! :)
You can center it by using display: flex; justify-content; on the parent element. Here is a great resource on centering things https://www.w3.org/Style/Examples/007/center.en.html
body {
margin: 0;
}
.navbar {
text-align: right;
background: black;
color: white;
padding: 10px;
}
ul {
list-style-type: none;
padding: 5px;
}
li {
display: inline-block;
}
.heading {
display: flex;
justify-content: center;
}
<header>
<nav class="navbar">
<ul>
<li>home</li>
<li>about</li>
<li>contact</li>
</ul>
</nav>
<div class="heading">
<h1>heading</h1>
</div>
</header>
a div displays block by default, so it's definitely important to declare if you want to display it otherwise.
However, again, as in another post i saw earlier, you have no css for the containing parent, the header, which would greatly assist you. You should apply any margin to be inherited to this, and there should be no need to apply a small width to your div.
body {
margin: 0;
}
header{margin: 0 auto;}
.navbar {
text-align: right;
background: black;
color: white;
padding: 10px;
}
ul {
list-style-type: none;
padding: 5px;
}
li {
display: inline-block;
}
.heading {
border: 2px solid black;
/*display: block; - even if you leave this out, it will display as block*/
text-align: center;
}
<header>
<nav class="navbar">
<ul>
<li>home</li>
<li>about</li>
<li>contact</li>
</ul>
</nav>
<div class="heading">
<h1>heading</h1>
</div>
</header>

CSS Issue UL dropping vertically instead of being horizontal

My Css menu is fine when I first set it then when I close I10 and re-open the menu is vertical. Have a look at my code.
/** MENU */
#menu {
overflow: hidden;
background: #101010;
}
#menu ul {
margin: 0px 0px 0px 0px;
padding: 0px 0px;
list-style: none;
line-height: normal;
}
#menu li {
display: inline-block;
}
#menu a {
display: block;
padding: 0px 40px 0px 40px;
line-height: 70px;
text-decoration: none;
text-transform: uppercase;
text-align: center;
font-size: 16px;
font-weight: 200;
color: rgba(255,255,255,0.5);
border: none;
}
#menu a:hover, #menu .current_page_item a {
text-decoration: none;
color: rgba(255,255,255,0.8);
}
#menu .current_page_item a {
}
#menu .last {
border-right: none;
}
Please let me know where I am going wrong.
The HTML:
<div id="wrapper">
<div id="header-wrapper">
<div id="header" class="container">
<div id="logo">
<h1>Investment Services</h1>
</div>
<div id="social">
<ul class="contact">
</ul>
</div>
</div>
<div id="menu" class="container">
<ul>
<li class="current_page_item">Homepage</li>
<li>Procedures</li>
<li>Task Rota</li>
<li>Docs & Links</li>
<li>Contact Us</li>
<li>Feedback</li>
</ul>
</div>
</div>
In cases like this, <ul> typically isn't the problem - it's the <li>
You'll be better using something like this to create a horizontal menu:
ul {
display: block
margin: 0;
padding: 0;
list-style: none;
}
li {
display: inline-block;
float: left;
}
Update
According to this JSFiddle, your menu is displayed horizontally?
Try this if your problem is not yet solved:
Give an Id for your "ul" which is inside the 'menu' div and then try the following class:
For suppose the ID of ur 'ul' is 'myUl'
#myUl li
{
display: inline;
}
Because li items need to be aligned horizontally and to be followed after their parent element while giving class name. Here the parent element will be ul but not the div

Navigation lists, inline and with spacing

i have a navigation bar and i want to be on one line it does this however there is only one space between each item, i want them to be spaced equally out, and flexible, so that when i change the window size they adjust.
this is my html
<div class="navigation">
<div class="navhead">
<h2>Navigation</h2>
</div>
<div class="navlist">
<ul>
<li>Home</li>
<li>Chat</li>
<li>Blog</li>
</ul>
</div>
</div>
and this is my css
.navlist li{
text-decoration: none;
color: #000000;
list-style-type: none;
display: inline;
text-indent: 10%;
}
please keep in mind i am in year 7 and don't use too complex words
Just apply width of your li and if needed add padding value. But change inline to table-cell. And to apply the space between them apply border-spacing value as followings:
.navlist{
border-spacing: 10px;
}
.navlist li{
text-decoration: none;
color: #000000;
list-style-type: none;
display: table-cell;
text-indent: 10%;
width: 20%;
padding: 1em;
}
Check this demo
You could display the ul as a table, like this:
HTML:
<ul>
<li>link</li>
<li>link</li>
<li>link</li>
</ul>
CSS:
ul {
width: 90%;
background: #222;
margin: 0;
padding: 10px;
display: table;
table-layout: fixed;
}
ul li {
display: table-cell;
}
ul li a {
display: block;
background: #555;
text-align: center;
color: white;
padding: 10px 0;
}
Also check this demo.