I'm trying to make a grid of 5 boxes (divs) where the first one has a portrait layout, and the rest have half the height of the first one (screenshot below)
My HTML looks like this:
<div class="container">
<div class="row">
<div class="col-md-4 portrait"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
</div>
</div>
It would have been easy using float:left and display block, but i'm using bootstrap 4 which uses flexbox so no support for float left and i can't get it to work as expected. Is there a way to do this even with flexbox ?
PS: the row div has flex-wrap: wrap;
.row {
display: flex;
flex-wrap: wrap;
flex-direction: column;
display: -webkit-flex;
-webkit-flex-wrap: wrap;
-webkit-flex-direction: column;
height: 200px;
justify-content: flex-start;
}
.col-md-4 {
flex: 0 0 50%;
-webkit-flex: 0 0 50%;
background: green;
}
.portrait {
-webkit-flex: 0 0 100%;
flex: 0 0 100%;
background: blue;
}
.red {
background: red;
}
<div class="container">
<div class="row">
<div class="col-md-4 portrait"></div>
<div class="col-md-4 red"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4 red"></div>
</div>
</div>
You can use the following to achieve the results displayed in your image.
<div class="container">
<div class="row">
<div class="col-md-6 portrait">PUT YOUR PORTRAIT CONTENT HERE</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-6">TOP LEFT AFTER PORTRAIT</div>
<div class="col-md-6">TOP RIGHT AFTER PORTRAIT</div>
</div>
<div class="row">
<div class="col-md-6">BOTTOM LEFT AFTER PORTRAIT</div>
<div class="col-md-6">BOTTOM RIGHT AFTER PORTRAIT</div>
</div>
</div>
</div>
</div>
This is done by creating a new row, inside of a col, which is itself inside of another row.
Related
I'm currently trying to figure out why my blank image placeholders aren't showing up when I use CSS grid. No matter what selectors I use and how I use them, I can't seem to figure out how to make it appear.
I've tried messing around with the class selectors and styles to make it appear, but I've been getting no luck in getting it to pop up. This is my current code right now revolving around the image placeholders:
.story {
display: flex;
flex-flow: row nowrap;
justify-content: flex-start;
align-items: flex-start;
align-content: flex-start;
gap: 1.618rem;
}
.story:nth-child(even) {
flex-direction: row-reverse;
}
.story .image {
aspect-ratio: 1;
background-color: #dadada;
flex: 0 0 calc(50% - 1.618rem);
}
.story .description {
flex: 1 0 50%;
}
#media screen and (min-width: 576px){
.story .image {
flex-basis: 30%;
}
}
#media screen and (min-width: 992px){
.story,
.story:nth-child(even) {
flex-flow: column wrap;
}
.story .image {
aspect-ratio: 16/9;
flex: 0 0 100%;
width: 100%;
background-color: blue;
}
}
And my HTML:
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-4 col-xl-12">
<h2>Articles</h2>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 col-xl-4">
<div class="story">
<div class="image">Image</div>
<div class="description">
<h2>Story Name</h2>
<p>Lorem ipsum...</p>
Link
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 col-xl-4">
<div class="story">
<div class="image">Image</div>
<div class="description">
<h2>Story Name</h2>
<p>Lorem ipsum...</p>
Link
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-4 col-lg-4 col-xl-4">
<div class="story">
<div class="image">Image</div>
<div class="description">
<h2>Story Name</h2>
<p>Lorem ipsum...</p>
Link
</div>
</div>
</div>
</div>
</div>
Please help me understand what I am doing wrong! T_T
I have pages with containers that contain rows containing columns.
I would like to enlarge the space between my columns
Here’s the code from my front page.
The first column of my second row must be the size of the first 2 columns of my first row.
<div class="container">
<div class="row">
<div class="col-md-4 card">
row 1 col 1
</div>
<div class="col-md-4 card">
row 1 col 2
</div>
<div class="col-md-4 card">
row 1 col 3
</div>
</div>
<div class="row">
<div class="col-md-8 card">
row 2 col 1
</div>
<div class="col-md-4 card">
row 2 col 2
</div>
</div>
</div>
For my second page, here is the code
<div class="container">
<div class="row">
<div class="col-md-8 card">
row 1 col 1
</div>
<div class="col-md-4 card">
row 1 col 2
</div>
</div>
</div>
I am this currently
Current Behavior
I want to expand the space between my columns. And that on the second line, while enlarging my margins, that the first column remains aligned with the first 2 of the first line
I have redone my page with flex but I still can’t align the first column of my second line with the first 2 of the first line
<div class="container">
<h3>Title</h3>
<div fxLayout="row" class="flex-row" fxLayout.xs="column" fxLayoutGap="16px" fxLayoutAlign="center">
<mat-card class="bloc-section" fxFlex="33.3%">
row 1 col 1
</mat-card>
<mat-card class="bloc-section" fxFlex="33.3%">
row 1 col 2
</mat-card>
<mat-card class="bloc-section" fxFlex="33.3%">
row 1 col 3
</mat-card>
</div>
<div fxLayout="row" class="flex-row" fxLayout.xs="column" fxLayoutGap="16px" fxLayoutAlign="center">
<mat-card class="bloc-section" fxFlex="67%">
row 2 col 1
</mat-card>
<mat-card class="bloc-section" fxFlex="33%">
row 2 col 2
</mat-card>
</div>
</div>
You can add css like this
.container .row {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
And fix your template
<div class="container">
<div class="row">
<div class="col-md-4 card">
row 1 col 1
</div>
<div class="col-md-4 card">
row 1 col 2
</div>
</div>
</div>
Do not try to copy the code. Try to understand the concept so you can create any grade.
At Bootstrap we have 12 columns (per row). This means that each row must be assigned to 12 columns
You should also know that: Bootstrap 3.
Note: It's different in Bootstrap 4.
xs: screens less than 768px wide
sm: screens equal to or greater than 768px wide
md: screens equal to or greater than 992px wide
lg: screens equal to or greater than 1200px wide
Your example:
In the first row, we have 3 equal columns: 12 / 3 = 4
In the second row, we have 2 columns.
That First column = first two columns in the previous row: 4 + 4 = 8
Example for xs: < 768px
Affects all sizes.
.row div div{
border-radius: 10px;
border: 2px solid;
background:#ddF;
text-align:center;
}
.row.mar{
margin-top:2px;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-4"><div>.col-xs-4</div></div>
<div class="col-xs-4"><div>.col-xs-4</div></div>
<div class="col-xs-4"><div>.col-xs-4</div></div>
</div>
<div class="row mar">
<div class="col-xs-8"><div>.col-xs-8</div></div>
<div class="col-xs-4"><div>.col-xs-4</div></div>
</div>
</div>
Example for sm: >= 768px
As a result, the size of each column is 12 (total). for less than 768px
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-sm-4"><div style="background:#ddd;">.col-sm-4</div></div>
<div class="col-sm-4"><div style="background:#ddd;">.col-sm-4</div></div>
<div class="col-sm-4"><div style="background:#ddd;">.col-sm-4</div></div>
</div>
<div class="row">
<div class="col-sm-8"><div style="background:#ddd;text-align: center;">.col-sm-8</div></div>
<div class="col-sm-4"><div style="background:#fdd;">.col-sm-4</div></div>
</div>
</div>
Custom example:
First row:
Only xs-4. For all sizes. Affects all sizes because no other size is defined.
Second row:
xs-4 and 4 md-7 and 5 lg-6 and 6 ( > 1200 (6) and > 992px(7,5) and other sizes(4))
.row div p{
border-radius: 10px;
border: 2px solid;
text-align:center;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-4"><p style="background:#E9F9B9;">All-4</p></div>
<div class="col-xs-4"><p style="background:#ddd;">All-4</p></div>
<div class="col-xs-4"><p style="background:#E9FCF9;">All-4</p></div>
</div>
<div class="row">
<div class="col-xs-8 col-md-7 col-lg-6"><p style="background:lavender;">xs-4 md-7 lg-6</p></div>
<div class="col-xs-4 col-md-5 col-lg-6"><p style="background:#FFF1F4">xs-4 md-5 lg-6</p></div>
</div>
</div>
Using flexbox
If you use Bootstrap 4, you don't need CSS code.
*{
box-sizing: border-box;
}
.flex-container{
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
background-color: lavender;
}
.col-4,.col-8{
position: relative;
width: 100%;
padding-right: 5px;
padding-left: 5px;
box-sizing: border-box;
}
.col-4 {
flex: 0 0 33.333333%;
max-width: 33.333333%;
}
.col-8{
flex: 0 0 66.666667%;
max-width: 66.666667%;
}
.flex-container > div div{
background:#E4F1F6;
padding:2px 6px;
margin:5px;
}
<div class="flex-container">
<div class="col-4"><div>1</div></div>
<div class="col-4"><div>2</div></div>
<div class="col-4"><div>3</div></div>
</div>
<div class="flex-container">
<div class="col-8"><div>A</div></div>
<div class="col-4"><div>B</div></div>
</div>
B
.flex-container {
display: flex;
align-items: stretch;
}
.flex-container > div {
background-color: lavender;
color: black;
margin: 3px;
text-align: center;
width: 33.3%;
line-height: 75px;
font-size: 30px;
}
<div class="flex-container">
<div style="flex-grow: 1">1</div>
<div style="flex-grow: 1">2</div>
<div style="flex-grow: 1">3</div>
</div>
<div class="flex-container">
<div style="flex-grow: 2; width: 67%;">A</div>
<div style="flex-grow: 1">B</div>
</div>
You are basically doing exactly what you should. You want the bootstrap md-X columns to handle most of the formatting.
However, your inner elements are what you want to align, more specifically you should probably add some:
https://www.w3schools.com/code/tryit.asp?filename=G8K0XRXB2V8B
And the html / CSS to get it to look this way:
<style>
.filler {
border-radius: 15px;
border-style: solid;
}
</style>
<div class="container-fluid">
<h1>Hello World!</h1>
<p>Resize the browser window to see the effect.</p>
<p>The columns will automatically stack on top of each other when the screen is less than 768px wide.</p>
<div class="row wide-fill">
<div class="col-sm-4 spacy" style="background-color:lavender;">
<div class="filler">.col-sm-4</div>
</div>
<div class="col-sm-4 spacy" style="background-color:lavenderblush;">
<div class="filler">.col-sm-4</div>
</div>
<div class="col-sm-4 spacy" style="background-color:lavenderblush;">
<div class="filler">.col-sm-4</div>
</div>
</div>
<div class="row">
<div class="col-sm-8 spacy" style="background-color:lavender;">
<div class="filler">.col-sm-8</div>
</div>
<div class="col-sm-4 spacy" style="background-color:lavenderblush;">
<div class="filler">.col-sm-4</div>
</div>
</div>
</div>
The requirement here is to display a series of nested, centered CSS grid items. We're rendering something like a hierarchical tree, with the head node on top, then second level nodes, third level nodes, and so on.
The problem is that later levels run off the div in the negative and positive X-axis directions.
Setting overflow-x: scroll solves the problem only for the positive X-axis direction. That is to say, you can scroll to the right, but not to the left.
The example below is a greatly simplified example of the real problem I'm trying to address. The red element on the 3rd level is off the page in the negative X-axis. You can scroll to the right, but not the left.
What can be done to make it so the entire tree, and all nodes, are able to be scrolled to?
Code follows(Link to JS Fiddle.):
#tree {
overflow-x: scroll;
}
.flex-col {
display: flex;
flex-direction: column;
align-items: center;
}
.grid {
display: grid;
}
.content {
width: 300px;
border-radius: 5px;
background-color: blue;
color: white
}
<div id="tree" class="flex-col">
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1">
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1;">
<div class="content" style="background-color: red;">** This is off the screen **</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
</div>
</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1">
<div class="content">Content</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
</div>
</div>
</div>
</div>
</div>
You can create an extra wrapper inside the tree element which will be an inline flexbox container so that it takes as much space as its contents. Use text-align: center to center the tree when there is no scroll - see demo below:
#tree {
overflow-x: auto; /* overflow horizontally here */
text-align: center; /* align horizontally */
}
#tree > .flex-col {
display: inline-flex; /* inline flex container */
text-align: initial; /* reset text align to default left */
}
.flex-col {
display: flex;
flex-direction: column;
align-items: center;
}
.grid {
display: grid;
}
.content {
width: 300px;
border-radius: 5px;
background-color: blue;
color: white
}
<div id="tree">
<div class="flex-col"> <!-- <-- extra wrapper -->
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1">
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1;">
<div class="content" style="background-color: red;">** This is off the screen **</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
</div>
</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1">
<div class="content">Content</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
</div>
</div>
</div>
</div>
</div>
</div>
You need to make sure that the grid elements don't expand wider than their container.
To resolve this, you can set max-width: 100% on .grid:
#tree {
overflow-x: scroll;
}
.flex-col {
display: flex;
flex-direction: column;
align-items: center;
}
.grid {
display: grid;
max-width: 100%;
}
.content {
width: 300px;
border-radius: 5px;
background-color: blue;
color: white
}
<div id="tree" class="flex-col">
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1">
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1;">
<div class="content" style="background-color: red;">** This is off the screen **</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
</div>
</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
<div class="grid">
<div class="flex-col" style="grid-column: 1">
<div class="content">Content</div>
</div>
<div class="flex-col" style="grid-column: 2">
<div class="content">Content</div>
</div>
</div>
</div>
</div>
</div>
I want to create a page where there are 3 rectangles (in black). The code show this. Now I want to create inside the 2nd horizontal rectangle, 3 vertical rectangles with different sizes(in red). Every and each text should be centered in each rectangle (vertically and horizontally).
How do I insert 3 columns inside the 2nd rectangle and centering all text in this:
<div class="wrap">
<div class="row_wrap">
<div class="head x">
ONE
</div>
</div>
<div class="row_wrap">
<div class="middle x">
TWO
</div>
</div>
<div class="row_wrap">
<div class="bottom x">
THREE
</div>
</div>
</div>
body {font-size:36px; color:green;}
.wrap {display: table; width:100%;}
.row_wrap{display:table-row;}
.x{display:table-cell; vertical-align:middle; text-align:center;}
.head{height:200px; background:#fa4;}
.middle{height:400px; background:#4af;}
.bottom{height:100px; background:#a4f;}
I know this question is old, but it's interesting to know it's possible to solve this using flexbox.
.row, .column{
display: flex;
}
.row{
width: 100%;
height: 10rem;
border: 3px solid black;
flex-flow:
}
.column{
border:3px solid red;
height: 100%;
flex: 1;
}
.column--big{
flex: 2;
}
.center{
justify-content: center;
align-items: center
}
<div class="row center">
<span>Center</span>
</div>
<div class="row center">
<div class="column center">
<span>Center</span>
</div>
<div class="column column--big center">
<span>Center</span>
</div>
<div class="column center">
<span>Center</span>
</div>
</div>
<div class="row center">
<span>Center</span>
</div>
This code could obviously be optimized, but it's a start.
I've seen this thread but its for 2 column (col-md-6) and I've tried the jsfidlle too for 3 column but column 1st and column 2nd become stack.
Bootstrap 3 Align Text To Bottom of Div
what I want for my project is
.row {
position: relative;
}
.bottom-align-text {
position: absolute;
bottom: 0;
right: 0;
}
<div class="container">
<div class="row">
<div class="bottom-align-text col-sm-4">
<h3>Some Text</h3>
</div>
<div class="col-sm-4">
<img src="//placehold.it/600x300" alt="Logo" class="img-responsive"/>
</div>
<div class="bottom-align-text col-sm-4">
<h3>Some Text</h3>
</div>
</div>
</div>
how to make them both align bottom?
flexbox can do that
.row div {
border: 1px solid red;
}
.flexy {
display: flex;
text-align: center;
}
.flexy .bottom-align-text {
display: flex;
justify-content: center;
align-items: flex-end;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row flexy">
<div class="bottom-align-text col-sm-4">
<h3>Some Text</h3>
</div>
<div class="col-sm-4">
<img src="//placehold.it/600x300" alt="Logo" class="img-responsive" />
</div>
<div class="bottom-align-text col-sm-4">
<h3>Some Text</h3>
</div>
</div>
</div>
You can wrap column 1st and column 2nd in to a row then we'll have 2 columns structure. See my jsFiddle demo: https://jsfiddle.net/robinhuy/kg1ykfL1/3/