I am trying to move my Nav Bar to the Top Right with my logo on the Top Left all on one line. But I am unable to do so and I could use some help. I am new to learning HTML and CSS. The nav bar currently rests below the name/logo on the top right.
Demo
.container{
padding: 40px 40px 40px 40px;
margin: 10px 10px 10px 10px;
position: relative;
display: block;
text-shadow: 1px 1px 1px rgba(0,0,0,0.2);
text-align: left;
border: 1px solid blue;
color:white;
}
.container h1{
text-align: left;
font-size: 50px;
}
.container nav{
height: 70px;
line-height: 70px;
border: 1px solid green;
}
.container nav ul {
list-style: none;
width: 100%;
border: 1px solid red;
text-align:right
}
.container nav ul li {
display: inline-block;
font-size: 100%;
color:white;
margin-right: 0;
border : 1px solid yellow;
}
<div class="container">
<header>
<h1>Srikanth Gowda</h1>
</header>
<nav class="navbar">
<ul>
<li>Design</li>
<li>Photography</li>
<li>About</li>
<li>Contact</li>
<li>Blog</li>
</ul>
</nav>
</div>
One way would be to use float.
Per documentation found on MDN Web Docs
The float CSS property specifies that an element should be placed along the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the web page, though still remaining a part of the flow (in contrast to absolute positioning).
Learn more about float
Add the following to your CSS
.container header {
float: left;
}
Change your .container nav to:
.container nav{
height: 70px;
line-height: 70px;
border: 1px solid green;
float: right;
}
This will get you what you want...
Working demo
A couple of quick and dirty methods as starting points:
Method 1, using flexbox:
.container{
display: flex;
align-items: center;
justify-content: space-between;
}
nav ul {
display: flex;
list-style: none;
}
nav ul li {
margin-right: 20px;
}
<div class="container">
<header>
<h1>Srikanth Gowda</h1>
</header>
<nav class="navbar">
<ul>
<li>Design</li>
<li>Photography</li>
<li>About</li>
<li>Contact</li>
<li>Blog</li>
</ul>
</nav>
</div>
Method 2, using floats:
header {
float: left;
}
nav {
float: right;
}
nav ul {
list-style: none;
padding-top: 18px;
}
nav ul li {
display: inline-block;
margin-right: 5px;
}
<div class="container">
<header>
<h1>Srikanth Gowda</h1>
</header>
<nav class="navbar">
<ul>
<li>Design</li>
<li>Photography</li>
<li>About</li>
<li>Contact</li>
<li>Blog</li>
</ul>
</nav>
</div>
you can use flex box property. and float right and left to align your div
.container{
position: relative;
display: block;
text-shadow: 1px 1px 1px rgba(0,0,0,0.2);
height:70px;
border: 1px solid blue;
color:blue;
}
.container h1{
font-size: 50px;
}
.container .navbar{
height: auto;
line-height: auto;
border: 1px solid green;
float:right;
}
.container .navbar ul {
list-style: none;
width: auto;
border: 1px solid red;
}
.container .navbar ul li {
display: inline;
font-size: 100%;
color:blue;
border : 1px solid yellow;
}
.header{
float:left;
}
.d1{
float:clear;
height:100px;
width:1000px;
}
<div class="container">
<div class="header">
<h4>Srikanth Gowda</h4>
</div>
<div class="navbar">
<ul>
<li>Design</li>
<li>Photography</li>
<li>About</li>
<li>Contact</li>
<li>Blog</li>
</ul>
</div>
</div>
<div>
<div class="d1">
rest of your content
eferfer
</div></div>
When you want to align elements in a div on the same line but on different sides always use the float property. You can change the header's position to left and nav to right by using the float property as below:
.header{
float:left;
}
.container nav{
height: 70px;
line-height: 70px;
border: 1px solid green;
float: right;
}
There are two ways. the first one is given below.
.container header{
display:inline-block;
float:left;
}
.navbar{
display:inline-block;
float:right;
}
The second method is you can put them in a table like below.
<div class="container">
<table>
<tr>
<td>
<header>
<h1>Srikanth Gowda</h1>
</header>
</td>
<td>
<nav class="navbar">
<ul>
<li>Design</li>
<li>Photography</li>
<li>About</li>
<li>Contact</li>
<li>Blog</li>
</ul>
</nav>
</td>
</tr>
</table>
</div>
Related
I have this HTML code:
<body>
<header id="masthead">
<div id="container">
<!-- logo -->
<img src="logo.png" alt="" width="200px" class="logo">
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>About Developers</li>
<li>History</li>
<li>Economy</li>
<li>Why Study in Dublin?</li>
<li>People and Culture</li>
</ul>
</nav>
</div>
</header>
And this CSS code:
.container {
width: 80%;
margin: 0 auto;
}
.logo {
float: left;
padding: 10px 0;
}
nav {
float: right;
}
header::after {
content : '';
display: table;
clear: both;
}
nav li {
display: inline-block;
margin-left: 70px;
padding-top: 2px;
position: relative;
padding-right: 0.1rem;
}
nav a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 14px;
}
However I want to make my nav bar to the left from the logo, but not down below. How can I do it using the given initial code that i hav pointed ? As you can see, align: right and align: left has been used, but had not helped me
Like on photo (Used arrows to point it )
Create two columns. In one column, place your logo, and in the second column, place your navigation bar.
<div class="row">
<div class="column" style="background-color:#aaa; width:20%;">
<!--pLACE Logo here--->
</div>
<div class="column" style="background-color:#bbb; width:80%">
<!--Place Navbar code here-->
</div>
</div>
Remember Adjust your css accordingly
Give your div with id container a display property of flex, direction property of row and then align or justify items as per your liking
#container{
display:flex;
flex-direction:row;
justify-content:space-between;
}
Also in your HTML code you've given tags ids but you're using class selectors in your CSS
Some resources that'll help you:
A Complete Guide to Flexbox
Basic Concepts of Flexbox
Flexbox Cheatsheet
You will have to change your CSS as shown below:
/*add this line to adjust paddings on the columns and remove default margin padding for all the html elements*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/*change class to # it's ID and not class*/
#container {
width: 80%;
margin: 0 auto;
}
/*recommended to add width in percentage in css and remove fix width from <img width='200px' /> tag*/
.logo {
float: left;
width:20%;
padding: 10px 0;
}
/*add width 80% for <nav> tag*/
nav {
float: right;
width: 80%;
margin-top: 10%;
}
nav li {
display: inline-block;
/* margin-left: 70px; */ /*remove*/
/* padding-top: 2px; */ /*remove*/
position: relative;
/* padding-right: 0.1rem; */ /*remove*/
padding: 0 5px; /*instead add this for space between li content*/
}
I would recommend you to use CSS FLEXBOX.
I used flexbox to do this. your id="container" was not the same as the CSS so I changed it to class="container"
I added some simple 1px borders just to illustrate what is where on the page.
This is likely not a finished solution and only a starting point.
.container {
width: 90%;
margin: 0 auto;
display: flex;
flex-direction: row;
flex: space-between font-size: 16px;
justify-content: center;
align-items: center;
}
.logo {
padding: 10px 0;
height: 3em;
border: 1px solid lime;
}
header::after {
content: '';
display: table;
clear: both;
}
nav {
border: 1px solid cyan;
justify-content: center;
align-items: center;
}
nav ul li::before {
content: "\200B";
}
nav ul {
display: flex;
flex-direction: row;
border: 1px solid blue;
list-style-type: none;
justify-content: center;
list-style-position: inside;
margin-left: 0;
padding-left: 0;
}
nav ul li {
padding-top: 0.2em;
padding-right: 0.1rem;
border: 1px solid pink;
margin-left: 0;
padding-left: 0;
}
nav li a {
color: #444;
text-decoration: none;
text-transform: uppercase;
font-size: 0.875em;
margin-left: 1em;
margin-right: 1em;
}
<header id="masthead">
<div class="container">
<img src="logo.png" alt="logo png" width="200px" class="logo">
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
<li>About Developers</li>
<li>History</li>
<li>Economy</li>
<li>Why Study in Dublin?</li>
<li>People and Culture</li>
</ul>
</nav>
</div>
</header>
Trying to set up a simple 'cheat sheet' of logo and nav bar that I can reuse. I'm having trouble preventing the right floated nav from overflowing into content below.
NB. I'm new to this so I have added borders to all elements to help me see what's happening.
I have tried to add a clear:left (even though I know it should be clear:right as the content has been floated right) to the element proceeding the navigation but that did not work of course!
*{
box-sizing: border-box;
}
header{
display: inline-block;
}
.logo{
background-image: url("logo.png");
background-repeat:no-repeat;
width: 140px;
height: 81px;
margin: 10px;
display: inline-block;
}
nav{
border: 1px solid black;
float: right;
width: auto;
}
ul{
list-style: none;
padding-left: 0;
border: 1px solid red;
vertical-align: middle;
}
li{
display: inline-block;
width: auto;
padding: 20px;
background-color: yellow;
margin-bottom: 5px;
border: 1px solid yellow;
}
.divider{
clear: both;
}
<header>
<div class="logo"></div>
<nav>
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Shopping</li>
<li>Contact Me</li>
</ul>
</nav>
</header>
<div class="divider"></div>
<main>
<h1>The display Property</h1>
So next I tried clear:both and that did not work.
I have also tried to set the nav to display:inline-block but that did not work.
Ideally I would like to solve this without using any pre-provided templates and my just raw code or display:flex.
Thanks in advance :)
Hope this helps.
.nav_bar {
margin: 10px;
padding: 10px;
background-color: #00021a;
color: white;
}
.logo {
display: inline-block;
vertical-align: middle;
width: 7%;
}
.logo img {
height: auto;
width: 50px;
}
.nav {
display: inline-block;
vertical-align: middle;
width: 90%;
}
.nav_container li {
list-style: none;
float: right;
padding: 10px;
}
<head>
<link rel="stylesheet" href="styles.css">
</head>
<div class="nav_bar">
<div class="nav_container">
<div class="logo">
<img class="logo"
src="https://t4.ftcdn.net/jpg/00/71/26/83/240_F_71268376_KIbJvY0SYwlvikWpahjNCv5IBfukykG9.jpg"/>
</div>
<nav class="nav">
<ul>
<li>About</li>
<li>Portfolio</li>
<li>Shopping</li>
<li>Contact Me</li>
</ul>
</nav>
</div>
</div>
Here's the Fiddle
I have a flex element <header> which contains two flex items: <span> and <ul>
My goal is to display flex items horizontally in one line like in this photo:
but the result which I get is:
I have figured out that my problem may be solved by using flex-basis: 100%; But the behaviour of the flexbox container is not understandable for me.
Why does it split <li> items in two lines instead of displaying them in one?
html:
<header>
<span>LOGO</span>
<ul>
<li>Library</li>
<li>Telegram channel</li>
<li>Contacts</li>
<li>Donate</li>
</ul>
</header>
css:
span {
border: 1px solid black;
margin-right: 10px;
}
ul {
padding: 10px;
}
li {
display: inline-block;
padding: 0 5%;
border: 1px solid black;
}
header {
display: flex;
align-items: center;
}
use width:100% and margin:0 for ul
span {
border: 1px solid black;
margin-right: 10px;
}
ul {
padding: 10px;
margin:0;
width:100%;
}
li {
display: inline-block;
padding: 0 5%;
border: 1px solid black;
}
header {
display: flex;
align-items: center;
}
<header>
<span>LOGO</span>
<ul>
<li>Library</li>
<li>Telegram channel</li>
<li>Contacts</li>
<li>Donate</li>
</ul>
</header>
I am trying to add a border to my header, but when I do, the line-height (or something like that) is getting messed up.
HTML -
<div class='header'>
<ul>
<li>Home</li>
<li>Services</li>
<li>Estimates</li>
<li>Co2 Calculator</li>
<li>Contact Us</li>
</ul>
</div>
CSS -
.header {
background-color: #006633;
height: 50px;
border-radius: 5px;
border: 1px solid gray;
}
ul.headerlist {
line-height: 50px;
float: left;
list-style-type: none;
width: 19%;
text-align: center;
color: white;
}
Is it something to do with using <ul> or <li>?
start by cleaning up you HTML structure.
You don't need to apply the class to all the LI's since they're all
the same.
Apply the block of CSS you had for .header to the ul instead
Use line-height that measures the height of the ul to get the li to
center vertically.
.header {
/* do whatever you want with the header */
width: 100%; /* Adjust as needed */
height: auto;
}
.header ul {
background-color: #006633;
height: 50px;
border-radius: 5px;
border: 1px solid gray;
display: block;
}
.header li {
width: 19%;
text-align: center;
color: white;
display: inline-block;
vertical-align: middle;
line-height: 50px;
}
<div class=header>
<ul>
<li>Home</li>
<li>Services</li>
<li>Estimates</li>
<li>Co2 Calculator</li>
<li>Contact Us</li>
</ul>
</div>
.header ul{margin-top:0;} There you go.
try this fiddle: http://jsfiddle.net/m71mchow/14/
and add this css :
ul, li{margin:0; padding:0}
I am trying to set the background to go horizontally across this bar to be behind the logo and nav
Here is the HTML
<!--NAV-->
<div class="header">
<div class="title">
<img src="img/logo.png">
</div><!--/.title-->
<nav class="nav">
<ul>
<li>Home</li>
<li>Services</li>
<li>Contact</li>
</ul>
</nav>
</div>
and here is the css
.title
{
display:inline;
float:left;
margin:0 auto;
}
.nav
{
font-size:20px;
color:#0F3;
display:inline;
float:right;
margin:0 1.7%;
padding:1% 4% 0;
}
ul
{
display:inline;
float:right;
list-style:none;
}
li
{
display:inline;
}
You can modify .header class.
Keep in mind that floating divs don't affect the size of the surrounding element. So you can use overflow: auto; in header class to make the surrounding element take the floating elements in consideration:
Example
.header {
background: url(http://lorempixel.com/800/100/abstract/);
width: 100%;
overflow: auto;
}
img {
padding: 12px;
}
.title {
display: inline;
float: left;
margin: 0 auto;
}
.nav {
font-size: 20px;
color: black;
text-shadow: 2px 2px 2px rgba(150, 150, 150, 1);
display: inline;
float: right;
margin: 0 1.7%;
padding: 0 4%;
padding-top: 1%;
margin-bottom: 0;
}
ul {
display: inline;
float: right;
list-style: none;
}
li {
display: inline;
}
<div class="header">
<div class="title">
<img src="http://lorempixel.com/100/40/people/">
</div>
<!--/.title-->
<nav class="nav">
<ul>
<li>Home</li>
<li>Services</li>
<li>Contact</li>
</ul>
</nav>
</div>