I got a problem with my grid. Doing it for the first time, so sorry for that beginner question.
What I want to achieve is shown in this image (black borders):
Unfortunately, I already got stuck on my first line of code:
body {
display: grid;
grid-template-columns: 10% auto 10% 10% 10%;
grid-template-rows: 60px auto; /*Isn't it recognizing my second row?*/
}
.temp {
background-color: black;
grid-column: 1 / 2;
grid-row: 2 / 3;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Max S. Rodenkirchen - Sinn Sehen - FH AC 2022 - bei Eva Vitting</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class = "menu">
</div>
<div class = "temp">
</div>
<div class = "draw">
</div>
<div class = "label">
</div>
<div class = "slider">
</div>
<div class = "check">
</div>
</body>
</html>
The temp class should be on the left-hand side in the second row.
Another question I have is probably a bit more advanced.
The square area is going to be a P5 canvas that is always squared and should always stay in full grid row height.
I was wondering if I need to code something like this instead:
grid-template-columns: auto 60% auto auto auto;
But I am pretty sure I am missing something here.
Hope for some help :) This is going to be for a university project.
Max
It seems easier to take the top menu out of the grid as its dimensions don't seem to be directly related to the rest of the elements.
By contrast the big square looks as though it is 8 times the width of the narrower columns.
The big square can be made to take the same height as its width by giving it aspect-ratio: 1 / 1;
So we can define a one row grid with 4 columns at 1fr and the square at 8fr.
To make it easy for it to be centered this snippet puts it inside containers which have flex.
* {
margin: 0;
padding: 0;
}
body {
width: 100vw;
}
.menu {
width: 100%;
height: 60px;
}
.outer {
display: flex;
justify-content: center;
margin-top: 1vw;
}
.container {
width: 95%;
display: grid;
grid-template-columns: 1fr 8fr 1fr 1fr 1fr;
grid-template-rows: 1fr;
gap: 1vw;
}
.menu,
.container div {
border: 1px solid black;
box-sizing: border-box;
}
.temp {
grid-row: 1 / 2;
}
.draw {
grid-column: 2 / 3;
aspect-ratio: 1 / 1;
}
.label {
grid-column: 3 / 4;
}
.slider {
grid-column: 4 / 5;
}
.check {
grid-column: 5 / 6;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Max S. Rodenkirchen - Sinn Sehen - FH AC 2022 - bei Eva Vitting</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="menu">
</div>
<div class="outer">
<div class="container">
<div class="temp">
</div>
<div class="draw">
</div>
<div class="label">
</div>
<div class="slider">
</div>
<div class="check">
</div>
</div>
</div>
</body>
</html>
Your second row is there and the .temp div is place in that row. It's just that because the .temp div has no content and the second row has a height of auto that row and the .temp div inside it have zero height and so are not visible. You can see what is going on more easily by adding outlines and minimum height:
body {
display: grid;
grid-template-columns: 10% auto 10% 10% 10%;
grid-template-rows: 60px auto;
}
div {
outline: 1px solid red;
min-height: 20px;
}
.temp {
background-color: black;
grid-column: 1 / 2;
grid-row: 2 / 3;
}
<div class="menu">
</div>
<div class="temp">
</div>
<div class="draw">
</div>
<div class="label">
</div>
<div class="slider">
</div>
<div class="check">
</div>
Related
I have the following HTML code:
<body>
<div id="id1" style="width: 100vw; height: 100vh; display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; background-color: khaki;">
<div id="id2" style="width: 100%; height: 100%; grid-row: 1; grid-column: 1;">
<div id="id3" style="width: 5000px; height: 3000px; background: dodgerblue"></div>
</div>
<div id="id4"style="width: 100%; height: 100%; grid-row: 1; grid-column: 2;"></div>
</div>
</body>
In my case "id1" div has size 1860px X 1340px. I expect "id2" and "id4" would both have 930px X 1340px size, and "id2" would have scrollbars because "id3" has greater size. In reality I get "id2" has 5000px X 3000px size, "id4" has 0px X 3000px size, and scrollbars appear in "id1", not "id2". If I remove "id3", "id2" and "id4" has expected sizes. How to make inserting "id3" not change the size of "id2" and "id3", but only lead to the appearance of scrollbars in "id2"?
This is simplified sample. In real life the size of "id1" is not known beforehand, so I cannot use 50vw X 100vh size for "id2" and "id4".
I dont understand the body of your question, but I do understand "forbid div enlarge its parents", yes, sometimes a div is bigger than its parent, there are ways to fix this:
Overflow Hidden Solution
<div id="parent" style="overflow:hidden">
<div id="child">
</div>
</div>
so you put the overflow hidden style on your parent, the child is still big but the only part of the child that you will be seen is the part that is within the parent's view box, the rest of the child is hidden. The attribute is on id2
<!DOCTYPE html>
<html>
<head >
<title>Title of the document</title>
</head>
<body>
<div id="id1" style="width: 100vw; height: 100vh; display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; background-color: khaki;">
<div id="id2" style="overflow:hidden;width: 100%; height: 100%; grid-row: 1; grid-column: 1;">
<div id="id3" style="width: 5000px; height: 3000px; background: dodgerblue"></div>
</div>
<div id="id4"style="width: 100%; height: 100%; grid-row: 1; grid-column: 2;"></div>
</div>
</body>
</html>
Overflow Scroll
This solution is like the one above but allows you to scroll to see the rest of the child element. I added the attribute to id2.
Here is the output of the code::
<!DOCTYPE html>
<html>
<head >
<title>Title of the document</title>
</head>
<body>
<div id="id1" style="width: 100vw; height: 100vh; display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; background-color: khaki;">
<div id="id2" style="overflow:scroll;width: 100%; height: 100%; grid-row: 1; grid-column: 1;">
<div id="id3" style="width: 5000px; height: 3000px; background: dodgerblue"></div>
</div>
<div id="id4"style="width: 100%; height: 100%; grid-row: 1; grid-column: 2;"></div>
</div>
</body>
</html>
Summary
You have to look into the overflow attribute, it gives you a lot of options on how to deal with children that are larger than their parents. If this does not answer your question, comment below with a more specific question or edit your question.
So, I'm in the process of learning CSS Grid and I'm running into a couple of issues here. I've been messing around in the inspection panel and removing some CSS every now and then and I can't come to a solution.
First, the background-image needs to be a bit lower, but the container doesn't -- so I basically need a higher view of the photo that's shown, so it's not showing the center of the photo only if that makes sense.
Second, any time I remove position: absolute; from .bg-img the causes the second screenshot to occur, shrinking my grid...I need the grid to preserve its division of the entire page into grid-template-columns: 1fr 4fr 1fr; and not adjust to the top left corner of the page as it does.
Any ideas here/concepts I'm missing? Learning web-dev is awesome so far, but fixing issues when you don't know what's wrong is rough! haha.
Screenshot of Page:
Removing the css for position: absolute on .bg-img{} causes the below view:
Screenshot of Page post-removal:
Code:
body,
html {
background-color: black;
}
/* bg image styline */
.bg-img {
position: absolute;
background-image: url(/Practice_Site-main/imgs/nature.jpg);
background-repeat: no-repeat;
background-position: center;
outline: solid white;
border-radius: 60px;
width: 100%;
height: 50%;
filter: brightness(0.7);
}
/* top of site heading and navbar */
#mainGrid {
display: grid;
grid-template-columns: 1fr 4fr 1fr;
grid-template-rows: repeat(5, 5fr);
}
#topContainer {
display: grid;
grid-template-columns: 1fr 4fr 1fr;
position: relative;
grid-column: 2;
grid-row: 2;
gap: 10px;
background-color: #09cc43;
border-radius: 40px;
outline: solid black;
opacity: 0.85;
filter: brightness(1) !important;
justify-content: center;
}
#siteHeader {
grid-column: 2;
justify-self: center;
text-align: center;
}
nav {
grid-column: 2;
grid-row: 2;
justify-items: center;
}
#primary-navigation {
grid-column: 2;
list-style-type: none;
justify-items: center;
}
ul {
grid-column: 2;
display: grid;
grid-template-columns: repeat(3, 1fr);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TESTER</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="mainGrid">
<div class="bg-img">
<div id="topContainer">
<h1 id="siteHeader">Testing Header</h1>
<nav>
<ul id="primary-navigation" class="primary-navigation">
<li class="active">
<span aria-hidden="true"></span>Home
</li>
<li>
<a href="photos.html">
<span aria-hidden="true"></span>Photos</a>
</li>
<li>
<span aria-hidden="true"></span>About
</li>
</ul>
</nav>
</div>
</div>
</div>
<div class="homeFolio"></div>
</body>
</html>
Because position: absolute; the width and height of the bg-img element is determined by the parent element with position: relative; set, if the parent element does not set position: relative, the width and height of the bg-img will be determined according to the body element, This is what you delete position: absolute cause a width and height error
I have five rows in a grid layout.
There is a header row at the top.
I want the content row to fill everything it can of the available space.
I want the footer row to be at the bottom.
Between the header, content and footer rows I have two rows which just adds height spacing at 15px.
Here is HTML-code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body class="body">
<div class="headerRow">
<div>Title</div>
<div>Fill</div>
<div>Image</div>
</div>
<div style="height: 15px;"></div>
<div>
content
</div>
<div style="height: 15px"></div>
<div class="footerRow">
<div>foo</div>
<div>bar</div>
</div>
</body>
</html>
Here is my CSS-code:
.body {
margin: 15px;
background: lime;
display: grid;
grid-template-rows: auto auto 1fr auto auto;
}
.headerRow {
display: grid;
grid-template-columns: auto 1fr auto;
background-color: #2196F3;
}
.footerRow {
display: grid;
grid-template-columns: auto auto;
background-color: red;
}
I got my "headerRow" to show three columns with the middle column to fill every available space with this line in the CSS:
grid-template-columns: auto 1fr auto;
So I tried this line in the .body-block in my CSS:
grid-template-rows: auto auto 1fr auto auto;
But that didn't work :'(
What is the problem?
Maybe like this:
html, body {
margin: 0;
padding: 0;
height: 100%;
}
.container {
margin: 15px;
background: lime;
display: grid;
grid-row-gap: 15px;
align-content: space-between;
height: 100%;
}
.headerRow {
display: grid;
grid-template-columns: auto 1fr auto;
background-color: #2196F3;
justify-items: center;
}
.footerRow {
display: grid;
grid-template-columns: auto auto;
background-color: red;
justify-content: space-between;
}
<!DOCTYPE html>
<html lang="en">
<div class="container">
<head>
<title>App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body class="body">
<div class="headerRow">
<div>Title</div>
<div>Fill</div>
<div>Image</div>
</div>
<div>
content
</div>
<div class="footerRow">
<div>foo</div>
<div>bar</div>
</div>
</div>
</body>
</html>
Why not use hr tag for adding space instead of another rows. Also if you want to make the three columns inside headerRow, Try adding float : left or adding columns from bootstrap would solve your problem.
I have some follow up questions after trying #Nikola Pavicevic solution:
Question 1:
In the html, body CSS-block the height: 100% seems to do the trick for me.But it makes the page in my web browser to have a vertical scrollbar.This scrollbar seems to be needed to show the "footer"-row.Is there anyway to decrease the height of the content row so the header row and footer row is always displayed?
Question 2:
The "content-row" seems to not fill all space available in the page.
I can see this by putting a background in the "content-row".
Is there a way to make sure the "content-row" fills all available space?
But keeps the "header-row" and "footer-row" visible.
Updated HTML:
<!DOCTYPE html>
<html lang="en">
<div class="container">
<head>
<title>App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body class="body">
<div class="headerRow">
<div>Title</div>
<div>Fill</div>
<div>Image</div>
</div>
<div class="spacerRow"></div>
<div class="contentRow">
content
</div>
<div class="spacerRow"></div>
<div class="footerRow">
<div>foo</div>
<div>bar</div>
</div>
</div>
</body>
</html>
Updated CSS
html, body {
margin: 0;
padding: 0;
height: 100%;
}
.container {
margin: 15px;
background: lime;
display: grid;
align-content: space-between;
height: 100%;
}
.headerRow {
display: grid;
grid-template-columns: auto 1fr auto;
background-color: #2196F3;
justify-items: center;
}
.footerRow {
display: grid;
grid-template-columns: auto auto;
background-color: red;
justify-content: space-between;
}
.spacerRow {
height: 15px;
background-color: yellow;
}
.contentRow {
background-color: purple;
}
This question already has answers here:
In CSS what is the difference between "." and "#" when declaring a set of styles? [duplicate]
(9 answers)
Closed 3 years ago.
All seems correct, but my CSS does not get loaded. It appears in the console on devTools (using chrome) but no CSS actually happens.
Have been through the numerous questions about this but no answers there provided a solution.
Have disabled the cache in devTools, no change, re-enabled it no change.
Have triple checked the spelling of my file names.
Moved the file to the same directory
Tried adding type="text/css" - no change.
the HTML:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Army Builder</title>
<link href="styles.css" rel="stylesheet">
</head>
<div id='Container'>
<div id="unitList">Unit List
<ul id="list" style="list-style-type:none"></ul>
</div>
<div id="roster">Army Roster
<ul id="armyRoster" style="list-style-type:none"></ul>
</div>
<script src="scripts/main.js"></script>
</div>
the CSS:
#charset "utf-8";
.Container {
background-color: blue;
display: grid;
grid-template-rows: [one] 50px [two] 50px [rowEnd];
grid-template-columns: [one] 150px [two] 300px [three] 150px [end];
grid-column-gap: 5px;
grid-row-gap: 10px;
text-align: center;
}
.unitList {
background-color: red;
grid-column: one / two;
grid-row: one / rowEnd;
}
.roster {
background-color: green;
grid-column: three / end;
grid-row: one / rowEnd;
}
I am fairly certain it must be a simply error, but my eyes can't see it ?
You are using ids in your HTML but using class selectors in your CSS. Change your CSS to:
#Container {
background-color: blue;
display: grid;
grid-template-rows: [one] 50px [two] 50px [rowEnd];
grid-template-columns: [one] 150px [two] 300px [three] 150px [end];
grid-column-gap: 5px;
grid-row-gap: 10px;
text-align: center;
}
#unitList {
background-color: red;
grid-column: one / two;
grid-row: one / rowEnd;
}
#roster {
background-color: green;
grid-column: three / end;
grid-row: one / rowEnd;
}
Alternatively, you can change your HTML to use classes instead of ids:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Army Builder</title>
<link href="styles.css" rel="stylesheet">
</head>
<div class='Container'>
<div class="unitList">Unit List
<ul id="list" style="list-style-type:none"></ul>
</div>
<div class="roster">Army Roster
<ul id="armyRoster" style="list-style-type:none"></ul>
</div>
<script src="scripts/main.js"></script>
</div>
There is mistake in you css.
In Html you are writing id which need to define in css as # and class as .
I am trying to combine CSS grid with position in order to have my page effectively adjust to different screen sizes. My thinking is to have one grid design for mobile and one for desktop using #media(screen-width).
My thinking would be to have the grid blocks be driven by grids and have the content in the grid blocks be driven by position.
Now my understanding is that grid would create a imaginary rectangle based on the columns and rows and that when i use position that it would latch on to the one corner of the grid block.
Question
01 Where is my misunderstanding of this concept?
02 Are there better ways to do this?
My code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Grid</title>
<style>
.wrapper_main {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-auto-rows: minmax(100px, auto);
grid-gap: 1em;
justify-items: stretch;
align-items: stretch;
}
.box0 {
grid-column: 1/6;
grid-row: 1;
border: 1px solid #333;
}
.box1 {
grid-column: 1/6;
grid-row: 2/6;
border: 1px solid #333;
}
.seconds01 {
position: fixed;
right: 20px;
top: 50px;
}
.seconds05 {
position: fixed;
right: 100px;
top: 50px;
}
</style>
</head>
<body>
<div class="wrapper_main">
<div class="box box0">Box 0</div>
<div class="box box1">
<a class="seconds01"><img src="img/Candle_Box_Tag.png"></a>
<a class="seconds05"><img src="img/Candle_Box_Tag.png"></a>
</div>
</div>
</body>
</html>
What i'm getting.
What i want.
Be aware when you positioning the element. When you apply position:fixed; its won't respect it parent containers it will strictly stick to the viewport's view.
Perhaps you can try with position:absolute; to the child element and give position:relative; to its parent container so that it will be in its parent container
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Grid</title>
<style>
.wrapper_main{
display:grid;
grid-template-columns:repeat(5, 1fr);
grid-auto-rows:minmax(100px, auto);
grid-gap:1em;
justify-items:stretch;
align-items:stretch;
}
.box0{
grid-column:1/6;
grid-row:1;
border:1px solid #333;
}
.box1{
grid-column:1/6;
grid-row:2/6;
border:1px solid #333;
position:relative;
}
.seconds01{
position:absolute;
right:20px;
top:50px;
}
.seconds05{
position:absolute;
right:100px;
top:50px;
}
</style>
</head>
<body>
<div class="wrapper_main">
<div class="box box0">Box 0</div>
<div class="box box1">
<a class="seconds01"><img src="img/Candle_Box_Tag.png"></a>
<a class="seconds05"><img src="img/Candle_Box_Tag.png"></a>
</div>
</div>
</body>
</html>