Image in navigation - html

This is what I am trying to recreate. A basic nav.
As you can see here, there is an image next to the "ABOUT US" text, that is what I am having trouble with. How exactly can I make the img appear before the "ABOUT US" text? It always appears above it. Thanks in advance!
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
img {
max-width: 100%;
height: auto;
}
body {
font-family: "Open Sans",sans-serif;
}
.container {
max-width: 900px;
margin: 0 auto;
}
header {
padding: 2em 4em;
background-color: #121b21;
}
.nav-area {
list-style: none;
text-align: center;
}
.nav-area li {
display: inline-block;
padding: 0 1.5em;
color: #c4cbcf;
font-weight: 700;
font-size: 0.9em;
background-color: transparent;
}
<header>
<div class="container">
<nav>
<img src="header_logo.png" alt="Logo header">
<ul class="nav-area">
<li>ABOUT US</li>
<li>CONSULTING</li>
<li>SKYLIGHT</li>
<li>CONTACT</li>
</ul>
</nav>
</div>
</header>

Give a display:flex; property to nav and it will work :) Then you can adjust other CSS accordingly.
You may also set display:flex; on .nav-area, then you don't need inline-block on .nav-area li's.
.nav-area {
list-style: none;
text-align: center;
display:flex;
align-items:center;
}
CODEPEN WORKING DEMO: https://codepen.io/emmeiWhite/pen/WNGYJjq
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
img {
width:25px;
height: auto;
margin-left:1.5em;
}
body {
font-family: "Open Sans",sans-serif;
}
.container {
max-width: 900px;
margin: 0 auto;
}
header {
padding: 2em 4em;
background-color: #121b21;
}
nav{
display:flex;
align-items:center;
}
.nav-area {
list-style: none;
text-align: center;
}
.nav-area li {
display: inline-block;
padding: 0 1.5em;
color: #c4cbcf;
font-weight: 700;
font-size: 0.9em;
background-color: transparent;
}
<header>
<div class="container">
<nav>
<img src="https://picsum.photos/200" alt="Logo header">
<ul class="nav-area">
<li>ABOUT US</li>
<li>CONSULTING</li>
<li>SKYLIGHT</li>
<li>CONTACT</li>
</ul>
</nav>
</div>
</header>

I tried like this.
nav {
display: flex;
align-items: center;
justify-content: center;
}
If you want logo goes to left and nav-menu goes to right, try like this.
nav {
display: flex;
align-items: center;
justify-content: space-between;
}

Add the float attribute to the img CSS declaration to obtain the desired lay-out.
img {
max-width: 100%;
height: auto;
float: left;
}

Related

I have tried putting display: inline-block in every scenario and I can't get it to cross the top of the page . .

I am trying to create a NAV bar and I have tried putting 'display: inline-block;' in every single element but for the life of me it won't cross the whole page. Any idea's? I want to have my logo in the center of the navigation bar with the other links centered across the rest of the top of the page. It doesn't matter where I put the 'display: inline-block;' it never centers it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Gingerich Tiling</title>
<link href="main.css" type="text/css" rel="stylesheet">
<link type="text/css" rel="stylesheet">
</head>
<body>
<header>
<nav class="nav1">
<div class="left-nav-bar">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
</ul>
</div>
<div class="logo">
<li><a class="logo" href="index.html"><img src="Images/Gingerich%20Final%20Logo.jpg" alt ="Gingerich Tiling Logo" height="250" width="300"</a></li>
</div>
<div class=right-nav-bar>
<ul>
<li>Tiling</li>
<li>Earthmoving</li>
<li>Septic</li>
</ul>
</div>
</nav>
</header>
</body>
</html>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
color: black;
}
body {
}
header {
text-align: center;
display: inline-block;
}
.nav1 {
align-content: center;
list-style: none;
text-decoration: none;
text-transform: uppercase;
}
nav a {
text-decoration: none;
}
nav ul {
}
.nav1 li {
display: inline-block;
}
.left-nav-bar {
margin: 0;
padding: 0;
text-decoration: none;
}
.logo {
}
.right-nav-bar {
margin: 0;
padding: 0;
text-decoration: none;
}
After re-reading the question, I think I got how you want it to look. The problem was that you were using inline-block a bit too much, actually!
You can achieve the look using display: flex (display: grid would also work, but I think flex layout is more straight-forward in this case) in conjunction with justify-content: space-evenly on your <nav> element.
Please also note that you cannot have a <li> element anywhere other than as a direct child element of a <ul> (that was not the case with your logo!)
Once you add a little padding to the sides of your navigation links, it should look good.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
color: black;
}
body {}
header nav {
text-align: center;
display: flex;
justify-content: space-evenly;
}
.nav1 {
align-content: center;
list-style: none;
text-decoration: none;
text-transform: uppercase;
}
nav a {
text-decoration: none;
padding: 0 0.3em;
}
nav ul {}
.nav1 li {
display: inline-block;
}
.left-nav-bar {
margin: 0;
padding: 0;
text-decoration: none;
}
.logo {
width: 48px;
}
.right-nav-bar {
margin: 0;
padding: 0;
text-decoration: none;
}
<header>
<nav class="nav1">
<div class="left-nav-bar">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Contact Us</li>
</ul>
</div>
<div class="logo">
<a class="logo" href="index.html"><img src="//placekitten.com/48/48" alt="Gingerich Tiling Logo" height="48" width="48"> </a>
</div>
<div class=right-nav-bar>
<ul>
<li>Tiling</li>
<li>Earthmoving</li>
<li>Septic</li>
</ul>
</div>
</nav>
</header>
You have a lot of unnecessary containers. But I didn't change the HTML but I did change your CSS. The best way to make navigation is by using display: flex;. They are easy to use and very effective.
Here is the CSS, just copy-paste this and see if you like the result :)
* {
box-sizing: border-box;
margin: 0;
padding: 0;
color: black;
}
/* body {
} */
header {
width: 100vw;
height: 100px;
max-width: 100%;
}
.nav1 {
list-style: none;
text-decoration: none;
text-transform: uppercase;
width: 90%;
height: 100%;
margin: auto;
display: flex;
justify-content: space-between;
align-items: center;
}
nav a {
text-decoration: none;
}
/* nav ul {
} */
.nav1 li {
display: inline-block;
}
.left-nav-bar,
.right-nav-bar {
width: 33%;
height: 100%;
text-decoration: none;
display: flex;
justify-content: space-around;
align-items: center;
}
.left-nav-bar ul,
.right-nav-bar ul {
width: 100%;
height: 100%;
display: flex;
justify-content: space-around;
align-items: center;
row-gap: 20px;
}

css reset margin problem.i cant get rid of margin

My problem is that I have a header that is supposed to take up the whole top of the page but for some reason, there is a margin on both sides.I tried * margin :0 padding :0 but doesn't work.
when I add div.container margins appears. i added .container because when i grow the page i want my name and nav to stay in the middle withsome space from the sides.as the picture that i shared but without whitespace/that margins. i hope i could explained
appreciate your help
thanks.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: black
}
html {
font-family: 'Space Mono', monospace, sans-serif;
}
#navbar {
background-color: yellow;
color: black;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.75rem 2rem 0.75rem 1rem;
}
#navbar ul {
list-style-type: none;
display: flex;
align-items: center;
}
#navbar ul li a {
padding-left: 0.75rem;
}
.container {
max-width: 1100px;
margin: 0 auto;
<html>
<head>
</head>
<body>
<header>
<div class="container">
<nav id="navbar">
<h1 class="">Hayden Dominic Christiansen</h1>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Bio</li>
<li>Contact</li>
</ul>
</nav>
</div>
</header>
</body>
</html>
Add the color to the header. See below:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: black
}
html {
font-family: 'Space Mono', monospace, sans-serif;
}
header { background: yellow; }
#navbar {
color: black;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.75rem 2rem 0.75rem 1rem;
}
#navbar ul {
list-style-type: none;
display: flex;
align-items: center;
}
#navbar ul li a {
padding-left: 0.75rem;
}
.container {
max-width: 1100px;
margin: 0 auto;
<html>
<head>
</head>
<body>
<header>
<div class="container">
<nav id="navbar">
<h1 class="">Hayden Dominic Christiansen</h1>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Bio</li>
<li>Contact</li>
</ul>
</nav>
</div>
</header>
</body>
</html>
You might need to add margin and padding on the body tag.
body{
margin:0;
padding:0;
box-sizing: border-box;
}
a{
text-decoration: none;
color:black
}
header {
width: 100%;
}
html{
font-family: 'Space Mono', monospace,sans-serif;
}
#navbar{
background-color: yellow;
color: black;
display: flex;
justify-content: space-between;
align-items: center;
padding:0.75rem 2rem 0.75rem 1rem;
}
#navbar ul{
list-style-type: none;
display: flex;
align-items: center;
}
#navbar ul li a {
padding-left: 0.75rem;
}
</head>
<body>
<header>
<div class="container">
<nav id="navbar">
<h1 class="">Hayden Dominic Christiansen</h1>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Bio</li>
<li>Contact</li>
</ul>
</nav>
</div>
</header>
</body>
</html>
Remove the max-width attribute from the the .container selector as it is fixing the width of your navbar to a max of 1100px. Removing it will solve your problem.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
color: black
}
html {
font-family: 'Space Mono', monospace, sans-serif;
}
#navbar {
background-color: yellow;
color: black;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.75rem 2rem 0.75rem 1rem;
}
#navbar ul {
list-style-type: none;
display: flex;
align-items: center;
}
#navbar ul li a {
padding-left: 0.75rem;
}
.container {
/* max-width: 1100px; Remove this line as it is fixing the navbar to a max of 1100px */
margin: 0 auto;
}
<html>
<head>
</head>
<body>
<header>
<div class="container">
<nav id="navbar">
<h1 class="">Hayden Dominic Christiansen</h1>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Bio</li>
<li>Contact</li>
</ul>
</nav>
</div>
</header>
</body>
</html>
Then try this header.container {margin: 0 !important; padding: 0 >!important}
and lose your job for that xD
first, if you find yourself using important in your own styles, you are doing something wrong.
second, his container is a div, divs do not have any default margins or paddings - nothing to set to 0.
If you don't want margin on your container class, try adding
.container {
margin: 0;
padding: 0
}

Text displays under image instead of inline

I'm trying to say "my website bby" right next to the image logo and for some reason it displays under the logo. Help me please. Here is what it is right now, check the image
Here is my html:
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background:#6BD326;
}
#logo {
float:left;
display:inline-block;
}
.branding {
float: left;
}
nav li {
float:left;
padding: 0px 20px;
list-style-type: none;
}
nav {
float:right;
}
#logo {
margin-top:5px;
width:40%;
}
<header>
<div class="branding">
<div id="logo">
<img src="https://picsum.photos/200/300" style="width:100px;" />
</div>
<span><h1><u>My</u> Website bby</h1></span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</header>
To have a better understanding of what I'm talking about, check the picture above. I just started out coding, really need to understand what's wrong.
You can also use a combination of display: inline-block and vertical-align: middle.
For example:
body {
font-size: 20px;
font-family: Optima, sans-serif;
line-height: 1.5;
padding: 0;
margin: 0;
}
.container {
width: 80%;
overflow: hidden;
}
header {
font-family: Monaco, sans-serif;
color: white;
padding-top: 100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background: #6BD326;
}
#logo {
float: left;
display: inline-block;
vertical-align: top;
}
.i-b {
display: inline-block;
vertical-align: top;
width: calc(100% - 40%);
}
.branding {
float: left;
}
nav li {
float: left;
padding: 0px 20px;
list-style-type: none;
}
nav {
float: right;
}
#logo {
margin-top: 5px;
width: 40%;
}
<div class="branding">
<div id="logo">
<img src="https://i.picsum.photos/id/237/536/354.jpg?hmac=i0yVXW1ORpyCZpQ-CknuyV-jbtU7_x9EBQVhvT5aRr0" style="width:100px;" />
</div>
<span class="i-b"><h1><u>My</u> Website bby</h1></span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</div>
Jsfiddle code
check this out i removed all the floats and worked with flex
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
margin: 0;
padding: 10px;
background:#6BD326;
display: flex;
justify-content:space-between;
flex-wrap: wrap;
}
#logo {
margin-top: 5px;
display: flex;
align-items: center;
}
#logo span{
margin-left: 10px;
}
nav ul {
display: flex;
flex-wrap: wrap;
}
nav li {
padding: 0px 20px;
list-style-type: none;
}
<header>
<div class="branding">
<div id="logo">
<img src="https://cdn.mos.cms.futurecdn.net/BVb3Wzn9orDR8mwVnhrSyd-1200-80.jpg" style="width:100px;" />
<span><u>My</u> Website bby</span>
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
</div>
</header>
Here is your code which is work
body like that :
<body>
<div class="branding">
<div id="logo">
<img src="./img/logo.png" style="width:100px;" />
</div>
<nav>
<ul>
<li>Home</li>
<li>Photogallery</li>
<li>Contact me</li>
</ul>
</nav>
<span><h1 style="padding: 30px; margin-left:130px;"><u>My</u> Website bby</h1></span>
</div>
</div>
</body>
your css should be like that :
body {
font-size:20px;
font-family: Optima, sans-serif;
line-height:1.5;
padding: 0;
margin:0;
}
.body img{
display: inline;
}
.container {
width:80%;
overflow:hidden;
}
header {
font-family: Monaco, sans-serif;
color:white;
padding-top:100px;
min-width: 90%;
min-height: 200px;
margin: 0;
padding: 10px;
background:#6BD326;
}
#logo {
float:left;
display:inline-block;
}
nav li {
float:left;
padding: 0px 20px;
list-style-type: none;
}
your mistake was here
.branding {
float: left;
}
inside the <div class="branding"> you can add another div with a style:
display: flex;
justify-content: space-between
Please run your code through the W3C Validator.
You will see that you have an error in the HTML:
Error: Element h1 not allowed as child of element span in this context. (Suppressing further errors from this subtree.)
You can find out more about the distinction between block and inline elements at Can <span> tags have any type of tags inside them? which in turn references the formal spec at https://www.w3.org/TR/html401/struct/global.html#h-7.5.3
For your immediate problem you could make the span a div (which can have h1 elements as children) and make both it and the image inline-blocks.
try with flex and delete float:left
.branding {
display:flex;
align-items:center;
}

margin-left not merging with auto

margin-left: auto seems not working
1- Header with an image and a navbar
2- Two unorder list in a navbar
2- Try to get one of the list on the left side and the second in the right side
If I use margin-left: 800px, it's working however, it's not with auto.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
header{
display: flex;
justify-content: flex-start;
align-items: center;
padding: 10px 10px;
}
nav{
font-family: "Montserrat", sans-serif;
font-weight: 500;
font-size: 16px;
margin-left: 15px;
}
.nav__links_R, .nav__links_L, a {
text-decoration: none;
list-style: none;
float: left;
color:rgba(0, 0, 0, .50);
}
.nav__links_R {
margin-left: auto;
}
.nav__links_L li {
display: inline-block;
padding: 0px 20px;
}
.nav__links_R li {
display: inline-block;
padding: 0px 20px;
}
<header>
<img src="../photo/logo_dark.png" alt="logo">
<nav>
<ul class=nav__links_L>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<ul class=nav__links_R>
<li>Register</li>
<li>Login</li>
</ul>
</nav>
</header>
Everything is displaying well as expecting. Only ".nav__links_R" not going to the right side with "auto"
Add the following two style rules to nav:
width: 100%;
display: flex;
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
header{
display: flex;
justify-content: flex-start;
align-items: center;
padding: 10px 10px;
}
nav{
font-family: "Montserrat", sans-serif;
font-weight: 500;
font-size: 16px;
margin-left: 15px;
display: flex;
width: 100%;
}
.nav__links_R, .nav__links_L, a {
text-decoration: none;
list-style: none;
float: left;
color:rgba(0, 0, 0, .50);
}
.nav__links_R {
margin-left: auto;
}
.nav__links_L li {
display: inline-block;
padding: 0px 20px;
}
.nav__links_R li {
display: inline-block;
padding: 0px 20px;
}
<header>
<img src="http://placekitten.com/200/40" alt="logo">
<nav>
<ul class=nav__links_L>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
<ul class=nav__links_R>
<li>Register</li>
<li>login</li>
</ul>
</nav>
</header>

How to vertically center text in a List containing an image

I'm starting in CSS and I can't figure out if it is possible to vertically center a text in an unordered list when it contains an Image.
I want to place a logo in my navbar ul and center the text but it sticks to the bottom of the Ul.
Heres the code and what I want:
Fiddle
Image showing what I want
HTML:
<body>
<header>
<nav id="navbar">
<ul>
<li><b><img class="logo" src="https://i28.servimg.com/u/f28/09/04/03/75/a_2_li10.png" alt="HOME"></b></li>
<li><b>MORE</b></li>
<li><b>SERVICES</b></li>
<li><b>BLOG</b></li>
<li><b>SHOP</b></li>
</ul>
</nav>
</header>
</body>
body {
background-color: #294F6D;
margin: 0;
}
#navbar ul {
margin: 0;
list-style: none;
background-color: #303E73;
text-align: center;
}
#navbar li {
display: inline-block;
background-color: #A13647;
padding: 20px 50px
}
.logo {
width: 100px;
}
All you need is vertical-align: middle;.
You can just add it to your CSS here:
#navbar li {
display: inline-block;
background-color: #A13647;
padding: 20px 50px;
vertical-align: middle;
}
Here's a working example:
body {
background-color: #294F6D;
margin: 0;
}
#navbar ul {
margin: 0;
list-style: none;
background-color: #303E73;
text-align: center;
}
#navbar li {
display: inline-block;
background-color: #A13647;
padding: 20px 50px;
vertical-align: middle;
}
.logo {
width: 100px;
}
<header>
<nav id="navbar">
<ul>
<li><b><img class="logo" src="https://i28.servimg.com/u/f28/09/04/03/75/a_2_li10.png" alt="HOME"></b></li>
<li><b>MORE</b></li>
<li><b>SERVICES</b></li>
<li><b>BLOG</b></li>
<li><b>SHOP</b></li>
</ul>
</nav>
</header>
display: flex is your friend here:
body {
background-color: #294F6D;
margin: 0;
}
#navbar ul {
align-items: center;
display: flex;
margin: 0;
list-style: none;
background-color: #303E73;
text-align: center;
}
#navbar li {
background-color: #A13647;
padding: 20px 50px
}
a {
text-decoration: none;
color:white;
}
a:hover {
text-decoration: underline;
}
.logo {
width: 100px;
}
<header>
<nav id="navbar">
<ul>
<li><b><img class="logo" src="https://i28.servimg.com/u/f28/09/04/03/75/a_2_li10.png" alt="HOME"></b></li>
<li><b>MORE</b></li>
<li><b>SERVICES</b></li>
<li><b>BLOG</b></li>
<li><b>SHOP</b></li>
</ul>
</nav>
</header>
Check out Flexbox on caniuse, as it still has a few edge-case issues in Internet Explorer. This example should work fine though.
body {
background-color: #294F6D;
margin: 0;
}
#navbar ul {
margin: 0;
list-style: none;
background-color: #303E73;
/* text-align: center; */
display: flex;
justify-content: center;
align-items: center;
}
#navbar li {
display: inline-block;
background-color: #A13647;
padding: 20px 50px;
}
#navbar li a {
text-decoration: none;
color: #000000;
}
.logo {
width: 100px;
}
<body>
<header>
<nav id="navbar">
<ul>
<li><b><img class="logo" src="https://i28.servimg.com/u/f28/09/04/03/75/a_2_li10.png" alt="HOME"></b></li>
<li><b>MORE</b></li>
<li><b>SERVICES</b></li>
<li><b>BLOG</b></li>
<li><b>SHOP</b></li>
</ul>
</nav>
</header>
</body>