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:
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!
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!
– SpammingOff
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
– luisaddor
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 =) !!!
– SpammingOff