skip to Main Content

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&nbspme &nbsp 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>
[enter image description here](https://phpout.com/wp-content/uploads/2023/10/IKi7v.png)

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


  1. Make your text bold by using the transform attribute.

    I removed the font-weight, height and width property from .cn:Hover and added the transform attribute to rescale the element on hover.

    <!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;
          background-color: white;
          border: none;
          border-radius: 10px;
          transition: all 1s;
          display: inline;
          margin-right: 0;
          transform: scale(1.1);
        }
        
        #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&nbspme &nbsp 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>
    Login or Signup to reply.
  2. 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:

    .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;
    }
    

    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.

    <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>
    

    While this would technically work, it is usually good practice to keep
    navbar elements, that are meant to be just a link to another page, as
    an HTML link element "<a></a>". Try not to use the button element
    to much while they are primarily used along form elements. They are
    meant to be used for more complex user interactions with your site,
    like sending or receiving data or opening a popup window. If you are
    working with JavaScript you technically don’t need button elements at
    all because you can listen to events like clicking or dragging on any
    element.

    In your case I would format the html like this:

    <nav>
        <h2>LAN Network Provider</h2>
    
            <a class="cn" id="hm">Home<a/>
            <a class="cn">About</a>
            <a class="cn">Services</a>
            <a class="cn">Contact</a>
    
            <button id="lg" class="bt">Login</button>
            <button id="rg" class="bt">Register</button>
    </nav>
    

    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 the display 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:

    nav {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    
    • display: flex will position your elements one after another in a certain direction
    • align-items: center will center all of your elements on a horizontal line
    • justify-content: space-between will space the elements evenly between the start and end of the nav container

    If 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.

    enter image description here

    To leave some space for the text inside your hover background just add some padding (inner spacing) to your navbar links:

    .cn {
        color: white;
        border: none;
        background-color: rgb(255, 255, 255, 0);
        cursor: pointer;
        padding: 10px
    }
    

    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:

    • If you want to keep a different spacing between your login and register button than the automatically calculated one by flex, just add a div around them and flex will treat the div together with everything inside it as one element.
    • Try to always use class instead of id, so you can reuse your styles for other components
    • Use more descriptive names for your classes: This will help you to better keep track of what you are actually styling. E.g. use nav-link instead of cn

    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 🙌

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search