Creating a line break in the HTML form

While creating a header interface, I encountered an issue where the form with text input and submit button was automatically moving down the page, unlike my anchor tags which remained on the same line.

I attempted using CSS properties like display: flex, align-items: center, and justify-content: center, but they didn't resolve the problem as the form continued to stay below the buttons.

body {
  background-color: red
}

input[type="text"],
textarea,
select {
  border: none;
  border-radius: 2px;
  border-bottom-right-radius: 0px;
  border-top-right-radius: 0px;
  padding: 7px;
  font-size: 19px;
  width: 200px;
}

#bar:focus {
  outline: none;
}

#button {
  border: none;
  background-color: #999;
  border-radius: 2px;
  border-bottom-left-radius: 0px;
  border-top-left-radius: 0px;
  color: #666;
  font-size: 19px;
  padding: 7px;
}

#button:hover {
  background-color: #888;
}

a {
  font-size: 19px;
  font-family: Arial, Helvetica, sans-serif;
  padding: 10px;
  background-color: #999;
  border-radius: 3px;
  text-decoration: none;
  color: #666;
}

a:hover {
  background-color: #888;
}
<nav>
  <a id="home" href="#">Home</a>
  <a href="contribute.html" id="contribute">Contribute</a>
  <a href="credits.html" id="credits">Credits</a>
  <a href="searchpage.html" id="random">Random Article</a>
  <a href="FAQ.html" id="faq">FAQ</a>
  <form method="get" action="search.html">
    <input type="text" id="bar" placeholder="Search..." required="" oninvalid="this.setCustomValidity('Do not leave search bar blank.')" oninput="setCustomValidity('')" autocomplete="off">
    <input type="submit" value="Search" id="button">
  </form>
</nav>

Answer №1

Adjust the form's display to inline to ensure it appears horizontally. To prevent wrapping on smaller screens, set the nav's whitespace property to nowrap.

body {
  background-color: red
}

input[type="text"],
textarea,
select {
  border: none;
  border-radius: 2px;
  border-bottom-right-radius: 0px;
  border-top-right-radius: 0px;
  padding: 7px;
  font-size: 19px;
  width: 200px;
}

#bar:focus {
  outline: none;
}

#button {
  border: none;
  background-color: #999;
  border-radius: 2px;
  border-bottom-left-radius: 0px;
  border-top-left-radius: 0px;
  color: #666;
  font-size: 19px;
  padding: 7px;
}

#button:hover {
  background-color: #888;
}

a {
  font-size: 19px;
  font-family: Arial, Helvetica, sans-serif;
  padding: 10px;
  background-color: #999;
  border-radius: 3px;
  text-decoration: none;
  color: #666;
}

a:hover {
  background-color: #888;
}

form {
  display: inline; /* Setting form display to inline */
}

nav {
  white-space: nowrap; /* Ensuring no wrap in navigation bar */
}
<nav>
  <a id="home" href="#">Home</a>
  <a href="contribute.html" id="contribute">Contribute</a>
  <a href="credits.html" id="credits">Credits</a>
  <a href="searchpage.html" id="random">Random Article</a>
  <a href="FAQ.html" id="faq">FAQ</a>
  <form method="get" action="search.html">
    <input type="text" id="bar" placeholder="Search..." required="" oninvalid="this.setCustomValidity('Do not leave search bar blank.')" oninput="setCustomValidity('')" autocomplete="off">
    <input type="submit" value="Search" id="button">
  </form>
</nav>

Answer №2

The adjustments I've implemented:

nav{
    display: flex;
    flex-wrap: wrap;
}

The snippet above accomplishes the task. However, I have also introduced additional tweaks to enhance horizontal spacing.

a {
    /* font-size: 19px; */
    font-size: 14px;
}

input[type="text"], textarea, select {
    /* width: 200px; */
    width: 100px;
}

body {
background-color: red
}

nav{
    display: flex;
    flex-wrap: wrap;
}

input[type="text"],
textarea,
select {
border: none;
border-radius: 2px;
border-bottom-right-radius: 0px;
border-top-right-radius: 0px;
padding: 7px;
font-size: 19px;
/* width: 200px; */
width: 100px;
}

#bar:focus {
outline: none;
}

#button {
border: none;
background-color: #999;
border-radius: 2px;
border-bottom-left-radius: 0px;
border-top-left-radius: 0px;
color: #666;
font-size: 19px;
padding: 7px;
}

#button:hover {
background-color: #888;
}

a {
/* font-size: 19px; */
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
padding: 10px;
background-color: #999;
border-radius: 3px;
text-decoration: none;
color: #666;
}

a:hover {
background-color: #888;
}
<nav>
    <a id="home" href="#">Home</a>
    <a href="contribute.html" id="contribute">Contribute</a>
    <a href="credits.html" id="credits">Credits</a>
    <a href="searchpage.html" id="random">Random Article</a>
    <a href="FAQ.html" id="faq">FAQ</a>
    <form method="get" action="search.html">
        <input type="text" id="bar" placeholder="Search..." required="" oninvalid="this.setCustomValidity('Do not leave search bar blank.')" oninput="setCustomValidity('')" autocomplete="off">
        <input type="submit" value="Search" id="button">
    </form>
</nav>

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

Achieving an element placement in a navigation bar with flexbox and grid layout | HTML and CSS exclusive techniques

Need help achieving the desired result using only flexbox and grid layout. The goal is to place a search bar vertically centered and to the right of the navbar, while keeping the existing items on the left intact. Can you assist? /* Reset */ * { mar ...

Is there a chrome extension that can scan and read text within specific HTML tags?

I am currently working on developing a Chrome extension that can extract text from a specific tag on a webpage, but I am facing difficulties in retrieving the value. My goal is to capture the text within the tag without including any HTML tags. Output: An ...

Adjusting the letter spacing of individual characters using HTML and CSS

Is there a way to set letter-spacing for each character with different sizes as the user types in a text box? I attempted to use letter-spacing in CSS, but it changed all characters to the same size. Is there a possible solution for this? Here is the code ...

The Jsoup after function fails to meet expectations

I am facing an issue with a method I have, where the property description is of type "Element" and I am attempting to add another element using the Jsoup.after method. Unfortunately, this results in an error message: Exception in thread "main" java.lang. ...

Is there a way to ensure that the images in the slider all remain consistent in size?

I recently created a slider using bootstrap and encountered an issue with image display on mobile screens compared to laptop screens. On a Laptop screen: On a Mobile screen: The images on the mobile screen are not fitting properly within the slider, and ...

Meteor: dynamic approach to assigning array values in Spacebars

It's hard to put this into words... I have a collection with an array, and as I iterate through it, I've been setting colors.[0].imageLink without changing the [0]. Now, I want this to be dynamic based on the value of a function (in this case, th ...

Twitter-Bootstrap: implementing text underlines for thumbnail captions

While experimenting with Bootstrap, I aimed to create a layout similar to ; a grid featuring bordered panels containing text and images. After some research, I concluded that Bootstrap's Thumbnail component would be the most suitable choice for my pro ...

Customizing Background Color in React Bootstrap Table Based on Value

Having a little issue that's causing me some trouble. I have a React Bootstrap Table displaying data from an API, and I'm looking to enhance it by changing the row color to green if a specific value is greater than zero. Here is an example: const ...

Unlinked from the main content, the Angular2 Material2 sidenav stands on its

I am in the process of implementing the material2 sidenav component. My goal is to have a standalone, full-height sidebar that smoothly slides in and out without any interference with the rest of the app layout caused by the sidenav-layout. The desired ou ...

Struggling with dynamically updating fields based on user input in real time

I have a goal to update a field based on the inputs of two other fields. Currently, all the fields are manual input. Below is the code I'm using to try and make the "passThru" field update in real-time as data is entered into the other fields. The ma ...

What is the best method for eliminating the empty space below the footer?

I noticed a gap at the bottom of my website Header and CSS# There seems to be some white space at the bottom of my website's header even though I'm using bootstrap The white space issue in the header of my website persists despite using boot ...

What are the proper steps to ensure images are displayed correctly on mobile devices?

https://i.sstatic.net/M68Ob.jpg https://i.sstatic.net/fPKXr.jpg Our background image is 1920x1080p and looks great on desktop browsers, but it's not displaying properly on mobile devices in either portrait or landscape mode. Is there a way to resolv ...

Generate an image on Canvas using a URL link

I have a unique URL that generates canvas images with specific parameters. It functions properly when loaded in a browser. The final line of code is: window.location = canvas.toDataURL("image/png"); However, when attempting to use this URL from another ...

Is there a way to prevent wrapping in a column, causing the last column to partially extend outside the container when the first column is hovered over?

Is there a way to prevent wrapping of a column so that when I hover over the first column, the last column displays half out of the container? I attempted to achieve this by setting the 25% to flex: 0 0 50%; in my column when hovering on the box-hover cla ...

JavaScript - Unable to Modify Select Value with Event Listener

It's interesting how I can change the selected option in a dropdown list using JavaScript without any issues. However, when I try to do the same thing within an event listener, the option does not seem to change. Here's the HTML code: <select ...

Error message: "Azure deployment encounters Vue CSS dependency missing issue"

I recently built a Vue app following this tutorial but without CRUD functionality: . The app was created using Vue CLI 4.3.1 with vue create. During local development, it runs smoothly with npm run serve and builds without any errors. Now, I'm atte ...

Troubleshooting Problems with Displaying and Concealing Images Using JavaScript

Currently, I am attempting to create a show/hide button that toggles all the images on the page on and off using getElementsByClassName. Initially, my code looked like this: if (document.getElementsByClassName('showHide').style.visibility =&apos ...

What is the best way to dynamically insert columns into HTML code?

This is an example of my HTML code: <div class="row text-center"> <div class="col h4">We Collaborate With:</div> <div class="col">company1</div> <div class="col">company2</div> ...

Python code to convert HTML into JSON format

"I'm currently searching for an efficient method to convert HTML to JSON using Python, but so far I've only come across complex ways that require manual building which seems impractical for large HTML files. Can anyone provide assistance? I' ...

Please enter a number using the "+" sign and a decimal point

In my input field, I have specified: <input type="number" step="0.01"> I expect all of these input values to produce the following outputs: 2.00 => 2.00 2,00 => 2.00 +2,00 => 2.00 However, when entering the value "+2.00", it fail ...