Center element in the middle of two float elements

Asked

Viewed 1,759 times

5

I am developing a site that, in a given DIV, has three elements with defined width and height. These three elements are stylized according to the following rules:

.blocks-h{
    width: 320px;
    height: 150px;
    background: #DEDEDE;
    margin: auto;
}

It so happens that, of these three elements, one must be positioned to the left, one to the center and one to the right. To do this I set a style responsible for aligning left or right any elements of my page:

.left{
    float: left;
}

.right{
    float: right;
}

My HTML is as follows:

<div class="blocks-h left"></div>
<div class="blocks-h"></div>
<div class="blocks-h right"></div>

This is the result I’m getting with the rules described above: Resultado

As you can see, the div positioned on the right is being "pushed" down. This is happening because the block style has no definition of flotation. And you can’t have it, because it doesn’t make sense, semantically speaking, to set float to an element that shouldn’t "float". This is why I created the classes . left and . right.

If I change the order of the Divs in my HTML, I have the expected result:

<div class="blocks-h left"></div>
<div class="blocks-h right"></div>
<div class="blocks-h"></div>

But, again, I will be writing a code whose semantics make no sense, since the second element displayed on the screen is the third that is defined in HTML.

Anyway. Is there any way to centralize an element that is in the middle of two others that "float", but that is correct?

I would not like to set margin for these blocks because it will make my life difficult when I create the rules of site responsiveness. And this is not the whole problem because, just below these blocks, I will have another div with FOUR elements that should be centered in the same way.

Can you help me? Thank you!

1 answer

2


Friend, the theory applied on top of such boxes is the same applied in grids responsive. I made a basic example:

HTML:

<div class="mae" >
   <div class="f1" > </div>
   <div class="f1" > </div>
   <div class="f1" > </div>
</div>

CSS:

.mae {
   margin-left:-5px;
   width:200px; background:#000;
   float:left;
}

.f1 {
   float:left;
   width: calc(33.333% - 5px);
   height:50px;
   background:#e1e1e1;
   margin-left:5px;
}

.mae {
  margin-left: -5px;
  width: 200px;
  background: #000;
  float: left;
}

.f1 {
  float: left;
  width: calc(33.333% - 5px);
  height: 50px;
  background: #e1e1e1;
  margin-left: 5px;
}
<div class="mae">
  <div class="f1"></div>
  <div class="f1"></div>
  <div class="f1"></div>
</div>

The negative margin on the mother div is to compensate for the margin-left of the first div f1, so even already solve your problem in the issue of responsiveness (as it can set a fixed width for the mother div or in percentage and align it with margin: 0 auto; for example). The only thing you will define is the distance you want to have between the Divs.

I hope I’ve helped.

  • So man... what I didn’t want was to have to define this distance between the Ivs. But it seems that there is no better solution to this problem than the one you put here. Thank you!

  • 1

    Dude, I didn’t test it, but we can try to put the margin in percentage too! So everything is relative even, both the size and the margin, you will only need to put both in the mother div and in the F1 div a height:auto, or min-height:x, with padding... so everything is relative including the content

  • Yeah. That’s what I’m gonna do anyway. The problem with height:auto is that the parent element has a somewhat idiotic behavior and does not show the correct height, but nothing that a clear does not solve =) !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.