Divide 3 divs in 3 columns without using float and flex - html

I tried by using display inline-block to achieve 3 columns but 3rd column comes at separate row:
.wrapper {
width: 100%;
}
.column {
display: inline-block;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}
<div class="wrapper">
<div class="column">abc</div>
<div class="column">def</div>
<div class="column">ghi</div>
</div>
Not able to figure out the reason.

I dont know this is exactly what you need , i have remove the default whitespace of the inline-block using font-size:0 and add box-size property you dont need to change the width 33.3% to 33% the width please check the snippet
.wrapper {
width: 100%;
font-size: 0;
}
.column {
display: inline-block;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
margin-bottom: 8px;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-ms-box-sizing: border-box;
-moz-box-sizing: border-box;
}
<div class="wrapper">
<div class="column">abc</div>
<div class="column">def</div>
<div class="column">ghi</div>
</div>

By Default inline-block count space as a element. You can do this in two different ways:
Method (Using Font Size)
*,*:after,*:before {
box-sizing: border-box;
}
.wrapper {
width: 100%;
font-size:0px;
}
.column {
display: inline-block;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
/*min-width: 300px;*/
margin-bottom: 8px;
font-size:16px;
}
<div class="wrapper">
<div class="column">abc</div>
<div class="column">def</div>
<div class="column">ghi</div>
</div>
Method Removing extra Space
*,*:after,*:before {
box-sizing: border-box;
}
.wrapper {
width: 100%;
}
.column {
display: inline-block;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
/*min-width: 300px;*/
margin-bottom: 8px;
}
<div class="wrapper">
<div class="column">abc</div><!--
--><div class="column">def</div><!--
--><div class="column">ghi</div>
</div>
As per your comment you want margin-right:5px and achieve same thing. for this you can use width in calc format. check below snippet
*,*:after,*:before {
box-sizing: border-box;
}
.wrapper {
width: 100%;
}
.column {
display: inline-block;
min-height: 150px;
width: calc(33.33% - 5px);
width: -moz-calc(33.33% - 5px);
width: -webkit-calc(33.33% - 5px);
border: 1px solid black;
/*min-width: 300px;*/
margin-bottom: 8px;
margin-right: 5px;
}
<div class="wrapper">
<div class="column">abc</div><!--
--><div class="column">def</div><!--
--><div class="column">ghi</div>
</div>

Use Table for it, Best Option
No width, No Float
table{table-layout:fixed;width:500px;border-collapse:collapse;text-align:center;}
<table border="1">
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</table>

By default browser renders a gap between two div which are positioned inline.
Making the parent width:100%, and children width:33.33% will not make children fit in the parent, because DOM calculates the gap between child div.
To make the child div fit the parent, you need to modify the width smaller than 33.33%.
If you still want to use 33.33% width. Try this
Link For reference
Unexpected gap between div inline-block
hope this helps..

One simple way to solve this issue is to set the .wrapper to display: table; and set its children to display: table-cell;. See the example below for the outcome.
.wrapper {
width: 100%;
display: table;
}
.column {
display: table-cell;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}
<div class="wrapper">
<div class="column">abc</div>
<div class="column">def</div>
<div class="column">ghi</div>
</div>

If you are working in bigger screen like (1600,1920) then please use media query. And nothing to do any changes in margin and other as well.
Please update this property
For < 1366 screen resolution use this css
width:32.90% //instead of 32.90% you may use width: 32%;
For > 1366 width:33% is working
in .column class
width: 33%;
So column class look like
.column {
display: inline-block;
min-height: 150px;
width: 33%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}
Because it's 1px border surrounded to div.And their is default Margin:8 to body tag.
Width:33% is not working in 1366 screen resolution. So here you must need to use width:32.90% either 32%

Do change in Width and add Box-Sizing will help you.
.column {
display: inline-block;
min-height: 150px;
width: 33.1%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
box-sizing: border-box;
}

Try keeping the div in one line in your editor.
<div class="wrapper">
<div class="column">abc</div><!--
--><div class="column">def</div><!--
--><div class="column">ghi</div>
</div>

You can use flex, flex is very good to make responsive div. It's a simple example.
--- HTML ----
<div class="flex-wrapper">
<your-element class="item"> 1 </your-element>
<your-element class="item"> 2 </your-element>
<your-element class="item"> 3 </your-element>
</div>
--- CSS ---
.flex-wrapper {
display: flex;
flex-wrap: wrap; // if you want to lots of items, will be wrapped
}
.item {
width: 33.3%;
}
and if your items have margin, you can calculate that like this.
.item {
margin: 5px;
width: calc(33.3% - 10px);
}
Example :
I hope will be helped you.

Use the following properties to your column class
.column {
display: inline-block;
min-height: 150px;
width:32%;
float:left;
margin-left:4px;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}
Reason
float:left; property will align your div's on left hand side
width:32%; property will give width of 32 % instead of 33.33% as
1px you are assigning to border, which needs to be accommodated from
the width itself.
margin-left:4px; property will add spacing between divs, this is
for nicety.

Your column class needs a couple of minor fixes, float and width changes:
.column {
display: inline-block;
float: left;
min-height: 150px;
width: 33%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}

Related

Why margin property is not working in the "container3" after using "clear" property (css)?

All 3 containers that you see in the attached file are inside one main container. I used "float: left" for the 1st two containers and "clear: both" property to the 3rd.
But, it seems like after applying "clear" property to the third container, margin-top isn't working.
Please help me with the following questions :
Why is this happening?
How can I fix this?
Thanks!
Code :
div#insideContainer1
{
max-width: 500px;
padding: 20px;
box-sizing: border-box;
border: 1px solid #e0e0e0;
border-radius: 5px;
float: left;
}
div#insideContainer2
{
max-width: 500px;
padding: 20px;
box-sizing: border-box;
border: 1px solid #e0e0e0;
border-radius: 5px;
margin-left: 20px;
float: left;
}
div#insideContainer3
{
max-width: 500px;
padding: 20px;
box-sizing: border-box;
border: 1px solid #e0e0e0;
border-radius: 5px;
margin-top: 30px;
clear: both;
}
CSS Output Screenshot
CSS Code Screenshot
Alright then,
1- When clear: both; a div, you clear what's beside it on left and right, so div#3 clear is doing nothing...
(for example: if you give both div#1 and div#2 clear: both; each div will take it's own block and it ruin the float: left;
2- To break the float in this case, you don't need clear" both; in any of these divs, it'll be a div itself to clear and sperate!
3- Since the divs has the same CSS style, we should select all at once (read more: CSS Selector Reference).
HTML:
<div class="container">
<div class="div1"></div>
<div class="div2"></div>
<div class="clear-both"></div>
<div class="div3"></div>
</div>
CSS:
.div1,
.div2,
.div3{
width: 500px;
padding: 20px;
box-sizing: border-box;
border: 1px solid #e0e0e0;
border-radius: 5px;
background-color: #f40b0b;
margin: 10px;
}
.div1, .div2 {
float: left;
}
.div3 {
background-color: aqua;
}
.clear-both {
clear: both;
}

Little help aligning flexboxes

I'm trying to build a tooltip for my items using flexboxes to align content inside them.
Here is a little image of how I imagined it would look:
here is my CSS code attempt of the image above:
.hud-tt-container {
display: flex;
width: 100%;
height: auto;
box-sizing: border-box;
}
.hud-tt-info-container {
width: auto;
height: 64px;
flex-grow: 1;
border: 1px solid yellow;
box-sizing: border-box;
}
.hud-tt-info-block {
width: auto;
height: 32px;
border: 1px solid grey;
box-sizing: border-box;
}
.col-full {
width: 100%;
height: 32px;
float: left;
border: 1px solid gray;
box-sizing: border-box;
}
.col-half {
width: 50%;
height: 32px;
float: left;
border: 1px solid gray;
box-sizing: border-box;
}
.hud-tt-lv-container {
width: 64px;
height: 64px;
flex-grow: 0;
border: 1px solid blue;
box-sizing: border-box;
}
<div class="hud-tooltip f16 fwhite">
<div class="hud-tt-container">
<div class="hud-tt-info-container">
<div class="col-full"></div>
<div class="col-half"></div>
<div class="col-half"></div>
</div>
<div class="hud-tt-lv-container">
<canvas id="Bar"></canvas>
</div>
</div>
</div>
I'm currently using col class to arange them this way. This way it also works, but I want to use flexboxes for this, so to use hud-tt-info-block
The level and info container align just as I want them to. Level container takes 64x64px, while info container takes all the space left. But I'm not sure how to align info blocks the way i want them to be using flexbox
Using flexbox to align col classes is very important due to large numbers. Flexbox can adapt width as numbers get bigger, whereas my current solution doesn't work well with large numbers
You can nest flex elements, where adding display: flex to the .hud-tt-info-container being a flex item, it also becomes a flex container, and its children becomes flex items and so on.
See notes in CSS
.hud-tt-container {
display: flex;
}
.hud-tt-info-container {
flex-grow: 1; /* fill remaining space */
display: flex; /* added */
flex-wrap: wrap; /* added, allow items to wrap */
height: 64px;
}
.hud-tt-info-block {
height: 32px;
border: 1px solid gray;
box-sizing: border-box;
}
.col-full {
flex-basis: 100%; /* changed, take full width and push
the other items to a new row */
}
.col-half {
flex-basis: 50%; /* changed */
}
.hud-tt-lv-container {
width: 64px;
height: 64px;
border: 1px solid blue;
box-sizing: border-box;
}
<div class="hud-tooltip f16 fwhite">
<div class="hud-tt-container">
<div class="hud-tt-info-container">
<div class="hud-tt-info-block col-full"></div>
<div class="hud-tt-info-block col-half"></div>
<div class="hud-tt-info-block col-half"></div>
</div>
<div class="hud-tt-lv-container">
<canvas id="Bar"></canvas>
</div>
</div>
</div>

centering divs on the same line

I'm trying to center these 3 floated divs on the same line. Here is a link to jsfiddle:
https://jsfiddle.net/dtps4fw8/2/
any suggestions?
HTML:
<div class="content">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
CSS:
.box {
width: 30%;
height: 200px;
float: left;
background: gray;
border: black solid 2px;
box-sizing: border;
margin: 5px;
}
See this fiddle
To make the 3 divs centered, first of all, remove the floatproperty and then to apply the floated effect, use display:inline-block. inline-block display gives a textual characteristics to the div. A text-align:center for the parent div would center these inline-block elements inside the parent.
Update your CSS as follows
.box {
width: 30%;
height: 200px;
display: inline-block;
background: gray;
border: black solid 2px;
box-sizing: border;
margin: 5px;
}
.content {
text-align: center;
}
First the float:left; is not relevant in your case, just like Lal said, instead of float:left; its should be display:inline-block; and you can also add a relative positioning position:relative;
I use flexbox. Very minimal and responsive.
.content {
width:100%;
display: flex;
flex-direction:row;
flex-wrap:wrap;}
.box {
height: 200px;
flex:1;
background: gray;
border: black solid 2px;
box-sizing: border;
margin: 5px;}

How do I make this html two columns of equal width with a margin in between?

I have some HTML that I cannot change, but I can change the CSS as much as I want. I need to make these:
two columns of equal width
A margin in between of 2em
They have to take all the remaining width (parent width - 2em)
The boxes need to have a padding inside
HTML:
<div class="parent">
<a href="/page1" class="box">
<img class="pic" src="/images/image1.png">
<div class="description">the description</div>
</a>
<a href="/page2" class="box">
<img class="pic" src="/images/image2.png">
<div class="description">the description</div>
</a>
</div>
I'm able to do it without any spacing between them with: box-sizing: border-box; but if I add in a margin-right, they no longer fit.
Give this a try : It makes use of the Calc() function in css.
Note: The border throws off the calculation a bit, so you will have to adjust the calc slightly. I just did it to show you how the boxes were laid out.
.parent {
position: relative;
width: 600px;
height: 300px;
border: 1px solid red;
}
.box {
border: 1px solid black;
width: calc(50% - 1.25em);
display: inline-block;
}
.box:first-child {
margin-right: 2em;
}
Fiddle
I was able to solve it:
.parent .box
{
position: relative;
width: 50%;
margin: 0em;
padding: 1em;
float: left;
font-size: 1em;
overflow: hidden;
z-index: 0;
display: block;
text-decoration: none;
box-sizing: border-box;
}
.parent .box:nth-child(odd)
{
border-right: solid 1em #ffffff;
}
.parent .box:nth-child(even)
{
border-left: solid 1em #ffffff;
}
The n-th child lets me add the spacing between them

'rogue' div is offsetting its siblings

Basically I have a set of divs that are pretty much identical in structure, then following them I have a div that is unique to the set. Basically the first divs are a bunch of different categories and the last div is a userprofile type square. What I can't figure out is why the user profile square is being rendered with a higher position than the other divs.
they all have the same css
cursor: pointer;
display: inline-block;
margin: 10px;
padding: 5px 5px 10px 5px;
width: 219px;
height: 219px;
background: #fff;
and the container has this css
display: block;
float: left;
width: 100%;
text-align: center;
padding: 15px;
white-space: nowrap;
overflow-x: auto;
overflow-y: hidden;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-o-box-sizing: border-box;
-ms-box-sizing: border-box;
this is what it looks like
I'm guessing it's because the divs internal structure is different, but I'm not sure why it's doing this. I also noticed that if for example one of the category divs' images do not load it behaves the same way as my rogue div.
Any light there is to be shed on this issue is much appreciated.
With display: inline-block; it's best to always add vertical-align: top; to the children (and then format from there as needed), especially if you have different element types or images in your container. Even images inside of your child elements can mess up the layout of inline-block;. inline-block elements also suffer from the "whitespace problem", which can affect layout. To prevent that you can either put all child elements together or comment out the whitespace.
Demo: http://jsfiddle.net/ThinkingStiff/wwwkJ/
HTML:
<div id="container">
<div class="child">one</div>
<div class="child">two</div>
<img class="child" />
</div>
<div id="container-align">
<div class="child-align">one</div>
<div class="child-align">two</div>
<img class="child-align" />
</div>
<div id="container-align-whitespace">
<div class="child-align">one</div><!--
--><div class="child-align">two</div><!--
--><img class="child-align" />
</div>
CSS:
.child {
border: 1px solid red;
display: inline-block;
height: 50px;
width: 50px;
}
.child-align {
border: 1px solid red;
display: inline-block;
height: 50px;
vertical-align: top;
width: 50px;
}
#container, #container-align, #container-align-whitespace {
border: 1px solid black;
height: 100px;
width: 300px;
}
Output: