I am displaying two columns in the center of my grid with contents and I need to make these two columns above each other in mobile view how I can do that?
.two-pics {
display: grid;
grid-gap: 50px;
grid-template-columns: 400px 400px;
object-fit: cover;
color: #444;
margin-top: 100px;
justify-content: center;
}
.box {
background-color: white;
color: #fff;
border-radius: 5px;
font-size: 150%;
}
.a {
grid-column: 1;
grid-row: 1 / 12;
}
.b {
grid-column: 2;
grid-row: 1 / 12;
}
<div class="two-pics">
<div class="box a">
<img src="assets/images/pic1.jpg">
<br>
<p>title</p>
<ul>
<li></li>
<li>dfgdfghdfhdfh</li>
<li>dfgdfghdfhdfh</li>
<li>dfgdfghdfhdfh</li>
</ul>
</div>
<div class="box b">
<img src="assets/images/pic2.jpg">
<br>
<p>ldkjh ldkjfh ldk hjf</p>
</div>
</div>
Using the auto-fit property you can create as many 400px grid tracks as will fit in the container.
codepen
body {
background: grey;
}
.two-pics {
display: grid;
grid-gap: 50px;
grid-template-columns: repeat(auto-fit, 400px);
color: #444;
margin-top: 100px;
justify-content: center;
}
.box {
background-color: white;
color: #000;
border-radius: 5px;
font-size: 150%;
}
<div class="two-pics">
<div class="box a">
<img src="https://unsplash.it/400">
<br>
<p>title</p>
<ul>
<li>dfdfd</li>
<li>dfgdfghdfhdfh</li>
<li>dfgdfghdfhdfh</li>
<li>dfgdfghdfhdfh</li>
</ul>
</div>
<div class="box b">
<img src="https://unsplash.it/400">
<br>
<p>ldkjh ldkjfh ldk hjf</p>
</div>
</div>
Related
I am trying to create a flexbox grid which should look like that
The basic grid layout is pretty easy to create, but I have a very hard time adding the spacing between the elements. As you can see in the code snippet, the spacing in the second line is not working properly.
#flexContainer {
display: flex;
flex-direction: column;
gap: 16px;
height: 200px;
width: 600px;
}
.rowContainer {
display: flex;
flex-direction: row;
gap: 16px;
}
.thirdElement {
flex-grow: 1;
height: 40px;
background-color: red;
}
.twoThirdsElement {
flex-grow: 2;
height: 40px;
background-color: red;
}
<div id="flexContainer">
<div class="rowContainer">
<div class="thirdElement">
</div>
<div class="thirdElement">
</div>
<div class="thirdElement">
</div>
</div>
<div class="rowContainer">
<div class="twoThirdsElement">
</div>
<div class="thirdElement">
</div>
</div>
</div>
This looks to me like a three column grid with the fourth item spanning two colums.
Here's a simplified version. Obviously you'll want to style the items as you need them.
#flexContainer {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 16px;
height: 200px;
width: 600px;
}
#flexContainer>* {
background-color: red;
}
.twoThirdsElement {
grid-column: auto / span 2;
}
<div id="flexContainer">
<div>
</div>
<div>
</div>
<div>
</div>
<div class="twoThirdsElement">
</div>
<div>
</div>
</div>
Use CSS grid
#flexContainer {
display: flex;
flex-direction: column;
gap: 16px;
}
.rowContainer {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 16px;
}
.thirdElement {
height: 40px;
background-color: red;
}
.twoThirdsElement {
grid-column:span 2;
height: 40px;
background-color: red;
}
<div id="flexContainer">
<div class="rowContainer">
<div class="thirdElement">
</div>
<div class="thirdElement">
</div>
<div class="thirdElement">
</div>
</div>
<div class="rowContainer">
<div class="twoThirdsElement">
</div>
<div class="thirdElement">
</div>
</div>
</div>
You not need to <div class="rowContainer">. It fixes only with one flex property for #flexContainer.
#flexContainer {
display: flex;
flex-wrap: wrap;
height: calc(80px + (600px / 20)); /* (600px/20) == 5% of 600px. result = 80px + 5%(600px). it is for a gap between rows that is equal with gap: 5%; for columns.*/
align-content: space-between;
gap: 5%;
width: 600px;
}
.thirdElement {
flex-basis: 30%;
height: 40px;
background-color: red;
}
.twoThirdsElement {
flex-basis: 65%;
height: 40px;
background-color: red;
}
<div id="flexContainer">
<div class="thirdElement">
</div>
<div class="thirdElement">
</div>
<div class="thirdElement">
</div>
<div class="twoThirdsElement">
</div>
<div class="thirdElement">
</div>
</div>
I hope you're well!
I am a bit new in writing css and I would like to achieve this result, but how?
Here is my try and I will be grateful for any advice and explanations.
<style>
#wrapper {
width: auto;
height: auto;
box-sizing: border-box;
display: grid;
grid-auto-flow: row;
grid-template-columns: repeat(6, 1fr);
}
#left{
text-align: left;
grid-column: 1/4;
margin-top: 2px;
background-color: red;
height: 200px;
}
#right {
text-align: right;
height: 100px;
padding-top: 10px;
margin-top: 2px;
grid-column: 4/6;
background-color: blue;
margin-left: 10px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="left" > div1 </div>
<div id="right" style="height:50px;"> div2 </div>
<div id="right" > div3 </div>
<div id="right" style="height:50px;"> div4 </div>
</div>
</body>
I think it's generally useful to think of these kinds of things as big blocks that contain smaller blocks. From that perspective you first have a layout with two columns, which you can achieve with a couple of simple grid or flex rules. There are many ways to do this, but here it is using grid:
.main-layout {
display: grid;
grid-template-columns: .35fr 1fr;
gap: 1rem;
min-height: 70vh;
}
.sidebar {
padding: 1rem;
background: skyblue;
}
.content {
padding: 1rem;
background: aliceblue;
}
<div class="main-layout">
<div class="sidebar">sidebar</div>
<div class="content">content</div>
</div>
Once you have that, you can fill in the "content" block without thinking much about the outer layout. This makes it pretty straightforward:
section {
padding: 0.5em;
margin-bottom: 1rem;
background: lightblue;
}
h1 {
font-size: 1.2rem;
margin: 0;
border-bottom: 1px solid white;
}
/* outer layout -- same as before */
.main-layout {
display: grid;
grid-template-columns: .35fr 1fr;
gap: 1rem;
min-height: 70vh;
}
.sidebar {
padding: 1rem;
background: skyblue;
}
.content {
padding: 1rem;
background: aliceblue;
}
<div class="main-layout">
<div class="sidebar">sidebar</div>
<div class="content">
<section>
<h1>This is a heading</h1>
<p>And this is content</p>
</section>
<section>
<h1>This is a heading</h1>
<p>And this is content</p>
</section>
<section>
<h1>This is a heading</h1>
<p>And this is content</p>
</section>
</div>
</div>
I have 7 divs. I am trying to make a three row three column layout. Two of the divs are different sizes. I have everything the way I want it, it's just on the third row one div jumps up to row two, and it wont budge down to row three even with clear:right.I am trying my hardest to have the design Internet Explorer 11 ready before I give up and cut off all traffic to IE.
The way the layout is rendering
The way it should be rendering
<style>
[div_glimg]{ width:390px; height:390px}
[glimg]{ float:left; background-size: cover; }
[div_glvideo], [glvideoobject]{ width:780px; height:390px; float:left}
</style>
<div style="background-color: white ;float:left" div_glimg>
<div glim>
<div glim1title glim1titlerez style=" margin-top:0 !important;">Life<a> in</a> </div>
<div glmaincdiv></div>
<div glimcontent>info.</div>
</div>
</div>
<div style="background-color: hsla(359,36%,62%,1.00); " div_glimg glimg> </div>
<div style="background-color: hsla(213,35%,62%,1.00); " div_glimg glimg> </div>
<div style="background-color: hsla(51,35%,62%,1.00); clear:left" glimgTall glimg> tall</div>
<div style="background-color: hsla(199,35%,62%,1.00); clear:right" div_glvideo> video </div>
<div style="background-color: hsla(302,35%,62%,1.00); " div_glimg glimg> box 1</div>
<div style="background-color: hsla(302,35%,62%,1.00); " div_glimg glimg> box 2</div>
```
Problem : First of all, the answer of the question, that why clear: right; is not working is because when the element, with clear: right; property is rendered, the element in the right of it has not been rendered. That is why, the right element after getting rendered is not affected.
Solution : Either you can choose the grid-system, or if you want to go with what you have, with fixed dimensions, you can do it like this :
<style>
[div_glimg] {
width: 78px;
height: 78px
}
[glimg] {
float: left;
background-size: cover;
}
[div_glvideo],
[glimgTall] {
float: left;
}
[div_glvideo] {
width: 156px;
height: 78px;
}
[glimgTall] {
height: 156px;
width: 78px;
}
[wrapper] {
height: 234px;
width: 234px;
}
</style>
<div wrapper>
<div style="background-color: white ;float:left" div_glimg>
<div glim>
<div glim1title glim1titlerez style=" margin-top:0 !important;">Life<a> in</a> </div>
<div glmaincdiv></div>
<div glimcontent>info.</div>
</div>
</div>
<div style="background-color: hsla(359,36%,62%,1.00); " div_glimg glimg> </div>
<div style="background-color: hsla(213,35%,62%,1.00); " div_glimg glimg> </div>
<div style="background-color: hsla(51,35%,62%,1.00);" glimgTall glimg> tall</div>
<div style="background-color: hsla(199,35%,62%,1.00);" div_glvideo> video </div>
<div style="background-color: hsla(302,35%,62%,1.00); " div_glimg glimg> box 1</div>
<div style="background-color: hsla(302,35%,62%,1.00); " div_glimg glimg> box 2</div>
</div>
I would suggest go for grid layout for better experience. Here is example.
.grid-container {
display: grid;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px;
font-size: 30px;
}
.item1 {
grid-column: 1;
grid-row: 1;
}
.item2 {
grid-column: 2;
grid-row: 1;
}
.item3{
grid-column:3;
grid-row:1;
}
.item5 {
grid-column: 2 / span 2;
grid-row: 2;
}
.item4{
grid-row: 2 / span 2;
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h1>A Five Items Grid Layout:</h1>
<div class="grid-container">
<div class="grid-item item1">1</div>
<div class="grid-item item2">2</div>
<div class="grid-item item3">3</div>
<div class="grid-item item4">4</div>
<div class="grid-item item5">5</div>
<div class="grid-item item6">6</div>
<div class="grid-item item7">7</div>
</div>
<p>Direct child elements(s) of the grid container automatically becomes grid items.</p>
<p>Item 1, 2, and 5 are set to span multiple columns or rows.</p>
</body>
</html>
It would be easier to use CSS Grid for your solution. The layout was generated using this tool: Layoutit Grid
html,
body,
.grid-container {
height: 100%;
margin: 0;
}
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "Div-1 Div-2 Div-3" "Div-4 Div-5 Div-5" "Div-4 Div-6 Div-7";
padding: 10px;
}
div {
border: 2px solid #000;
}
.Div-1 {
grid-area: Div-1;
background: #E975AF;
}
.Div-2 {
grid-area: Div-2;
background: #0075AE;
}
.Div-3 {
grid-area: Div-3;
background: #27408F;
}
.Div-4 {
grid-area: Div-4;
background: #FFF200;
}
.Div-5 {
grid-area: Div-5;
background: #40AD47;
}
.Div-6 {
grid-area: Div-6;
background: #EC008C;
}
.Div-7 {
grid-area: Div-7;
background: #00ADEF;
}
<div class="grid-container">
<div class="Div-1"></div>
<div class="Div-2"></div>
<div class="Div-3"></div>
<div class="Div-5"></div>
<div class="Div-6"></div>
<div class="Div-7"></div>
<div class="Div-4"></div>
</div>
I have created a masonry style grid in HTML/CSS. To do this I have used the
display:grid properties with 'grid-row' and 'grid-column.'
See attached pen.
This works great in Chrome, Firefox and Edge, not tried on Safari yet, but unfortunately, there are issues when it renders in the IE 11 browser.
Instead of getting a nice grid layout as shown in the attached pen it just displays 4 1x1 columns on top of one another.
Are there any IE specific properties that I can assign to my CSS classes in order to get this to display correctly across all browsers?
#container {
display:inline-block;
overflow: hidden; /* clip the excess when child gets bigger than parent */
}
#container img {
display: block;
transition: transform .4s; /* smoother zoom */
}
#container:hover img {
transform: scale(1.3);
transform-origin: 50% 50%;
}
.wrapper {
display: grid;
grid-gap: 0px;
grid-template-columns: 33% 33% 33%;
grid-template-rows: 400px 400px;
color: #000;
}
.a {
grid-column: 1;
grid-row: 1/3;
text-align:center;
color:#fff;
display:block;
position:relative;
overflow:hidden;
}
.b {
grid-column: 2 / span 2;
grid-row: 1;
color:#fff;
position:relative;
overflow:hidden;
}
.c {
grid-column: 2;
grid-row: 2;
color:#fff;
position:relative;
overflow:hidden;
}
.d {
grid-column: 3;
grid-row: 2;
color:#fff;
position:relative;
overflow:hidden;
}
.box {
border-radius: 0px;
padding: 10px;
font-size: 150%;
display:block;
position:relative;
}
.ctr {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
.imex-btn {
background-color: #4080fa;
border: none;
color: white;
padding: 10px 30px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin-top: 70px;
cursor: pointer;
font-family: "Century Gothic";
}
.imex-btn:hover {
background-color: #000;
color: #4080fa;
}
<div class="wrapper">
<div class="box a">
<div id="container">
<img id="image" src="https://via.placeholder.com/500x900
C/O https://placeholder.com/">
<div class="ctr"><h2 class="white">BOX A<h2>
</div>
VIEW NOW
</div>
</div>
<div class="box b">
<div id="container">
<img id="image" src="https://via.placeholder.com/1200x900/C/O https://placeholder.com/">
<div class="ctr"><h2 class="white">BOX B</h2>
</div>
VIEW NOW
</div>
</div>
<div class="box c">
<div id="container">
<img id="image" src="https://via.placeholder.com/500x500
C/O https://placeholder.com/">
<div class="ctr"><h2 class="white" style="text-align:center">BOX C</h2>
</div>
READ MORE
</div>
</div>
<div class="box d">
<div id="container">
<img id="image" src="https://via.placeholder.com/500x500
C/O https://placeholder.com/">
<div class="ctr"><h2 class="white" style="text-align:center">BOX D</h2>
</div>
READ MORE
</div>
</div>
</div>
Try to change the CSS from
grid-template-columns: 33% 33% 33%;
To
grid-template-columns: 1fr 1fr 1fr;
Some elements are not properly rendered on IE11 due to their implementation of the standards.
Try adding a -ms- prefix to elements such as display: grid, grid-column and grid-row.
You will probably face the same kind of problems when testing on older versions of Safari (in which case try the -webkit prefix).
I've added some css to make it work in IE. Pay attention to the -ms- prefix added in css. You can also refer to this article which explains how to support css grid in IE.
#container {
display: inline-block;
overflow: hidden; /* clip the excess when child gets bigger than parent */
}
#container img {
display: block;
transition: transform .4s; /* smoother zoom */
}
#container:hover img {
transform: scale(1.3);
transform-origin: 50% 50%;
}
.wrapper {
display: -ms-grid;
display: grid;
grid-gap: 0px;
grid-template-columns: 33% 33% 33%;
-ms-grid-columns: 33% 33% 33%;
grid-template-rows: 400px 400px;
-ms-grid-rows: 400px 400px;
color: #000;
}
.a {
grid-column: 1;
grid-row: 1/3;
-ms-grid-column: 1;
-ms-grid-row-span: 2;
text-align: center;
color: #fff;
display: block;
position: relative;
overflow: hidden;
}
.b {
grid-column: 2 / span 2;
grid-row: 1;
-ms-grid-row:1;
-ms-grid-column-span: 2;
-ms-grid-column:2;
color: #fff;
position: relative;
overflow: hidden;
}
.c {
grid-column: 2;
grid-row: 2;
-ms-grid-column:2;
-ms-grid-row:2;
color: #fff;
position: relative;
overflow: hidden;
}
.d {
grid-column: 3;
grid-row: 2;
-ms-grid-column:3;
-ms-grid-row:2;
color: #fff;
position: relative;
overflow: hidden;
}
.box {
border-radius: 0px;
padding: 10px;
font-size: 150%;
display: block;
position: relative;
}
.ctr {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
.imex-btn {
background-color: #4080fa;
border: none;
color: white;
padding: 10px 30px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin-top: 70px;
cursor: pointer;
font-family: "Century Gothic";
}
.imex-btn:hover {
background-color: #000;
color: #4080fa;
}
<div class="wrapper">
<div class="box a">
<div id="container">
<img id="image" src="https://via.placeholder.com/500x900
C/O https://placeholder.com/">
<div class="ctr">
<h2 class="white">BOX A</h2>
</div>
VIEW NOW
</div>
</div>
<div class="box b">
<div id="container">
<img id="image" src="https://via.placeholder.com/1200x900/C/O https://placeholder.com/">
<div class="ctr">
<h2 class="white">BOX B</h2>
</div>
VIEW NOW
</div>
</div>
<div class="box c">
<div id="container">
<img id="image" src="https://via.placeholder.com/500x500
C/O https://placeholder.com/">
<div class="ctr">
<h2 class="white" style="text-align:center">BOX C</h2>
</div>
READ MORE
</div>
</div>
<div class="box d">
<div id="container">
<img id="image" src="https://via.placeholder.com/500x500
C/O https://placeholder.com/">
<div class="ctr">
<h2 class="white" style="text-align:center">BOX D</h2>
</div>
READ MORE
</div>
</div>
</div>
Can I achieve this layout with flexbox with the below document structure?
I want the big <img> on the left with two smaller images on the right and wrapping.
This is what I did, with display: flex on gallery-container and flex-wrap.
.container {
height: 100%;
}
.container .gallery-container {
background-color: #f6f6f6;
display: flex;
flex-wrap: wrap;
width: 300px;
align-items: flex-start;
}
.container .gallery-container .gallery-big-image {
display: block;
width: 200px;
height: 200px;
background: lavender;
}
.container .gallery-container .gallery-small-img {
display: block;
width: 100px;
height: 100px;
background-color: purple;
}
<div class="container">
<div class="gallery-container">
<div class="gallery-big-image">big</div>
<div class="gallery-small-img">small</div>
<div class="gallery-small-img">small</div>
<div class="gallery-small-img">small</div>
<div class="gallery-small-img">small</div>
<div class="gallery-small-img">small</div>
</div>
</div>
(codepen)
The layout is clunky and inefficient with flexbox, for reasons explained here: CSS-only masonry layout
However, the layout is relatively simple and easy with CSS Grid.
No changes to HTML.
.gallery-container {
display: grid;
grid-template-columns: repeat(auto-fill, 100px);
grid-auto-rows: 100px;
width: 300px;
background-color: #f6f6f6;
}
.gallery-big-image {
grid-column: span 2;
grid-row: span 2;
background: lavender;
}
.gallery-small-img {
background-color: purple;
color: white;
}
<div class="container">
<div class="gallery-container">
<div class="gallery-big-image">big</div>
<div class="gallery-small-img">small 1</div>
<div class="gallery-small-img">small 2</div>
<div class="gallery-small-img">small 3</div>
<div class="gallery-small-img">small 4</div>
<div class="gallery-small-img">small 5</div>
<div class="gallery-small-img">small 6 (continues wrapping)</div>
<div class="gallery-small-img">small 7 (continues wrapping)</div>
</div>
</div>
How about using grid layout instead?
.container {
height: 100%;
}
.gallery-container {
background-color: #f6f6f6;
width: 300px;
height: 100px;
display: grid;
grid-template-rows: repeat(3, 1fr);
grid-template-columns: repeat(3, 1fr);
}
.gallery-img {
background: purple;
width: 100px;
height: 100px;
}
.gallery-img-large {
background: lavender;
width: 200px;
height: 200px;
grid-column-start: 0;
grid-column-end: span 2;
grid-row-start: 0;
grid-row-end: span 2;
}
<div class="container">
<div class="gallery-container">
<div class="gallery-img-large">big</div>
<div class="gallery-img">small</div>
<div class="gallery-img">small</div>
<div class="gallery-img">small</div>
<div class="gallery-img">small</div>
<div class="gallery-img">small</div>
</div>
</div>