What is the best way to ensure that the input search bar, microphone icon, and two small boxes adapt to different screen

Upon submitting my exercise for The Odin Project, I was under the impression that everything looked perfectly aligned and centered on my MacBook Pro 15-inch screen. However, when I viewed the completed webpage on a larger computer screen at work, I noticed that the input search box was no longer centered. Additionally, the microphone image within the search box and the two grey buttons below it seemed to roam freely when the window was resized.

Instead of starting over from scratch, I attempted to group the form box, microphone image, and buttons into their own Divs in an effort to center the box, but unfortunately, this did not solve the issue. I also experimented with flexbox, margin: 0 auto, and adjusting the widths, but none of these tactics worked.

You can view the project here:

The code for the project can be found here: https://repl.it/@jl88s/RoyalblueFarTrust

<section class="main-container">

        <h1 class="google-logo-header">Google</h1>
        <img src="google-logo.svg" alt="google-logo" id="google-logo">

        <form action="/action_page.php">
            <input class="form-box" type="text" name="search">
            <a href="#"><img src="google-mic.png" id="google-mic" alt="microphone"></a>
        </form>

        <button class="btn-one buttons">Google Search</button>
        <button class="btn-two buttons">I'm Feeling Lucky</button>

</section>

My hope is that resizing the window will result in the search box, mic, and buttons being perfectly centered and responsive to the screen size.

Answer №1

Check out this helpful demo:


<form action="/action_page.php">
            <input class="form-box" type="text" name="search">
            <a href="#"><img src="https://police.un.org/sites/default/files/radio-microphone.png" id="google-mic" alt="microphone"></a>
        </form>

    form{
    position:relative;
}
input{
    width:100%;
    height: 50px;
    border-radius: 10px;
    margin:0;
}
a{
    margin:0;
    padding: 10px 5px;
    position:relative;
    right:0;
    top:0;
    position:absolute;
}
a img{
    width: 30px;
    margin:2px 10px;
}

Check out the demo here for more details.

Answer №2

Here are some suggestions to improve your styles, you may want to consider replacing your current style.css file with the following:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
    height: 100vh;
}

li {
    list-style-type: none;
}

li a {
    text-decoration: none;
    color: #000;
    opacity: 0.75;
}

li a:hover {
    text-decoration: underline;
}

.header-container {
    width: 100%;
    height: 60px;
}

.nav-list ul {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.nav-list ul li {
    display: flex;
    justify-content: space-between;
}

.left-half {
    margin-right: auto;
    padding-left: 15px;
}

.left-half li a{
    margin: 15px 8.5px 0 10px;
}

.right-half {
    margin-left: auto;
}

.dot-box {
    padding: 0 3px;
}

.dots {
    height: 16px;
    width: 16px;
    opacity: .7;
    margin-top: 5px;
}

.dots:hover {
    opacity: 1;
}

.sign-in-btn {
    padding-right: 22px;
}

#sign-in-button {
     color: #fff;
     border: 1px solid #4285f4;
     font-weight: bold;
     background:#4387fd;
     line-height: 28px;
     padding: 0 12px;
     border-radius: 2px;
     opacity: 1;
     text-decoration: none;
}

/* #02. Title / Search Bar */

.google-logo-header {
    display: inline-block;
    text-indent: -9999999px;
}

#google-logo {
    display:inline-block;
    width: 370px;
    z-index: 0;
    opacity: .91;
}

.main-container {
    text-align: center;
    position: relative;
}

.form-box {
    box-shadow: 0 2px 2px 0 rgba(0,0,0,0.16), 
    0 0 0 1px rgba(0,0,0,0.08);
    width: 584px;
    height: 44px;
    border-radius: 2px;
    border: none;
    outline: none;
    position: relative;

    font-size: medium;
    padding-left: 16px;
}

.form-box:hover {
    box-shadow: 0 3px 8px 0 rgba(0,0,0,0.2), 
    0 0 0 1px rgba(0,0,0,0.08);
}

#google-mic {
    position: relative;
    right: 38px;
    height: 20px;
}

.buttons {
    background-color: #f2f2f2;
    border: 1px solid #f2f2f2;
    border-radius: 2px;
    color: #757575;
    cursor: pointer;
    font-size: 13px;
    font-weight: bold;
    margin: 11px 4px;
    min-width: 54px;
    padding: 9px 16px;
    text-align: center;
}

.buttons:hover {
    border: 1px solid #c6c6c6;
    box-shadow: 0 1px 1px rgba(0,0,0,0.1);
    color: #000;
}

.btn-one {
    position: relative;

}
input{

}
.btn-two {
    position: relative;

}

/* #03. Footer */

.footer-container {
    background-color: #f2f2f2;
    border-top: 1px solid #e4e4e4;
    width: 100%;
    bottom: 0;
    position: fixed;
    line-height: 40px;
}

.footer-list {
    display: flex;
    align-items: center;
}

.footer-list li a {
    padding-left: 27px;
}

.settings-pad {
    padding-right: 27px;
}

.footer-right {
    margin-left: auto;
}

Answer №3

Your primary challenge in creating a responsive website is the heavy use of position: absolute; and fixed left and right positions. This approach can hinder responsiveness. The ideal solution involves utilizing Flexbox or CSS Grid along with Media Queries, all achievable with standard CSS.

If you'd like to delve deeper, here are some resources to explore:

Flexbox:

CSS Grid:

Media Queries:

Modifications to Your Code

The changes I made to your code involve transitioning it to Flexbox for better responsiveness while maintaining the HTML structure intact. Here's an overview:

<section class="main-container">
    <img src="images/google-logo.svg" alt="google-logo" id="google-logo">
    <form class="search-bar" action="/action_page.php">
        <input class="form-box" type="text" name="search">
        <a href="#"><img src="images/google-mic.png" id="google-mic" alt="microphone"></a>
    </form>
    <div class="btn-row">
        <button class="btn-one buttons">Google Search</button>
        <button class="btn-two buttons">I'm Feeling Lucky</button>
    </div>
</section>

To enhance responsiveness, I changed the styles for .main-container to a flexbox column aligning items vertically centered:

.main-container {
  display: flex;
  flex-direction: column;
  align-items: center
}

Removed position: absolute and fixed positioning rules, ensuring elements are more adaptable and responsive. Added classes like .search-bar as flexbox row for vertical alignment:

.search-bar {
  display: flex;
  flex-direction: row;
  align-items: center;
}

#google-mic {
  height: 25px;
  margin-left: -30px;
}

Introduced .btn-row div for button alignment in a flexbox row fashion for improved responsiveness:

.btn-row {
  display: flex;
  flex-direction: row;
}

Take a look at this JSFiddle for a functional demonstration. Keep an eye out for fixed pixel widths on elements that could cause issues on mobile screens. We recommend utilizing percentage widths and refining SVG placement to minimize whitespace around the Google logo. Adjustments may be needed based on screen sizes.

We suggest exploring the provided resources and adopting responsive design practices for optimal site performance.

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

Getting the most out of Twitter Bootstrap: How to effectively implement the `.btn` class in a navbar?

Currently, I have set up a navigation bar for the usual navigation options, and now I want to include buttons for both Sign in and Sign up. To implement this, I am using an a tag with the classes btn btn-large btn-success. However, it seems that the navba ...

How to toggle between two background colors in a webpage with the click of a button using JavaScript

I need help with a unique website feature. I want to implement a button that cycles between two different background colors (white and black) as well as changes the font color from black to white, and vice versa. My goal is to create a negative version of ...

Tips for utilizing comment tags efficiently

I have encountered an issue with IE7 where my CSS is not functioning properly. In an attempt to target IE7 and lower versions specifically, I inserted IE comment tags into the HTML code. However, despite my efforts, it does not seem to be effective. Initia ...

Resolved navigation bar problems

As a beginner in web development, I am working hard to launch my first website. Today, I have a question for the stack overflow community regarding a fixed navbar issue. The navbar I have created is functioning properly on Chrome, but when it comes to Fire ...

Tips for convincing the React/MUI Theme Engine to apply namespaces to all MUI CSS rules?

Encountering an issue with React Apps imported as microfrontends. Our Landingpage (built in React/MUI) imports multiple Apps (created in React/MUI v4 or v5, designed to run independently with their own themes). The problem arises when each App creates its ...

Issue with Angular controller failing to load when link is clicked

I am currently developing a webpage that incorporates Angular and responds to ajax requests by populating data into a table. When I manually enter the address, everything works as expected. However, if I navigate to the page using a link ( <a href="/pro ...

Sending a parameter to the window.onload callback function

Is there a way to pass parameters from ModelAndView to a window.onload function in JavaScript within an HTML file? Here is an example of how it can be done: @RequestMapping(value = "/admin/printtext") public ModelAndView printtext() { ModelAndView mo ...

Toggle the panel upwards using jQuery's slideToggle function

Looking to create an expandable footer triggered by a click on the '+' sign... Initially, the footer will display basic copyright information and social media links. Upon clicking the sign, I want a sitemap and additional content to slide above t ...

Employing JavaScript for fading divs in and out sequentially

Hey there! I'm currently struggling with implementing transitions for my tool tips. Any assistance would be greatly appreciated! I am looking to have my "fader" divs fade in and out on click of a button, with each transition lasting 5 seconds. It&apo ...

Incorrectly colored buttons upon clicking

I am experiencing an issue with my website where the color of the container div is not changing to the correct color when a button representing a color is clicked. Instead, it seems to be displaying the next color in the array of color codes that I have se ...

relocate HTML components

I've been trying to reposition HTML elements within the body of a webpage, but I'm struggling with getting a form tag to appear exactly in the center of the browser - and only that form tag. Can anyone offer guidance or suggest relevant topics fo ...

Design elements for React and Angular JS

Is there a way to design UI components in a style that is compatible with both Angular JS and React? These two technologies will be utilized simultaneously within the same application. ...

Utilizing JQuery's .Toggle function in conjunction with a stylish CSS transform button

I am trying to create a toggle menu that shows and hides itself without requiring the button to be clicked twice. I have added e.preventDefault(); which solves the issue, but now the button does not transform as intended. $(document).ready(function() { ...

apostrophe cutting off a portion of the input field's value

I am facing an issue with a reloaded input box on a web page that is updated through an ajax call. Whenever the input contains a single quote, the rest of the value gets cut off. How can I resolve this problem? The value assigned to myVal dynamically from ...

Can a website be designed to render consistently across all browsers without relying on a CSS reset?

Is it possible to create a website that renders well across all browsers without using CSS reset? Should CSS reset be used for websites of all sizes - small, one-page, large? Is it better to write all the CSS without a reset and only address necessary rend ...

Populating a read-only field with information from a database upon selecting an option

My goal is to select a name from an option field and then display the user's ID in a non-editable field. Essentially, when I choose a username, I want to automatically show their user ID. Below is the PHP and HTML code I currently have: <form id=" ...

Adding Exciting Background Images and Text with CSS

I am looking to enhance my website by incorporating text in the foreground and background images on my webpages for SEO purposes. Can anyone recommend the most effective HTML/CSS approach to achieve this? Currently, I am facing compatibility issues with I ...

Why am I unable to view the image in the material-ui-next "Cards" example?

I came across this example on the material-ui-next website, but strangely I am unable to view the lizard image when I try to run it on my computer. Why is the image not showing? There are no errors in my console, and the "simple card" example on the websi ...

What is the best way to resize an element such as an image?

When an image is resized using a percentage, it preserves its aspect ratio properly. I am looking for a way to replicate this behavior with a div. My current challenge involves precisely positioning an element relative to an image. The image fills 100% of ...

Issue with dynamically adjusting flex box width using JavaScript

Currently, I am developing a user interface that heavily relies on flexbox. The layout consists of a content area and a sidebar that can be toggled by adding or removing a specific class. Whenever the sidebar is toggled, the content area needs to be manua ...