Why is the align-self/justify-self: start/center/end (or any variation) working within my 'nestedheader' container. I'm trying to get the Header box on the left-hand side, but I feel like it already should be as it is in a grid.
.header {
grid-area: header;
background-color: #222222;
}
.nestedheader {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 70px;
grid-gap: 20px;
grid-template-areas: "headername headername headercopy headercopy" "headername headername headercopy headercopy";
color: white;
font-family: 'Rubik', sans-serif;
}
.headername {
grid-area: headername;
font-size: 30px;
padding-right: 20px;
border: 5px solid red;
justify-self: start;
}
.headercopy {
grid-area: headercopy;
font-weight: lighter;
padding-right: 20px;
border: 5px solid red;
}
Here is the CodePen: https://codepen.io/anon/pen/dezVpO
You code was quite fine. The only thing you did not check was the size of the .header and of the .nestedheader.
They did not fill the whole first row.
Check out the changes on header and nestedheader. I simply set the width to 100%.
html, body {
margin: 0;
font-size: 100%;
}
.container {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: minmax(100px, auto);
grid-gap: 7px;
grid-template-areas:
"header header header header"
"intro intro main main"
"intro intro main main"
"bottom bottom bottom bottom"
"bottom bottom bottom bottom"
"footer footer footer footer";
text-align: center;
}
.container > div {
padding: 5px;
border: 3px solid #222222;
display: flex;
align-items: center;
justify-content: center;
color: #2B0E24;
}
/* --- Header Start --- */
.header {
grid-area: header;
box-sizing: border-box;
width: 100%;
background-color: #222222;
}
.nestedheader {
box-sizing: border-box;
width: 100%;
height: 100%;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 20px;
grid-template-areas:
"headername headername headercopy headercopy";
color: white;
font-family: 'Rubik', sans-serif;
}
.headername {
grid-area: headername;
font-size: 30px;
padding-right: 20px;
border: 5px solid red;
}
.headercopy {
grid-area: headercopy;
font-weight: lighter;
padding-right: 20px;
border: 5px solid red;
}
/* --- Header End --- */
.intro {
grid-area: intro;
height: 450px;
}
.main {
grid-area: main;
height: 450px;
}
.bottom {
grid-area: bottom;
height: 800px;
}
.footer {
grid-area: footer;
height: 325px;
background-color: #222222;
color: white;
}
/* --- Footer Start --- */
.footertext {
color: white;
font-family: 'Rubik', sans-serif;
font-size: 30px;
}
.footerlinks {
font-size: 16px;
font-weight: lighter;
}
a {
color: #20bde5;
text-decoration: none;
padding: 10px;
}
a:hover {
color: white;
}
/* --- Footer End --- */
<div class="container">
<!-- Header Start -->
<div class="header">
<div class="nestedheader">
<div class="headername">Header Name</div>
<div class="headercopy">This is the page copy</div>
</div>
</div>
<!-- Header End -->
<div class="intro">Intro</div>
<div class="main">Main</div>
<div class="bottom">Bottom</div>
<!--Footer Start-->
<div class="footer">
<div class="footertext">
Here we go...<br><br>
<div class="footerlinks">
about
contact
social
</div>
</div>
<!--Footer End-->
</div>
Related
Here is the JSfiddle complete code link:
CODE
my clock code output
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background: #0b172a;
font-family: sans-serif;
}
#container {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 5rem;
color: white;
text-transform: uppercase;
}
.clock-ctr {
position: relative;
border: 2px solid white;
height: 80vh;
width: 80vw;
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
}
.hour-ctr {
grid-area: hour;
}
.min-ctr {
grid-area: min;
}
.sec-ctr {
grid-area: sec;
}
.ampm {
grid-area: ampm;
background: #bc4123;
}
.time-ctr {
position: absolute;
height: 70%;
width: 90%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
gap: 10px;
grid-template-areas: "hour min sec" "ampm ampm ampm";
}
h1 {
margin-bottom: 1rem;
letter-spacing: 8px;
font-size: 2.5rem;
}
.time-box {
background: #bc4123;
border-radius: 10px;
display: grid;
justify-items: center;
align-items: center;
height: auto;
text-align: center;
font-weight: bold;
font-size: 28px;
}
<div id="container">
<h1 class="clock-title">Clock</h1>
<div class="clock-ctr">
<div class="time-ctr">
<div class="hour-ctr time-box">
<p class="hour-value">00</p>
<p class="hour-title">Hour</p>
</div>
<div class="min-ctr time-box">
<p class="min-value">00</p>
<p class="min-title">Minute</p>
</div>
<div class="sec-ctr time-box">
<p class="sec-value">00</p>
<p class="sec-title">Second</p>
</div>
<p class="ampm time-box">AM</p>
</div>
</div>
</div>
Can any one tell me how to improve this code
I tried to make is completely responsive but it is not not working,
I tired to use flex to make the element appear in center of page.
Then I use grid to create the clock layout and i didn't knew how to align the cells so I used grid again in them. I was using rem and em to make responsive code but it didn't work out well. please review my code.
This is because of the font-size of the time-box div that is not responsive (28px whatever the device size), To make it responsive I added media queries to change the font depending on the device width, As presented in this example:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background: #0b172a;
font-family: sans-serif;
}
#container {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 5rem;
color: white;
text-transform: uppercase;
}
.clock-ctr {
position: relative;
border: 2px solid white;
height: 80vh;
width: 80vw;
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
}
.hour-ctr {
grid-area: hour;
}
.min-ctr {
grid-area: min;
}
.sec-ctr {
grid-area: sec;
}
.ampm {
grid-area: ampm;
background: #bc4123;
}
.time-ctr {
position: absolute;
height: 70%;
width: 90%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
gap: 10px;
grid-template-areas: "hour min sec" "ampm ampm ampm";
}
h1 {
margin-bottom: 1rem;
letter-spacing: 8px;
font-size: 2.5rem;
}
.time-box {
background: #bc4123;
border-radius: 10px;
display: grid;
justify-items: center;
align-items: center;
height: auto;
text-align: center;
font-weight: bold;
}
#media (min-width:768px) {
.time-box {
font-size: 18px;
}
}
#media (min-width:1024px) {
.time-box {
font-size: 22px;
}
}
#media (min-width:1280px) {
.time-box {
font-size: 28px;
}
}
<div id="container">
<h1 class="clock-title">Clock</h1>
<div class="clock-ctr">
<div class="time-ctr">
<div class="hour-ctr time-box">
<p class="hour-value">00</p>
<p class="hour-title">Hour</p>
</div>
<div class="min-ctr time-box">
<p class="min-value">00</p>
<p class="min-title">Minute</p>
</div>
<div class="sec-ctr time-box">
<p class="sec-value">00</p>
<p class="sec-title">Second</p>
</div>
<p class="ampm time-box">AM</p>
</div>
</div>
</div>
You can as well use calc() function, so you can calculate your font size relative to the screen width like this:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background: #0b172a;
font-family: sans-serif;
}
#container {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 5rem;
color: white;
text-transform: uppercase;
}
.clock-ctr {
position: relative;
border: 2px solid white;
height: 80vh;
width: 80vw;
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
}
.hour-ctr {
grid-area: hour;
}
.min-ctr {
grid-area: min;
}
.sec-ctr {
grid-area: sec;
}
.ampm {
grid-area: ampm;
background: #bc4123;
}
.time-ctr {
position: absolute;
height: 70%;
width: 90%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
gap: 10px;
grid-template-areas: "hour min sec" "ampm ampm ampm";
}
h1 {
margin-bottom: 1rem;
letter-spacing: 8px;
font-size: 2.5rem;
}
.time-box {
background: #bc4123;
border-radius: 10px;
display: grid;
justify-items: center;
align-items: center;
height: auto;
text-align: center;
font-weight: bold;
font-size: calc(18px + 0.390625vw);
}
<div id="container">
<h1 class="clock-title">Clock</h1>
<div class="clock-ctr">
<div class="time-ctr">
<div class="hour-ctr time-box">
<p class="hour-value">00</p>
<p class="hour-title">Hour</p>
</div>
<div class="min-ctr time-box">
<p class="min-value">00</p>
<p class="min-title">Minute</p>
</div>
<div class="sec-ctr time-box">
<p class="sec-value">00</p>
<p class="sec-title">Second</p>
</div>
<p class="ampm time-box">AM</p>
</div>
</div>
</div>
the issue with the CSS code in the Stack Overflow question is that the left and right values for the #nav element are set to 0. This causes the element to take up the full width of its parent element, which is likely not the intended behavior.
To fix this issue, you can try setting the left and right values to auto like this:
#nav {
position: fixed;
top: 0;
left: auto;
right: auto;
width: 100%;
height: 60px;
background-color: white;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
z-index: 1000;
}
With this change, the #nav element will no longer take up the full width of its parent element and will instead be positioned at the top of the page with its width set to 100%.
I have started using Grid CSS and i am stuck in building a Layout using Grid system.
I am looking to build a layout where the column would overlap a row and take full height. I have attached a screenshot of the layout and what i have tried so far.
body {
margin: 40px;
}
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
}
.header {
grid-area: header;
}
.wrapper {
display: grid;
grid-gap: 10px;
grid-template-columns: 120px 120px 120px;
grid-template-areas:
"header header header"
"sidebar content content";
background-color: #fff;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
.header {
background-color: #999;
}
.overlay {
background-color: red;
z-index: 10;
grid-column: content-start / content-end;
grid-row: header-start / content-end;
}
<div class="wrapper">
<div class="box header">Header</div>
<div class="box sidebar">Sidebar</div>
<div class="overlay">Content</div>
</div>
Depending on how you want to overlap the following code works, but need to be adapted:
There's a lot of other way to do this...
body {
margin: 40px;
}
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
}
.header {
grid-area: header;
}
.wrapper {
display: grid;
grid-gap: 10px;
grid-template-columns: 120px 120px 120px;
grid-template-rows: 30px 30px auto auto;
grid-template-areas:
"header header header"
"sidebar content content";
background-color: #fff;
color: #444;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
.header {
background-color: #999;
grid-row: 1 / 3;
}
.sidebar {
background-color: red;
z-index: 10;
grid-row: 2 / 4;
height: 300px;
}
.overlay {
background-color: red;
z-index: 10;
grid-column: content-start / content-end;
grid-row: 3 / 4;
}
<div class="wrapper">
<div class="box header">Header</div>
<div class="box sidebar">Sidebar</div>
<div class="overlay">Content</div>
</div>
I am trying to center anchor tags (Link 1, Link 2, Link 3, Link 4) in a CSS grid nested div. I also want the links to have padding around them, but it seems I have missed something. Have managed so far as below. Also, how can I achieve flexible row height for the graph 1 and graph 2 boxes without mentioning the height in the grid-template-rows?
I am trying to achieve something like this image:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', Verdana, Geneva, Tahoma, sans-serif;
}
main {
width: 98 %;
margin: 5 px 5 px;
border: 1 px solid grey;
display: grid;
color: white;
grid-template-rows: 60px 40px 300px 300px 60px;
grid-template-columns: repeat(4, 1fr);
grid-gap: 5px;
}
.div-header {
background-color: #ff4757;
display: grid;
grid-column: 1 / -1;
}
.page-navbar {
grid-template-rows: 1fr;
grid-column: 1 / -1;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10 px;
background-color: #EEF5FB;
color: #000;
}
.div-box1 {
color: #000;
border-right: 1px solid skyblue;
}
.div-link {
text-align: center;
}
.page-navbar a {
color: #000;
text-decoration: none;
}
.div-box2 {
border-right: 1px solid skyblue;
}
.div-box3 {
border-right: 1px solid skyblue;
}
.div-box4 {}
.div-main-1 {
color: #000;
background-color: lightblue;
grid-column: 1 / -1;
}
.div-main-2 {
color: #000;
background-color: lightblue;
grid-column: 1/-1;
}
.div-footer {
background-color: steelblue;
grid-column: 1/-1;
}
nav {
width: 100%;
background-color: steelblue;
padding-left: 30px;
padding-right: 10px;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
display: inline-block;
}
.nav-links {
list-style: none;
display: inline-block;
display: flex;
}
.nav-item a {
padding: 10px 15px;
text-decoration: none;
color: #fff;
text-transform: uppercase;
}
<main>
<div class="div-header">
<nav>
<a href="#" class="logo">
<img src="water_drop_black_24dp.svg" alt="logo" />
</a>
<ul class="nav-links">
<li class="nav-item">Page 1</li>
<li class="nav-item">Page 2</li>
<li class="nav-item">Profile</li>
<li class="nav-item">Logout</li>
</ul>
</nav>
</div>
<div class="page-navbar">
<div class="div-box1">
Link 1
</div>
<div class="div-box2">
Link 2
</div>
<div class="div-box3">
Link 3
</div>
<div class="div-box4">
Link 4
</div>
</div>
<div class="div-main-1">Chart 1</div>
<div class="div-main-2">Chart 2</div>
<div class="div-footer">Footer</div>
</main>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
I use grids to customise my html layout and it does not work as I want. I want my nav to take all available width.
Here is my code:
header {
text-align: center;
grid-area: header;
border: solid;
}
#nav1 {
text-decoration: none;
border: solid;
background-color: green;
grid-area: MH;
width: 1fr;
text-align: center;
}
#nav2 {
text-decoration: none;
border: solid;
background-color: red;
grid-area: MC;
width: 1frs;
text-align: center;
}
#nav3 {
text-decoration: none;
border: solid;
background-color: yellow;
grid-area: start;
width: 1fr;
text-align: center;
}
footer {
grid-area: footer;
border: solid;
background-color: orange;
margin-top: 0px;
height: 100px;
}
main {
text-decoration: none;
border: solid;
background-color: gray;
grid-area: main;
height: 500px;
color: #ff00eb;
border-color: black;
}
aside {
width: 0.5fr;
border: solid;
background-color: purple;
grid-area: aside;
}
#big-container,
nav {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
grid-template-areas: 'header header header' 'MH MC start' 'main main aside' 'footer footer footer ';
grid-gap: 0px;
padding: 0px;
<!-- detta är min header-->
<header>
<h1></h1>
</header>
<div id="big-container">
<nav>
mitt hus
min calender
main
</nav>
<main>
main
</main>
<aside>
aside
</aside>
<footer>
footer
</footer>
</div>
I would assign to the nav bar the display flex attribute. Like that:
header {
text-align: center;
grid-area: header;
border: solid;
}
footer {
grid-area: footer;
border: solid;
background-color: orange;
margin-top: 0px;
height: 100px;
}
main {
text-decoration: none;
border: solid;
background-color: gray;
grid-area: main;
height: 500px;
color: #ff00eb;
border-color: black;
}
aside {
width: 0.5fr;
border: solid;
background-color: purple;
grid-area: aside;
}
#big-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
grid-template-areas: 'header header header' 'MH MC start' 'main main aside' 'footer footer footer ';
grid-gap: 0px;
padding: 0px;
}
nav {
width: 100%;
background: lightblue;
display: flex;
justify-content: center;
}
nav > a {
display: block;
text-decoration: none;
border: solid;
text-align: center;
width:100%;
}
nav > a:nth-child(1) {
background-color: green;
}
nav > a:nth-child(2) {
background-color: green;
}
nav > a:nth-child(3) {
background-color: green;
}
<!-- detta är min header-->
<header>
<h1></h1>
</header>
<nav>
mitt hus
min calender
main
</nav>
<div id="big-container">
<main>
main
</main>
<aside>
aside
</aside>
<footer>
footer
</footer>
</div>
Here is a simplified version (in terms of code) of what you wanna achieve:
body {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-areas: "header header header" "nav nav nav" "main main aside" "footer footer footer ";
}
header {
text-align: center;
grid-area: header;
border: solid;
}
nav {
grid-area: nav;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
#nav1 {
text-decoration: none;
border: solid;
background-color: green;
text-align: center;
}
#nav2 {
text-decoration: none;
border: solid;
background-color: red;
text-align: center;
}
#nav3 {
text-decoration: none;
border: solid;
background-color: yellow;
text-align: center;
}
main {
text-decoration: none;
border: solid;
background-color: gray;
grid-area: main;
height: 500px;
color: #ff00eb;
border-color: black;
}
aside {
width: 0.5fr;
border: solid;
background-color: purple;
grid-area: aside;
}
footer {
grid-area: footer;
border: solid;
background-color: orange;
margin-top: 0px;
height: 100px;
}
<header>
header
</header>
<nav>
mitt hus
min calender
main
</nav>
<main>
main
</main>
<aside>
aside
</aside>
<footer>
footer
</footer>
I would like to fix both the header and the sidebars positioned with css-grids. I tried to use position:fixed but it ends up messing up the interface. Is there a simple way to do this? Or is the only way relative positioning and height/width adjustments? I wouldn't want to make the use of css-grids pointless...
Here's the link to the Codepen if you find it easier (code without lipsum included below): https://codepen.io/fergos2/pen/MWgLqgL?editors=1100
Thanks in advance for helping this newbie!
<div class="container">
<header class="header pd">Header</header>
<div class="left-sidebar pd">
<div class="box-1 pd">
Box-1
</div>
<footer class="footer pd">
Footer
</footer>
</div>
<div class="main-content pd">
Main content
</div>
<div class="right-sidebar pd">
<div class="box-2 pd">
Box-2
</div>
<div class="box-3 pd">
Box-3
</div>
</div>
</div>
.container {
width: 1000px;
margin: 30px auto;
background-color: #eee;
display: grid;
grid-template-rows: min-content 1fr;
grid-template-columns: 1fr 2fr 1fr;
grid-gap: 15px;
grid-template-areas: "head head head"
"leftbar main rightbar";
& > * {
background-color: pink;
color: #ggg;
font-size: 20px;
font-family: sans-serif;
}
}
.pd {
padding: 15px;
}
.header {
grid-area: head;
}
.left-sidebar {
grid-area: leftbar;
display: flex;
flex-direction: column;
justify-content: flex-start;
.box-1 {
color: red;
border: 1px solid purple;
margin-bottom: 5px;
}
.footer {
color: green;
border: 1px solid purple;
}
}
.main-content {
grid-area: main;
}
.right-sidebar {
grid-area: rightbar;
display: flex;
flex-direction: column;
justify-content: flex-start;
.box-2 {
color: red;
border: 1px solid purple;
margin-bottom: 5px;
}
.box-3 {
color: green;
border: 1px solid purple;
}
}
UPDATE:
position: -webkit-sticky;
position: sticky;
top: 0;
seems to work for the header but not for the sidebars (even when setting top:200px for example). Any suggestions on what to do with the sidebars?