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

What is the best way to include an active item within an li tag inside a div container?

Here is the structure for a widget that I have, and I want to add the active class to the li. I've tried doing this but it's not working and I'm unsure where the issue lies. var selector = '#recent-posts-5'; var url = window. ...

Is there a way to create triangles on all four corners of an image using canvas in JavaScript?

In my code, I am currently working on drawing rectangles on the corners of an image using JavaScript. To achieve this, I first extract the image data from a canvas element. // Get image data let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height) ...

Dynamic Form Layout with Angulajs' Adaptive Data

As a developer, I have created a form using a directive and I am supplying the layout data from my controller. TheController.js [{ "type":"selectbox", "name":"name_one", "label":"Name One" }] This information is then sent to my form_directive which handl ...

Change the appearance of a specific div within a collection of div elements using React

I'm currently working on a visualizer for sorting algorithms where there are colorful bars that will animate when a specific sorting algorithm is triggered by clicking the play button. To achieve this, I created an array of div elements representing ...

Uncommon Two-Toned CSS Gradients

How can I create a button that matches the attached image using CSS? I know how to achieve the border-radius, but I'm struggling with the color. Any tips? I attempted something like this: .button { border-radius: 0 4px 4px 0; background-ima ...

Creating a scrolling background effect using HTML and CSS: ``Background Parallax

I'm trying to keep the background of my HTML document fixed in the upper left corner, so that when a user scrolls through the page, they only see parts of it. Here's the code I'm using: <body style="background-image: url(background.png) ...

jQuery Slider not appearing on the page

I am attempting to incorporate the Basic jQuery Slider into my website. Below is the code I have used: For HTML file: <div id="slides"> <ul class="bjqs"> <li><a href="somelink"><img src="someim ...

Is $ROW not defined?

I am encountering the error message: "Undefined variable: row" when reaching "return ($row);" in the code. Shouldn't $ROW be defined within $ROW[] = Array(...)? <?php function fetchImageState(){ $state = $_GET['state'] ...

Avoiding conflicts: Using Tabs with Revolution Slider without jQuery interference

I'm running into an issue with the tabs on my website. The revolution slider is working perfectly, but the tab widget seems to be displaying all the tab contents at once instead of each one separately. You can see the problem here: at the bottom of t ...

"Step-by-step guide on creating a web app with a transparent background while simultaneously running other processes

Would love to create a web application for real-time debugging similar to "android->developer options->show touch data" or "android->developer options->show CPU usage". Unfortunately, I have not been able to find a suitable method using HTML5 ( ...

Struggling to integrate buttons into an h2 element with the use of createElement() and appendChild() in HTML/CSS/JS

As I work on developing a website, one of the features I've been implementing is the ability for users to add books to a list and then review or delete them. The process was smooth sailing until I reached the point of adding buttons for these actions. ...

Retrieving the CSS property values for a specific element using Selenium

Imagine a scenario where I locate an element by its XPath using: WebElement element = driver.findElement(By.xpath("specific XPath")); While I can retrieve the value of a single CSS property with element.getCssValue("specific property"), is there a way to ...

designing items in various color palettes

section { background-color:#d5ecf2; width:1024px; height:200px; margin: 0 auto; } .sectiontext { float:right; height:180px; width:720px; margin:10px; } <section> <div class="sectiontext"> <h3>GDS 114.01: HTML and Javascript</h3 ...

The function 'find' cannot be invoked on an undefined object

I'm currently working on implementing objects in my jQuery code. So far, I have the following: var options = { ul: $(this).find('.carousel'), li: options.ul.find('li') } The li property is causing an error - Cannot call meth ...

Denied running the inline script in Redocly

Experimenting with redoc-cli (https://github.com/Redocly/redoc/) in order to generate static documentation. Unfortunately, encountered some stumbling blocks due to errors related to the Content-Security-Policy, which seems to be blocking JavaScript code ex ...

Responsive div not displaying background image

Here is a straightforward and easy-to-understand code snippet: HTML markup: <div class="jumbotron text-center top-jumbotron"> </div> CSS: .top-jumbotron { background: #ffcc00; background-image: url('../../bootstrap/img/home-pag ...

What is the best way to define ngOptionValue for my ng-option selection?

After updating my select/option code to include a search feature, it caused an issue with my function create. Here is the HTML code: <div class="input-group"> <label htmlFor="categoria" class="sr-only"> ...

Issues arise when attempting to determine the accurate dimensions of a canvas

Looking at my canvas element: <canvas id='arena'></canvas> This Element is set to fill the entire website window. It's contained within a div Element, both of which are set to 100% size. I attempted running this script: var c ...

Problem with the `file` input not working properly

Currently, I am working on creating a form with two input fields - one for text and the other for file upload. However, the issue I am facing is that the Browse button for the file input is being displayed inside the input box. My goal is to have the butto ...

Unable to identify the Xpath selector for the "Account settings button" and "find my iPhone" features within iCloud

When trying to access my iCloud account settings and locate the "Find my iPhone" button on iCloud.com, I am encountering issues with my selectors. It seems like there are no frames on the page, but the Xpaths I'm using are not matching. Here are the ...