Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position two forms next to each other centered over an image

Tags:

html

css

forms

I am pulling my hair out over the correct way to position two forms using html and css. I have tried both float and display:inline-block but only managed to get half of it working using one of the methods.

My target is to have the two forms display next to each other centered in a DIV that is only 70% of the page and each form takes up 50% of the available space. Both forms need to have a minimum width and should be pushed into separate lines if there isn't enough space to display both next to each other (i.e. when displaying the page on a phone in portrait mode)

If I float the two DIVs containing the forms they are displayed side by side but are not centered correctly (as they float either left or right and I need to set the size of each DIV to 40% or they don't fit next to each other). If I use display:inline-block the DIVs are in the correct size and centered but are in two separate lines and not next to each other.

Here is the current code using display: inline-block

#background {
  background-image: url(pic.jpg);
  height: 400px;
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  background-position: center;
}
#form-wrapper {
  width: 70%;
  margin: 0 auto;
  text-align: center;
}
#form1 {
  width: 50%;
  display: inline-block;
}
#form2 {
  width: 50%;
  display: inline-block;
}
<div id="background">
  <div id="form-wrapper">
    <div id="form1">
      <form>some form code here</form>
    </div>
    <div id="form2">
      <form>some form code here</form>
    </div>
  </div>
</div>

Why are the forms on different lines when using display:inline-block ?

like image 698
chipotle Avatar asked May 15 '15 18:05

chipotle


People also ask

How do I align two forms side by side?

style="float:left;" in the one and style="float:right;" in the other... The 2nd example will also center your both forms on the screen.

How do I make two divs sit next to each other?

With CSS properties, you can easily put two <div> next to each other in HTML. Use the CSS property float to achieve this. With that, add height:100px and set margin.

How do you put two pictures on top of each other in HTML?

Wrap the images in a <div> with the overlay image first and the actual image second, and can set the css of the div to position: relative . The two images can then be given the css {position: absolute; top: 0; left: 0;} . If you really want to be safe, you can set the z-index of each image. Save this answer.


1 Answers

You might be having trouble getting the two inline-block elements next to each other because 50% times two plus the white space between the elements is greater than 100% of the container. Therefore, the second element doesn't have enough space and wraps to the next line.

inline-block elements will respect white space in the HTML code. The white space between the two elements is demonstrated below:

#background {
  background-image: url(pic.jpg);
  height: 400px;
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  background-position: center;
}
#form-wrapper {
  width: 70%;
  margin: 0 auto;
  text-align: center;
}
#form1 {
  width: 40%;
  display: inline-block;
  background-color: #CCC;
}
#form2 {
  width: 40%;
  display: inline-block;
  background-color: #CCC;
}
<div id="background">
  <div id="form-wrapper">
    <div id="form1">
      <form>some form code here</form>
    </div>
    <div id="form2">
      <form>some form code here</form>
    </div>
  </div>
</div>

So, one solution to your issue is to remove the white space, as shown below.

I have also given each element a minimum width so that they wrap to separate lines when the window is below a specified width. To see this action, click the "Full page" button in the upper right corner and resize your browser window.

#background {
  background-image: url(pic.jpg);
  height: 400px;
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  background-position: center;
}
#form-wrapper {
  width: 70%;
  margin: 0 auto;
  text-align: center;
}
#form1 {
  width: 50%;
  display: inline-block;
  min-width:200px;
}
#form2 {
  width: 50%;
  display: inline-block;
  min-width:200px;
}
<div id="background">
  <div id="form-wrapper">
    <div id="form1">
      <form>some form code here</form>
    </div><div id="form2">
      <form>some form code here</form>
    </div>
  </div>
</div>
like image 153
showdev Avatar answered Sep 30 '22 05:09

showdev