Adding margin to an HTML element causes the entire page to shift downward

Despite successfully applying margin to other elements, I encountered an issue with #searchbarcontainer where it causes the entire page to shift downward. My goal is to center it slightly below the middle of the page, but I'm struggling to find a solution. As a novice in HTML and CSS, I am attempting to create something akin to a Google clone. Below is the code snippet:

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

#nav {
  display: flex;
  justify-content: flex-end;
  margin-top: -40px;
  margin-right: -5px;
  padding: 20px;
}

.child {
  padding: 15px;
}

.link {
  color: rgb(95, 91, 91);
  text-decoration: none;
}

.link:hover {
  color: black;
}

#logo {
  margin-top: 110px;
  text-align: center;
}

#searchbarcontainer {
  text-align: center;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="75171a1a01060107140535405b445b46">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div id="form">
  <form action="https://google.com/search">
    <div id="searchbarcontainer">
      <input id="searchbar" type="text" name="q">
    </div>
    <div id="submitcontainer">
      <input id="submit" type="submit" value="Google Search">
    </div>
  </form>
</div>

<div id="nav">
  <div class="child">
    <button type="button" class="btn btn-light"><a class="link" href="images.html">Image Search</a></button>
  </div>
  <div class="child">
    <button type="button" class="btn btn-light"><a class="link" href="advanced.html">Advanced Search</a></button>
  </div>
</div>
<div id="logo">
  <img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
</div>

Answer №1

The current layout of your page does not allow for the search bar container to be repositioned without affecting other elements on the page. Although you mentioned wanting it lower, there seems to be limited space available for this adjustment without shifting everything downwards.

If you intend to move the search bar lower, consider restructuring it so that the search input and the "Google Search" button are within the same container and centered accordingly. https://i.stack.imgur.com/SkpCK.png

Answer №2

When creating HTML content, it is important to structure your code in a way that reflects the layout of your website vertically.

A common mistake is writing from top to bottom instead of bottom to top, leading to confusion and inefficient coding.

Consider optimizing your code by removing unnecessary elements and organizing it for better readability and maintainability.

Below is an example of streamlined CSS and HTML code that showcases a simplified approach to creating a responsive design:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "sans-serif";
}

input {
  outline: 0;
  border: 0.3vh solid #bbb;
  font-size: 1.6vh;
}

nav {
  width: 100%;
  height: 6.7vh;
  border-bottom: 0.2vh solid #bbb;
  display: flex;
  justify-content: flex-end;
  padding: 0 3vh;
}

nav>div {
  height: 6.7vh;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

nav>div>a {
  color: black;
  text-decoration: none;
  font-size: 1.6vh;
  background: yellow;
}

nav>div>a:first-child {
  margin-right: 1vh;
}

section {
  height: 93.3vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

section>img {
  width: 45vw;
  height: 15vh;
  object-fit: contain;
}

section>form {
  width: 45vw;
  display: flex;
  justify-content: space-around;
  margin-top: 3vh;
}

section>form>input[type="text"] {
  padding: 1vh;
  border-radius: 1vh;
  width: 65%;
}

section>form>input[type="submit"] {
  width: 25%;
  border-radius: 1vh;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Search</title>
  <link rel="stylesheet" href="styles.css" />
</head>

<body>
  <main>
    <nav>
      <div><a href="images.html">Image Search</a><a href="advaced.html">Advanced Search</a></div>
    </nav>
    <section>
      <img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" />
      <form action="https://google.com/search">
        <input id="searchbar" type="text" name="q" />
        <input id="submit" type="submit" value="Search" />
      </form>
    </section>
  </main>
</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

On smaller screens in portrait mode, CSS automatically incorporates margin adjustments

Here is the code I am working with: <html> <head> <style> body { margin: auto; width: 720px; } </style> </head> <body> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ip ...

Arrangement: Div beside vertically-aligned hyperlinks

Example code: p { color: red; } span { font-weight: bold; } <p>This is a paragraph.</p> <p>Another paragraph here.</p> <span>Bold text</span> This is the desired outcome: https://example.com/image.png It&ap ...

Words fail to display

.sport { content: url("../img/sport.png"); background-color: rgba(0,0,0,1.0); top: 61px; height: 310px; width: 300px; position: absolute; margin: 0; left: 0; border-radius: 4px; overflow: hidden; } .sportsandactivis { background-colo ...

Place a fresh banner on top of the Ionicon icon

I recently started using Ionicons from and I am not too familiar with css. My question is, can a banner that says 'new' be overlaid on the top right corner of the Icon? Is there a simple or standard method to achieve this? (whether it's an ...

Using the jQuery method `fadeToggle` to handle the functionality of multiple buttons toggling hidden text

I am struggling to make fadeToggle work properly. I want to reveal text when clicking on another div. Since I plan to use this for multiple elements, I am looking for a smart and efficient way to handle them all. Here is my current attempt which does not ...

AngularJS: dynamic autocomplete field with scroll functionality

This is my HTML code: <div class="form-group"> <label class='control-label col-md-4' for='id_paymentCurrency'>{{'PAYMENT_CURRENCY' | translate}}</label> <div class='col-md-4'> ...

Encountering a problem with the divLogoBlock element in the code snippet below

I am experiencing an issue with the code below. The divLogoBlock does not appear when I copy and paste the entire page into Outlook as a signature. I believe there may be a problem with the CSS. Can someone please assist me? <html lang="en"><he ...

Tips for creating a responsive React Monaco Editor:

I'm currently working with react-monaco-editor and having trouble making it responsive. The component has a fixed height and width, so when I resize the screen, the width stays the same. <MonacoEditor width="1200" height=& ...

Oxygen-style with a sleek, shadow-free frame

Currently, I'm utilizing the qtoxygen style and I am curious if anyone knows how to remove the shadow frame on QPlainTextEdit. Even after attempting to customize the CSS with { border: none }, the shadow continues to be displayed. ...

How can you use HTML, CSS, and JavaScript to group objects with one color and then change the color of one specific object?

I have a set of 6 labels that act as buttons. When one is clicked, it changes from white to blue. If another label is clicked, the previously blue label turns white and the newly clicked one turns blue. Currently, the code below sets all labels to white b ...

Assistance with HTML and CSS to position an image in the corner

I need assistance with positioning a corner image on top of another image. I want the span, which has a small background image, to be in the top left corner of the main image. Below is my code: <div id="wrapkon"> <span style="width: 4px; height: ...

Tips on how to customize an HTML form submission appearance

Take a look at my file upload form below: <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="filetoupload" id="filetoupload" onchange="this.form.submit();"> </form> I am trying to figure out ...

Issue with the top margin of the body

Recently, I've encountered a peculiar problem with a standard website layout that I use. The issue seems to be centered around the space between the top and the first div: navbar. No matter what I try, I just can't seem to remove it, as the reas ...

Is there a way to customize the background color of Material-UI MenuItem when hovering over it through AutoComplete props?

AutoComplete utilizes the Menu component to display MenuItems as demonstrated in the documentation on those respective pages. I am looking to modify the backgroundColor of a MenuItem when it is hovered over. While I have been successful in changing the co ...

The dropdown item in Tailwindcss is unexpectedly flying off the edge of the screen rather than appearing directly under the dropdown button

Currently, I am developing an application using Rails, Vue, and TailwindCss 1.0+ I am facing an issue while creating a dropdown menu for my products. When I click on the dropdown button, the items in the dropdown fly off to the edge of the screen instead ...

How come the picture extends beyond the borders of its parent container when I expand the width?

Despite my efforts, I haven't been able to figure this out successfully. My goal is to align the text elements next to the image just like in the example picture below. Initially, I achieved this using absolute positioning. absolute positioning Howe ...

<Select> Placeholder customization

For my react project, I am utilizing material-Ui and I am looking to have a placeholder that stands out by having grey text instead of black when compared to the selected item. <Select name="answer" value={values.answer} onChange={handleChange} onBlur= ...

Is there a way to arrange the components of my form so that the questions are positioned on the left side and the choices are displayed on the right

As I am creating a form, my goal is to align the information within it in a way that places the questions on the left side and the options on the right. I have experimented with utilizing IDs, centering them, and adjusting their positioning from left to r ...

Repairing the Performance of the 'Save' Feature

Can the 'Save' button in my code save team assignments for players selected using drag and drop? I'm considering using localStorage, but unsure about implementation. Note: To run the code properly, copy it as an HTML file on your computer. ...

having difficulties in separating the mat-table header

It may seem like a basic question, but I'm having trouble figuring it out. I'm not sure if this requires a CSS change or something else. I'm looking to add a divider between my table header similar to the one highlighted in the screenshot be ...