Nav and Div are appearing are too far from each other? - html

I just started with CSS and HTML. I am not able to sync my div and nav tag along the same line. The problem is that my div and nav are too far away from each other. What's the error here?
/* CSS RESET */
* {
margin: 0;
padding: 0;
}
#navbar {
align-items: center;
display: flex;
}
/* Logo And Image */
#logo {
margin: 10px 20px;
}
#logo img {
margin: 0px 0px;
}
/* Navigation Bar:-List Styling */
#navbar ul {
display: flex;
}
<nav id="navbar">
<div id="logo">
<img src="top.png" alt="Image Failed To Load !" width="10%" />
</div>
<ul>
<li class="item">Home</li>
<li class="item">About Us</li>
<li class="item">Recommendations</li>
<li class="item">Contact Me</li>
</ul>
</nav>
<section class="home">
<h1 class="h-primary">
Lorem ipsum
</h1>
</section>

Remove or change this margin:
#logo {
margin: 10px 20px;
}
The 10px adds space to the top and bottom.
The 20px adds space to the left and right.

I checked and tested your code, and from what you asked i understand you want div and ul to be same line, which are located inside nav. Then you should use display:flex; for element.
Don't forget to link your css to your HTML.
<link rel="stylesheet" href="test.css">
It seems perfectly fine in my testing, div and ul at the same line

Related

Padding of a div gets bigger when element inside it is big

I would like to place an image on a div which I use for a navigation bar, but when I resize the image to 50px or above, the padding on the div gets bigger as well. I don't like this since the image will either be too small to see or the navigation bar will be too big to look pleasing, any ideas on how to fix this?
.navbar{
background-color:green;
padding:20px;
}
.navbar #image1{
width:40px;
margin-left:950px;
padding:0px;
}
<div class='navbar'>
<a href='home.html'>Home</a>
<a href='1.html'>Profile</a>
<a href='2.html'>Transactions</a>
<a href='3.html'><p>Settings</p></a>
<img src='https://picsum.photos/200/300' id='image1'/>
</div>
You should first start by removing
margin-left:950px
as the margin will exclude your item from your navbar
then apply flex properties to your navbar
.navbar {display : flex}
so your navbar items become in the same line
I recommend checking out this flex-box guide as well flexbox properties
can you please share the image's link so we can help you?
also you most edit margin-left:950px; to margin-left: auto; if you want to center it
and this is an example navbar code (press run to see what is it)
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.navbar{
padding: 10px 20px;
background: black;
color: white;
display: flex;
align-items: center;
}
.navbar li{
list-style: none;
display: inline-block;
padding: 0 20px;
cursor: pointer;
}
.navbar li a{
color: white;
text-decoration: none;
}
.navbar li:hover,
.navbar li a:hover{
color: #666;
}
.navbar img{
width: 50px;
height: 50px; /*or :auto ; */
}
<ul class="navbar">
<li>home</li>
<li>project</li>
<li>contact</li>
<li><a>settings</a></li>
<img src="https://www.calliaweb.co.uk/wp-content/uploads/2015/10/600x600.jpg"/>
</ul>
<br><br><br>
<p>or</p>
<br><br><br>
<ul class="navbar">
<img src="https://www.calliaweb.co.uk/wp-content/uploads/2015/10/600x600.jpg"/>
<li>home</li>
<li>project</li>
<li>contact</li>
<li><a>settings</a></li>
</ul>
I think you need to learn the basics before start doing websites

Background image not appearing from the top CSS

There is a gap above the top of the image. What is causing it? I want the image to appear from the very top of the page. The image covers takes up half the width of the webpage.
The HTML (Here I have added a navbar and there is a div tag which consists an image.)
<nav class="navbar">
<ul>
<li><div class ="line-effect">Home</div></li>
<li><div class="line-effect">About</div></li>
<li><div class="line-effect">Portfolio</div></li>
</ul>
</nav>
<div class="bg-image">
<img src="newvec.png" height="800px" width="750px">
</div>
The CSS (This is the styling for the navbar. There is no style applied to the image. The "box:sizing:border-box" property has been applied to the whole document.)
.navbar{
padding:5px;
display: flex;
justify-content: flex-end;
align-items: center;
position:fixed;
padding:50px 10px;
width:100%;
height:8vh;
}
.navbar ul{
display: flex;
list-style: none;
padding: 20px 40px;
}
.navbar ul li a{
font-family: Nunito;
color:black;
text-decoration: none;
font-size: 20px;
display: block;
padding: 5px 25px;
}
did you checked your <body> tag?
If you didn't used reset.css there is standard margin in <body> tag.
Just try to add
body {
margin : 0;
}
on your css tag.

How to remove padding from images

* {
}
body {
margin: 0;
text-align: center;
}
/* =====Header===== */
header {
display: inline;
}
.logo {
background-color: #dedede;
display: inline;
float: left;
}
<header>
<div class="navContainer">
<a href=#>
<img src="https://i.imgur.com/1fsTc5I.png" alt="headLogo" class="logo">
</a>
</div>
<nav>
<li>Home</li>
<li>About</li>
<li>Projects</li>
<li>Contact</li>
</nav>
</header>
So I have an image and it seems to me it has a padding of 32px around it and I dont want it, I already tried padding: 0; padding: 0!important; I am getting frustrated please help me mates!
I added a background of gray so you can see the padding of the image
{Edit I added a background so you can see the padding}
If you look under the "Computed" tab when accessing dev-tools, you'll see this image has no padding at all. You just need to use photoshop to remove the extra white space around your text in the image, and you should be fine.

Menu and content overlaps image in header

I'm trying to add a site banner above the menu on my site using a simple img tag but whenever I do so the menu just overlaps the image. What I want to achieve is the menu to be pushed down by the image above it so it appears right under it.
HTML:
<header>
<img src="site_logo.jpg" alt="">
<img src="banner_small.png" alt="" id="banner">
<nav>
<ul>
<li class="selected">Main page
</li>
<li>Classes
</li>
<li>Game modes
</li>
<li>Contact
</li>
</ul>
</nav>
</header>
My first img is for the site logo and the second one is in the menu. I want to position the first img to be on top of the site and push the menu down
CSS:
header {
height: 50px;
background: #333333;
}
header nav {
display: inline-block;
vertical-align: middle;
}
header nav ul {
margin: 0;
padding: 0;
}
#banner,
header ul li {
display: inline-block;
vertical-align: middle;
}
header nav > ul > li {
box-sizing: border-box;
height: 50px;
padding: 12px;
position: relative;
}
Try to put the images inside div elements:
<div id='x'><img src="site_logo.jpg" alt=""></div>
<div id='y'><img src="banner_small.png" alt="" id="banner"></div>
Then if you want to increse ou decrease the space between elements use the Margin property:
x{margin-bottom:...px;}
y{margin-top:...px; margin-bottom:..px;}

Image and menu positioning

I'm trying to get my menu working with an image on the left side. For some reason whenever I try to align the image in same line with the menu it's not working out. This is what the html looks like, I can't get the CSS working at all. It's either throwing the menu under the image or the background disappears and the content overlaps the menu but the image is in the right place. The image is 50px in height as well so it shouldn't be a problem.
HTML:
<div>
<img src="logo_small2.png" alt="" id="banner">
<nav>
<ul>
<li class="selected">Main page</li>
<li>Classes</li>
<li>Game modes</li>
<li>Contact</li>
</ul>
</nav>
</div>
CSS:
header div {
height: 50px;
background: #333333;
}
#banner,
header ul li {
display: inline-block;
}
header nav > ul > li{
box-sizing: border-box;
height: 50px;
padding: 12px;
position: relative;
}
What happens now is that the banner is in place over the background of the div and the menu is under the banner and the background in a new line. If I replace the img with a simple h1 it works as a charm >.> I'm clueless, please help
Your CSS does not match the HTMl, there is no header shown.
Assuming that the div is, in fact the header, the nav needs to be inline-block too I suspect. It's currently block level and so 100% wide.
Then you can just align the elements.
header {
height: 50px;
background: tomato; /* for demo only */
}
header nav {
display: inline-block;
vertical-align: middle;
}
header nav ul {
margin: 0;
padding: 0;
}
#banner,
header ul li {
display: inline-block;
vertical-align: middle;
}
header nav > ul > li {
box-sizing: border-box;
height: 50px;
padding: 12px;
position: relative;
}
<header>
<img src="http://www.fillmurray.com/200/50" alt="" id="banner">
<nav>
<ul>
<li class="selected">Main page
</li>
<li>Classes
</li>
<li>Game modes
</li>
<li>Contact
</li>
</ul>
</nav>
</header>
Possible reason is the width of the image not allowing the inline-block comand:
try this:
img{ float:left; width:50%; vertical-align:middle;}
ul{float:right;width:40%;vertical-align:middle;}