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:

<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

Image Not Displayed on Page

I am facing an issue where I have a div class 'breadcam_bg' with an image url, but the image is not being displayed on the page even though the console detects the div. html <div class="bradcam_area breadcam_bg"> This div! & ...

Adjust the width of an item on CSS Grid upon hovering

I recently created a basic CSS grid layout with the following structure... .container { display:grid; grid-template-columns:1fr 1fr 1fr 1fr; } .col1 { padding:20px; background:red; color:white; text-align:center; } .col2 { padding:20px; ...

Organize my code with CSS styling

Is there a method to organize a css file in the following structure?: a:link { color: red; font-weight: bold; a:hover { text-decoration: none; font-weight: normal } } I would like a regular link to appear underlined and bold, but when hov ...

Utilize Google Maps to receive directions to a specific destination and discover current traffic conditions along the route using their comprehensive API

I am currently working on implementing the Google Maps direction identifier functionality with traffic details. However, I am able to obtain directions but not specific traffic details for a selected path; it seems to apply overall traffic data instead. ...

Ways to eliminate header and footer data while printing a webpage

In my attempt to implement the code displayed below, I am encountering an issue where the header and footer continue to appear on the printed page. Despite numerous attempts with various CSS elements, I have been unsuccessful in removing the header and fo ...

I would like to apply my CSS styles to all the hyperlinks on my website

This is the CSS code I created: img{width:85%; height:auto;} #thumbwrap { position:relative; } .thumb img { border:1px solid #000; margin:3px; float:left; } .thumb span { position:absolute; visibility:hidden; } .thumb:hover, .thu ...

What is the best way to stack several items vertically beside an image?

As a beginner in the world of web development, I recently embarked on my first project. However, I am facing an issue where I cannot align multiple elements under each other next to an image. One element floats at the top beside the image, while the other ...

I'm having trouble with the calculator, unable to identify the issue (Typescript)

I'm struggling with programming a calculator for my class. I followed the instructions from our lesson, but it's not functioning properly and I can't pinpoint the issue. I just need a hint on where the problem might be located. The calculat ...

Tips for showcasing beautifully formatted XML content on a webpage

I have some XML data that I want to display on my website, but I'd prefer to show it in a styled format like a bootstrap table. However, I'm not sure how to go about doing this. Here's an example of the XML: <?xml version="1.0" encoding ...

Consolidate radio group in Vuetify platform

I'm having trouble centering the v-radio-group. Here's my current setup: <v-container grid-list-md text-xs-center> <v-form ref="form"> <div v-if="question.question_type == 'YESNO' "> <v-radio-group ...

Setting various font sizes for a single element

I'm looking to enhance my text by incorporating an embedded font in addition to Arial as a backup option. However, the embedded font requires a larger font size compared to Arial. Is there a way to ensure that when displaying the same text element, A ...

Steps for inserting a script tag in an Angular component

My Angular/Typescript website needs to integrate a third-party script that generates a table. However, I'm facing issues with rendering the table within the home.component.html file. I've managed to embed the script, but it doesn't seem to ...

Looking for advice on using media queries to resize my background image - any tips?

I'm struggling to resize my background image using media queries. Can anyone offer suggestions or solutions? I've tried applying the media query for my background image like I do with other elements, and it's not working properly. The issue ...

What is the best way to prevent 2 divs from overlapping on a webpage?

I'm having trouble getting the smaller div to display right below the larger red div. I tried using the display block css property, but it doesn't seem to be working as expected. Unfortunately, I can't provide an image here, but you can vie ...

Having trouble locating and interacting with the textarea element in Salesforce using Selenium Webdriver

Encountering an issue with the Selenium Webdriver where it throws an error stating that the element is not visible and cannot be interacted with when attempting to access a textarea. 1. The textarea is located within a pop-up window, which can be accessed ...

Avoid Conversion of HTML Entities in Table Cells

<table> <tr> <td>&gt;</td> </tr> <tr> <td>&&#xfeff;GT</td> </tr> </table> In the code snippet above, I have table cells containing HTML entities. A re ...

Utilizing external Cascading Style Sheets in AngularJS 1

Can an external CSS be applied to a component in AngularJS1? If yes, then how can it be done? I have only seen examples of applying inline css... ...

Implementing an unordered list in an inline format, even when it is collapsed with Bootstrap

Recently, I've been delving into Bootstrap and experimenting with coding a basic website. Below is the code for the navbar: <nav class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="container"> <div class ...

Tips on combining multiple HTML tags within a single div element

After extracting a value from an XML document using Javascript, I encountered a problem while attempting to generate multiple image tags based on that value. Despite my attempt with a for loop, only one image was being displayed. for(i=0; i<red; i++){ ...

Is it possible to display partial html templates by accessing the local url and blending them with the main index.html file?

Is there a way to view partial HTML templates locally without copying them into the main index.html file? I'm looking for a solution that allows me to access my partial templates through a local URL without having to manually paste them into the main ...