I want to float a pair of fluid divs across my page, each taking up half of their container's width, but with a margin of 10px between them. I've done this JSFiddle http://jsfiddle.net/andfinally/sa53B/5/, and here's the HTML:
<div class="clearfix">
<a class="prev" href="#">Previous</a>
<a class="next" href="#">Next</a>
</div>
And CSS:
.prev {
background: black;
color: white;
font-size: 16px;
margin-bottom: 10px;
display: block;
float: left;
box-sizing: border-box;
width: 50%;
margin-right: 5px;
}
.next {
background: black;
color: white;
font-size: 16px;
margin-bottom: 10px;
display: block;
float: right;
box-sizing: border-box;
width: 50%;
margin-left: 5px;
}
The box-sizing rule isn't enough to make this work - the divs are more than 50% wide. In one of the answers to this question somebody suggested using CSS display-table. Can anyone explain how to make that work in this situation?
Thanks!
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.
Use the padding css property of the container (div) instead. Padding is the space inside and border of the element, while margin is the space outside the border of the element. Check out box model.
To position the divs side by side, we are using the float property to float each . float-child element to the left. Since they are both floating to the left, they will display side by side if there's enough space for both to fit.
html - Div still has margin even with margin: 0; - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
You can either a) lower 50%
to 48%
and make the margin 2%
or b) use CSS3 calc
, which is not supported everywhere, but should be an option in the near future. (Specifically, when IE8 is off the table) (See http://caniuse.com/#feat=calc for compatability)
Example using percentages: http://jsfiddle.net/sa53B/9/
.prev {
background: black;
color: white;
font-size: 16px;
display: block;
float: left;
box-sizing: border-box;
width: 48%;
margin: 0 2% 10px 0
}
.next {
background: black;
color: white;
font-size: 16px;
display: block;
float: right;
box-sizing: border-box;
width: 48%;
margin: 0 0 10px 2%
}
Example using calc
: http://jsfiddle.net/sa53B/6/
.prev {
background: black;
color: white;
font-size: 16px;
display: block;
float: left;
box-sizing: border-box;
width: 47%;
width: -webkit-calc(50% - 5px);
width: calc(50% - 5px);
margin: 0 5px 10px 0;
}
.next {
background: black;
color: white;
font-size: 16px;
display: block;
float: right;
box-sizing: border-box;
width: 47%;
width: -webkit-calc(50% - 5px);
margin: 0 0 10px 5px;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With