css columns and content - html

This probably is the most frequently asked question however I am not really able to find anything that seems to work for me - I am a newbie # styling/css.
I would like a cross browser compliant solution (IE9 or higher btw) where I can have as a header (first row) 3 columns:
Name_____DOB______Contact
underneath, there will be content. a row of data and I want the row of data (which is contained in a div) to be aligned correctly with the column headings.
Currently I have this:
<div style="width: 100%">
<div style="width: 300px; float:left">Name</div>
<div style="width: 200px; float:left">DOB</div>
<div style="width: 100px; float:left">Contact</div>
</div>
in terms of the data, I am using MVC4 with Razor so I am just using a for loop to go through a collection of data and spitting it out in a div i.e:
[for loop here]
<div id="refitem_#counter">
[data here]
</div>
[end for loop]

You can continue with the same approach.
<div>
<div style="width: 100%">
<div style="width: 300px; float:left">Name</div>
<div style="width: 200px; float:left">DOB</div>
<div style="width: 100px; float:left">Contact</div>
</div>
<div style="width: 100%">
<div style="width: 300px; float:left">Name 1</div>
<div style="width: 200px; float:left">DOB 1</div>
<div style="width: 100px; float:left">Contact 1</div>
</div>
<div style="width: 100%">
<div style="width: 300px; float:left">Name 2</div>
<div style="width: 200px; float:left">DOB 2</div>
<div style="width: 100px; float:left">Contact 2</div>
</div>
..
</div>
I'm strongly dis-advise you to use that. use a table instead.
also (as a BTY): don't mix your markup and CSS, use something like that.
HTML:
<div>
<div>
<div class="column col1">Name</div>
<div class="column col2">DOB</div>
<div class="column col3">Contact</div>
</div>
<div>
<div class="column col1">Name 1</div>
<div class="column col2">DOB 1</div>
<div class="column col3">Contact 1</div>
</div>
<div>
<div class="column col1">Name 2</div>
<div class="column col2">DOB 2</div>
<div class="column col3">Contact 2</div>
</div>
..
</div>
CSS:
.column
{
float: left;
}
.col1
{
width: 300px;
}
.col2
{
width: 200px;
}
.col3
{
width: 100px;
}
width: 100%; is the default behaviour for a block element (like a div) so you should omit it.

Related

Div with absolute width is smaller than specified [duplicate]

This question already has an answer here:
flex items ignoring width
(1 answer)
Closed 1 year ago.
I am trying to have a number of columns with exact widths, and their heights split evenly between some number of elements. For some reason, despite my indicating an exact 200px width on each column, they are instead getting a computed width of 162px somehow.
Chrome dev tools is showing some weird arrow thing indicating that it it was shrunk from it's intended size for some reason. I've even tried removing all of the content from the div's as possible so as to rule out some weird interaction with the size of children.
The HTML content for the relevant area is this:
div {
background: rgba(255, 0, 0, .1);
}
<div style="display: flex;">
<div style="width: 200px; margin-right: 100px;">
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 200px;"></div>
<div style="height: 200px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 400px;"></div>
</div>
</div>
Including some dev-tools highlighting (showing the arrow thing I described) it is rendering like this (the "round" labels at the top are not in the HTML content above but are properly 200px + 100px margin):
I have never seen anything like this before, especially those arrow things from the dev tools. Is there something obvious I'm missing or something I should look for to diagnose this?
Setting display: flex turns the sizing of child elements over to the flex container. If you don't want the individual elements to resize, set flex-grow: 0, flex-shrink: 0, and flex-basis: 200px. You can do all three using the flex shorthand:
flex: 0 0 200px;
.container {
display: flex;
}
.container > * {
flex: 0 0 200px;
margin-right: 100px;
}
div {
background: #cccccccc;
}
<div class="container">
<div style="width: 200px; margin-right: 100px;">
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 200px;"></div>
<div style="height: 200px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 400px;"></div>
</div>
</div>
This is the default behaviour for Flexbox. If you add up all your widths, so 200 width + the 100 margin, you get 300 * 4 = 1200px. If your viewport is smaller than 1200px then the browser will try to calculate the best width it can to fit all your div along the main axis. thus you are getting 162 + 100 * 4 is just shy of 1200. Try resize your viewport or the browser screen to bigger than this and you should get the expected behaviour.
The arrow you are seeing is Chrome dev tools way of telling you your original width has been made smaller to fit all content.

Make flexbox item width equal to combined width of brother elements

I have a row of fixed width flex items, and a footer element that should span the width of the combined elements above. How can I go about doing this?
Here is what I have so far, however, the bottom row is wider than the width of the first row.
<div class="container">
<div class="container" style="flex-basis: 100%">
<div style="width: 50px;">Div 1</div>
<div style="width: 50px;">Div 2</div>
<div style="width: 50px;">Div 3</div>
</div>
<div style="width: 100%">Div 4</div>
</div>
.container {
display : inline-flex;
flex-flow: row wrap;
}
.container > div {
border: 1px solid black;
background: #ececec;
}
https://jsfiddle.net/y1kham4u/1/
you better reset flex-direction than flex-wrap:
.container {
display: inline-flex;
flex-flow: column;/* no wrapping needed */
}
.container .container {
flex-direction: row;/* reset */
}
.container>div {
border: 1px solid black;
background: #ececec;
}
* {
box-sizing: border-box;
}
<div class="container">
<div class="container" style="flex-basis: 100% /* not really needed , but does not hurt */">
<div style="width: 50px;">Div 1</div>
<div style="width: 50px;">Div 2</div>
<div style="width: 50px;">Div 3</div>
</div>
<div style="width: 100%">Div 4</div>
</div>
https://jsfiddle.net/qm9o27ed/ (with also an inline-grid)
OKay first of all
https://flexboxfroggy.com/
play this game this game will help you to learn flex-box its pretty funnier way to learn flex-box properties
and for your problem and Use following jsfiddle
https://jsfiddle.net/dupinderdhiman/4d58c2nh/2/
.container {
display: inline-flex;
flex-flow: column;
border: 1px solid black;
background: #ececec;
}
.innerContainer{
display: flex;
flex-grow: 1;
}
.innerContainer > div {
border: 1px solid black;
background: #ececec;
}
<div class="container">
<div class="innerContainer" style="">
<div style="width: 50px;">Div 1</div>
<div style="width: 50px;">Div 2</div>
<div style="width: 50px;">Div 3</div>
</div>
<div style="width: 100%">Div 4</div>
</div>
https://jarodpeachey.github.io/breeze_css/layout.css
If you use this framework, you can easily create rows. The col-4 means it's 4 out of 12 grid spaces. If we set the width of the row to 150, each div will take up 50 pixels. (*Note: I would recommend using the framework to understand how it works, and then copy the code onto your local machine, as this framework is for my personal use, and the code may change)
<div class="container">
<div class="row m-auto" style="width: 150px">
<div class="col col-4">Div 1</div>
<div class="col col-4">Div 2</div>
<div class="col col-4">Div 3</div>
<div class="col col-12">Div 4</div>
</div>
</div>
Hope this helps!!

Bootstrap layout with mystery pulls and pushes

I trying to make uncommon layout with bootstrap, but pulls and pushes works in wierd way. Look at it:
http://jsfiddle.net/tfgdveop/1/
<div class="container" style="background: white">
<div class="row">
<div class="col-xs-3" style="background: red; height: 300px;">catalog</div>
<div class="col-xs-9" style="background: blue; height: 50px;">bNNER</div>
<div class="col-xs-3 col-xs-push-6" style="background: yellow; height: 500px;">News</div>
<div class="col-xs-6 col-xs-pull-3" style="background: green; height: 280px;">Catalog</div>
<div class="col-xs-6 col-xs-pull-6" style="background: pink; height: 100px;">Promo</div>
</div>
</div>
I need pink promo block 9 cols width, but if i made it wider 6, it fall under news block.
I changed order a bit and Yellow News to float to right. I also removed all unnnecessary pulls and pushes. Here is the result:
http://jsfiddle.net/tfgdveop/3/
<div class="container" style="background: white">
<div class="row">
<div class="col-xs-3" style="background: red; height: 300px;">catalog</div>
<div class="col-xs-9"style="background: blue; height: 50px;">bNNER</div>
<div class="col-xs-6"style="background: green; height: 280px;">Catalog</div>
<div class="col-xs-3 pull-right"style="background: yellow; height: 500px;">News</div>
<div class="col-xs-9" style="background: pink; height: 100px;">Promo</div>
</div>
</div>
Is this what you are looking for?

Different number of columns for each row

I'm not using the tag, so I can't use the colspan attribute.
I'd like to create a table with three cells in the first row, one cell in the last row and two cells in the other rows.
Here's my code (minimal):
.row {
display: table-row;
}
.cell {
display: table-cell;
}
HTML:
<div style="display: table;">
<div class="row" style="width: 100%;">
<div class="cell" style="width: 33% !important;">
aaaa
</div>
<div class="cell" style="width: 33% !important;">
bbbbb
</div>
<div class="cell" style="width: 33% !important;">
ccccc
</div>
</div>
<div class="row" style="width: 100%;">
<div class="cell" style="width: 50% !important;">
ddddd
</div>
<div class="cell" style="width: 50% !important;">
eeeee
</div>
</div>
<div class="row" style="width: 100%;">
<div class="cell" style="width: 50% !important;">
fffff
</div>
<div class="cell" style="width: 50% !important;">
ggggg
</div>
</div>
<div class="row" style="width: 100%;">
<div class="cell" style="width: 100% !important;">
last cell
</div>
</div>
</div>
And this is what I get (I can't post images): http://gyazo.com/cc036ed406f6c1a166955522d40e05b0.png
I would build this layout as follows.
For the HTML:
<div class="parent">
<div class="row r3">
<div class="cell">aaaa</div>
<div class="cell">bbbbb</div>
<div class="cell">ccccc</div>
</div>
<div class="row r2">
<div class="cell">ddddd</div>
<div class="cell">eeeee</div>
</div>
<div class="row r2">
<div class="cell">fffff</div>
<div class="cell">ggggg</div>
</div>
<div class="row r1">
<div class="cell">last cell</div>
</div>
</div>
and apply the following CSS:
.row {
display: table;
border: 1px dashed blue;
width: 100%
}
.cell {
display: table-cell;
border: 1px dotted blue;
}
.r3 .cell {
width: 33.333333%;
}
.r2 .cell {
width: 50%;
}
.r1 .cell {
width: 100%;
}
Use display: table for each div.row block element with 100% width.
You don't need to explicitly define a CSS table row, one will be created anonymously as needed.
See reference: http://www.w3.org/TR/CSS2/tables.html#anonymous-boxes
See demo: http://jsfiddle.net/audetwebdesign/72yb5th2/
You're trying to emulate a table with divs. Why? The <table> tag is made for exactly this kind of tabular data.

CSS layout block below previous block

How to make third block below the first?
Now third block below second.
Demo http://jsfiddle.net/SdR6e/1/
HTML
<!DOCTYPE html>
<html>
<body>
<div style="width: 400px;">
<div class="semiblock" style="height: 200px;">
First
</div>
<div class="semiblock" style="height: 100px;">
Second
</div>
<div class="semiblock" style="height: 200px;">
Third
</div>
<div class="semiblock" style="height: 200px;">
Fourth
</div>
</div>
</body>
</html>​
CSS
.semiblock {
border: 1px solid black;
float: left;
margin: 0;
width: 198px;
}​
I need this:
Try to insert clear:both like this:
DEMO
HTML
<!DOCTYPE html>
<html>
<body>
<div style="width: 400px;">
<div class="semiblock" style="height: 200px;">
First
</div>
<div class="semiblock" style="height: 100px;">
Second
</div>
<div class="clear">
</div>
<div class="semiblock" style="height: 200px;">
Third
</div>
<div class="semiblock" style="height: 200px;">
Fourth
</div>
</div>
</body>
</html>​
CSS:
.semiblock {
border: 1px solid black;
float: left;
margin: 0;
width: 198px;
}
.clear {
clear:both;
}
​
Method of "Alessandro Minoccheri" is good (+1),
but you want , also, just add "clear:left" value in your third block :
<!DOCTYPE html>
<html>
<body>
<div style="width: 400px;">
<div class="semiblock" style="height: 200px;">
First
</div>
<div class="semiblock" style="height: 100px;">
Second
</div>
<div class="semiblock" style="height:200px;clear:left;">
Third
</div>
<div class="semiblock" style="height: 200px;">
Fourth
</div>
</div>
</body>
</html>
Here is the solution: http://jsfiddle.net/SdR6e/2/
Use clear:both; when you want next element below another element.
If you use this with multiple blocks then write it in a class and add class to target elements.
.clearall{
clear:both;
}
Please use below HTML.. and used same CSS OR i have updated your given fiddle. Please check it using below URL.
http://jsfiddle.net/SdR6e/11/
<div style="width: 400px;">
<div class="semiblock" style="height: 200px;">
First
</div>
<div class="semiblock" style="height: 100px;">
Second
</div><div style='clear:both;'></div>
<div class="semiblock" style="height: 200px;">
Third
</div>
<div class="semiblock" style="height: 200px;">
Fourth
</div>
</div>