I've been trying to achieve the following layout using CSS and Flexbox but without any luck. Maybe someone here could help me out and point me the mistakes I've made and even suggest me the best course to do it.
This should be the final result (having all items different heights and widths but I'm starting to think that flexbox is not the best choice to do it):
Live example: https://jsfiddle.net/bogdaniel/Lzugkva3/5/
<div class="container">
<div class="blog-container">
<div class="blog-item" style="height: 286px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item One</h2>
<span>height: 286px;</span>
<p>Item Should Be First In The List On The Left Column</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style=";height: 203px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Two</h2>
<span>height: 203px;</span>
<p>Item Should Go To The right next to the height 286px;</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style="height: 255px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Three</h2>
<span>height: 255px;</span>
<p>Item Should Be Second On the First Row In the List</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style="height: 325px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Four</h2>
<span>height: 325px;</span>
<p>Item Should Go To The right Of Item Three On The Second Column</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style="height: 251px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Five</h2>
<span>height: 251px;</span>
<span>width: 523px;</span> Item Should Start From The Left And Span 2 Columns Almost
</div>
</div>
</div>
</div>
<div class="blog-item" style="height: 282px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Six</h2>
<span>height: 282px;</span>
<span>width: 186px;</span>
<p>Item Should Be Portrait And Span On The Right Column</p>
</div>
</div>
</div>
</div>
<div class="blog-item" style=" width: 100%%; height: 204px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Seven</h2>
<span>height 204px;</span>
<span>with: 523px;</span>
</div>
</div>
</div>
</div>
<div class="blog-item" style="width: 186px; height: 174px;">
<div class="blog-post">
<div class="post-body">
<div class="post-title">
<h2>Item Eight</h2>
<span>height: 174px;</span>
<span>width: 186px;</span>
</div>
</div>
</div>
</div>
</div>
</div>
SCSS code:
.blog-container {
display: flex;
flex-flow: column wrap;
/* Your container needs a fixed height, and it
* needs to be taller than your tallest column. */
min-height: 1400px;
height: 100vh;
/* Force new columns */
&:before,
&:after {
content: "";
flex-basis: 100%;
width: 0;
order: 2;
}
/* Optional */
background-color: #f7f7f7;
border-radius: 3px;
margin: 15px auto;
counter-reset: items;
}
.blog-item {
width: 50%;
padding: 14px;
.blog-post {
height: 100%;
/* Optional */
position: relative;
border-radius: 3px;
border: 1px solid #4290e2;
box-shadow: 0 2px 2px rgba(0, 90, 250, 0.05),
0 4px 4px rgba(0, 90, 250, 0.05), 0 8px 8px rgba(0, 90, 250, 0.05),
0 16px 16px rgba(0, 90, 250, 0.05);
color: #000;
padding: 15px;
&:before {
counter-increment: items;
content: counter(items);
}
.post-body {
padding: 15px;
}
}
}
.blog-item:nth-child(2n + 1) {
order: 1;
}
.blog-item:nth-child(2n + 2) {
order: 2;
}
.blog-item:nth-child(2n + 3) {
order: 1;
}
.blog-item:nth-child(2n + 4) {
order: 2;
}
while waiting for a comment, if the layout is to be like the image, then you can use grid and set ahead amount of rows and cells to span for each element.
useful ressource : https://css-tricks.com/snippets/css/complete-guide-grid/ & https://gridbyexample.com/
body {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(6, auto);
}
div:nth-child(1) {
grid-row: span 2;
}
div:nth-child(2) {
grid-column: span 3;
}
div:nth-child(3) {
grid-row: 3
}
div:nth-child(4) {
grid-column: span 3;
grid-row: span 2;
}
div:nth-child(5) {
grid-column: span 3;
}
div:nth-child(6) {
grid-row: span 2
}
div:nth-child(7) {
grid-column: span 3;
grid-row: span 2
}
/* makup */
body {
counter-reset: divs;
background: rgb(236, 244, 175);
}
div:before {
counter-increment: divs;
content: counter(divs);
background: tomato;
width: 1.2em;
height: 1.2em;
border-radius: 50%;
text-align: center;
color: green;
text-shadow: 0 0 3px white;
box-shadow: 0 0 3px;
}
div {
border-radius: 3px;
border: 1px solid darkblue;
background: lightblue;
}
body {
margin: 0;
grid-gap: 2vh;
padding: 2vh;
min-height: 100vh;
box-sizing: border-box;
}
div {
display: flex;
align-items: center;
justify-content: center;
font-size: calc(8px + 1.5vh + 1.5vw)
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
here is a codepen to test and play with (resize/add content, ... )
Related
i can rotate the div or implement the clip-path property on to it. but i am not getting desire results with my approach. i have searched the codepen and other results from google search but nada.
i have used clip-path and skew property on the div but again i think i am not following the correct approach.i want to make my div lie on to each other with appropriate spacing and in the skew state as you can see.
i want it like that......
here is what i want
I think you want something like this, I used transform property to do it:
.flexContainer {
width: 500px;
display: flex;
flex-wrap: wrap;
}
.flexContainer>* {
flex: 0 0 33%;
}
.theContainer {
margin: 0 0.5em;
padding: 2em 2em 7em 2em;
color: #000;
display: block;
width: 100px;
height: 30px;
background: #fff;
border-radius: 10px;
box-shadow: 2px 2px 2px 2px #ededed;
transform: skew(0, -5deg);
}
.theContainer.first {
margin-top: 1.5em;
}
.theContent {
position: absolute;
transform: skew(0, 5deg);
}
<div class="flexContainer">
<div class="theContainer first">
<div class="theContent">
<h2> Title </h2>
<p> Content test </p>
</div>
</div>
<div class="theContainer second">
<div class="theContent">
<h2> Title </h2>
<p> Content test </p>
</div>
</div>
<div class="theContainer first">
<div class="theContent">
<h2> Title </h2>
<p> Content test </p>
</div>
</div>
<div class="theContainer second">
<div class="theContent">
<h2> Title </h2>
<p> Content test </p>
</div>
</div>
</div>
Hope this helps...
<div class="container">
<div class="item">
<h1>1</h1>
</div>
<div class="item">
<h1>2</h1>
</div>
<div class="item">
<h1>3</h1>
</div>
<div class="item">
<h1>4</h1>
</div>
</div>
<style>
.container {
display: flex;
flex-wrap: wrap;
transform: skew(0, -6deg);
}
.container .item {
text-align: center;
width: 40%;
margin: 10px;
padding: 50px;
border-radius: 10px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
}
.container .item h1 {
transform: skew(0, 6deg);
}
</style>
I am working on one of my personal projects where I am designing a website for a restaurant using HTML and CSS.....I have got a code for a Carousel and I am trying to integrate it with my website....
But I am facing a problem with the carousel...As I slide down my window...the carousel also tends to move upward and doesn't stick to it's original position....How am i supposed to fix this issue....
This is the first one among all my personal projects.
I would be pleased and more than happy if the experts or members of this community would help me out in this process of learning and aid me in getting this project completed....
This is the project
This is my index.html.The code for Carousel is at the last section with the comment "Carousel Starts"
<!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>Restaurant Page</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<nav>
<div class="navigation container">
<div class="logo_container">
<img src="images/logo.png" alt="logo" />
</div>
<div class="allmenus">
<ul>
<li class="menu">Home</li>
<li class="menu" >About Us</li>
<li class="menu" >Our Services</li>
</ul>
</div>
</nav>
<script
src="https://kit.fontawesome.com/8b6347e91f.js"
crossorigin="anonymous"
></script>
<!--First Section-->
<div class="container" >
<div class="first">
<div class="first_image">
<img src="images/hero_image.png" alt="Vegetables" />
</div>
<div class="first_content">
<div class="tag">50% Off On All Products</div>
<h1>Enjoy Your Delicious Food</h1>
<p>
Make reservations, hours, and locations front-and-center, and
prominently get your order within few minutes of getting placed
</p>
<button class="explore">Order Now</button>
</div>
</div>
<section class="second_section" >
<div class="feature">
<img src="images/discount 1.png" />
<div class="feature_content">
<h1>Discount Voucher</h1>
<p>Get a discount voucher worth Rs.100 on yout first order</p>
</div>
</div>
<div class="feature">
<img src="images/fresh.png" />
<div class="feature_content">
<h1>Fresh Healthy Food</h1>
<p>Get a discount voucher worth Rs.100 on yout first order</p>
</div>
</div>
<div class="feature">
<img src="images/delivery.png" />
<div class="feature_content">
<h1>Instant delivery</h1>
<p>Get a discount voucher worth Rs.100 on yout first order</p>
</div>
</div>
</section>
</div>
<!--Popular Dishes -->
<div class="container dishes" >
<h1 class="popdishes">Our Popular Dishes</h1>
<div class="subcontainer">
<div class="section">
<img class="dish" src="images/dish-3.png" alt="">
<h2 class="recipe">Chicken Leg Piece</h2>
<div class="priceandbutton">
<span>$15</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
<div class="section">
<img class="dish" src="images/dish-1.png" alt="">
<h2 class="recipe">Burger</h2>
<div class="priceandbutton">
<span>$20</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
<div class="section">
<img class="dish" src="images/dish-2.png" alt="">
<h2 class="recipe">Chicken Nuggets</h2>
<div class="priceandbutton">
<span>$12</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
</div>
<!-- Second SubContainer Starts-->
<div class="subcontainer">
<div class="section">
<img class="dish" src="images/dish-4.png" alt="">
<h2 class="recipe">Pizza</h2>
<div class="priceandbutton">
<span>$12</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
<div class="section">
<img class="dish" src="images/dish-5.png" alt="">
<h2 class="recipe">Chocolate Cookies</h2>
<div class="priceandbutton">
<span>$15</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
<div class="section">
<img class="dish" src="images/dish-6.png" alt="">
<h2 class="recipe">Broasted</h2>
<div class="priceandbutton">
<span>$20</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
</div>
<!-- Third SubContainer Starts-->
<div class="subcontainer">
<div class="section">
<img class="dish" src="images/menu-33 new.jpg" alt="">
<h2 class="recipe">Crepes</h2>
<div class="priceandbutton">
<span>$12</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
<div class="section">
<img class="dish" src="images/menu-44.jpg" alt="">
<h2 class="recipe">Waffles</h2>
<div class="priceandbutton">
<span>$15</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
<div class="section">
<img class="dish" src="images/menu-55.jpg" alt="">
<h2 class="recipe">Pasteries</h2>
<div class="priceandbutton">
<span>$20</span>
<button class="addtocart">Add To Cart</button>
</div>
</div>
</div>
</div>
<!-- Carousel Starts -->
<div class="container">
<div class="mycarousel" >
<input type="radio" name="position" checked />
<input type="radio" name="position" />
<input type="radio" name="position" />
<input type="radio" name="position" />
<input type="radio" name="position" />
<main id="carousel">
<div class="item" >
<div class="reviews">
<img class="reviewimages" src="images/pic-1.png">
<div class="content">
<h1>John Doe</h1>
<p>In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.
</div>
</div>
</div>
<div class="item">
<div class="reviews">
<img class="reviewimages" src="images/pic-2.png">
<div class="content">
<h1>John Doe</h1>
<p>In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.
</div>
</div>
</div>
<div class="item">
<div class="reviews" >
<img class="reviewimages" src="images/pic-1.png">
<div class="content">
<h1>John Doe</h1>
<p>In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.
</div>
</div>
</div>
<div class="item">
<div class="reviews">
<img class="reviewimages" src="images/pic-1.png">
<div class="content">
<h1>John Doe</h1>
<p>In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.
</div>
</div>
</div>
<div class="item">
<div class="reviews">
<img class="reviewimages" src="images/pic-1.png">
<div class="content">
<h1>John Doe</h1>
<p>In publishing and graphic design, Lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document or a typeface without relying on meaningful content. Lorem ipsum may be used as a placeholder before final copy is available.
</div>
</div>
</div>
<main>
</div>
</div>
</body>
</html>
This is my css file
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body {
line-height: 1.4;
font-weight: 300;
font-family: "Roboto", sans-serif;
}
nav {
box-shadow: 5px 5px 5px rgba(1, 1, 1, 0.05);
background-color: white;
position: sticky;
top: 0;
margin-bottom: 32px;
padding: 10px;
}
.navigation {
display: flex;
justify-content: space-between;
align-items: center;
color: rgba(28, 22, 22, 0.414);
font-weight: bolder;
font-size: large;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
ul {
text-align: right;
display: flex;
font-family: Helvetica, sans-serif;
font-weight: bolder;
list-style: none;
}
li {
padding: 0 20px;
margin-top: 30px;
}
li:hover {
color: #134a7a;
text-decoration: underline;
}
.logo_container {
margin-left: 15px;
}
/* First Section */
.first {
display: flex;
gap: 100px;
justify-content: center;
align-items: center;
}
.first_content {
display: flex;
flex-direction: column;
gap: 20px;
}
.tag {
background-color: #134a7a;
align-self: flex-start;
padding: 5px 10px;
color: white;
}
.first_content h1 {
font-size: 72px;
line-height: 1.2;
font-weight: 500;
}
button {
padding: 10px 15px;
color: white;
font-weight: 500;
font-size: 22px;
background: crimson;
border: none;
border-radius: 30px;
align-self: flex-start;
width: 250px;
}
button:hover {
transition: 0.2s ease-in-out;
background:#e5eaee;
color: #134a7a;
font-weight: 500;
}
.second_section {
display: flex;
flex-direction: row;
gap: 20px;
margin-top: 100px;
justify-content: center;
}
.feature {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
gap: 15px;
height: 165px;
background-color: #134a7a;
color: azure;
}
.feature:hover {
transition: 0.2s ease-in-out;
background-color: azure;
color:#134a7a;
}
.dishes{
background-color: #e5eaee;
margin-top: 20px;
height:1250px;
margin-bottom: 20px;;
}
.dishes .subcontainer {
display: flex;
flex-direction: row;
justify-content: space-evenly;
gap: 25px;
}
.recipe{
font-weight: 300;
}
.section {
width: 350px;
height: 350px;
background: white;
padding-top: 32px;
margin-top: 25px;
}
.section{
display:flex;
flex-direction:column;
justify-content: center;
align-items: center;
gap: 10px;
}
h1.popdishes {
padding-top: 20px;
color:#134a7a;
text-align: center;
}
.priceandbutton {
display: flex;
flex-direction: row;
gap:10px
}
span {
padding-top: 5px;
font-weight: 400;
color:#134a7a;
font-size: 30px;
}
.addtocart{
padding: 10px 15px;
color: white;
font-weight: 500;
font-size: 22px;
background:#134a7a;
border: none;
border-radius: 30px;
align-self: flex-start;
width: 200px;
}
.addtocart:hover {
background:#e5eaee;
color: #134a7a;
font-weight: 500;
transition: 0.2s ease-in-out;
}
.reviewimages{
height:150px;
width:170px;
border-radius:50%;
}
.reviews{
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
padding:20px
}
.content{
text-align:center
}
.mycarousel{
height: 600px;
margin: 0;
display: grid;
grid-template-rows: 500px 100px;
grid-template-columns: 1fr 30px 30px 30px 30px 30px 1fr;
align-items: center;
justify-items: center;
background-color: #134a7a;
}
main#carousel {
grid-row: 1 / 2;
grid-column: 1 / 8;
width: 100vw;
height: 500px;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
transform-style: preserve-3d;
perspective: 600px;
--items: 5;
--middle: 3;
--position: 1;
pointer-events: none;
}
div.item {
position: absolute;
width: 300px;
height: 400px;
background-color: coral;
--r: calc(var(--position) - var(--offset));
--abs: max(calc(var(--r) * -1), var(--r));
transition: all 0.25s linear;
transform: rotateY(calc(-10deg * var(--r)))
translateX(calc(-300px * var(--r)));
z-index: calc((var(--position) - var(--abs)));
overflow: hidden;
}
div.item:nth-of-type(1) {
--offset: 1;
}
div.item:nth-of-type(2) {
--offset: 2;
}
div.item:nth-of-type(3) {
--offset: 3;
}
div.item:nth-of-type(4) {
--offset: 4;
}
div.item:nth-of-type(5) {
--offset: 5;
}
input:nth-of-type(1) {
grid-column: 2 / 3;
grid-row: 2 / 3;
}
input:nth-of-type(1):checked ~ main#carousel {
--position: 1;
}
input:nth-of-type(2) {
grid-column: 3 / 4;
grid-row: 2 / 3;
}
input:nth-of-type(2):checked ~ main#carousel {
--position: 2;
}
input:nth-of-type(3) {
grid-column: 4 /5;
grid-row: 2 / 3;
}
input:nth-of-type(3):checked ~ main#carousel {
--position: 3;
}
input:nth-of-type(4) {
grid-column: 5 / 6;
grid-row: 2 / 3;
}
input:nth-of-type(4):checked ~ main#carousel {
--position: 4;
}
input:nth-of-type(5) {
grid-column: 6 / 7;
grid-row: 2 / 3;
}
input:nth-of-type(5):checked ~ main#carousel {
--position: 5;
}
.reviewimages{
height:150px;
width:170px;
border-radius:50%;
border:5px solid black;
}
.content h1{
color:#134a7a;
}
.reviews{
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
padding:20px
}
.content{
text-align:center
}
.mycarousel{
height: 600px;
margin: 0;
display: grid;
grid-template-rows: 500px 100px;
grid-template-columns: 1fr 30px 30px 30px 30px 30px 1fr;
align-items: center;
justify-items: center;
}
main#carousel {
grid-row: 1 / 2;
grid-column: 1 / 8;
width: 100vw;
height: 500px;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
transform-style: preserve-3d;
perspective: 600px;
--items: 5;
--middle: 3;
--position: 1;
pointer-events: none;
}
div.item {
position: absolute;
width: 300px;
height: 400px;
background-color: coral;
--r: calc(var(--position) - var(--offset));
--abs: max(calc(var(--r) * -1), var(--r));
transition: all 0.25s linear;
transform: rotateY(calc(-10deg * var(--r)))
translateX(calc(-300px * var(--r)));
z-index: calc((var(--position) - var(--abs)));
}
div.item:nth-of-type(1) {
--offset: 1;
background-color: #e5eaee;
}
div.item:nth-of-type(2) {
--offset: 2;
background-color: #e5eaee;
}
div.item:nth-of-type(3) {
--offset: 3;
background-color: #e5eaee;
}
div.item:nth-of-type(4) {
--offset: 4;
background-color: #e5eaee;
}
div.item:nth-of-type(5) {
--offset: 5;
background-color: #e5eaee;
}
input:nth-of-type(1) {
grid-column: 2 / 3;
grid-row: 2 / 3;
}
input:nth-of-type(1):checked ~ main#carousel {
--position: 1;
}
input:nth-of-type(2) {
grid-column: 3 / 4;
grid-row: 2 / 3;
}
input:nth-of-type(2):checked ~ main#carousel {
--position: 2;
}
input:nth-of-type(3) {
grid-column: 4 /5;
grid-row: 2 / 3;
}
input:nth-of-type(3):checked ~ main#carousel {
--position: 3;
}
input:nth-of-type(4) {
grid-column: 5 / 6;
grid-row: 2 / 3;
}
input:nth-of-type(4):checked ~ main#carousel {
--position: 4;
}
input:nth-of-type(5) {
grid-column: 6 / 7;
grid-row: 2 / 3;
}
input:nth-of-type(5):checked ~ main#carousel {
--position: 5;
}
Try adding justify-content: space-around; so it stays the same width as the page:
.mycarousel {
height: 600px;
margin: 0;
display: grid;
grid-template-rows: 500px 100px;
grid-template-columns: 1fr 30px 30px 30px 30px 30px 1fr;
align-items: center;
justify-items: center;
justify-content: space-around;
}
For the slide part, I see it goes over the navbar you can solve it by adding a z-index: 1; to the navbar.
nav {
box-shadow: 5px 5px 5px rgb(1 1 1 / 5%);
background-color: white;
position: sticky;
top: 0;
margin-bottom: 32px;
padding: 10px;
z-index: 1;
}
This question already has an answer here:
CSS Grid vs dynamic definition list autoplacement
(1 answer)
Closed 6 months ago.
I am building a calculator with HTML, CSS, and JavaScript. I am facing difficulty building the basic design of the app.
I have no clue why the space is in there. I tried adding negative margin but that didn't work too.
GOAL
CURRENT STATE
.container {
height: 500px;
width: 400px;
background-color: #3333;
border-radius: 10px;
}
.output {
height: 100px;
padding: 3rem 3rem 6rem 3rem;
font-size: 2.5rem;
text-align: right;
background-color: #eee;
border-radius: 10px;
}
.buttons {
width: 400px;
display: grid;
row-gap: 1%;
padding: 1rem;
justify-self: center;
}
button {
width: 80px;
height: 40px;
font-size: 1.2rem;
border: none;
border-radius: 2px;
background-color: #70a1ff;
color: #130f40;
cursor: pointer;
}
.item0,
.item7,
.item10,
.item13,
.item16 {
grid-column: 1;
}
.item1,
.item8,
.item11,
.item14,
.item17 {
grid-column: 2;
}
.item2,
.item9,
.item12,
.item15 {
grid-column: 3;
}
.item3,
.item4,
.item5,
.item6,
.item18 {
grid-column: 4;
}
.item18 button {
background-color: #eb4d4b;
color: #fff;
}
.item5 button,
.item3 button,
.item4 button,
.item6 button {
background-color: #4834d4;
color: #fff;
}
<div class="container">
<div class="output">
<h3>69</h3>
</div>
<div class="buttons">
<div class="item0">
<button>AC</button>
</div>
<div class="item1">
<button>C</button>
</div>
<div class="item2">
<button>%</button>
</div>
<div class="item3">
<button>÷</button>
</div>
<div class="item4">
<button>×</button>
</div>
<div class="item5">
<button>-</button>
</div>
<div class="item6">
<button>+</button>
</div>
<div class="item7">
<button>1</button>
</div>
<div class="item8">
<button>2</button>
</div>
<div class="item9">
<button>3</button>
</div>
<div class="item10">
<button>4</button>
</div>
<div class="item11">
<button>5</button>
</div>
<div class="item12">
<button>6</button>
</div>
<div class="item13">
<button>7</button>
</div>
<div class="item14">
<button>8</button>
</div>
<div class="item15">
<button>9</button>
</div>
<div class="item16">
<button>0</button>
</div>
<div class="item17">
<button>.</button>
</div>
<div class="item18">
<button>=</button>
</div>
</div>
</div>
</div>
There is no reason to wrap every single button in a div. The whole reason to use a grid in the first place is to not hard-code everything HTML-wise but only apply the grid to the container. Simply create a 4-column grid (grid-template-columns: repeat(4, 1fr) and let the "equal" button span 2 columns:
.container {
height: 500px;
width: 400px;
background-color: #3333;
border-radius: 10px;
}
.output {
height: 100px;
padding: 3rem 3rem 6rem 3rem;
font-size: 2.5rem;
text-align: right;
background-color: #eee;
border-radius: 10px;
}
.buttons {
width: 400px;
display: grid;
grid-gap: 2px;
grid-template-columns: repeat(4, 1fr);
}
button {
height: 40px;
font-size: 1.2rem;
border: none;
border-radius: 2px;
background-color: #70a1ff;
color: #130f40;
cursor: pointer;
}
.buttons button:last-child {
grid-column: span 2;
background-color: red;
}
<div class="container">
<div class="output">
<h3>69</h3>
</div>
<div class="buttons">
<button>AC</button>
<button>C</button>
<button>%</button>
<button>÷</button>
<button>×</button>
<button>-</button>
<button>+</button>
<button>1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>6</button>
<button>7</button>
<button>8</button>
<button>9</button>
<button>0</button>
<button>.</button>
<button>=</button>
</div>
</div>
So I've been trying to make these boxes clickable with links for a while now, but haven't been able to figure it out. I'd like each box to have its own link. Can anyone help me refine my code?
https://jsfiddle.net/Anonymous32794/07suq4pg/8/
<div class="wrapper">
<article class="main">
<img src="image" alt="">
<div class="text">
<b>Stuff</b>
<button>Button</button>
<p>Stuff</p></div>
</article>
<aside class="aside aside-2">
<div class="dropdown">
<button class="dropbtn">Downloads</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</aside>
<footer class="footer">
<div class="box-container">
<div class="box">Chapter 1</div>
<div class="box">Chapter 2</div>
<div class="box">Chapter 3</div>
<div class="box">Chapter 4</div>
<div class="box">Chapter 5</div>
<div class="box">Chapter 6</div>
<div class="box">Chapter 7</div>
<div class="box">Chapter 8</div>
<div class="box">Chapter 9</div>
</div>
</footer>
</div>
First you get all elements with class box. then you bind every element with a click Event Listener. Then you can handle the click event. In this case i print the innerHTML out.
const boxes = document.querySelectorAll('.box');
boxes.forEach((box) => {
box.addEventListener('click', function(e) {
alert('will redirect to:' + e.target.getAttribute('data-href'));
window.location = e.target.getAttribute('data-href');
});
});
.wrapper {
display: flex;
flex-flow: row wrap;
text-align: center;
}
.wrapper > * {
padding: 0px;
flex: 1 100%;
}
.footer {
}
.main {
text-align: left;
}
.aside-2 {
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 6px;
font-size: 16px;
border: none;
cursor: pointer;
top: 8px;
}
.dropdown {
position: relative;
display: inline-block;
float: center;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
.box {
border: .1rem solid #cccccc;
background: #5cc80f;
color: white;
width: 25%;
}
.box-container {
align-content: center;
align-items: center;
align-self: stretch;
display: flex;
flex-wrap: wrap;
flex-direction: row;
flex-grow: 1;
font-weight: 600;
justify-content: left;
min-width: 0;
}
.box::after {
clear: both;
content: "";
}
img {
float: left;
width: 120px;
height: 171px;
padding: 10px;
border-radius: 20px;
}
#media all and (min-width: 600px) {
.aside { flex: 1 0 0; }
}
#media all and (min-width: 800px) {
.main { flex: 3 0px; }
.aside-1 { order: 1; }
.main { order: 2; }
.aside-2 { order: 3; }
.footer { order: 4; }
}
<div class="wrapper">
<article class="main">
<img src="image" alt="">
<div class="text">
<b>Stuff</b>
<button>Button</button>
<p>Stuff</p></div>
</article>
<aside class="aside aside-2">
<div class="dropdown">
<button class="dropbtn">Downloads</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</aside>
<footer class="footer">
<div class="box-container">
<div class="box" data-href="http://www.google.de">Chapter 1</div>
<div class="box" data-href="http://www.bing.de">Chapter 2</div>
<div class="box">Chapter 3</div>
<div class="box">Chapter 4</div>
<div class="box">Chapter 5</div>
<div class="box">Chapter 6</div>
<div class="box">Chapter 7</div>
<div class="box">Chapter 8</div>
<div class="box">Chapter 9</div>
</div>
</footer>
</div>
I am trying to create a photo gallery app. Currently, I have it so that up until four images, each image takes up 100 / n % of the div. When the number of images is more than 5, I want to make a grid appear as:
How could I achieve this? I have been reading on CSS-grid but they only seem to show examples of aligning images in one line.
Here is my code:
#photos {
width: 634px;
height: 339px;
}
.photos-gallery {
width: 634px;
height: 275.03px;
}
.photos-gallery-header {
font-size: 24px;
font-weight: bold;
line-height: 32px;
color: #333333;
border-bottom: 1px solid #E1E1E1;
padding-bottom: 16px;
margin: 0 0 16px 0;
display: flex;
justify-content: space-between;
}
.photos-gallery-content {
height: 200px;
}
.photos-gallery-layout {
list-style: none;
padding: 0;
margin: 0;
display: flex;
}
.photos-gallery-li {
flex: 1;
}
.photo,
.photo img {
height: 100%;
}
.photo img {
width: 100%;
object-fit: cover;
}
<body>
<div id= "photos-gallery" class="photos content-block">
<h2 class="photos-gallery-header"> 2 Photos </h2>
<div class="photos-gallery-content">
<ul class="photos-gallery-layout">
<li class="photos-gallery-li">
<div class="photo">
<img src="https://i.imgur.com/8pTwPlXb.jpg"/>
</div>
</li>
<li class="photos-gallery-li">
<div class="photo">
<img src="https://i.imgur.com/OPAR3PCb.jpg"/>
</div>
</li>
<li class="photos-gallery-li">
<div class="photo">
<img src="https://i.imgur.com/A8eQsll.jpg"/>
</div>
</li>
<li class="photos-gallery-li">
<div class="photo">
<img src="https://i.imgur.com/8pTwPlXb.jpg"/>
</div>
</li>
<li class="photos-gallery-li">
<div class="photo">
<img src="https://i.imgur.com/OPAR3PCb.jpg"/>
</div>
</li>
<li class="photos-gallery-li">
<div class="photo">
<img src="https://i.imgur.com/A8eQsll.jpg"/>
</div>
</li>
<li class="photos-gallery-li">
<div class="photo">
<img src="https://i.imgur.com/8pTwPlXb.jpg"/>
</div>
</li>
</ul>
</div>
<div>
</body>
Use display: grid to achieve this. Using grid allows you to control column and row placement. Use this as a reference https://css-tricks.com/snippets/css/complete-guide-grid/
CSS
#photos-gallery {
height: 275px;
width: 634px;
}
.photos-gallery-header {
border-bottom: 1px solid #e1e1e1;
color: #333;
display: flex;
font-size: 24px;
font-weight: bold;
justify-content: space-between;
line-height: 32px;
margin: 0 0 16px;
padding-bottom: 16px;
}
.photos-gallery-content {
display: grid;
grid-template-columns: 20% 40% 20% 20%;
grid-column-gap: 0;
grid-row-gap: 0;
height: 200px;
}
.photos-gallery-content .item {
box-shadow: inset 0 0 0 1px #000;
overflow: hidden;
}
.photos-gallery-content .item-1 {
grid-column-start: 1;
grid-column-end: 1;
grid-row-start: 1;
grid-row-end: 1
}
.photos-gallery-content .item-2 {
grid-column-start: 1;
grid-column-end: 1;
grid-row-start: 2;
grid-row-end: 2;
}
.photos-gallery-content .item-3 {
grid-column-start: 2;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 3;
}
HTML
<div id="photos-gallery" class="photos content-block">
<h2 class="photos-gallery-header">2 Photos</h2>
<div class="photos-gallery-content">
<div class="item item-1">
<div class="photo">
<img src="https://i.imgur.com/8pTwPlXb.jpg"/>
</div>
</div>
<div class="item item-2">
<div class="photo">
<img src="https://i.imgur.com/OPAR3PCb.jpg"/>
</div>
</div>
<div class="item item-3">
<div class="photo">
<img src="https://i.imgur.com/A8eQsll.jpg"/>
</div>
</div>
<div class="item item-4">
<div class="photo">
<img src="https://i.imgur.com/8pTwPlXb.jpg"/>
</div>
</div>
<div class="item item-5">
<div class="photo">
<img src="https://i.imgur.com/OPAR3PCb.jpg"/>
</div>
</div>
<div class="item item-6">
<div class="photo">
<img src="https://i.imgur.com/A8eQsll.jpg"/>
</div>
</div>
<div class="item item-7">
<div class="photo">
<img src="https://i.imgur.com/8pTwPlXb.jpg"/>
</div>
</div>
</div>
<div>
DEMO