No matter how hard I attempt to align my header elements using CSS display properties like inline and inline-block, I just can't seem to get

I'm struggling to line up the divisions within the header element instead of having them stack on top of each other. My goal is to have the logo float left, while the social media icons and search bar float right. Additionally, I want the magnifying glass icon in the search bar to align with the text input field.

Here's the HTML code:

<!DOCTYPE html>
<html>
    <head>
        <title>Maj's Php Project 2022</title>
        
        <!-- Jquery Link -->
        
        <!-- CSS Stylesheet Link -->
        <link href="css/styles.css" rel="stylesheet" type="text/css" />
        
        
        <!-- Bootstrap Links -->
                
        <!-- Metadata Information -->
        
        <!-- Website Comment Information -->
        
    </head>
    
    <body>
       <div id="wrapper">
        <header>
            
            <div id="logo">
            
            </div>
            
            <div id="social-media-icons">
                <img src="img/fb.png" />
                <img src="img/fb.png" />
                <img src="img/fb.png" />
                <img src="img/fb.png" />
            </div>
            
            <div id="search-bar">
                <img src="img/search.png" width="25px" height="25px" />
                <form action="#" method="POST">
                    <input type="text" placeholder="Search" name="search" id="search" />
                    <input type="submit" value="GO" name="submit" id="submit" />
                </form>
            </div>
            
            <nav>
                <ul>
                    <li><a href="">HOME</a></li> |
                    <li><a href="">ABOUT</a></li> |
                    <li><a href="">SERVICES</a></li> |
                    <li><a href="">BLOG</a></li> |
                    <li><a href="">CONTACT</a></li> |
                    <li><a href="">LOGIN</a></li>
                </ul>
            </nav>
        
        </header>

And here's the CSS code:

/* GLOBALS */
/* ELEMENT */
body {
    background-color: white; 
}

header {
    width: 960px;
    background-color: #336699;
    border-radius: 15px;
    padding: 10px;
}
    
main {
    background-color: #33FF99;
    border-radius: 15px;
    padding: 10px;
    color: #330099;
}
    
li {
    display: inline;
    padding: 5%;
}
    
li > a {
    text-decoration:none;
    color: #33CC99;
}
    
li > a:hover {
    color: #330066;
}

footer {
    background-color: #336699;
    border-radius: 15px;
    padding: 10px;
    color: #33CC99;
}
    
header {
    display: inline-block;
}
    
/* IDS */
#wrapper {
    border-radius: 15px;
    width: 960px;
    clear: both;
    margin: 0 auto;
    padding-top: 5px;
}
    
#logo {
    width: 100px;
    height: 100px;
    margin: 5px;
    margin-top: 5px;
    background-color: white;
    border-radius: 15px;
}   
    
#search-bar {
    display: inline;
    float: right;
}
    
#social-media-icons {
    float: right;
}

I've tried floating the elements to get them to align correctly, but it's not working. Any advice on what I might be missing would be greatly appreciated. Thank you!

Answer №1

If you're looking to simplify things and achieve better alignment, consider leveraging either Bootstrap or Flexbox. These tools can assist with your layout desires, offering external resources in the case of Bootstrap and a built-in solution for Flexbox. Personally, I prefer Flexbox due to its enhanced flexibility.

For more information on Flexbox, visit: here

I have created a quick example based on your code with some minor adjustments to ensure that the social media icons and search functionality float "right". Should you want them to float "left", it's an easy tweak to make.

HTML

<body>
  <div id="wrapper">
    <header>
      <div class="header-utility">
        <div id="logo"></div>

        <div class="social-media-icons">
          <img src="img/fb.png" />
          <img src="img/fb.png" />
          <img src="img/fb.png" />
          <img src="img/fb.png" />
        </div>

        <div class="search-bar">
          <form action="#" method="POST">
            <img src="img/search.png" />
            <input type="text" placeholder="Search" name="search" class="search-input" />
            <input type="submit" value="GO" name="submit" id="submit" class="search-submit" />
          </form>
        </div>
      </div>

      <nav>
        <ul>
          <li><a href="">HOME</a></li> |
          <li><a href="">HOME</a></li> |
          <li><a href="">HOME</a></li> |
          <li><a href="">HOME</a></li> |
          <li><a href="">HOME</a></li> |
          <li><a href="">HOME</a></li>
        </ul>
      </nav>

    </header>
  </div>
</body>

CSS

/* GLOBALS */



/* ELEMENT */

body {
  background-color: white;
}

header {
  width: 960px;
  background-color: #336699;
  border-radius: 15px;
  padding: 10px;
}

main {
  background-color: #33FF99;
  border-radius: 15px;
  padding: 10px;
  color: #330099;
}

li {
  display: inline;
  padding: 5%;
}

li > a {
  text-decoration: none;
  color: #33CC99;
}

li > a:hover {
  color: #330066;
}

footer {
  background-color: #336699;
  border-radius: 15px;
  padding: 10px;
  color: #33CC99;
}

/* IDS */

#wrapper {
  border-radius: 15px;
  width: 960px;
  clear: both;
  margin: 0 auto;
  padding-top: 5px;
}

#logo {
  width: 100px;
  height: 100px;
  margin: 5px;
  margin-top: 5px;
  background-color: white;
  border-radius: 15px;
}


/* CLASSES */

.header-utility {
  display: flex;
  align-items: center;
}

.social-media-icons {
  flex-grow: 1;
  text-align: right;
  padding-right: 30px;
}

.search-input,
.search-submit {
  padding: 0px;
  height: 25px;
}

Feel free to experiment with the mockup as needed. Check it out here: here

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

The JavaScript code is failing to retrieve the longitude and latitude of the location on a mobile browser

I am having an issue with my Javascript code not properly retrieving the longitude and latitude from the mobile Chrome browser. While this code works fine on laptop or desktop browsers, it seems to be failing on mobile devices: <script> if (nav ...

How can I create a CSS div with a 20px margin-top distance from the div directly above

Here is the code that I am working with: https://jsfiddle.net/kc94aq33/ I am looking to set the top margin of the .wrapper to be 20px from the bottom of the .header. Does anyone know how I can achieve this? ...

Mistake in labeling a checkboxgroup in ExtJs4.2 by wrapping a boxLabel incorrectly

I am encountering an issue where the box labels of the checkboxes are wrapping under the checkbox: http://jsfiddle.net/6pYWh/ { xtype: 'checkboxgroup', columns: 1, vertical: true, items: [ ...

A guide on monitoring SASS files in all subdirectories with an npm script

Is there a way to watch all sass directories and generate CSS files to the corresponding 'CSS' folder, including subdirectories? The current method involves listing each directory separately in the NPM script. "scripts": { "sas ...

Link aligned to the right

Recently, I added some menu links in the blue area on . However, I am now looking to add a right aligned link while keeping the others left aligned. My attempt using [method] didn't work. Any suggestions on how I can achieve this? ...

Display the concealed mat-option once all other options have been filtered out

My current task involves dynamically creating multiple <mat-select> elements based on the number of "tag types" retrieved from the backend. These <mat-select> elements are then filled with tag data. Users have the ability to add new "tag types, ...

Identifying specific text enclosed within tags in an external file using PHP

I recently started learning about php, and I am looking for a script that can identify text between specific tags in an external file. I came across a solution here that extracts the text within tags from a given string. However, I am unsure how to mo ...

I would like to retrieve my data using my personal API and have them displayed as checkboxes

https://i.sstatic.net/qPSqe.jpgHere is an excerpt of the progress I have made main.html (it's actually a form) <div class="form-group form-check-inline"> <input class="form-check-input" type="radio" name=& ...

Transitioning JS/CSS effects when the window is inactive

My latest project involved creating a small slider using JavaScript to set classes every X seconds, with animation done through CSS Transition. However, I noticed that when the window is inactive (such as if you switch to another tab) and then return, the ...

Having trouble understanding how to display an HTML file using Next.JS?

Currently, I am working on a project that involves utilizing Next.JS to develop a webpage. The main goal is to display a PDF file on the left side of the screen while integrating Chaindesk on the right side to create a chat bot capable of extracting inform ...

Position images to the left, stacked on top of each other, with text centered on the right side of the picture

I have 5 images that I want to stack on the left side, with text displayed beside each image to the right and centered vertically. I've managed to stack the images, but the text is appearing below them instead of beside. Yes, I'm trying to creat ...

The Ultimate Guide to Setting up SVG's Viewbox, Width, and Height for Scalability and Responsiveness

As a beginner in SVG design, I find it challenging to scale, zoom in/out with the container and ensure responsiveness across different devices. I wonder if it's possible to create an SVG that adapts to all device sizes and effectively handles browsin ...

The Carousel feature in Bootstrap 4 is malfunctioning on the website

I've inserted a carousel into my webpage but it's not functioning as intended. I'm attempting to use this carousel code from "codepen.io/decibeldesign/details/bMrQKN" and the full webpage code can be found at "paste.ofcode.org/saJPkuhPPixFmX ...

Change the height of textarea dynamically using jQuery

I am trying to create a comment box similar to Facebook's, where it resizes as text fills it using Expanding Text Areas Made Elegant This is how my view looks: <div class='expandingArea'> <pre><span></span></ ...

Guide for verifying the minimum and maximum values when selecting a particular product from a dropdown menu

My goal is to implement validation for the width textbox when a user selects a specific product from the dropdown menu. The validation should only allow the user to enter a value between 10 and 200. I have successfully retrieved the selected value, but I ...

Automatically scroll to the end of the data retrieved through ajax

I am currently developing a live chat website for my users and I am using ajax to fetch the chat messages. However, I am facing an issue where whenever I enter a new message, it does get added to the database and displayed, but the scroll doesn't auto ...

How can I access a variable from one function within a different function?

function var1() { console.log("inside function one"); var varval1 = "purpose"; alert("value of var one is" + varval1); return varval1; } function var2() { console.log("within function two"); alert("value of var two is" + varval1); ...

Creating a dual-row HTML table heading

Looking to create a table with a two-row header - the first row containing a search input box and the second row containing column names. Here's what I have so far: <thead> <tr class="bg-primary"> <th colspan="5"> ...

Tips on inserting table data into a textarea input using a button

Is there a way to display the text from the table in a textarea box when clicking the "add" button, without removing the data once it's added? I simply want the text "Add this text to textarea" to show up in the textarea box. https://jsfiddle.net/bj ...

The issue of not being able to type in the sweetalert 2 input within a material UI modal in a

Within a card displaying an order, there is a sweetalert2 popup that opens when trying to cancel the order, asking for a cancellation reason. This feature works seamlessly on the orders screen. https://i.sstatic.net/RWhOA.png <Grid item md={8} sm={12}&g ...