I've been trying to get all those elements on the same grey background but I don't know why the header and navigation bar are separated, and the logo not being on top of the grey background.
The code:
.header {
background-color: #666;
text-align: center;
font-size: 35px;
color: white;
margin:0;
}
#navigation {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #666;
}
#navigation li {
float: left;
}
#navigation li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#navigation li a:hover {
background-color: #111;
}
<div>
<h2 class = "header" >Personal Portal</h2>
</div>
<div>
<div>
<nav>
<a> <img src="logo.png" alt="Logo" style="float:right; position:relative; "> </a>
<ul id = "navigation">
<li><a class="active" href="Home.htm">Home</a></li>
<li>Opened Tickets</li>
<li>FAQ</li>
<li>Stats</li>
</ul>
</nav>
</div>
<div>
<div id="loggedas" style = "float:right; padding-top:0; background-color:#666; color:#FFFFFF"> Logged in as </div>
This is a very beginner-style question but I can't find the answers I want. Any help is appreciated thank you!
You could try something like this:
<div style="background-color: #666; height: 50px">
<nav style="clear: both">
<ul id = "navigation">
<li><a class="active" href="Home.htm">Home</a></li>
<li>Opened Tickets</li>
<li>FAQ</li>
<li>Stats</li>
</ul>
<h2 class = "header" >Personal Portal</h2>
<div class="rightInfo">
<div id="loggedas" style = "float:right; padding-top:0; background-color:#666; color:#FFFFFF">
Logged in as
</div>
<a>
<img src="logo.png" alt="Logo" style="float:right; position:relative; ">
</a>
</div>
</nav>
.header {
background-color: #666;
text-align: center;
font-size: 35px;
color: white;
float: left;
display: inline-block;
margin-top: 0;
}
.rightInfo{
float: right;
}
#navigation {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #666;
float: left;
}
#navigation li {
float: left;
}
#navigation li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
#navigation li a:hover {
background-color: #111;
}
Your three display elements should have a common parent, otherwise, they will not display as you want them to, except if you use position: absolute to position them (I would not recommend that)
To complete the previous answer (regarding the question why the title and nav bar are separated). You have placed those elements in separate divisions each with its own background color. You could correct the situation by removing the margin between them, but: if you want two (or more) things to be on a common background, it is best to set that background on a common parent.
Also, in any case when you're wondering why the browser rendered your code as it did: ask the browser. Press Ctrl-Shift-I (works on Chrome, Chromium and Firefox). It will show you all the applied styles (and exactly where they came from), plus the element size, padding, borders and margins.
Related
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").
I am new to all this. I cant align the navbar with the logo because for some reason margin-top/ bottom don't seem to work.
<HTML>
<HEAD>
<link href="style.css" rel="stylesheet">
</HEAD>
<BODY>
<div class="nav_header" "nav">
<ul>
<img src="assets/logo2.png" class='title'>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
</BODY>
Here is CSS code
body {
background-image: url('assets/bg.png');
background-repeat: repeat;
}
a:link, a:visited {
color: gray;
font-weight: normal;
text-decoration: none;
}
a:hover {
text-decoration: none;
color: white;
}
.nav {
list-style-type: none;
height: 60px;
background-color: #000000;
}
.nav_header {
height: 60px;
background: #000000 left no-repeat;
}
.title {
margin-left: 80px;
}
li {
display: inline-block;
margin-left: 40px;
margin-bottom: 40px;
font-size: 20;
float: none;
}
You code should look like this:
<div class="nav_header" "nav">
<div class="logo">
<img src="assets/logo2.png" class='title'>
</div>
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
add css:
.nav_header ul, .nav_header ul li, .nav_header .logo {
float: left;
}
it very recommended to set height and width to div that wrap image.
Try wrapping your image in <li></li> tags and then manage the image dimensions.
Try putting the he <img /> tag outside your <ul><ul> but still inside your <div><div> and then float the <li><li> and the image left.
After a bit of guessing what you actually want to achieve...
You can use CSS vertical-align :
vertical-align: middle;
Then you can also use margin-bottom to further align them if you want.
DEMO
add img in <li> tag
a:link, a:visited {
color: gray;
font-weight: normal;
text-decoration: none;
}
a:hover {
text-decoration: none;
color: white;
}
.nav_header {
height: 60px;
list-style-type: none;
background-color: #000000;
display:block;
}
.title {
margin-left: 80px;
}
li {
display: inline-block;
margin-left: 40px;
margin-bottom: 40px;
font-size: 20px;
float: none;
}
li img {
border:10px solid #333;
}
<div class="nav_header">
<ul>
<li><img src="assets/logo2.png" class='title'></li>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
See JSfiddle:
<div class="nav_header nav">
<img src="https://s.w.org/about/images/logos/wordpress-logo-32-blue.png" class="title" height="32" width="32" />
<ul>
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
The CSS:
.nav_header {
height: 34px;
padding:13px 0 13px 20px;
background: #000000 left no-repeat;
}
.nav_header > ul,
.nav_header > img{display:inline-block; vertical-align:middle; margin:0; padding: 0;}
li {
display: inline-block;
margin-left: 40px;
font-size: 20;
}
Semantically you'll want to pull your image out of your unordered list. If you set both img and ul as inline-blocks, you can align themselves to eachother. I've vertically centered both image and navigation to your header by compensating the nav's height for some padding (top and bottom).
A few notes about your above code:
The only elements that can be direct descendents of ul and ol are li. If you want to add an img to a list, it has to be included within a li.
Multiple classes need to be inluced within the same double quotations, see nav_header and nav.
Always declare your height and width attributes on your image tags.
Keep a certain standard when writing HTML in regards to using single our double quotations for your attribute value (see title class) and uppercase and lowercase HTML tags.
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
I'm relatively new to HTML (only a few weeks now), and I'm stuck. The "about" section on my page doesn't seem to be clickable and won't bring me to the linked page. The confusing thing is that the link appears to be clickable since the floating hand icon appears when hovering over the link. I can right-click and open the link in a new tab. If I delete the css and try the link without the formatting, it is also valid.
I'm confused... is the CSS somehow messing with my href tag?
here's the html:
<div id="header">
<div class="container">
<div class="row">
<div class="twelwecol last">
<div id="navigation"> <!-- Navigation Links -->
<ul>
<li>about</li>
<li>members</li>
<li>events</li>
<li>media</li>
<li>social</li>
</ul>
</div>
</div>
</div>
</div>
</div>
and the code from the "navigation.css" page:
#header {
width: 100%;
height: 49px;
color: white;
background-color: #1b1e25;
position: fixed;
z-index: 9999;
}
#logo {
background-image: url('../images/assets/logo.png');
background-repeat: no-repeat;
height: 80px;
width: 80px;
display: block;
float: left;
}
#navigation {
padding-top: 2px;
width: 480px;
display: block;
margin: 0px auto;
}
#navigation ul {
padding:0px;
margin: auto;
text-transform: uppercase;
}
#navigation ul li {
display:inline;
float:left;
list-style:none;
padding: 13px 20px 13px 20px;
}
#navigation ul li {
display:inline;
float:left;
list-style:none;
padding: 13px 20px 13px 20px;
}
#navigation ul li a {
color: gray;
text-decoration: none;
font-family:'Scada', sans-serif;
font-size: 13px;
}
#navigation ul li a:hover {
color: white;
}
.current {
border-bottom: 1px solid #cb1c1c;
color: white;
}
#contact {
padding-top: 12px;
float: right;
position: relative;
}
#contact a {
text-decoration: none;
text-transform: uppercase;
font-size: 9px;
color: gray;
padding: 11px;
}
#contact a:hover {
color: white;
border: #333333 1px solid;
padding: 10px;
}
Could someone please tell me what is going on here? Any help would be most appreciated!
it appears clickable because it is contained within the a tag. When you link something your href needs to have a proper extension. Are you sure youre using .htm and not html?
furthermore CSS affects Style (css= cascading style sheet) it wont affect html (or any other) functions like the href function.
try changing your href to href="about.html"
I have a very plain navigation menu using an unordered list laid out horizontally using display:inline;. The previews in my HTML editor show the page coming together just fine. However, when it's viewed in Chrome and IE, there's a strange padding on top of the nav menu and only on the top. Using the process of elimination, I know this is a problem with my CSS for the <li> tag but I'm not sure what the problem is.
So far I've tried display:inline-block, lowering the font size, setting the <ul> tag in the nav menu to display:inline, and a myriad other things. None seems to be helping. Any advice for where the CSS went wrong? Here is the HTML in question...
<body>
<div id="wrapper">
<div id="header"></div>
<div id="navigation">
<ul>
<li>welcome</li>
<li>who we are</li>
<li>what we do</li>
<li>contact</li>
</ul>
</div>
<div id="content"> </div>
</div>
</body>
And here is the CSS...
body {
background-color: #000000;
margin: 0;
padding: 0;
font-family: Arial, Helvetica, Sans-Serif;
text-align: center;
}
#header {
background-color: #ffffff;
height: 100px;
}
#wrapper {
width: 960px;
text-align: left;
}
#navigation {
height: 45px;
background-color: #C0C0C0;
font-size: 1.3em;
text-align: right;
}
#navigation a {
color: #00132a;
text-decoration: none;
}
#navigation a:hover {
color: #483D8B;
}
#navigation ul {
padding-top: 10px;
}
#navigation ul li {
display: inline;
list-style-type: none;
padding: 0 30px 0 30px;
}
#navigation-symbol {
font-size: 1em;
}
#content {
background-color: #ffffff;
text-align: left;
font-size: 14px;
}
And for interactive fun there's a jsFiddle as well which shows the exact same phenomenon I'm seeing. Thanks ahead for the advice!
Simply set margin to zero
#navigation ul {
margin: 0;
padding-top: 10px;
}