What is the best way to vertically center a floated input element within a fixed div?

Looking for some assistance with centering the input elements in the top div of a simple Webshop I'm creating.

HTML/CSS

body {
  margin: 0;
  background: lightcyan;
}

.top {
  position: fixed;
  background: lightblue;
  width: 100%;
}

.top img {
  width: 80px;
  float: left;
}

.top h1 {
  float: left;
}

.top form {
  float: right;
  height: 100%;
}

.top form input {
  display: block;
  float: right;
}
<html>

<head>
  <title>Webshop</title>
</head>

<body>
  <div class="top">
    <img src="ImageUsed.png">
    <h1>Webshop Title</h1>
    <form>
      <input type="submit" value="register">
      <input type="submit" value="login">
    </form>
  </div>
</body>

</html>

The alignment of the div, img, and h1 elements is on point, but I'm struggling to vertically center the two input elements within the form element inside the div. Any suggestions on how to achieve this?

Answer №1

Eliminate all floats as they become unnecessary when utilizing flexbox. Assign flex to both top and your <form>, then utilize align-items: center;.

I have also introduced an additional div to enclose your img and h1, applying flex properties to ensure they are displayed next to each other using space-between on the top element.

body {
  margin: 0;
  background: lightcyan;
}

.top {
  position: fixed;
  background: lightblue;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.top img {
  width: 80px;
}

form,
.top > div:nth-child(1) {
  display: flex;
  align-items: center;
}

.top form {
  height: 100%;
}

.top form input {
  display: block;
}
<html>

<head>
  <title>Webshop</title>
</head>

<body>
  <div class="top">
  <div>
    <img src="ImageUsed.png">
    <h1>Webshop Title</h1>
  </div>
    <form>
      <input type="submit" value="register">
      <input type="submit" value="login">
    </form>
  </div>
</body>

</html>

Answer №2

Utilize flexbox over floats for better alignment of elements along a single axis.

<style>
    body{
    margin: 0;
    background: lightcyan;
  }
  .top{
    position: fixed;
    background: lightblue;
    width: 100%;
    display: flex;
    align-items: center;
  }

  .top h1{
    margin-right: auto;
  }

  .top form input{
   display: block;
   float: right;
  }
</style>
<html>
  <head>
    <title>Webshop</title>
  </head>
  <body>
    <div class="top">
      <img src="ImageUsed.png">
      <h1>Webshop Title</h1>
      <form>
        <input type="submit" value="register">
        <input type="submit" value="login">
      </form>
    </div>
  </body>
</html>

Answer №3

Utilizing flexbox simplifies the layout

.header {
  position: fixed;
  background: skyblue;
  width: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

Answer №4

To center a fixed-position header vertically with a fixed height and relative position, you can apply position: absolute;, top: 50%, and translateY(-50%) to the parent element of input elements. Additionally, use right: 0 instead of float: right for alignment:

body{
    margin: 0;
    background: lightcyan;
  }
  .top{
    position: fixed;
    background: lightblue;
    width: 100%;
    height: 80px;
    position: relative;
  }
  .top img{
    width: 80px;
    float: left;
  }
  .top h1{
    float: left;
  }
  .top form{
    display: block;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
  }
  .top form input{
    display: block;
    float: right;
  }
<html>
  <head>
    <title>Webshop</title>
  </head>
  <body>
    <div class="top">
      <img src="ImageUsed.png">
      <h1>Webshop Title</h1>
      <form>
        <input type="submit" value="register">
        <input type="submit" value="login">
      </form>
    </div>
  </body>
</html>

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

Change background according to URL query

My goal is to dynamically change background images based on a URL parameter, specifically the destination code. With numerous background images available, it's crucial to display the correct one depending on the URL string. For instance, if the URL re ...

Activate the selected image on click and deactivate all other images

Looking to use Javascript onclick to create an event where the clicked image is enabled and others are disabled. For instance, if there are 6 pictures, how can I achieve this: Clicking on any picture, let's say number 3, will enable that picture whil ...

Eliminate XML namespaces from HTML content generated through XSL in PHP

In my PHP routine, I am utilizing XSL to convert XML into HTML. The issue I am facing is that xmlns namespaces are being added as attributes to the h2 element during the transformation process. PHP: $url = "http://www.ecb.europa.eu/stats/eurofxref/eurofx ...

Stylize the div within the hyperlink when hovering over it

I am trying to add a hover effect to a div inside a hyperlink using CSS, but I'm not sure how to do it. The divs in question have background images set in the CSS. <div class="header_menu"> <div class="foa_logo"><img src="images/FOA ...

Failure to connect HTML and CSS files

My HTML and CSS files are not linking even though they are in the same folder. Here is my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatibl ...

"Step-by-step guide on triggering the ng-click function for the first row within an ng

I'm facing an issue where the first set of data from the ng-repeat div is not loading before clicking on the ng-repeat. Below is the code snippet: <div class"profile" ng-repeat="data in information" ng-click="getData(data.i ...

An alternative in the Android platform that simulates the functionality of CompositeOperation in

When working with HTML5/JavaScript, you may come across the compositeoperation property, which allows you to set the mode for placing graphics on a canvas (add/xor/over etc.). Is there an equivalent behavior for manipulating graphics in Android's Can ...

Issue with Bootstrap 5: bg-customcolor and text-customcolor classes are not functioning as expected

Recently, I decided to update an old bootstrap page to the latest Bootstrap 5.3.1 version. Thanks to this amazing guide, I managed to configure my custom colors successfully: How to extend/modify (customize) Bootstrap with SASS However, I encountered a pr ...

Struggling to implement Bootstrap in my code

Struggling to apply Bootstrap styling to my webpage, I've correctly linked it using <link href="css/bootstrap.min.css" rel="stylesheet"> as per the official documentation. However, despite having both files in the same folder, Bootstrap styles ...

Tips for restricting search results in Angular12

I've been attempting to restrict the number of results displayed in the table using ngrepeat with limitTo, but unfortunately, it's not functioning as expected. Below is a snippet of my code: busqueda.component.ts import { Component, OnInit } fr ...

What is the best approach to implement a basic image upload functionality?

I have a vision for my new website where users can upload an image with the click of a button, store it locally on one page and then retrieve its absolute URL to share on forums or other websites for preview purposes. While I've managed to create a fu ...

The fixed navbar in Bootstrap is missing a bottom margin

I have created a basic theme using bootstrap 4. The navbar in this theme has a fixed-top class. Right below the navbar, there is a div that displays the page title as its content. <div class="aloldal_text_div"> <div class="container"> < ...

Media queries in CSS appear to be dysfunctional when used on Microsoft Edge

@media (min-width: 992px) and (max-width: 1140px) { .mr-1024-none { margin-right: 0px !important; } .mt-1024 { margin-top: 1rem !important; } .d-1024-none { display: none !important; } } Utilizing the ...

Mall magnitude miscalculation

I am currently experiencing an issue with Galleria and the Flickr plugin. Some images are displaying correctly, while others appear scaled and parts of them are cut off. How can I fix this problem? Below is the HTML code for the Galleria gallery on my web ...

Discover the method to retrieve HTML content by sending an HTTP POST request in AngularJS, similar to loading content

My attempt to retrieve HTML content using Angular Js Post HTTP method was successful, but only for text contents like P tags. I am now trying to fetch HTML contents that include input types such as text boxes using Angular JS. Interestingly, when I utilize ...

Can you explain the guidelines for overlapping CSS media queries?

When it comes to spacing out media queries accurately to prevent overlap, how should we approach it? Let's examine the following code as an example: @media (max-width: 20em) { /* styles for narrow viewport */ } @media (min-width: 20em) and (max ...

Difficulty displaying background image on iOS devices like iPhone and iPad

Our Magento website has a background image that expands according to the content, working well on PCs and Macs. However, the white background does not show up on iOS devices. I have attached two screenshots - one showing how it appears on a regular PC bro ...

Anchor tag images are not functioning as clickable links

I recently created a portfolio that showcases my work through a grid of images. My intention was for each image to link to another page providing more details about the project when clicked. However, I am encountering an issue where the anchor tags are no ...

Ways to verify if the length of a string is zero in Angular JS

An array was created and initialized with various fields, including targaauto. Within the add function, there is a check for an error alert if the length of the car plate is equal to 0. However, this functionality does not work correctly as the alert doe ...

I struggle with generating a transition effect in the input box through label transformation

Why is the input box not using the specified CSS styles for the input and label tags? The transform property doesn't seem to be working as expected. I've highlighted the areas where I'm facing issues with bold text, and I've included bo ...