aligning a form vertically in a container

<div class="parent">
    <div class="left-child">
        <img src="logo.jpg" alt="logo">
    </div>
    <div class="right-child">
        <form action="#">
            <input type="text" value="search">
        </form>
    </div>
</div>

Is there a way to vertically align the right-child form as the height of the left-child logo changes?

Any assistance would be greatly appreciated.

Note: The logo's height will vary since it is uploaded by the user. Therefore, the exact height is unknown to me.

Answer №1

I have customized a new div structure to better suit the layout of your form in a vertical orientation. I took the liberty of adjusting the image width, though feel free to remove this modification if needed. You can view the updated format here.

Below is the CSS code:

.parent {
    border: 1px dashed red;
    display: table;        
    overflow: auto;
}

.left-child img {
    width: 50px;
    height: 50px;
}

.left-child {
    border: 1px dashed blue;        
}

.right-child {
    border: 1px dashed green;
    display: table-cell;
    vertical-align: middle;                        
}

#your_form {
    margin: 0 auto;
    width: 200px;
}

And here is your html structure:

<div class="parent">
    <div class="left-child">
        <img src="icons.jpg" alt="logo" />
    </div>
    <div class="right-child">
        <div id="your_form">
            <form action="#">
                <input type="text" value="search" />
            </form>            
        </div>            
    </div>
</div>

Warm regards, Vin.

Answer №2

If you have divs set to display: inline-block, then you can utilize vertical-align: middle. However, this method is not compatible with floats:

.left-section,
.right-section {
    display: inline-block;
    vertical-align: middle;
}

For a live example, check out this link: http://jsfiddle.net/abc123/def456/

Keep in mind that using inline-block may not work on older browsers such as IE7. More information can be found here: http://caniuse.com/inline-block

Answer №3

HTML Code:

<div class="wrapper">
    <div class="left-side">
         <div><img src="logo.jpg" alt="logo" /></div>
    </div>
    <div class="right-side">
        <form action="#">
            <input type="text" value="search1" /><br/>
            <input type="text" value="search2" /><br/>
            <input type="text" value="search3" />
        </form>
    </div>
</div>

CSS Styles:

.wrapper {
    height: 400px;
    width: 100%;
    position: relative;
}

.left-side {
    width: 50%;
    height: 100%;
    display: table;
    position: absolute;
}
.left-side div {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

.right-side {
    width: 50%;
    height: 100%;
    display: table;
    position: absolute;
    left: 50%;
}

.right-side form {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

Source

Live Demo

Answer №4

The optimal solution moving forward involves implementing the flex box model

Below is the CSS code required to facilitate your needs http://jsfiddle.net/BfZFJ/1/

.parent {
    /* Utilize flex box layout for arranging its children */
    display: box;
    /* Vertically center its children */
    box-align: center;    
}    

.right-child {
    /* The child on the right will occupy all remaining horizontal space */
    box-flex: 1;
}

/* No additional adjustments needed for left-child, it automatically takes up its natural size */

It's important to note that in practice (at least for now), you must incorporate vendor prefixes:

  • display: [-webkit-box, -moz-box]
  • -webkit-box-flex, -moz-box-flex
  • -webkit-box-align, -moz-box-align

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

Tips for aligning multiline text in the center of images with varying widths

Excuse my lack of experience, but I have a question, I am looking to center a header and some text next to an image with variable width. Additionally, the text should be able to position on either side of the image, while still being coded first in the HT ...

Utilize Python to extract a table from an HTML document

I would like to retrieve a table from an HTML file. Below is the code-snippet I have written in order to extract the first table: import urllib2 import os import time import traceback from bs4 import BeautifulSoup #find('table',{'class&ap ...

How can you determine the specific type of "display" utilized by CSS Bootstrap for a particular element?

When manipulating the "display" property of a bootstrap element like "row" or "col-md-3" using JavaScript, how can I determine the default value set by Bootstrap CSS? For instance, the Bootstrap source code likely sets the "display" value for the "row" cl ...

The setStyle() method in JavaFX is like CSS Selectors

Is it possible to bind CSS files to style JavaFX components and also change properties dynamically in code? I'm struggling with the setStyle() method as there's limited documentation available on how to use it effectively. Instead of modifying t ...

Switch between different content sections using a button in a Bootstrap tab-pane

Here is the code snippet I am currently working with: <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-s ...

Implementing CSS styles with jQuery

Looking for a way to dynamically add CSS attributes to different form elements like text fields, text areas, checkboxes, and dropdowns? There's also a separate block that lists possible CSS properties such as font, font-style, width, and padding. What ...

Upon returning to the website homepage from another page, the JavaScript animation ceases

I implemented a unique typewriter animation on my homepage by utilizing code from this source and making minor HTML edits to customize the content. https://codepen.io/hi-im-si/pen/DHoup. <h5> <body>Hello! I am Jane.</body> < ...

Using JavaScript to convert the text within a div into negative HTML code

I am working with this specific div: <div class="signs" id="signs" onclick="toggle()">&#43;</div> It currently displays the positive sign. I have set up a JavaScript function that is triggered when the div is ...

Ways to add extra dots to your listing style

Is there a CSS property or list style that can display an expanding list? For example: Order now for a discount! <ol style="list-style: none"> <li>* Available only for the first 12 months</li> <li>** Only for new customers ...

What steps do I need to take in order to ensure that each webpage displays unique thumbnails?

As a newcomer to website development, I recently looked into implementing open graph on my site. However, I ran into an issue where I could only set one thumbnail for the entire website. This posed a problem as I wanted each navigation menu tab (Home, Abou ...

Verifying file types with HTML5 drag and drop feature

Is it possible to change the drop zone's background color to green or red based on whether the dragged payload contains supported file types (JPEG)? Do Gecko and Webkit browsers have the ability to determine the file type of drag and drop files? ...

Ways to prevent a particular link from functioning in HTML or JavaScript

Hey there, I am currently using a Gchat voice and video chat script. Check out my website if you're interested. The issue I'm facing is that someone logs into my chatroom and uses this flash link to crash other users' browsers. Whenever th ...

Creating a customizable navigation bar using only CSS

I'm currently working on creating a navigation bar using only HTML and CSS, without the need for JavaScript to open and close it. However, I've encountered an issue where the navigation bar remains open even after setting display: none or visibi ...

Printing from a Windows computer may sometimes result in a blank page

Looking to incorporate a print button into an HTML page, I'm facing an issue. The majority of the content on the page should not be included in the printed version, so my approach involves hiding everything in print and then showing only the designate ...

Adjust positioning of navigation when hovered over

Need help creating a cool navigation effect like this. Live example: https://hookandbarrelrestaurant.com/ Here is my code: https://codepen.io/Dhaval182/pen/rQPMoW ...

A step-by-step guide on setting up a confirmation pop-up message using Gravity Forms

Has anyone attempted to implement a pop-up confirmation message in Gravity Forms? I am also looking to prevent the form from disappearing after submission. By the way, I have selected text as the confirmation type in my Gravity Form settings because I do ...

What is the best way for me to make my hamburger animation play in reverse?

Having trouble achieving smooth animation effects. I've designed a burger icon using three divs: <div class="container"> <div class="burger-contain"> <div id="line-1" class="line"></div> <div id="line-2" class="lin ...

Having trouble scrolling on a Chrome browser with the Muse website?

My significant other is having some trouble with her muse-created website at . It seems to have an issue scrolling horizontally on Chrome, but works fine on Safari and Edge. The site is part of a university project and she hopes it will eventually look lik ...

Tips for confining a float within a div with fluctuating space

I have a scenario where I have multiple divs ('group') containing text, and there is a floated div ('toggle') in the bottom corner. The current code works well if the text length in 'group' is consistent, but the position of t ...

Eliminate the horizontal overflow caused by the HTML video tag

I'm currently facing an issue with using a video as the background for a div on my website. The problem arises when I view it on a smaller resolution, causing overflow on the right side. I've been attempting to solve this without success... You ...