CSS 2 columns grid to 2 rows - html

Almost 2 weeks later, countless tries later, even asking a similar question before, I can't make something so simple. CSS doesn't return any info as we know, therefore I don't even know why.
All the examples that I find work, they work on their own, or with media query, while mine, never acts as I want it to, even if I copy the solution. The grids do shrink, and only that happens, no new row ever appears anywhere.
I simplified my design, my website grew over almost those 2 weeks, and I'm sharing just the minimum here, so someone can maybe tell me why grid never adjusts itself, regardless of everything. I want to achieve a very simple thing.
Top consists of 2 columns [top_left + top_right], and I want them to change into 2 rows at a certain screen width, same applies to bottom part [bot_left + bot_right]. In theory and practice, this is doable with both media query and without. No media queries using auto-fill seems to be a better option, because it just adjusts on its own to the screen size, making everything simpler, I guess.
<div class="container">
<div class="top_left"></div>
<div class="top_right"></div>
<div class="top_mid"> </div>
<div class="main"></div>
<div class="bot_left"></div>
<div class="bot_right"></div>
<div class="bot"></div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 100vh;
width: 100vw;
background-color: #E6E6E3;
overflow-x: hidden;
}
.container {
height: 100%;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto;
gap: 0px;
}
.top_left {
height: 100px;
display: grid;
background-color: white;
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 2;
border: 1px solid red;
}
.top_mid {
display: grid;
background-color: #36454F;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 3;
}
.top_right {
height: 100px;
display: grid;
background-color: white;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
border: 1px solid blue;
}
.main {
max-width: 80%;
margin: auto;
display: grid;
background-color: white;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 3;
grid-row-end: 5;
border-left: 1px solid #78BE21;
border-right: 1px solid #78BE21;
}
.bot_left {
height: 150px;
display: grid;
background-color: #BCD9ED;
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 5;
grid-row-end: 6;
border-top: 1px solid #191919;
border-bottom: 1px solid #191919;
}
.bot_right {
height: 150px;
display: grid;
background-color: #7F8778;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 5;
grid-row-end: 6;
border-top: 1px solid #191919;
border-bottom: 1px solid #191919;
}
.bot {
height: 250px;
display: grid;
background-color: deepskyblue;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 6;
grid-row-end: 8;
}

Related

How do I add an h1 element to a grid cell so that the h1 does not overlap

I'm just starting to work with grid for a website layout in html. My problem is that when creating the h1 within the grid, it slides out. Strangely, the h1 is not fixed in the grid.
Maybe someone can show me a short example how to get these elements into the grid cell so that they are adjusted
body {
margin: 0;
}
.container {
width: 100vw;
height: 100vh;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 50px 1fr 1fr 100px;
gap: 10px;
padding: 10px;
box-sizing: border-box;
}
.container div {
padding: 10px;
border: 1px solid #000000;
}
.gird-header {
grid-column-start: 1;
grid-column-end: 4;
}
header {
grid-area: header;
}
h1 {
text-align: center;
}
.content-game {
grid-row-start: 2;
grid-row-end: span 2;
grid-column-start: 1;
grid-column-end: 3;
}
.content-player {
grid-column-start: 3;
grid-column-end: 4;
;
grid-row-start: 2;
grid-row-end: span 1;
}
.content.buttons {
grid-column-start: 4;
grid-column-end: 4;
grid-row-start: 3;
grid-row-end: span 1;
}
.content-footer {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 4;
grid-row-end: span 1;
}
<div class="container">
<header class="grid-header">
<h1>Mensch Aerger dich nicht</h1>
</header>
<div class="content-game">Spiel</div>
<div class="content-player">Spieler</div>
<div class="content-buttons">Buttons</div>
<div class="content-footer">Footer</div>
</div>
It's because you've allocated a grid-area to the header element and you've not defined it (using grid-template-areas see here developer.mozilla.org/en-US/docs/Web/CSS/grid-template-areas) so the browser doesn't know where to put it. You've also got a syntax error in that you've defined .grid-header in your html and gird-header in your CSS. Remove the grid-area property and rename your class in your CSS and it'll work.
body {
margin: 0;
}
.container {
width: 100vw;
height: 100vh;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
/* made the first row 100px rather than 50px so the header will fit */
grid-template-rows: 100px 1fr 1fr 100px;
gap: 10px;
padding: 10px;
box-sizing: border-box;
}
.container div {
padding: 10px;
border: 1px solid #000000;
}
.grid-header { /* fixed syntax error */
grid-column-start: 1;
grid-column-end: 4;
}
header {
/* removed this grid-area: header; */
}
h1 {
text-align: center;
}
.content-game {
grid-row-start: 2;
grid-row-end: span 2;
grid-column-start: 1;
grid-column-end: 3;
}
.content-player {
grid-column-start: 3;
grid-column-end: 4;
;
grid-row-start: 2;
grid-row-end: span 1;
}
.content.buttons {
grid-column-start: 4;
grid-column-end: 4;
grid-row-start: 3;
grid-row-end: span 1;
}
.content-footer {
grid-column-start: 1;
grid-column-end: 4;
grid-row-start: 4;
grid-row-end: span 1;
}
<div class="container">
<header class="grid-header">
<h1>Mensch Aerger dich nicht</h1>
</header>
<div class="content-game">Spiel</div>
<div class="content-player">Spieler</div>
<div class="content-buttons">Buttons</div>
<div class="content-footer">Footer</div>
</div>

From 2 grids in line, to 2 grids below each other (responsiveness)

Simple code that took me +9 hours to make, sad reality. I tried a lot of tutorials and pages, but even they are unable to help me. I copied/modified multiple lines of code that I came across, but none does anything. This is just a learning page, where I'm trying to incorporate grid responsiveness for variety of devices.
I attached screens below, with how it is, and how I want it to be.
I believe it is quite simple thing to do, seing how some tutorial code consists of 1/2 lines, but it still seems too much for me to comprehend it.
Code below is work in progress, and responsiveness seems to be todays standard, and if anyone can explain to me how to do it, I will be grateful.
<div class="container">
<div class="top">
<div class="t_e1"></div>
<div class="t_e2"></div>
<div class="t_e3"></div>
</div>
<div class="main"></div>
<div class="bottom">
<div class="b_e1"></div>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 100vh;
width: 100fv;
background-color: brown;
}
.container {
min-height: 100%;
background-color: aquamarine;
grid-template-columns: 1fr 1fr;
display: grid;
}
.top {
background-color: blue;
display: grid;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
}
.t_e1 {
background-color: antiquewhite;
display: grid;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 3;
}
.main {
background-color: blueviolet;
display: grid;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 5;
}
.bottom {
background-color: chartreuse;
display: grid;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 5;
grid-row-end: 6;
}
.b_e1 {
background-color: coral;
display: grid;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 6;
}
[Currently][1]
[Desired effect][2]
[1]: https://i.stack.imgur.com/Vs0lR.jpg
[2]: https://i.stack.imgur.com/wUH7H.jpg
This is my solution:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 100vh;
width: 100fv;
background-color: brown;
}
.container {
min-height: 100%;
background-color: aquamarine;
grid-template-columns: 1fr 1fr;
display: grid;
}
.top {
background-color: blue;
display: grid;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
}
.t_e1 {
background-color: antiquewhite;
display: grid;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 3;
}
.main {
background-color: blueviolet;
display: grid;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 5;
}
.bottom {
background-color: chartreuse;
display: grid;
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 5;
grid-row-end: 6;
}
.b_e1 {
background-color: coral;
display: grid;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 6;
}
/*new code from here*/
#media (max-width: 640px) {
.top,
.bottom {
display: flex;
flex-direction: column-reverse;
}
.t_e1,
.t_e2,
.t_e3,
.b_e1 {
display: block;
}
.t_e1,
.b_e1 {
height: 15vh;
}
}
<div class="container">
<div class="top">
<div class="t_e1"></div>
<div class="t_e2"></div>
<div class="t_e3"></div>
</div>
<div class="main"></div>
<div class="bottom">
<div class="b_e1"></div>
</div>
</div>
The media query can be set to the desired "screen size" you want your responsive look to appear.
So the word responsiveness is a bit vague. Do you mean that the grids should be expanding to the width of window? if so that is the default behaviour. Or did you mean grid should stack up vertically when screen width gets small enough. I'd assume that's what you meant.
HTML
<div class="cont">
<div class="a"></div>
<div class="b"></div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
.cont {
display: flex;
margin-bottom: 10px;
}
.a,
.b {
flex: 1;
padding: 20px;
}
.a {
background: red;
}
.b {
background-color: blue;
max-width: 300px;
}
#media screen and (max-width: 1000px) {
.cont {
flex-direction: column;
}
.b {
max-width: none;
}
}
Explanation:
I create 2 divs
Each div has two more divs
Container divs have css flexbox property
Child divs get flex: 1 which tells them to expand (learn flexbox properly) https://www.codecademy.com/learn/learn-intermediate-css/modules/layout-with-flexbox
div B has max width so it expands but not more than that
I use media query max-width which makes it that styles under only apply IF the condition is met, the condition here is that width of browser is NOT more than 1000px
In there I change flexbox-direction to column, which means child divs will stack vertically not horizontally
I also remove the max-width on div B

CSS Styling covering HTML hyperlink

I have a hover effect creating a label over an image that is also a hyperlink to a separate website, however, the hover effect CSS styling is blocking my HTML hyperlink to the other site.
I have the image aligned with multiple other images in a section of my current project, whenever I alter the div to create the link, it misaligns the from the center.
Is there a quick fix that allows me to keep my image centered within CSS Grid and also allows the entire <div class="big-image">to be the hyperlink instead of just the image behind the label?
Here are the codeblocks for reference :
#projects{
background-color: var(--main-bg-color);
/* Test color: #D7D427 */
display: grid;
grid-template-columns: 1.25fr 2fr 1.25fr;
grid-template-rows: 10% 1fr 1fr 1fr ;
margin: 0;
box-sizing: border-box;
justify-items: center;
align-items: center;
}
#work-title{
grid-column-start: 2;
grid-column-end: 3;
}
.img1{
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 2;
grid-row-end: 3;
max-width: 100%;
max-height: 100%;
}
.img2{
grid-column-start: 3;
grid-column-end: 4;
grid-row-start: 2;
grid-row-end: 3;
}
.img3{
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 4;
grid-row-end: 5;
max-width: 100%;
}
.img4{
grid-column-start: 3;
grid-column-end: 4;
grid-row-start: 4;
grid-row-end: 5;
max-width: 100%;
}
.big-image{
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 3;
grid-row-end: 4;
max-width: 100%;
}
/* Image Borders */
.img1 img, .img2 img, .img3 img,.img4 img, .big-image img{
border: 3px solid var(--blue-border);
border-radius: 25px;
}
/* Image/Project :hover effects for Labels */
.big-image {
position: relative;
}
.big-label{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
color: var(--primary-color);
font-family: Verdana, Geneva, Tahoma, sans-serif ;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
opacity: 0;
border-radius: 25px ;
transition: opacity 0.25s;
/* pointer-events: none; */
}
.big-label > *{
transform: translateY(20px);
transition: transform 0.25s;
}
.big-label:hover{
opacity: 1;
}
.big-label:hover > *{
transform: translateY(0);
}
.big-label-title{
font-size: 2em;
font-weight: bold;
}
.big-label-description{
font-size: 1.25em;
margin-top: 0.25em;
}
<a href="https://mberti13.github.io/run-buddy/">
<div class="big-image">
<img id= "big-image" src="./assets/images/run-buddy-photo.jpg" width="500px" height="350px" alt="Website built for Run Buddy Inc. Made Using HTML and CSS">
<div class="big-label">
<div class="big-label-title">
<h3>Run Buddy</h3>
</div>
<div class="big-label-description">
<h5>HTML, CSS</h5>
</div>
</div>
</div></a>
This line is one Problem:
<h3>Run Buddy</h3>
You have to put Text inner the anchor link. Like that:
<h3>Run Buddy</h3>

Applying css to divs

I'm trying to develop set of div elements as in the following image expanding the full web page.But I don't have any idea how to do it.The div elements may have any heights and widths but the appreance should be as follows
please help me to do this
You could try using css grid. You divide the page in columns and rows
* {
margin: 0;
padding: 0;
}
.main-grid {
display: grid;
width: 500px;
height: 370px;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-gap: 10px;
}
.main-grid :nth-child(1) {
background: orange;
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: -1;
}
.main-grid :nth-child(2) {
background: blue;
grid-column-start: 2;
grid-column-end: -1;
grid-row-start: 1;
grid-row-end: 2;
}
.main-grid :nth-child(3) {
background: green;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: -1;
}
.main-grid :nth-child(4) {
background: yellow;
grid-column-start: 3;
grid-column-end: -1;
grid-row-start: 2;
grid-row-end: -1;
}
<div class="main-grid">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
This is a task for CSS grid - a very good tool for building responsive layouts. You can read more about it here: CSS Grid
HTML:
<html>
<div class="grid">
<div class="left"></div>
<div class="right-up"></div>
<div class= "right-down-a"></div>
<div class= "right-down-b"> </div>
</div>
</html>
CSS:
.grid {
display: grid;
position: absolute;
top: 0;
left: 0;
grid-template-columns: 33vw 33vw 33vw;
grid-template-rows: 50vh 50vh;
grid-template-areas:
"left right-up right-up"
"left right-down-a right-down-b";
grid-gap: 5px;
}
.left {
grid-area: left;
background-color: blue;
}
.right-up {
grid-area: right-up;
background-color: green;
}
.right-down-a{
grid-area: right-down-a;
background-color: red;
}
.right-down-b{
grid-area: right-down-b;
background-color: purple;
}
JSfiddle: Fiddle link

flexbox order: I want box to open under a element which is not a sibling

I want yellow to be under blue when screen size is small.
I know why it dont work now, since yellow is not a sibling to the other boxes, what can I do to fix this?
https://jsfiddle.net/0vbdcms0/
.container1 {
display: flex;
flex-flow: column;
}
.container2 {
display: flex;
flex-flow: row;
}
.box {
border: 1px solid black;
width: 50px;
height: 50px;
margin: 5px 5px 5px 5px;
}
#b1 {
background-color: red;
}
#b2 {
background-color: blue;
}
#b3 {
background-color: green;
}
#b4 {
background-color: yellow;
}
#media screen and (max-width:500px) {
.container2 {
display: flex;
flex-flow: column;
}
#b1 {
order: 1;
-webkit-order: 1;
}
#b2 {
order: 2;
-webkit-order: 2;
}
#b3 {
order: 4;
-webkit-order: 4;
}
#b4 {
order: 3;
-webkit-order: 3
}
}
<div class="container1">
<div class="container2">
<div class="box" id="b1"></div>
<div class="box" id="b2"></div>
<div class="box" id="b3"></div>
</div>
<div class="box" id="b4"></div>
</div>
I need to add some more text to satisfy SO, I think I got all relevant info in my text above :)
Using order will not work because order is in relation to the container not the DOM.
The CSS order property specifies the order used to lay out flex items in their flex container. Elements are laid out in the ascending order of the order value. Elements with the same order value are laid out in the order in which they appear in the source code.
MDN - "order"
CSS Grid can do this though.
Codepen Demo
.container {
padding: 5px;
display: grid;
width: 500px;
margin: 1em auto;
grid-auto-columns: 50px;
grid-auto-rows: 50px;
grid-gap: 5px;
}
.box {
border: 1px solid black;
width: 50px;
height: 50px;
}
#b1 {
background-color: red;
grid-column-start: 1;
grid-column-end: 2;
}
#b2 {
background-color: blue;
grid-column-start: 2;
grid-column-end: 3;
}
#b3 {
background-color: green;
grid-column-start: 3;
grid-column-end: 4;
}
#b4 {
background-color: yellow;
grid-column-start: 1;
grid-column-end: 2;
}
#media screen and (max-width: 500px) {
#b1,
#b2,
#b3,
#b4 {
grid-column-start: 1;
grid-column-end: 2;
}
#b3 {
grid-row-start: 4;
grid-row-end: 5;
}
#b4 {
grid-row-start: 3;
grid-row-end: 4;
}
}
<div class="container">
<div class="box" id="b1"></div>
<div class="box" id="b2"></div>
<div class="box" id="b3"></div>
<div class="box" id="b4"></div>
</div>
It might be possible in Flexbox but the necessary properties are not yet fully supported in all browsers. I think it's Firefox only as of the time of writing.