5 same size elements, on the same line, with margin - html

I have an issue and I can't find the right keywords on Google.. But it seems prettry "classic".
I have a webpage, let's say with a max-width of 1500px;
I want to add a line, with 5 "boxes" (div) of the same size each, separated with a margin.
So I set a width of 20%, and a margin-right of 10px. My issue is that my last div always goes down to the next line, because of the margin. (Because with the margin, the width of my line is higher than the max-width of the page).
If I remove the margin, all the boxes are correctly on the same line.
What should I do to make it work ? (Except using outerWidth of jQuery, it is my next step if I can't do it easily with css)
Here is my code the code I have now :
<div id="page">
<div id="numbers">
<div class="numberwrap">
<div class="number">
Number
</div></div>
<div class="numberwrap">
<div class="number">
Number
</div></div>
<div class="numberwrap">
<div class="number">
Number
</div></div>
<div class="numberwrap">
<div class="number">
Number
</div></div>
<div class="numberwrap">
<div class="number">
Number
</div></div>
</div>
</div>
#page
{
max-size: 500px;
background-color:grey;
}
.number
{
background-color:white;
}
.numberwrap
{
float:left;
width:20%;
padding-right:10px;
}
I also made a fiddle, to test : http://jsfiddle.net/jKMp5/
Thank you !
Solution : I just have to set the padding property on the .number, not the wrapper !
Or use box-sizing !
Thanks to everybody

Div's with a width percentage adds margins and paddings width on to that.
Meaning a div with width 50% amd margin-right: 20px; will be 50% + 20px.
You can do the following.
<div style="width: 20%;">
<div style="margin-right: 20px;"></div>
</div>
That will sort it out.
or just the following
.number
{
background-color:white;
padding-right:10px;
}
.numberwrap
{
float:left;
width:20%;
}

The problem is (as you already said) that the margin is affecting to each div making it bigger than that 20%, so one solution could be to tell to that div that the margin is included in the total width with the property box-sizing
So add:
.numberwrap {
box-sizing: border-box;
}
See jsFiddle example: http://jsfiddle.net/jKMp5/2/

In the default box modal,
The padding area extends the content area with the empty area between the content and the eventual borders surrounding it.
You can change this behavior using box-sizing property by applying box-sizing:border-box
border-box:
The width and height properties include the padding and border, but not the margin.
.numberwrap
{
box-sizing:border-box;
/*other styles*/
}
Demo

You can use disaply:table and display:table-cell:
css
#page
{
max-size: 500px;
background-color:grey;
}
.number
{
background-color:white;
}
.numberwrap
{
float:left;
width:20%;
padding-right:10px;
display:table-cell;
/*border: 1px solid black;*/
}
#numbers{
display:table;
}
fiddle

Related

Inline-block - Alignment Issue When Browser Resized

I have a max-width wrapper, within it are four boxes of a fixed width and height. When the browser window is reduced, the boxes fall inline and centered within the container, but the alignment is slightly out, as if there's a small left margin.
Here is a jsfiddle: http://jsfiddle.net/ve2fyrh7/
If I remove "text-align:center;" from #boxcontainer it clears up the alignment issue, but then the boxes won't be center-aligned inside the container which I require when the browser window is reduced.
If I remove ".box:last-child" then that also solves the alignment issue, but then all four boxes won't be inline when the browser window is >960px.
I appreciate any help/suggestions on where I'm going wrong.
CSS
#wrapper {
max-width:960px;
margin:0px auto 0px auto;
}
#boxcontainer {
width:100%;
background:#f8f8f8;
font-size:0;
text-align:center;
}
.box {
width:225px;
height:200px;
display:inline-block;
background:#ccc;
font-size:18px;
margin:0px 20px 20px 0px;
}
.box:last-child {
margin-right:0px;
}
HTML
<div id="wrapper">
<div id="boxcontainer">
<div class="box">
<h3>Test</h3>
</div>
<div class="box">
<h3>Test</h3>
</div>
<div class="box">
<h3>Test</h3>
</div>
<div class="box">
<h3>Test</h3>
</div>
</div>
</div>
Add float:left to your box class.
If you want to solve this on a specific media device use media query.
The margin-right:0px; on the the last div is causing the spacing issue. If i remove that, it seems to be fine. http://jsfiddle.net/ve2fyrh7/2/.

Wrap child elements that have% as margin with inline-block

I want some elements on one line and get their parent to an equal size to the kids, the only problem is when I put % margin on the children, then they do not fit
It works with pixels in margin instead but I need to use % in this case to make it work responsively
jsfiddle: http://jsfiddle.net/
html:
<div class="holder">
<div class="blue">
</div>
<div class="red">
</div>
<div class="green">
</div>
</div>
css:
.holder
{
background-color:yellow;
display:inline-block;
}
.holder > div
{
width:200px;
height:200px;
display:inline-block;
margin-left:2%;
}
.blue
{
background-color:blue;
}
.red
{
background-color:red;
}
.green
{
background-color:green;
}
update below css. check link jsfiddle
.holder
{
background-color:yellow;
display:inline-flex;
}
It wouldn't...If you use inline-block then the holder div collapses to the width of the contents. So 2% would be OK but if you add in the 6% (3 * 2%) then the holder gets wider and the margin would then be increased...and so on and so on ad infinitum.
What happens is the holder div keeps it's original size (since it can't be calculated due to the infinity margin loop) as though the 'extra space' wasn't applied but adds the margin in anyway (once) and the layout breaks.
SEE - http://jsfiddle.net/19s19oL7/
AS mentioned by Ankush Kondhalkar flexbox is a solution although support is relatively limited at present. In the interim, use px and media queries.

CSS not behaving, 2 columns and 1 row spanning both columns

I never have any luck with CSS. I have a dialog which is displayed when the user clicks a button. In this div there is an image, and 2 other divs (containing more elements).
It should all look like the following.
The second columns which contained "Applied Filters" should be on the right of the image, and span the width between the end of the image, and the end of the main container. But instead, it has gone under the image.
See JSFiddle here
Here is the HTML:
<div class="filter_dialog">
<img src="data/images/20140206/0/emma-watson-hot-43.jpg" width="550px" />
<div class="applied_filters">
<p>Applied Filters</p>
</div>
<div class="filters">dsadsa</div>
</div>
CSS:
.filter_dialog {
background-color:#333333;
border:solid 1px #666666;
display:inline-block;
margin:0 auto;
border-radius:15px;
}
.filter_dialog img {
border-radius:15px 15px 0px 0px;
float:left;
}
.applied_filters {
float:left;
background-color:#1a1a1a;
width:100%;
}
.filters {
background-color:#1a1a1a;
height:8em;
width:100%;
border-radius:0px 0px 15px 15px;
display:inline-block;
}
You'll need to set a fixed width on the two filters divs. I'd suggest wrapping them in one div with the fixed width, set to float: right, and then float the image left.
You'll need to define widths on the .filter-image and .applied-filter DIVs. See this JSFiddle for an example:
<div class="filter-dialog">
<div class="filter-container">
<div class="filter-image">
<img src="http://aerodesign.okstate.edu/projects/kittyhawk%20page/A_dragon.png" />
</div>
<div class="applied-filters">applied</div>
</div>
<div class="filter-options">options</div>
</div>
100% width means 100% width of the nearest relative/absolute positioned PARENT so no that is not the way to do it.
Solutions:
A) The width of the "Applied Filters" and the image-width are always the same => add it a proper width so it could fit next to each other. could be done like:
img{ max-width: 75% } /*or proper px*/
.filterClass{ width: 24%; } /*or proper px*/
B) The image size is not fixed => use a javascript framework like jQuery to count the current image size and change the .filterClass's width like $('.filterClass').css('width', countedWidth);

Why won't div center w/ auto margins?

I'm at a complete loss here. I've tried everything. Different displays, positions, floats... Nothing seems to work:
Markup:
<div id="today-leads" class="total-wrap">
<h2>Leads</h2>
<div id="leads-wrap">
<div id="leads-total" class="total">224</div>
<div id="leads-ticker-wrap"> <i class="icon-caret-up"></i>
<div id="leads-percentage-change" class="higher">56%</div>
</div>
</div>
CSS:
h2 {
text-align:center;
}
#leads-wrap{
display:inline-block;
margin: 0 auto;
}
#leads-ticker-wrap{
float:left;
}
#leads-total{
float:left;
}
Here's the jsfiddle
Which div?
leads-wrap won't centre because it is an inline-block not a block, so it is influenced by the text-align property of its containing block.
leads-ticker-wrap and leads-total won't centre because (a) they are floating left and (b) you haven't set auto margins on them.
Auto left/right margins only work when the element in question has a known width. So add something like
#leads-wrap
{
width: 65%;
}
Obviously you adjust the actual width to whatever you need.

div with 100% height in dynamicly sized element

To describe my item- I have a div element which grows in size if more content is added- also the parent div grows in size as it should be.
Then I have 2 div elements one floated left and the other on the right. I set height to 100% but that don't work. How could I achieve such behavior?
http://109.91.124.194/nucleareducated_2/index.php
This is a typical problem that unfortunately does not have a simple solution. Do a quick Google search for equal height columns and you will see what I mean. Your code is not working because height:100% does not work unless the parent container has a specified height to calculate what 100% is. Without a specified height set, then height:100% defaults to height:auto and you end up with divs that do not expand to the size of the parent.
As you have probably guessed, it's pretty hard to set the height of the parent div because it changes. The answers include setting the height dynamically with Javascript, or more often than not, just faking it so that it appears that the columns expand to the size of the parent.
IMO the best way is to use the css table attribute if you only care about newer browsers, it does not work on IE7 or older.
#containerdiv {
display:table;
}
#childdivleft, #childdivcenter, #childdivright {
display: table-cell;
}
The next best is to use large values for padding and a corresping negative margin on the bottom of the child containers.
#leftdiv,#rightdiv {
padding-bottom: 32767px;
margin-bottom: -32767px;
}
You can also use -
jQuery - Columns of Equal Height with JQuery
several other solutions - CSS - Equal Height Columns?
let me know if this is what you are looking for: tested in chrome*
<html>
<head>
<style>
html, body {
height:100%;
}
</style>
</head>
<body>
<div style="clear: both; height:100%;">
<div class="pageShadow" style="background-color: blue; height: 100%; width: 47px; float: left;"></div>
<div class="pageShadow" style="background-color: blue; height: 100%; width: 52px; float: right;"></div>
<div class="pageShadow" style="background-color: green; margin-left: 47px; margin-right: 52px;"></div>
</div>
</body>
</html>
Maybe this is what you mean? You can add a div around the left and right div if you want an area with 100% height.
CSS:
* { margin:0; padding:0 }
#around { background:yellow; float:left }
#left { float:left; width:40%; background:red }
#right { float:right; width:60%; background:green }
#bottom { clear:both; background:blue }
HTML:
<div>
<div id="around">
<div id="left"></div>
<div id="right"></div>
</div>
<div id="bottom"></div>
</div>