FOR EXAMPLE, SEE THIS PEN
I have 6 menu items and I want to achieve this grid layout without nesting. I'm a newbie in CSS grid and I thought using the CSS grid will be the best way to achieve what in the design.
This is what I have done so far.
ul {
list-style: none;
padding-left: 0;
}
.menu-items {
text-align: center;
display: grid;
grid-template-columns: 100%;
grid-template-rows: repeat(3, 33.33%);
}
.menu-items {
width: 100%;
height: 100vh;
overflow: hidden;
position: fixed;
top: 0;
left: 0;
grid-template-columns: 1fr 1fr;
grid-template-rows: 50% 50%;
grid-template-areas: "item1 item2" "item3 item4";
}
.menu__item {
height: 100%;
background: #212528;
border: 1px solid #333;
display: flex;
justify-content: center;
align-items: center;
}
.menu__item--1 {
grid-area: item1;
}
.menu__item--2 {
grid-area: item2;
}
.menu__item--3 {
grid-area: item3;
}
.menu__item--4 {
grid-area: item3;
}
.menu__item--5 {
grid-area: item3;
}
.menu__item--6 {
grid-area: item4;
}
.menu__item--3,
.menu__item--4,
.menu__item--5 {
display: block;
}
.menu-item-inner {
align-items: center;
}
.mainmenu {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.mainmenu {
counter-reset: menuitem;
}
.mainmenu__item {
text-transform: lowercase;
overflow: visible;
margin: 0.5rem 0;
padding: 0 0.5rem;
position: relative;
transition: color 0.3s;
}
.mainmenu__item:hover {
color: cyan;
}
.mainmenu__item:hover::after {
opacity: 1;
transform: scale3d(1, 1, 1);
}
ul.menu-items {
margin: 0;
padding: 0;
}
.mainmenu__item {
position: relative;
overflow: hidden;
transition: color 0.1s;
margin: 0.25rem 0;
display: block;
color: #fff;
font-size: 32px;
font-weight: 800;
}
<ul class="menu-items">
<li class="menu__item menu__item--1">
<div class="menu-item-inner">
<div class="mainmenu">
Menu 1
</div>
</div>
</li>
<li class="menu__item menu__item--2">
<div class="menu-item-inner">
<div class="mainmenu">
Menu 2
</div>
</div>
</li>
<li class="menu__item menu__item--3">
<div class="menu-item-inner">
<div class="mainmenu">
Menu 3
</div>
</div>
</li>
<li class="menu__item menu__item--4">
<div class="menu-item-inner">
<div class="mainmenu">
Menu 4
</div>
</div>
</li>
<li class="menu__item menu__item--5">
<div class="menu-item-inner">
<div class="mainmenu">
Menu 5
</div>
</div>
</li>
<li class="menu__item menu__item--6">
<div class="menu-item-inner">
<div class="mainmenu">
Menu 6
</div>
</div>
</li>
</ul>
CODPEN
Can anyone please guide me achieving this layout using CSS-Grid without nesting
(Changing the visual order without changing the logical order of the
document)
You can use grid-template-areas if you want complex layout:
* {
box-sizing: border-box;
}
body {
margin: 0;
display: flex;
align-items: center;
justify-content:center;
height: 100vh;
min-height: 600px;
}
.box {
display: flex;
align-items: center;
justify-content:center;
border: 1px solid #333;
background: #444;
color: #f9f9f9;
font-size: 2rem;
font-family: Open-sans, Arial, sans-serif;
}
.container {
width: 800px;
height: 100%;
display: grid;
padding: .5rem;
grid-gap: .5rem;
/* This is where Grid is defined*/
grid-template-areas:
'box-1 box-1 box-2 box-2'
'box-1 box-1 box-2 box-2'
'box-3 box-5 box-6 box-6'
'box-4 box-5 box-6 box-6';
}
.box-1 {
grid-area: box-1;
}
.box-2 {
grid-area: box-2;
}
.box-3 {
grid-area: box-3;
}
.box-4 {
grid-area: box-4;
}
.box-5 {
grid-area: box-5;
}
.box-6 {
grid-area: box-6;
}
<div class="container">
<div class="box box-1">1</div>
<div class="box box-2">2</div>
<div class="box box-3">3</div>
<div class="box box-4">4</div>
<div class="box box-5">5</div>
<div class="box box-6">6</div>
</div>
Related
I have an online shop I created. The way I did the cart function was have it display on my home,about us and online shop pages. But I have run into an issue.
The issue is that, if the cart goes to far down it over laps the footer and the footer moves up the more the cart moves down.
I think the overflow:scroll can work, but I think its also has to do with the way I did the CSS of the footer.
I have also attached the screenshot of the issue
/*Online Cart*/
.product-container {
position: absolute;
background-color: var(--dark-sienna-color);
display: grid;
z-index: 5;
width: 20%;
margin-left: 95.18rem;
display: none;
margin-top: -3rem;
overflow: scroll;
}
.cart-hide {
display: block !important;
}
.products {
color: var(--camel-color);
margin: 0 3rem;
}
.remove {
margin-top: 1rem;
}
.item-name {
display: grid;
margin-top: 1rem;
text-align: center;
}
.item-name img {
max-width: 100%;
}
.product svg {
width: 10%;
filter: invert(43%) sepia(32%) saturate(310%) hue-rotate(347deg) brightness(90%) contrast(88%);
}
.basketTotalContainer {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 8rem;
width: 100%;
padding: 10px 0;
border-top: 1px solid var(--camel-color);
margin-top: 1rem;
}
.basketTotalTitle,
.basketTotal {
font-size: 20px;
}
/*Online Cart*/
.footer {
background-color: var(--dark-sienna-color);
height: 100px;
}
.container-footer {
display: flex;
flex-direction: row;
margin-left: 30rem;
}
.collection h3,
.blog h3,
.contact h3 {
color: var(--camel-color);
text-decoration: underline;
padding-top: 0.5rem;
padding-left: 10rem;
font-size: 15px;
}
.list-group {
list-style-type: none;
text-align: justify;
color: var(--camel-color);
margin-left: 10rem;
width: 10rem;
}
.list-group li {
font-size: 10px;
}
.coll-hl {
border-left: 1px solid var(--camel-color);
height: 4rem;
position: absolute;
margin-top: 1rem;
margin-left: 27rem;
}
.blog-hl {
border-left: 1px solid var(--camel-color);
height: 4rem;
position: absolute;
margin-top: 1rem;
margin-left: 45rem;
}
<!--Cart span-->
<div class="product-container">
<div class="product-header">
<h5 class="product-title"></h5>
<h5 class="price"></h5>
<h5 class="quantity"></h5>
<h5 class="total"></h5>
</div>
<div class="products">
</div>
</div>
<!--Cart span-->
<footer>
<div class="footer">
<div class="container-footer">
<div class="collection">
<h3>COLLECTION</h3>
<ul class="list-group">
<li>SOFA'S</li>
<li>BEDS</li>
<li>BED SIDE TABLES</li>
<li>COFFEE TABLES</li>
</ul>
</div>
<div class="coll-hl"></div>
<div class="blog">
<h3>BLOG</h3>
<ul class="list-group">
<li>SUPERBLOG</li>
<li>CATABLOG</li>
</ul>
</div>
<div class="blog-hl"></div>
<div class="contact">
<h3>CONTACT US</h3>
<ul class="list-group">
<li>TEL: 021 568 7523</li>
<li>44 Harrington St, Zonnebloem, Cape Town, 8001
</li>
</ul>
</div>
</div>
</div>
</footer>
Add max-height: 100vh; to .product-container besides setting overflow-y: auto;.
Like this:
.product-container {
max-height: 100vh; /* Added */
overflow-y: auto; /* Changed */
position: absolute;
background-color: var(--dark-sienna-color);
display: grid;
z-index: 5;
width: 20%;
margin-left: 95.18rem;
display: none;
margin-top: -3rem;
}
Note: You can set max-height to whatever you want (e.g., 600px).
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>
i'm trying to justify text in a p tag to the height of a div.
as shown in the picture below:
how can I do this in css?
here's the html:
so the expected result is that the text should be as long as the height of the parent div.
body{
height:100vh;
}
.about-2 {
display: grid;
grid-template-columns: 5% 90%;
column-gap: 5%;
height: 100%;
position: relative;
}
.about-2 .skills-text p {
transform: rotate(270deg);
transform-origin: 0 0;
position: absolute;
bottom: 0;
left: 0;
font-size: 1.5em;
color: #face6f;
padding-left: 35px;
}
<div class="about-2">
<div class="skills-text">
<p class="dash">My skills</p>
</div>
<div class="skillset">
<div>skill 1</div>
<div>skill 1</div>
<div>skill 1</div>
<div>skill 1</div>
</div>
</div>
something more or less like this, I guess:
body {
background: #0c0c0c;
color: #eee;
padding: 24px;
}
.about {
display: grid;
grid-template-columns: min-content 1fr;
grid-gap: 24px;
}
.title {
display: flex;
align-items: center;
margin: 0;
font-size: 1.4em;
color: #face6f;
text-align: end;
writing-mode: vertical-lr;
transform: scale(-1);
}
.title::before {
display: block;
content: '';
flex: 1 1 auto;
width: 2px;
margin-bottom: 6px;
background-color: #face6f;
}
.skillset, .skillset-item {
list-style: none;
margin: 0;
padding: 0;
}
.skillset {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: 24px;
}
.skillset-item { padding-top: 12px; }
.skillset-item::after {
display: block;
content: '';
width: 100%;
height: 3px;
background-color: #face6f;
margin-top: 12px;
}
<div class="about">
<h3 class="title">My skills</h3>
<ul class="skillset">
<li class="skillset-item">skill 1</li>
<li class="skillset-item">skill 2</li>
<li class="skillset-item">skill 3</li>
<li class="skillset-item">skill 4</li>
</ul>
</div>
I'm just playing about with some code and trying to create a dropdown menu without using a list.
I cannot get the dropdown to show and I can't figure out what I'm doing wrong.
I have visibility set to hidden on the dropdown class and the visibility set to visible when on hover but it won't show. If I set the visibility to visible on the dropdown class it shows so it must be something to do with my hover?
* {
margin: 0;
padding: 0;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
display: grid;
margin: 0px;
grid-gap: 0px;
grid-template-columns: 1fr;
grid-template-rows: 70px calc(100vh - 70px);
}
.header {
background: red;
display: grid;
}
.header--container {
width: 1160px;
justify-self: center;
display: grid;
grid-column: 1;
grid-row: 1;
grid-auto-flow: column;
}
.header--container span:first-child {
background: purple;
display: grid;
grid-auto-flow: column;
justify-items: left;
display: grid;
align-items: center;
}
.header--container nav {
background: black;
display: grid;
grid-auto-flow: column;
justify-items: center;
display: grid;
align-items: center;
}
.header--container span:last-child {
background: yellow;
display: grid;
grid-auto-flow: column;
justify-items: right;
display: grid;
align-items: center;
}
.hero {
background: blue;
display: grid;
}
.hero--container {
width: 1160px;
justify-self: center;
background: pink;
}
.site-navigation .menu-item {
color: #fff;
background: #3498db;
display: block;
}
.site-navigation .menu-item a {
color: #fff;
text-decoration: none;
display: block;
}
.site-navigation .menu-item:hover {
background: #52a6df;
cursor: pointer;
}
.dropdown {
background: #52a6df;
visibility: hidden;
position: absolute;
transition: visibility 0.65s ease-in;
z-index: 999;
}
a:hover>.dropdown,
a .dropdown:hover {
visibility: visible;
z-index: 9999;
}
<div class="header">
<div class="header--container">
<span>
<img src="imgs/ID_Logo_Websitesmall.jpg" alt="logo" height="50px">
</span>
<nav id="navigation" class="site-navigation" role="navigation">
News
<a href="#" class="menu-item">Snippets
<div class="dropdown">
CSS
HTML
jQuery
PHP
WordPress
</div>
</a>
Inspiration
Tools
Tutorials
</nav>
<span>
01442 817767
</span>
</div>
</div>
<div class="hero">
<div class="hero--container">cd</div>
</div>
An anchor tag inside another is invalid syntax - you can see while inspecting that the dropdown div is moving out and getting place as the next sibling element. Change the a tags to say span - see demo below:
* {
margin: 0;
padding: 0;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
display: grid;
margin: 0px;
grid-gap: 0px;
grid-template-columns: 1fr;
grid-template-rows: 70px calc(100vh - 70px);
}
.header {
background: red;
display: grid;
}
.header--container {
width: 1160px;
justify-self: center;
display: grid;
grid-column: 1;
grid-row: 1;
grid-auto-flow: column;
}
.header--container span:first-child {
background: purple;
display: grid;
grid-auto-flow: column;
justify-items: left;
display: grid;
align-items: center;
}
.header--container nav {
background: black;
display: grid;
grid-auto-flow: column;
justify-items: center;
display: grid;
align-items: center;
}
.header--container span:last-child {
background: yellow;
display: grid;
grid-auto-flow: column;
justify-items: right;
display: grid;
align-items: center;
}
.hero {
background: blue;
display: grid;
}
.hero--container {
width: 1160px;
justify-self: center;
background: pink;
}
.site-navigation .menu-item {
color: #fff;
background: #3498db;
display: block;
}
.site-navigation .menu-item a {
color: #fff;
text-decoration: none;
display: block;
}
.site-navigation .menu-item:hover {
background: #52a6df;
cursor: pointer;
}
.dropdown {
background: #52a6df;
visibility: hidden;
position: absolute;
transition: visibility 0.65s ease-in;
z-index: 999;
}
a:hover>.dropdown,
a .dropdown:hover {
visibility: visible;
z-index: 9999;
}
<div class="header">
<div class="header--container">
<span><img src="https://via.placeholder.com/100" alt="logo" height="50px"></span>
<nav id="navigation" class="site-navigation" role="navigation">
News
<a href="#" class="menu-item">Snippets
<div class="dropdown">
<span href="#"class="menu-item sub-menu">CSS</span>
<span href="#" class="menu-item sub-menu">HTML</span>
<span href="#" class="menu-item sub-menu">jQuery</span>
<span href="#" class="menu-item sub-menu">PHP</span>
<span href="#" class="menu-item sub-menu">WordPress</span>
</div>
</a>
Inspiration
Tools
Tutorials
</nav>
<span>01442 817767</span>
</div>
</div>
<div class="hero">
<div class="hero--container">cd</div>
</div>
I need to create a web page that uses 100vh, or takes up 100% of the screen height and width (no scroll). I created a container(height:100vh) that holds everything in it, and within that container, I need everything in there to be responsive.
Design concept:
The outer container height is 100vh and I need the inner container to be responsive:
#root {
position: relative;
height: 100vh;
overflow: hidden;
}
#root-inner-container {
width: 100%;
}
The problem I run into is by using 100vh, the content inside the container does not stay responsive and tends to overflow.
Jsfiddle to what I have so far: https://jsfiddle.net/fm6hmgpk/
Flex Solution
body {
margin: 0;
color: white;
}
.container {
background: grey;
width: 100vw;
height: 100vh;
}
.navbar {
height: 15vh;
background: darkblue;
margin: 0 10px;
}
.bottom {
background: lightgrey;
height: 85vh;
margin: 0 10px;
display: flex;
}
.left-bottom {
background: red;
width: 70%;
display: flex;
flex-direction: column;
}
.right-bottom {
flex: 1;
background: lightgrey;
}
.content-list {
display: flex;
height: 80%;
background: maroon;
}
.text {
flex: 1;
background: green;
}
.content {
width: 80%;
background: orange;
}
.list {
flex: 1;
}
<div class="container">
<div class="navbar">
NAVBAR
</div>
<div class="bottom">
<div class="left-bottom">
<div class="content-list">
<div class="content">
CONTENT
</div>
<div class="list">
LIST
</div>
</div>
<div class="text">
TEXT
</div>
</div>
<div class="right-bottom">
IMAGE
</div>
</div>
</div>
Calculation: (For scrolling issue)
<body> default margin:8px; and your border:2px solid black;
Sums up to 10px so we need to deduct twice of 10px
Hence height: calc(100vh - 20px);
EDIT:
To make it responsive you need to get rid of fixed px value to your li
li {}
#root {
display: flex;
position: relative;
height: calc(100vh - 20px);
border: 2px solid black;
}
#root-inner-container {
flex: 1;
width: 100%;
display: flex;
}
.app-container {
flex: 1;
display: flex;
}
.div-1,
.div-2 {
flex: 1;
display: flex;
}
ul {
flex: 1;
display: flex;
flex-direction: column;
}
li {
flex: 1;
border: 1px solid red;
}
<div id="root">
<div id="root-inner-container">
<div class="app-container">
<div class="div-1">
<ul>
<li>div 1 - One</li>
<li>div 1 - Two</li>
<li>div 1 - Three</li>
<li>div 1 -Four</li>
</ul>
</div>
<div class="div-2">
<ul>
<li>div 2 - One</li>
<li>div 2 - Two</li>
<li>div 2 - Three</li>
<li>div 2 -Four</li>
</ul>
</div>
</div>
</div>
</div>
You may want to consider using grid. For browser support you can check here.
To learn about using grid, check here.
body {
margin: 0
}
#root {
display: grid;
grid-gap: 10px;
grid-template-columns: 3fr 1fr 3fr;
grid-template-rows: 1fr 3fr 1fr;
background-color: #fff;
color: #444;
width: 100vw;
height: 100vh;
}
.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
}
.navbar {
grid-column: 1 / 4;
grid-row: 1;
}
.content {
grid-column: 1;
grid-row: 2 / 3;
}
.list {
grid-column: 2;
grid-row: 2;
}
.image {
grid-column: 3 / 4;
grid-row: 2 / 4;
}
.text {
grid-column: 1 / 3;
grid-row: 3 / 4;
}
<div id="root">
<div class="navbar box">Navbar</div>
<div class="content box">Content</div>
<div class="list box">List</div>
<div class="image box">Image</div>
<div class="text box">Text</div>
</div>
I think it's because your li height is fixed height so if your root height is less than the sum of those li height it will overflow. you can use vh for them too.