Arrange the header and input within a div using flexbox to achieve different alignments

I need help aligning the header section vertically and ensuring responsiveness. Using margin-top isn't fully responsive, so I want to align the header at the beginning of the input field.

{
    margin: 0px;
    padding: 0px;
    color: white;
}

#header{
    height: 300px;
    width: 100%;
    background-image: url(healthy.jpg);
}

#headersection{
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
#headersection input{
   width: 50%;
   height: 40px;
   border-radius: 20px;
  border: none;
}

#navigation{
    display: flex;
    flex-wrap: wrap;
}
#navigation li{
    font-weight: bold;
    font-size: 1rem;
    padding: 20px;
    list-style: none;
}
<nav id="header">
        <ul  id="navigation">
            <li>Home</li>
            <li>About Us</li>
            <li>Register</li>
            <li>Contact Us</li>
        </ul>
        <div id="headersection">
            <h1>
                Health is Wealth
            </h1>
            <input type="search" name="Search" id="" placeholder="Search">
        </div>
 </nav>
    

I would like to align the input and header sections in the blue area and also move the header part to the beginning of this input section as marked by the yellow lines.

Answer №1

To vertically center #headersection, you can utilize the valign attribute with a value of "middle" instead of relying on margin-top. This will automatically position it in the middle without the need for align-items in #headersection.

Answer №2

When the headersection is the same width as the input, you have the option to utilize the following CSS:

#headersection{
align-items: flex-start;

}

Answer №3

Begin by inserting a

<div class="container">
</div>.

To adjust the start of the navigation text, set the max-width value for this div.

If you want to align the header part with yellow lines at the beginning of the input section, include

#navigation > li:nth-child(1){padding-left: 0;}
in your CSS file.

The provided code snippet should assist you in achieving the desired layout.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0px;
        padding: 0px;
        color: white;
        background-color: lightgreen;
        background-image: url(healthy.jpg);
      }

      #header {
        height: 300px;
        width: 100%;
      }

      #headersection {
        display: flex;
        flex-direction: column;
      }
      #headersection input {
        width: 100%;
        height: 40px;
        border-radius: 20px;
        border: none;
        background: white;
      }

      #navigation {
        display: flex;
        flex-wrap: wrap;
      }
      #navigation li {
        font-weight: bold;
        font-size: 1rem;
        padding: 20px;
        list-style: none;
      }
      #navigation > li:nth-child(1){
          padding-left: 0;
      }
      .container {
          max-width: 768px;
          margin-left: auto;
          margin-right: auto;
      }
    </style>
  </head>
  <body>
      <div class="container">
    <nav id="header">
      <ul id="navigation">
        <li>Home</li>
        <li>About Us</li>
        <li>Register</li>
        <li>Contact Us</li>
      </ul>
      <div id="headersection">
        <h1>Health is Wealth</h1>
        <input type="search" name="Search" id="" placeholder="Search" />
      </div>
    </nav>
</div>
  </body>
</html>

Answer №4

Check out this code snippet:

*{
    margin: 0px;
    padding: 0px;
    color: white;
}

#header{
    height: 300px;
    width: 100%;
    background-color: red; /*instead your image. only for seen*/
}

#headersection{
    display: flex;
    flex-direction: column;
    justify-content: center;
    height: 100%; /*added*/
    width: 50%; /*added*/
}

#headersection input{
   width: 100%; /*edited*/
   height: 40px;
   border-radius: 20px;
  border: none;
}

#navigation{
    display: flex;
    flex-wrap: wrap;
}

#navigation li{
    font-weight: bold;
    font-size: 1rem;
    padding: 20px;
    list-style: none;
}
#container{ /*added*/
    display: flex;
    justify-content: center;
    width: 100%;
    height: 60%;
}
<nav id="header">
        <ul  id="navigation>
            <li>Home</li>
            <li>About Us</li>
            <li>Register</li>
            <li>Contact Us</li>
        </ul>
        <div id="container">
        <div id="headersection">
            <h1>
                Health is Wealth
            </h1>
            <input type="search" name="Search" id="" placeholder="Search">
        </div>
        </div>
    </nav>

I made some edits to the code. Let me know if it meets your requirements.

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

React does not support having two :focus selectors on a single JSX element

I'm working with a React script that includes a specific element. I want to apply more than one :focus-within selector in CSS, but for some reason only the dropdown selector is being expressed when I focus on the element. Here's the code: React J ...

When I try to host my Jekyll site on GitHub Pages using my custom domain, the CSS doesn't seem to be rendering at all

Everything looks perfect when I test my site on local host, but once I push it to my GitHub repo, only the HTML appears with no CSS or JavaScript. I've attempted changing my URL in the config.yml file to https://chanvrequebec.com and modifying href="/ ...

Activate the clicked tab in the Bootstrap navbar when selected

I have gone through similar posts, but I am unable to get mine to work. My goal is to activate the clicked tab. Here is what my bootstrap navbar looks like in HTML: <div class="navbar navbar-default" role="navigation"> <div class="container" ...

The size of the panel header does not seem to be adjusting as expected within the React

I need help adjusting the header size in a React-Bootstrap Panel within a Typescript application. I've tried wrapping the text in <h1> tags as specified in the documentation, but there seems to be no change. Even with attempts to add inline sty ...

Ways to create an overlapping effect for 3 elements using CSS

There are 3 elements in my setup: <div class="foo"></div> <div class="bar"></div> <div class="foobar"></div> I am looking to have .foo overlap .bar, .bar to overlap .foobar, and .foobar to overlap .foo. This is the de ...

Incorporate JavaScript functionality with HTML dropdown lists

I am attempting to achieve the following: The user can choose between "Option One" or "Option Two". If they select "Option One", the result will be 66 + 45, and if they select "Option Two", the result will be 35 + 45. How can I make this work using a com ...

Guide on displaying a real-time "Last Refreshed" message on a webpage that automatically updates to show the time passed since the last API request

Hey all, I recently started my journey into web development and I'm working on a feature to display "Last Refreshed ago" on the webpage. I came across this website which inspired me. What I aim to achieve is to show text like "Last Refreshed 1 sec ago ...

Samsung devices exclusively showcase background in responsive design

I am encountering a major problem with my website design, specifically with its compatibility on mobile devices. To ensure its responsiveness, I have tested the design on my iPhone, iPad, and Windows Phone. Unfortunately, since I lack an Android device, I ...

Splitting an array into multiple arrays with distinct names: A step-by-step guide

I have a data set that looks like this: [A,1,0,1,0,1,B,1,0,0,1,A,1]. I want to divide this array into smaller arrays. Each division will occur at the positions where "A" or "B" is found in the original array. The new arrays should be named with the prefix ...

Tips for making text flow around an image

How can I achieve a design where the text wraps around an image positioned in the bottom right corner? [Image Removed by Owner] Your guidance on implementing this effect is greatly appreciated. Thank you. ...

When using jQuery to focus on an input element, the cursor fails to show up

Looking to enhance user experience by focusing on an input element upon clicking a specific div. The HTML structure being used is as follows: <div class="placeholder_input"> <input type="text" id="username" maxlength="100" /> <div ...

Discover the index of the row when the value in the dropdown list is updated

I'm faced with a challenge regarding an HTML Table that contains a dropdown list in every row. I would like the background of each row to change whenever the value in the dropdown list is modified. Below is the code snippet: <table id="table1"> ...

Steps to modify the background color of an IconButton upon clicking in Material UI

After clicking on the IconButton, the background color changes to an oval shape. I now need to modify the background color when it is clicked. CSS The CSS code for the IconButton changes the background color upon hover. I require a similar effect for the ...

The Impact of CSS Border Width on the Size of a PHP Form

Creating a PHP form with a tabbed box has been quite the challenge. With 3 tabs, each requiring different field inputs, I set out to add a border around the form headings and tabs. The border looks great, but it poses a problem - its width spans the entire ...

Overlap one element entirely with another

Currently, I am working on a way for an element called overlayElement to completely cover another element known as originalElement. The overlayElement is an iFrame, but that detail may not be significant in this scenario. My goal is to ensure that the over ...

Issue with rendering components list in React.js

I am currently working on a project using React, and I'm facing an issue where my list is not displaying on render(). In my parent component, I have a list of components coming from SearchResult. Below is the relevant portion of my code: class Create ...

Personalizing the fileTemplate selection in FineUploader

My English is not the best, so apologies in advance. I'm struggling to customize the FineUploader FileTemplate option. I don't want to use fineUploaderBasic; I want total customization. Initially, I managed to hide the file name and size after a ...

Implement a CSS style for all upcoming elements

I'm currently developing a Chrome extension tailored for my personal needs. I am looking to implement a CSS rule that will be applied to all elements within a certain class, even if they are dynamically generated after the execution of my extension&ap ...

Unable to see background image inside div

Why is the background image not showing up in the left column? I've applied the CSS background-image to .left but it's still not working. Any suggestions on what might be causing this issue? It seems like a small problem, but it's really bot ...

Is there a way to prevent HTML code from being executed when sharing it as text? I want to be able to share code without it being activated as HTML on the page

Seems like I have a straightforward issue related to HTML tags. I am looking to post some code snippets on my blog, specifically the <h1> heading</h1>. I want visitors to view and not just the bolded text of heading. Do I need to incorporate J ...