Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to align two divs side by side when one has margin auto

Tags:

html

css

margin

I have a div that has margin: auto so it can float in the center of the page. This page also has a zoom feature that zooms in on that element and I want there to be a min-margin-left on that element.

I have tried creating a new element that has a fixed width, but when I use float: left on both of those elements it doesn't work, and the one that has a fixed width (blue) floats on top of the other one (red).

I tried looking at other questions, but they do not deal with a div that has margin: auto, and a question about min-margin doesn't deal with two elements.

Here's my current code:

.main_field {
  width: 1000px;
  margin: 20px auto;
  height: 100px;
  border: none;
  display: inline-block;
  padding: 75px;
  background-color: red;
}
<div style="background-color: blue; height: 100px; display: inline-block; width: 50px;"></div>
<iframe class="main_field "></iframe>
like image 497
Andrei Avatar asked Aug 12 '19 23:08

Andrei


People also ask

How do I align two divs side by side?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I align two divs on the same line?

To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values. The value left indicates the div tag will be made to float on the left and right to float the div tag to the right.


1 Answers

The problem is the display - to make your centering work you need the element to be a block:

.papertextfield {
  width: 200px;
  margin: auto;
  background-color: red;
  display: block;
}
<iframe name="textfield" id="textareathingy" class="papertextfield"></iframe>

To fake a minimum margin you can use a container with padding:

.papertextfield {
  width: 200px;
  margin: auto;
  background-color: red;
  display: block;
}

.container {
  padding-left: 60px;
  padding-right: 60px;
  background-color: blue;
}
<div class="container">
  <iframe name="textfield" id="textareathingy" class="papertextfield"></iframe>
</div>
like image 94
kabanus Avatar answered Sep 20 '22 23:09

kabanus