Hello first of i would like to say i am very new to html and css so if i make rookie mistake please help and explain those to me
The problem is when i hover over the Home,About,Service buttons on top the move the rest of the elements on the page
here is my code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
@font-face {
font-family: title;
src: url(Modern Age.ttf);
}
@font-face {
font-family: body;
src: url(modern M.ttf);
}
body {
background: black; /*this was added to better show the problem!*/
background-image: url(ezgif.com-webp-to-jpg.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
nav>h2 {
color: white;
font-family: Title;
font-weight: bold;
font-size: 30px;
margin-left: 20px;
display: inline-block;
}
.cn {
color: white;
border: none;
background-color: rgb(255, 255, 255, 0);
cursor: pointer;
display: inline-flex;
margin-left: 55px;
}
.cn:hover {
color: black;
font-weight: bold;
background-color: white;
height: 35px;
width: 90px;
border: none;
border-radius: 10px;
transition: all 1s;
display: inline;
margin-right: 0;
}
#hm {
border-left: 55px;
}
#lg {
background-color: white;
color: black;
font-family: title;
height: 35px;
width: 75px;
border: none;
border-radius: 5px;
font-weight: 500;
cursor: pointer;
margin-left: 125px;
}
#rg {
background-color: white;
color: black;
font-family: title;
height: 35px;
width: 75px;
border: none;
border-radius: 5px;
font-weight: 500;
cursor: pointer;
margin: 20px;
}
#lg:hover {
color: white;
background-color: rgb(233, 143, 69);
transition: 0.5s;
}
#rg:hover {
color: white;
background-color: rgb(233, 143, 69);
transition: 0.5s;
}
#head {
color: white;
font-family: title;
margin-left: 45px;
font-weight: bolder;
font-size: 65px;
}
#body {
color: white;
text-align: justify;
font-family: body;
font-weight: 100;
font-size: 20px;
margin-left: 45px;
}
#fn {
color: black;
background-color: white;
height: 55px;
width: 200px;
border: none;
border-radius: 25px;
font-family: title;
font-weight: bold;
font-size: 20px;
margin-left: 45px;
cursor: pointer;
}
#fn:hover {
color: white;
background-color: rgb(233, 143, 69);
transition: 0.5s;
}
</style>
</head>
<body>
<div>
<nav>
<h2>LAN Network Provider</h2>
<button class="cn" id="hm">Home</button>
<button class="cn">About</button>
<button class="cn">Services</button>
<button class="cn">Contact</button>
<button id="lg" class="bt">Login</button>
<button id="rg" class="bt">Register</button>
</nav>
</div>
<div class="text">
<h1 id="head">Hello,<br>It's me   Net- Manger</h1>
<pre id="body">
I help create and manage fast and effective
LAN networks with easy and intuitive managements
systems and training for staff
</pre>
</div>
<button id="fn">GET A QUOTE</button>
</body>
</html>
I have tried making the position absolutes for the home and other button but that just makes them overlap each other
I have tired making the buttons at the end absolutes or fixed but it dosnt fix and messes with the alignment
2
Answers
Make your text bold by using the
transform
attribute.I removed the
font-weight
,height
andwidth
property from.cn:Hover
and added thetransform
attribute to rescale the element on hover.I know starting with CSS can be quite challenging especially when you try to position elements the way you want.
The problem with your navbar jumping around lies primarily in the css part where you change the hover state of the "cn" class:
You should try to change the style of an element as little as possible just for the hover state, especially when it comes to positioning options like display, margin, padding, width, height and so on.
But first, start by optimising your HTML. Your navbar currently looks like this.
In your case I would format the html like this:
This should already remove most of the content jumping around, but there is still some room for improvements. So let’s get back to your css:
Because you probably don’t want to change the positioning or size of the navbar links when hovering over them you should put allot of the css option from
.cn:hover
into.cn
as all styles in.cn
will also be applied to the hover state if not overwritten in.cn:hover
.Allot of problems are often caused by the
display
option. Changing thedisplay
option on hover will most definitely cause you some trouble.If you want to get your positioning perfect you should learn about
css flex
.To improve the positioning of your navbar elements we will remove the
display
option from all of your navbar elements. Instead we will control the positioning from the parent element (thats the ).To do that, we will add the following code to your css:
display: flex
will position your elements one after another in a certain directionalign-items: center
will center all of your elements on a horizontal linejustify-content: space-between
will space the elements evenly between the start and end of the nav containerIf you want try out the different options of css flex you can play with it in your browsers developer tools where you can change the options directly in the browser.
To leave some space for the text inside your hover background just add some
padding
(inner spacing) to your navbar links:using padding is usually better than trying to create whitespace by using width and height as it will automatically push your content more into the center.
Some last things I want to mention:
nav-link
instead ofcn
I hope I could help you on your journey learning html and css. It might take sometime but at the end you will be able to create amazing things.
Have fun and a great day 🙌