HTML: CSS: Place element somewhere by class - html

Say I have 6 elements:
<img src="..." class="a"/>
<img src="..." class="b"/>
<img src="..." class="c"/>
<img src="..." class="d"/>
<img src="..." class="e"/>
<img src="..." class="f"/>
which are not necessarily images like shown above, but can be any element, like some <div>.
Is it possible to create a grid of 3x2 and place each element of these in one container?

Just using classes as you pretend, mixing a lot of elements as you don't know which will be used.
option #1 - using flexbox
body {
margin: 0
}
#flex {
display: flex;
flex-wrap: wrap;
height:100vh
}
.a,
.b,
.c,
.d,
.e,
.f {
flex: 1 0 calc(100% / 3);
height:50%;
background: lightblue;
}
<div id="flex">
<div class="a">div with Class A</div>
<img class="b" src="//lorempixel.com/100/100" />
<span class="c">span with Class C</span>
<article class="d">article with Class D</article>
<div class="e">div with Class E</div>
<section class="f">Div with Class F</section>
</div>
option #2 - using inline-block
body,html {
margin: 0;
height:100%
}
#ib {
font-size: 0;
height:100%
}
.a,
.b,
.c,
.d,
.e,
.f {
display: inline-block;
vertical-align: top;
background:lightblue;
width: calc(100% / 3);
height:50%;
font-size: 16px
}
<div id="ib">
<div class="a">div with Class A</div>
<img class="b" src="//lorempixel.com/100/100" />
<span class="c">span with Class C</span>
<article class="d">article with Class D</article>
<div class="e">div with Class E</div>
<section class="f">Div with Class F</section>
</div>

I just tried using CSS :nth-child() Selector and you can move required elements to next line and form matrix
<div class="a">1</div>
<div class="b">2</div>
<div class="c">3</div>
<div class="d">4</div>
<div class="e">5</div>
<div class="f">6</div>
div {
display: inline;
}
div:nth-child(4) {
display: inline;
}
div:nth-child(3):after {
content: "\a";
white-space: pre;
}
Hope this is helpful for you :)
codepen URL for reference -http://codepen.io/nagasai/pen/BzNmBz

Note the th tag is for headings, tr tag for rows and td tag is where you need to write your data. Hope this helps
<table>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
`

The generic approach to this is to use a class for the container in which the items will be laid out, and a class for each item that will be in the container.
This sample mostly takes the form of #dippas' "using flexbox" example, but the CSS styles are not particular to any set of ids or classes ... you can attach the additional class if necessary. <div class="a">...</div> could become <div class="a grid-item">...</div> or omit the class a as I do.
You can see you just attach the grid-item class to any sort of thing you want.
This can also be styled using the inline-block method if that's preferable.
UI frameworks commonly take this approach, where you just attach the appropriate class(es) to your elements to give them the layout / style desired.
.grid-container {
display: flex;
flex-wrap: wrap;
height: 100vh;
}
.grid-item {
flex: 1 0 calc((100% / 3) - 4px);
height: 50%;
background-color: antiquewhite;
border: 1px dotted burlywood;
border-collapse: collapse;
}
<div class="grid-container">
<div class="grid-item">A</div>
<div class="grid-item">B</div>
<span class="grid-item">C</span>
<img class="grid-item" src="//lorempixel.com/100/100" />
<section class="grid-item">Section E</section>
<div class="grid-item">F</div>
</div><!-- grid-container -->
You can even omit the class for the items if everything in the grid-container will be laid out like this...
.grid-container {
display: flex;
flex-wrap: wrap;
height: 100vh;
}
/* select all immediate children of the .grid-container */
.grid-container > * {
flex: 1 0 calc((100% / 3) - 4px);
height: 50%;
background-color: antiquewhite;
border: 1px dotted burlywood;
border-collapse: collapse;
}
<div class="grid-container">
<div>div A</div>
<div>div B</div>
<span>span C</span>
<img src="//lorempixel.com/100/100" />
<section>section E</section>
<div>div F</div>
</div><!-- grid-container -->

Related

How to shrink column to max width of that column in all rows, like td width in a table?

I want to replace a table with flexbox elements.
Basically from this:
<table>
<tr>
<td>row_1.col_1</td>
<td>row_1.col_2</td>
</tr>
<tr>
<td>row_2.col_1.with_longer_content</td>
<td>row_2.col_2</td>
</tr>
</table>
To this:
<div>
<div>
<div>row_1.col_1</div>
<div>row_1.col_2</div>
</div>
<div>
<div>row_2.col_1.with_longer_content</div>
<div>row_2.col_2</div>
</div>
</div>
In the case of the table, the first row's first cell will expand in width so that the second cell row_1.col_2 will align properly with the cell beneath it row_2.col_2
-------------------------------------------------
| row_1.col_1 | row_1.col_2 |
| row_2.col_1.with_longer_content | row_2.col_2 |
-------------------------------------------------
How can I do the same with divs, using flexbox, so that the first column consumes the least amount of width, but the second column is still aligned?
Is this even possible?
The reason why I want to do this is because I want each row to be a Quasar q-card, but the elements in there should align with the cards above and below, yet still consume the least amount of space and not be width-controlled through the "12-column"-grid-system.
Basically like this, where I need the badges and inputs to be aligned as if it were a table (I cannot use a q-table, and -- due to the use of UMD -- also not a q-markup-table):
<q-card>
<q-card v-for='element in elements'>
<q-badge>{{element.badge}}</q-badge>
<q-input v-model='element.text'></q-input>
</div>
</div>
Instead of flex, it would probably be better to use display:table
div {
display: table;
}
div>div {
display: table-row;
}
div>div>div {
display: table-cell;
}
<div>
<div>
<div>row_1.col_1</div>
<div>row_1.col_2</div>
</div>
<div>
<div>row_2.col_1.with_longer_content</div>
<div>row_2.col_2</div>
</div>
</div>
Using display: flexbox you can do this by organizing the data by column instead of by row. Using display: table you can maintain the structure of the table. Examples:
.flex-table {
display: flex;
}
.column {
padding: 4px;
}
.table {
display: table;
}
.table-row {
display: table-row;
}
.table-cell {
display: table-cell;
padding: 0 4px;
}
<h3>table</h3>
<table>
<tr>
<td>row_1.col_1</td>
<td>row_1.col_2</td>
</tr>
<tr>
<td>row_2.col_1.with_longer_content</td>
<td>row_2.col_2</td>
</tr>
</table>
<h3>flexbox table</h3>
<div class="flex-table">
<div class="column">
<div>row_1.col_1</div>
<div>row_2.col_1.with_longer_content</div>
</div>
<div class="column">
<div>row_1.col_2</div>
<div>row_2.col_2</div>
</div>
</div>
<h3>display table</h3>
<div class="table">
<div class="table-row">
<div class="table-cell">row_1.col_1</div>
<div class="table-cell">row_1.col_2</div>
</div>
<div class="table-row">
<div class="table-cell">row_2.col_1.with_longer_content</div>
<div class="table-cell">row_2.col_2</div>
</div>
</div>
<style>
.row {
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin-right: -15px;
margin-left: -15px;
}
.col {
-ms-flex-preferred-size: 0;
flex-basis: 0;
-ms-flex-positive: 1;
flex-grow: 1;
max-width: 100%;
position: relative;
width: 100%;
padding-right: 15px;
padding-left: 15px;
}
.text-wrap {
white-space: normal !important;
}
</style>
<div>
<div class="row">
<div class="col text-wrap">row_1.col_1</div>
<div class="col text-wrap">row_1.col_2</div>
</div>
<div class="row">
<div class="col text-wrap">row_2.col_1.with_longer_content</div>
<div class="col text-wrap">row_2.col_2</div>
</div>
</div>
Try writing like this & if you don't want to use the above styles use the bootstrap css link instead.

Set same height of independent div's with same class name using CSS [duplicate]

This question already has an answer here:
Maintain same height in elements inside columns located side by side with CSS
(1 answer)
Closed 2 years ago.
I want to set the same height of independent div's that have the same class name(i.e 'description', 'features', 'rules') and height should be w.r.t div that have max-content.
Note: Content is dynamic and HTML structure must be the same as below.
.products {
display: inline-flex;
width: 300px;
}
.products .product {
width: 50%;
border: 1px solid black;
}
.products .product div {
border-bottom: 1px solid black;
}
<div class="products">
<div class="product">
<div class="description">product 1 description description description description description</div>
<div class="features">product 1 features</div>
<div class="rules">product 1 rules rules rules rules rules rules rules rules</div>
</div>
<div class="product">
<div class="description">product 2 description</div>
<div class="features">product 1 features features features features features</div>
<div class="rules">product 1 rules</div>
</div>
</div>
Looking for only CSS solution!
Consider using grid layout https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout
View the example below with aligned rows using a nested grid layout.
.products {
display: grid;
grid-template-columns: 1fr 1fr;
border: 3px solid blue;
}
.products .product {
display: grid;
grid-template-rows: 1fr 1fr 1fr;
border: 3px solid green;
}
.product .description {
grid-column: 1;
grid-row: 1;
border-bottom: 1px solid yellow;
}
.product .features {
grid-column: 1;
grid-row: 2;
border-bottom: 1px solid red;
}
.product .rules{
grid-column: 1;
grid-row: 3;
}
<div class="products">
<div class="product">
<div class="description">product 1 description description description description description</div>
<div class="features">product 1 features</div>
<div class="rules">product 1 rules rules rules rules rules rules rules rules</div>
</div>
<div class="product">
<div class="description">product 2 description</div>
<div class="features">product 2 features features features features features</div>
<div class="rules">product 2 rules</div>
</div>
</div>
Another good resource if you don't want to nest grids
https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-areas#html
For your case, I prefer to use an HTML table:
.products {
border: 1px solid black;
border-collapse: collapse;
}
table td {
width: 50%;
border: 1px solid black;
}
<table class="products">
<tr>
<td>product 1 description description description description description</td>
<td>product 2 description</td>
</tr>
<tr>
<td>product 1 features</td>
<td>product 1 features features features features features</td>
</tr>
<tr>
<td>product 1 rules rules rules rules rules rules rules rules</td>
<td>product 1 rules</td>
</tr>
</table>
Maybe it is far from your HTML but possible it could be an answer.

How do I correctly nest a flexbox to achieve a form layout?

I am looking to achieve the following layout:
Here is how I'm picturing it (with grids):
Black bar is the nav (we can ignore this)
A title and subtitle (purple) - these should be aligned and take up approx 70% of width - I think I've done this
A form which has 3 columns (should take up 70ish percent of the 70%, I don't want inputs to be too wide)
Column 1: Heading + text pairs
Column 2: it will have some icon/character - these must be perfectly aligned
Column 3: Heading + input boxes - these must be the same width
Here is my starting HTML:
.title-container {
display: flex;
justify-content: center;
background: red;
}
.title-item {
flex-basis: 75%;
}
.data-container {
display: flex;
justify-content: center;
background: blue;
}
.column-items {
flex-basis: 70%;
display: flex;
flex-direction: row;
}
.column-1-item {
background: green;
flex-grow: 0.5;
}
.column-2-item {
background: yellow;
flex-grow: 0.1;
align-self: center;
}
.column-3-item {
background: orange;
flex-grow: 1;
}
<div class="title-container">
<div class="title-item">
<h2>Title</h2>
<p>This is some text</p>
</div>
</div>
<div class="data-container">
<div class="column-items">
<div class="column-1-item">
<p>Heading1</p>
<p>SomeText</p>
</div>
<div class="column-2-item">
<p>--></p>
</div>
<div class="column-3-item">
<p>Heading1</p>
<input type="text" name="lname">
</div>
</div>
</div>
I have tried to expand on this, but no matter what I try, I end up further away from my design making me think there is something wrong with my initial design (and flex understanding). If I add additional 'row', it breaks my layout. I also think my data-container is wrongly setup, since this will take up far more space than I want it to
Here is a code pen.
Could someone help get me closer to my design?
I would wrap your entire html in a wrapper class so that you can get the general layout of the page like so:
<div class="wrapper">
<div class="title-container">
<h2>Title</h2>
<p>
Subtitle should be aligned with title
</p>
</div>
<div class="form-container">
<div class="item">
<div class="column">
<p>Heading1</p>
<p>Some Text</p>
</div>
<div class="column">
<p>-></p>
</div>
<div class="column">
<p>Heading1</p>
<p>[ input textfield ]</p>
</div>
</div>
<div class="item">
<div class="column">
<p>Heading3</p>
<p>Some Text</p>
</div>
<div class="column">
<p>-></p>
</div>
<div class="column">
<p>Heading2</p>
<p>[ input textfield ]</p>
</div>
</div>
<div class="item">
<div class="column">
<p>Heading3</p>
<p>Some Text</p>
</div>
<div class="column">
<p>-></p>
</div>
<div class="column">
<p>Heading3</p>
<p>[ input textfield ]</p>
</div>
</div>
<div class="item">
<div class="column"></div>
<div class="column"></div>
<div class="column submit-button">
<p>[ Button ]</p>
</div>
</div>
</div>
</div>
Then you can specify the width for the title-container and form-container with the width property. Making each of the item classes in the form container have a display: flex property lets you format the children column classes to have flex-grow: 1 so they can fill up the available space. The css then looks like:
.wrapper {
display: flex;
flex-direction: column;
align-items: center;
outline: 1px solid red;
}
.title-container {
width: 70%;
outline: 1px solid red;
}
.form-container {
width: 50%;
outline: 1px solid red;
}
.item {
display: flex;
outline: 1px solid red;
}
.column {
/* flex-grow: 1; */
flex: 1 1 0px;
outline: 1px solid red;
}
.submit-button {
display: flex;
align-items: flex-end;
justify-content: flex-end;
}
Alternately you can remove the flex-grow: 1 property from the column class and add justify-content: space-between to the item class to get a result similar to your example.
Here is the codepen.
Your .data-container just needs a flex-direction: column; because you want the .column-items to stack.

how can I separate 2 divs equally from left and right?

Im a new in css and html. How can I separate 2 divs equally from left and right?? Here's my html code.
<div class="first-div">
<h1>About<h1>
</div>
<div class="second-div">
<h1>Services<h1>
</div>
you can try this. Learn Bootstrap grid or flexbox that would be easy for these kinds of task.
.container{
width:100%;
display:flex; /* for display it's child div beside each other */
}
.first-div,.second-div{
width:50%; /* for divide container into 2 equal divs */
border: 1px solid black; /* for border around divs */
}
<div class="container">
<div class="first-div">
<h1>About<h1>
</div>
<div class="second-div">
<h1>Services<h1>
</div>
</div>
You have several options and it all depends on your exact use case.
The first option is to set both to 50% of the available width (left and right):
.first-div {
display: inline-block;
width: 50%;
}
.second-div {
display: inline-block;
width: 50%;
}
<div class="first-div">
<h1>About</h1>
</div><div class="second-div">
<h1>Services</h1>
</div>
Another option is to use flex:
.wrapper {
display: flex;
}
.first-div {
flex-grow: 1;
}
.second-div {
flex-grow: 1;
}
<div class="wrapper">
<div class="first-div">
<h1>About</h1>
</div>
<div class="second-div">
<h1>Services</h1>
</div>
</div>
If you're not familiar with flex, flexbox froggy is a great interactive way to learn.
And a final option is to use grid:
.wrapper {
display: grid;
grid-template-columns: 1fr 1fr;
}
<div class="wrapper">
<div class="first-div">
<h1>About</h1>
</div>
<div class="second-div">
<h1>Services</h1>
</div>
</div>
If you're not familiar with grid, grid garden is a great interactive way to learn.
Sidenote: make sure to also properly close the <h1> tags with a </h1>.
Try this code. Hope it useful for your question.
/* CSS */
.container-box{
width:100%;
display:flex;
}
.first-div,.second-div{
width:50%;
border:1px solid #ddd; //just to look
}
<!-- HTML -->
<div class="container-box">
<div class="first-div">
<h1>About<h1>
</div>
<div class="second-div">
<h1>Services<h1>
</div>
</div>
Check Css FlexBox .
FlexBox Tutorial
<div class="mainContainer" style="
display: flex;
justify-content: space-around;
">
<div class="first" style="
border: 1px solid;
">
<h1>About</h1><h1>
</h1></div>
<div class="second-div">
<h1>Services</h1><h1>
</h1></div>
</div>

CSS: Using flexbox for structure content

I want to create a layout like below image. It includes:
A left part
A right part
A title stretch whole row
I can do as structure html like this and using some layout such as Flexbox, div positioning ...
<div class="wrapper">
<div class="status">
<div class="left">12/05/2015</div>
<div class="right>Adventure Story</div>
</div>
<div class="title">A long story about the Atlantic</div>
</div>
I just learn Flexbox and I want to simplify to this html (no wrapper against the first row)
<div class="wrapper">
<div class="left">12/05/2015</div>
<div class="right>Adventure Story</div>
<div class="title">A long story about the Atlantic</div>
</div>
Can I use Flexbox for above html structure. If yes, please tell me how.
You can simply do this :
.wrapper {
display: flex;
flex-wrap: wrap; /* to avoid overflow and force title going to new line */
}
.left,
.right {
flex: 0 0 50%; /*each element will take 50% of the width */
}
.right {
text-align: right;
}
.title {
flex: 0 0 100%; /*this element will take 100% of the width*/
}
div {
border: 1px solid red;
box-sizing: border-box;
}
<div class="wrapper">
<div class="left">12/05/2015</div>
<div class="right">Adventure Story</div>
<div class="title">A long story about the Atlantic</div>
</div>
You can use flex-wrap: wrap on flex-container and flex: 0 0 100% on title element so that it goes to new row and margin-left: auto on second element in order to position it to the right side.
.wrapper {
display: flex;
flex-wrap: wrap;
}
.title {
flex: 0 0 100%;
}
.right {
margin-left: auto;
}
<div class="wrapper">
<div class="left">12/05/2015</div>
<div class="right">Adventure Story</div>
<div class="title">A long story about the Atlantic</div>
</div>