What is the best way to align child elements on the opposite side of their parent using flexbox in CSS?

I'm currently designing the header for a website project. I have my logo positioned on the right side, and I want to place the search bar on the left side. To achieve this layout, I am utilizing flexbox in CSS.

I've experimented with various approaches, and here is the HTML code snippet I have implemented:

 <body>
      <header>
        <div class="logo">
          <logo picture/>
        </div>
        <div class="search-set">
            <form id="s-bar-holder" role="search">
              <input type="query" id="search-bar" name="q" placeholder="Search..." aria-label="Search the site."/>
            </form>
        </div>
        <div></div>
      </header>
      <main></main>
    </body>

Furthermore, here's the relevant CSS code that corresponds to the header styling:

header {
    display: flex;
    background-color: rgb(78,78,78);
    margin-top: -8px;
    margin-left: -7px;
    margin-right: -8px;
    height: 60px;
    width: auto;
}
.logo {
  height: 60px;
  width: 300px;
  overflow: hidden;
  display: flex;
  align-self: flex-start;
}
.logo > img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.search-set {
    display: flex;
    align-self: flex-end;
}
#search-bar {
    background-color: rgb(78,78,78);
    border: none;
    color: rgb(230,230,230);
    outline: none;
    height: 58px;
    width: 200px;
    display: flex;
    justify-content: wrap;

If you have any insights or suggestions, please feel free to share. Thank you!

Answer №1

My attempt was to create a header section that resembles your desired design. Please take a look at the code and run the snippet in full-page view.

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap'); 
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
  }

body{
    background: rgb(235, 235, 235);
    
  }
header {
    display: flex;
    background-color: rgb(78,78,78);
    height: 60px;
    width: auto;    
}

.container{
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
}

.logo p{
    font-size: 30px;
    font-weight: 800;
    margin: 5px;
    margin-right: 50px;
}

#search-bar {
    background-color: rgb(78,78,78);
    border: none;
    color: rgb(230,230,230);
    outline: none;
    height: 58px;
    display: flex;
    justify-content: wrap;
    margin-left: 50px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="q4.css" />
    <title>Flexbox</title>
  </head>
  <body>
    <header>
    <div class="container">
        <div class="search-set">
            <form id="s-bar-holder" role="search">
              <input type="query" id="search-bar" name="q" placeholder="Search..." aria-label="Search the site."/>
            </form>
        </div>

        <div class="logo">
            <p>LOGO</p>
        </div>
        
    </div>
    </header>
  </body>
</html>

Answer №2

header { using flex display to create a stylish layout; setting background color to dark gray (rgb 78, 78, 78); adjusting margins for optimal positioning; defining height and width measurements; arranging items in row-reverse direction with space between them; }

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

Creating Hover Effects for Touch Screen Devices with jQuery

Recently, I came across a piece of code that enables a link to become active on the second tap when using touch screen devices. I found this code snippet from: hnldesign.nl (function($) { var landingLink = '.nav-landing > li'; if ((' ...

I am attempting to create a webpage with a primary center image and two additional images on either side using PHP or HTML

I have a coding dilemma - I want to display three images in a single row, one in the center, one on the left, and one on the right. However, my current PHP code is causing the left and right images to appear below the center image. How can I adjust my co ...

HTML5 - the enigmatic header and outline procedure

While revisiting the HTML5 spec, I came across a question about nesting a nav element within a header. The description for the header element seemed a bit peculiar to me, mainly because I couldn't fully grasp it. In the given example, the page inc ...

Is there a way to adjust the transparency of a span element's background without affecting the text itself?

I have some code that needs to be customized. This is my HTML: <div class="text-rotator add-top-half add-bottom-half align-left white-txt medium-txt uppercase highlight-bg"> <div class="rotator-wrap" style="display: block;"> <sp ...

Exploring methods to detect when a PHP page has been printed using the 'ctrl+p' shortcut and then subsequently updating

Hey there! I'm currently managing a php website that functions as an accounting system. Within this system, there are receipts, invoices, and other forms of documentation in use. However, I've encountered a problem with the main table in my myS ...

What might be causing the status problem to not display correctly?

My toggle feature to switch between no issue and issue is not displaying the status correctly. Despite trying to troubleshoot, I can't figure out why the issue section is not showing up. <!DOCTYPE html> <html> <script src="https://aj ...

The object of type 'NoneType' does not have the 'next_element' attribute

Here we have an HTML snippet of a website with a single itemprop="brand": <dd itemprop="brand" class="attributeValue-2574930263"> <a class="link-3970392289 link__default-1151936189 attributeLink-2414863004&qu ...

Text display appears uneven

After downloading the 'exo' font from Google Fonts, I was pleased with how it appeared on my htc smartphone. However, on my laptop screen, it looked as if a swarm of caterpillars had nibbled away at it. I attempted various techniques like anti-a ...

Creating form components in a row

I'm currently working on an app and I'm attempting to align 3 form elements in a ratio of 25/25/50, but it's proving to be quite challenging. At the end of this message, you'll find a picture showing the current layout. Additionally, I& ...

Invoke a JavaScript function once the div has finished loading

After clicking on a radio button, I am dynamically loading a div element. I want to hide a specific part of the div once it is loaded. $(function($) { $('.div_element').on('load', function() { $('.textbox').hide(); } ...

Editing HTML on an ASPX website can be a breeze with

Hello there, I've been tasked with editing a website for a client, but the site is located on a local web server and was designed using aspx. As I review all the files, I am encountering difficulty finding the HTML code associated with runat=server w ...

Creating a dynamic table displaying data fetched from a MySQL database, with the added functionality of seamlessly filtering the table content without the need to refresh the

Currently, I am developing a website that showcases leaderboard data retrieved from a MySQL database in the form of a table. The users have the ability to filter and search through this data. By utilizing PHP calls, I was successful in constructing the tab ...

What are the steps to create a hovering dropdown menu that changes the background window to transparent when hovered over?

Is there a way to create a dropdown menu that changes the background window to transparent when hovered over? Here is an example of what I am looking for: https://i.sstatic.net/FplLm.png The dropdown menu should look like this when hovered over: https: ...

The code is malfunctioning on this site (yet functions perfectly on a different website)

So I have a question that may seem silly, but it's worth asking. I'm attempting to create a sticky div element that stays in place when you scroll past a certain point on the page. This is the script I have: <script type="text/javascript"> ...

CSS documents are being displayed as type text/plain

My goal is to host my static blog (built with Jekyll) on my Ubuntu server, but I'm encountering a problem where the CSS isn't being applied and I keep seeing this error: "Resource interpreted as Stylesheet but transferred with MIME type text/pla ...

I'm having trouble modifying the border color of a TableCell component in material-ui

I am struggling to change the border color of a table cell. Despite numerous attempts, I have been unsuccessful in my efforts. Can someone please assist me with this? const customTheme = createTheme({ overrides: { MuiTableCell: { root: { ...

AngularJS - How to Deactivate List Items

Currently, I am working on developing a breadcrumb navigation system using AngularJS. However, I am facing an issue where some of the links need to be disabled based on certain user requirements not being met. After researching the Angular documentation, ...

Techniques for transferring information to Highchart directly from session storage

I am attempting to populate a pie highchart with data from a session storage variable, but I am not seeing any results. Here is the desired code: $(function () { Highcharts.chart('container', { chart: { type: 'pie', ...

The Angular Material md-input-container consumes a significant amount of space

Currently, I am exploring a sample in Angular Material. It appears that the md-input-container tag is taking up a substantial amount of space by default. The output is shown below: https://i.sstatic.net/hQgpT.png However, I have come across the md-input- ...

Is it possible to tap into the stylus Abstract Syntax Tree (AST) for

Is there a way to access the abstract syntax tree (AST) of the CSS style generated by stylus without re-parsing it using css-parse? I'm curious if the AST of the generated styles can be accessed publicly. ...