I want to create a responsive navigation bar for my page. I use floating divs for the logo and navigation link bar.
If you want to test the fiddle I recommend opening this on full screen.
#navbar {
top: 0;
position: sticky;
height: 80px;
padding: 0 160px;
background: #232323;
}
.navbarItemContainer {
height: 100%;
}
#navbarLogoContainer {
display: flex;
float: left;
align-items: center;
}
#navbarLinkContainer {
display: grid;
grid-template-columns: 100px 100px 100px;
float: right;
text-align: center;
align-items: center;
}
.navbarLink {
color: #039be5;
transition: 0.2s;
}
.navbarLink:hover {
color: #25bdf7;
transition: 0.2s;
}
.navbarLink>img {
width: 32px;
height: 32px;
display: none;
}
#media only screen and (max-width: 1000px) {
#navbar {
padding: 0 60px;
}
.navbarLink>span {
display: none;
}
.navbarLink>img {
display: block;
}
.navbarLink>img {
width: 24px;
height: 24px;
}
#navbarLinkContainer {
grid-template-columns: 24px 24px 24px;
grid-gap: 0 40px;
}
}
#media only screen and (max-width: 400px) {
/* place logo above button bar */
#navbar {
background: red;
/* just a test */
}
}
<div id="navbar">
<div id="navbarLogoContainer" class="navbarItemContainer">
<a>
<img class="img" src="/resources/logo.png">
</a>
</div>
<div id="navbarLinkContainer" class="navbarItemContainer">
<a class="navbarLink">
<span>
Link 1
</span>
<img class="img" src="">
</a>
<a class="navbarLink">
<span>
Link 2
</span>
<img class="img" src="">
</a>
<a class="navbarLink">
<span>
Link 3
</span>
<img class="img" src="">
</a>
</div>
</div>
When it comes to mobile screen sizes the navigation bar with 6 icons is too big. I want to adjust the navigation bar at a screen size of 400px.
#media only screen and (max-width: 400px) {
#navbar {
background: red; // placeholder
}
}
I want to remove the floating divs and place the logo above the button bar. When it comes to a screen size smaller than 400px the logo would have to move one row up. The result would be
How can I achieve this?
EDIT:
I tried
#media only screen and (max-width: 400px) {
#navbarLogoContainer {
clear: left;
}
#navbarLinkContainer {
grid-template-columns: 33.33% 33.33% 33.33%;
clear: right;
}
}
but it did not help.
Try this:
#media only screen and (max-width: 400px) {
/* place logo above button bar */
#navbar {
background: red;
/* just a test */
}
#navbarLogoContainer {
display:block;
float:none;
text-align:center;
}
#navbarLinkContainer {
display: block;
width: 100%;
text-align: center;
}
.navbarItemContainer {
height: 30px;
padding: 5px 0;
}
.navbarLink {
display:inline-block;
}
}
I also made a snippet:
#navbar {
top: 0;
position: sticky;
height: 80px;
padding: 0 160px;
background: #232323;
}
.navbarItemContainer {
height: 100%;
}
#navbarLogoContainer {
display: flex;
float: left;
align-items: center;
}
#navbarLinkContainer {
display: grid;
grid-template-columns: 100px 100px 100px;
float: right;
text-align: center;
align-items: center;
}
.navbarLink {
color: #039be5;
transition: 0.2s;
}
.navbarLink:hover {
color: #25bdf7;
transition: 0.2s;
}
.navbarLink>img {
width: 32px;
height: 32px;
display: none;
}
#media only screen and (max-width: 1000px) {
#navbar {
padding: 0 60px;
}
.navbarLink>span {
display: none;
}
.navbarLink>img {
display: block;
}
.navbarLink>img {
width: 24px;
height: 24px;
}
#navbarLinkContainer {
grid-template-columns: 24px 24px 24px;
grid-gap: 0 40px;
}
}
#media only screen and (max-width: 400px) {
/* place logo above button bar */
#navbar {
background: red;
/* just a test */
}
#navbarLogoContainer {
display:block;
float:none;
text-align:center;
}
#navbarLinkContainer {
display: block;
width: 100%;
text-align: center;
}
.navbarItemContainer {
height: 30px;
padding: 5px 0;
}
.navbarLink {
display:inline-block;
}
}
<div id="navbar">
<div id="navbarLogoContainer" class="navbarItemContainer">
<a>
<img class="img" src="/resources/logo.png">
</a>
</div>
<div id="navbarLinkContainer" class="navbarItemContainer">
<a class="navbarLink">
<span>
Link 1
</span>
<img class="img" src="">
</a>
<a class="navbarLink">
<span>
Link 2
</span>
<img class="img" src="">
</a>
<a class="navbarLink">
<span>
Link 3
</span>
<img class="img" src="">
</a>
</div>
</div>
Let me know if this helped.
here is a simple working example that can help you with your code
.container {
height: 100vh;
}
.navbar {
display: flex;
}
.navbar .logo-container {
flex: 1;
display: flex;
}
.navbar .logo-container span {
flex: 1;
}
.navbar .links-container {
flex: 1;
display: flex;
}
.navbar .links-container div {
flex: 1;
}
#media only screen and (max-width: 400px) {
.navbar {
display: grid;
grid-template-rows: 150px auto;
grid-template-columns: repeat(6, 1fr);
}
.navbar .logo-container {
grid-column: 1/7;
display: flex;
justify-content: center;
text-align: center;
}
.navbar .links-container {
grid-column: 1/7;
display: flex;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="container">
<div class="navbar">
<div class="logo-container">
<span>image</span>
</div>
<div class="links-container">
<div>link1</div>
<div>link2</div>
<div>link3</div>
<div>link4</div>
<div>link5</div>
<div>link6</div>
</div>
</div>
</div>
</body>
</html>
For 400px I think I'll go for this one
What do you think about this solution?
#media only screen and (max-width: 400px){
#navbar {
height: 100px;
padding: 0 5px;
}
#navbarLogoContainer {
height: 50%;
display: block;
float: none;
text-align: center;
}
#navbarLinkContainer {
height: 50%;
display: grid;
float: none;
grid-template-columns: 16.66% 16.66% 16.66% 16.66% 16.66% 16.66%;
justify-items: center;
grid-gap: 0 5px;
}
}
#navbar {
top: 0;
position: sticky;
height: 80px;
padding: 0 160px;
background: #232323;
}
.navbarItemContainer {
height: 100%;
}
#navbarLogoContainer {
display: flex;
float: left;
align-items: center;
}
#navbarLinkContainer {
display: grid;
grid-template-columns: 100px 100px 100px;
float: right;
text-align: center;
align-items: center;
}
.navbarLink {
color: #039be5;
transition: 0.2s;
}
.navbarLink:hover {
color: #25bdf7;
transition: 0.2s;
}
.navbarLink>img {
width: 32px;
height: 32px;
display: none;
}
#media only screen and (max-width: 1000px) {
#navbar {
padding: 0 60px;
}
.navbarLink>span {
display: none;
}
.navbarLink>img {
display: block;
}
.navbarLink>img {
width: 24px;
height: 24px;
}
#navbarLinkContainer {
grid-template-columns: 24px 24px 24px;
grid-gap: 0 40px;
}
}
#media only screen and (max-width: 400px) {
/* place logo above button bar */
#navbar {
background: red;
/* just a test */
}
}
<div id="navbar">
<div id="navbarLogoContainer" class="navbarItemContainer">
<a>
<img class="img" src="/resources/logo.png">
</a>
</div>
<div id="navbarLinkContainer" class="navbarItemContainer">
<a class="navbarLink">
<span>
Link 1
</span>
<img class="img" src="">
</a>
<a class="navbarLink">
<span>
Link 2
</span>
<img class="img" src="">
</a>
<a class="navbarLink">
<span>
Link 3
</span>
<img class="img" src="">
</a>
</div>
</div>
Related
So, right now my header doesn´t look good on mobile. - The words overlap. They should remain in the same order... I tried to use line-height, which did not really change anything. Maybe you have some suggestions on how I can fix this problem. I am thankful for every suggestion!
[enter
.header {
width: 100%;
height: 7vh;
padding: 0 5%;
color: white;
font-size: larger;
background-color: black;
z-index:100;
position: sticky;
top: 0;
display: flex;
justify-content: space-between;
overflow: hidden;
line-height: 10px;
}
nav {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
padding: 4px 0;
}
nav ul li {
display: inline-block;
list-style: none;
margin: 10px 30px;
}
nav ul li a {
text-decoration: none;
color: white;
}
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.Menü {
color: white;
text-decoration: none;
margin-left: 40px;
}
<div class="header" >
<nav>
<div>
<Button class="ImageButton"> <input class="ImageButton" type="image" src="/Images/Camellion Logo Website.png"></Button>
</div>
<ul>
<a class="Menü" href="/Galerie/Galerie.html">Galerie</a>
<a class="Menü" href="#Leistungen">Leistungen</a>
<a class="Menü" href="#Kontakt">Kontakt & Standort <i class="fa-solid fa-location-dot"></i></a>
</ul>
</nav>
</div>
There is a thing in CSS called media queries. With media queries you can write for example CSS that only takes effect if screen is certain size.
For example
#media only screen and (min-width: 360px) and (max-width: 800px) {
.header {
height: 60px;
}
}
Will set header height to 60px if the device that the page is opened on has screen width more then 360px and less then 800px.
Here is your code with couple adjustments
* { padding: 0; margin: 0; box-sizing: border-box; }
.header {
width: 100%;
padding: 0 20px;
color: white;
background-color: black;
position: sticky;
z-index:100;
top: 0;
}
.navigation {
margin: auto;
max-width: 1200px;
height: 80px;
display: flex;
align-items: center;
justify-content: space-between;
}
.menu-icon { /* CSS to just to simulate Icon */
height: 40px;
width: 40px;
background-color: white;
border-radius: 10px;
}
.menu {
display: flex;
align-items: center;
gap: 40px;
}
.menu-item {
color: white;
text-decoration: none;
white-space: nowrap; /* Will not let words to break to next line */
font-size: 20px;
font-weight: 700;
}
#media(max-width: 600px) {
.header { padding: 0 10px; }
.navigation { height: 60px; }
.menu { gap: 20px;}
.menu-item { font-size: 14px; }
}
#media(max-width: 400px) {
.menu { gap: 10px;}
.menu-item { font-size: 12px; }
}
<header class="header" >
<nav class="navigation">
<div class="menu-icon" title="Menu icon / hamburger icon"></div>
<div class="menu">
<a class="menu-item" href="/Galerie/Galerie.html">Galerie</a>
<a class="menu-item" href="#Leistungen">Leistungen</a>
<a class="menu-item" href="#Kontakt">Kontakt & Standort <i class="fa-solid fa-location-dot"></i></a>
</div>
</nav>
</header>
You can adjust your css even further. It's very popular to have icons with menu elements and on mobile view they become a bottom navigation bar with big icons and tiny text.
#media (max-width:767px) {
* {
box-sizing: border-box;
}
nav {
flex-direction: column;
}
nav .Menu {
margin-inline: 9px
}
}
good luck
I currently have this navigation bar in 1280 pixel resolution as follows:
To make it responsive, I want to have the title at the bottom of the logo image when it reaches 1080 pixels. Below is the sample I want it to have it exactly:
I have been using CSS Grid to divided the columns and rows. I am using media queries to have my website responsive, but for some reason nothing works when I try to increase the size of the grid-template-rows elements in CSS.
So far, this is what I got:
I can only assume that the title is hiding behind the image since the row is not large enough to fit below it. I'm not exactly sure how to fix my code.
HTML
#media only screen and (max-width: 1280px) {
.main {
display: grid;
height: 100vh;
grid-template-columns: 1fr 1fr 1fr 1.2fr;
grid-template-rows: 0.2fr 0.4fr 0.2fr 0.70fr 0.45fr;
grid-template-areas:
"nav nav nav nav"
"main-heading main-heading main-heading main-heading"
"sub-heading sub-heading sub-heading sub-heading"
"icons icons icons icons"
"images images images contents";
grid-gap: 0.2rem;
}
#navbar {
display: inline-block;
grid-area: nav;
background: orange;
border-radius:var(--main-radius);
padding-top: var(--main-padding);
}
#navbar img, header, ul, li {
display: inline-block;
vertical-align: middle;
}
#navbar img {
border-radius: 50%;
margin-left: 20px;
top: -13px;
position: absolute;
}
h3 {
margin-left: 120px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
/*display: inline-block;*/
float: right;
margin-top: 10px;
margin-right: 20px;
}
li {
display: inline-block;
padding: 12px;
}
#main-heading {
grid-area: main-heading;
background: yellow;
font-weight: bold;
font-size: 30px;
display: flex;
align-items: center;
justify-content: center;
}
#sub-heading {
grid-area: sub-heading;
background: pink;
font-weight: bold;
font-size: 20px;
display: flex;
align-items: center;
justify-content: center;
}
#icons {
grid-area: icons;
background: lightblue;
display: flex;
align-items: center;
justify-content: center;
}
#icons img {
padding: 30px;
}
#images {
grid-area: images;
background: orange;
}
#images_heading {
margin-left: 20px;
}
#images img {
margin-left: 30px;
padding: 15px;
}
#contents {
grid-area: contents;
background: brown;
}
#contents_first img {
float:left;
}
#contents_first h5 {
font-size: 15px;
}
/*#contents_first {
display: flex;
margin-left: 10px;
}*/
/*#contents_first h5 {
float: right;
margin-left: 20px;
}/*
/*#contents_first h5 {
margin-top: 3px;
margin-left: 10px;
font-size: 15px;
}*/
#contents_second img {
float:left;
}
#contents_second h5 {
font-size: 15px;
}
#contents_third img {
float: left;
}
#contents_third h5 {
font-size: 15px;
}
#contents_fourth img {
float: left;
}
#contents_fourth #name {
font-size: 17px;
}
footer {
text-align: right;
}
}
#media only screen and (max-width: 1080px)
{
.main {
display: grid;
height: 100vh;
grid-template-columns: 1fr 1fr 1fr 1.2fr;
grid-template-rows: 0.2fr 0.4fr 0.2fr 0.70fr 0.45fr 0.1fr;
grid-template-areas:
"nav nav nav nav"
"main-heading main-heading main-heading main-heading"
"sub-heading sub-heading sub-heading sub-heading"
"icons icons icons icons"
"images images images images"
"contents contents contents contents";
grid-gap: 0.2rem;
}
#navbar {
display: inline-block;
grid-area: nav;
background: ;
border-radius:var(--main-radius);
padding-top: var(--main-padding);
}
#navbar img, header, ul, li {
display: inline-block;
vertical-align: middle;
}
#navbar img {
border-radius: 50%;
margin-left: 500px;
top: -13px;
position: absolute;
}
h3 {
margin-left: 450px;
margin-bottom: -900px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
/*display: inline-block;*/
float: right;
margin-top: 10px;
margin-right: 20px;
}
li {
display: inline-block;
padding: 12px;
}
#main-heading {
grid-area: main-heading;
background: yellow;
font-weight: bold;
font-size: 30px;
display: flex;
align-items: center;
justify-content: center;
}
#sub-heading {
grid-area: sub-heading;
background: pink;
font-weight: bold;
font-size: 20px;
display: flex;
align-items: center;
justify-content: center;
}
#icons {
grid-area: icons;
background: lightblue;
display: flex;
align-items: center;
justify-content: center;
}
#icons img {
padding: 30px;
}
#images {
grid-area: images;
background: orange;
}
#images_heading {
margin-left: 20px;
}
#images img {
margin-left: 30px;
padding: 15px;
}
#contents {
grid-area: contents;
background: brown;
}
#contents_first img {
float:left;
}
#contents_first h5 {
font-size: 15px;
}
/*#contents_first {
display: flex;
margin-left: 10px;
}*/
/*#contents_first h5 {
float: right;
margin-left: 20px;
}/*
/*#contents_first h5 {
margin-top: 3px;
margin-left: 10px;
font-size: 15px;
}*/
#contents_second img {
float:left;
}
#contents_second h5 {
font-size: 15px;
}
#contents_third img {
float: left;
}
#contents_third h5 {
font-size: 15px;
}
#contents_fourth img {
float: left;
}
#contents_fourth #name {
font-size: 17px;
}
footer {
text-align: right;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive J Web</title>
<link rel="stylesheet" href="Lab04.css">
</head>
<body>
<div class = "main">
<section id = "navbar">
<img src="lens.jpg" alt=lens width=90px height=90px>
<header>
<h3>
Art of Photography
</h3>
</header>
<ul>
<li><div class="navbar_left">Photography</div></li>
<li><div class="navbar_left">History</div></li>
<li><div class="navbar_right">Samples</div></li>
<li><div class="navbar_right">About</div></li>
</ul>
</section>
<section id = "main-heading">SELF-PORTRAIT & STREET PHOTOGRAPHY
</section>
<div id = "sub-heading">FROM VANCOUVER, BC</div>
<div id = "icons">
<img src="first_circle.png" alt="first_circle" width=70px height=70px>
<img src="second_circle.png" alt="second_circle" width=70px height=70px>
<img src="third_circle.png" alt="third_circle" width=70px height=70px>
<img src="fourth_circle.png" alt="fourth_circle" width=70px height=70px>
</div>
<div id = "images">
<p id = "images_heading">Navigation page</p>
<img src="camera_atmosphere.jpg" alt="camera_atmosphere" width=250px height=140px>
<img src="camera_guy.jpg" alt="camera_guy" width=250px height=140px>
<img src="graph.jpg" alt="graph" width=250px height=140px>
<img src="rolliflex.jpg" alt="graph" width=250px height=140px>
</div>
<div id="contents">
<p id = "contents_heading">
News
</p>
<section id = "contents_first">
<img src="camera_atmosphere.jpg" alt="camera_atmosphere" width=50px height=50px>
<h5>PHOTO CAPTURED IN VANCOUVER, STANLEY PARK.</h5>
</section>
<section id ="contents_second">
<img src="camera_guy.jpg" alt="camera_guy" width=50px height=50px>
<h5>CAMERA GUY WHO ALWAYS LOOK FOR ADVENTURE.</h5>
</section>
<section id = "contents_third">
<img src="first_circle.png" alt="first_circle" width=50px height=50px>
<h5>CIRCLE CAPTURED IN SOMEWHERE IN THE WORLD.</h5>
</section>
<br/>
<section id = "contents_fourth">
<img src="mail_icon.jpg" alt="mail_icon" width= 20px height=20px>
<p id="name">J<span style="font-weight:bold">A</span></p>
<p id="name_info">Please reach out to J A for more information.</p>
<footer>© J A</footer>
</section>
</div>
</div>
</body>
instead of using '1fr' use 'auto'
you can do this
grid-template: auto 1fr auto/ auto 1fr auto;
How would I make it so the image resizes properly when viewed on smaller screens. Right now, the image is over the container when viewed on smaller screens. There are also small gaps between the top/left of the container and the image. Would I have to resize the image in the media query or expand the width of my container in the media query?
.container {
width: 88%;
margin: 50px auto;
margin-top: 0px;
}
.heading {
text-align: center;
font-size: 30px;
margin-bottom: 50px;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-around;
flex-flow: wrap;
}
.card {
width: 30%;
background: #fff;
border: 1px solid #ccc;
margin-bottom: 50px;
}
.image {
width: fit-content;
height: fit-content;
}
.card-body {
padding: 30px 10px;
text-align: left;
font-size: 18px;
}
.card-body .btn {
display: block;
color: #fff;
text-align: center;
background: black;
margin-top: 30px;
text-decoration: none;
padding: 10px 5px;
}
#media screen and (max-width: 1000px) {
.card {
width: 40%;
}
.heading {
text-align: auto;
}
.card-header {
margin: auto;
}
.image {
width: fit-content;
height: fit-content;
}
}
#media screen and (max-width: 620px) {
.container {
width: 100%;
}
.heading {
padding: 20px;
font-size: 20px;
text-align: auto;
}
.card {
width: 80%;
}
.image {
width: fit-content;
height: fit-content;
}
}
<div class="container">
<div class="heading">
<h1>Latest Posts</h1>
</div>
<div class="row">
<div class="card">
<div class="image">
<img src="https://via.placeholder.com/1920x1080.jpg">
</div>
<div class="card-body">
<p>
Text Here
</p>
Read more
</div>
</div>
I typically put width: 100%; on images in my projects and height: auto this way the image will be more responsive and scale up and down. You can reduce the width for the smaller media query if you want an even smaller image (width: 85%; for example) or I would probably personally end up reducing the width of the container to get the desired result.
1st: Remove your CSS for the class .image
2nd: Add this CSS line to the base-css (not within the media queries):
img {
object-fit: contain;
width: 100%;
}
What will that do?
object-fit: contain will keep the image aspect ratio while width: 100% will cause the image to fit exactly the given space. The height is set automatically according to the width while it maintain the image aspect ratio as mentioned above.
.container {
width: 88%;
margin: 50px auto;
margin-top: 0px;
}
.heading {
text-align: center;
font-size: 30px;
margin-bottom: 50px;
}
.row {
display: flex;
flex-direction: row;
justify-content: space-around;
flex-flow: wrap;
}
.card {
width: 30%;
background: #fff;
border: 1px solid #ccc;
margin-bottom: 50px;
}
.image {
width: fit-content;
height: fit-content;
}
.card-body {
padding: 30px 10px;
text-align: left;
font-size: 18px;
}
.card-body .btn {
display: block;
color: #fff;
text-align: center;
background: black;
margin-top: 30px;
text-decoration: none;
padding: 10px 5px;
}
img {
object-fit: contain;
width: 100%;
}
#media screen and (max-width: 1000px) {
.card {
width: 40%;
}
.heading {
text-align: auto;
}
.card-header {
margin: auto;
}
}
#media screen and (max-width: 620px) {
.container {
width: 100%;
}
.heading {
padding: 20px;
font-size: 20px;
text-align: auto;
}
.card {
width: 80%;
}
}
<div class="container">
<div class="heading">
<h1>Latest Posts</h1>
</div>
<div class="row">
<div class="card">
<div class="image">
<img src="https://via.placeholder.com/1920x1080.jpg">
</div>
<div class="card-body">
<p>
Text Here
</p>
Read more
</div>
</div>
I'm finding that on mobile, short text that i know can appear in just 1 line is taking up 2 lines.
I've found that "white-space: nowrap" will make sure that it will appear in 1 line. However, now I have a problem where long text that is supposed to take up 2 lines is now forced to appear in 1 line, which is a problem.
What's causing this issue and how do I solve it?
It's the third Lorem Ipsum
<div class="name">Lorem Ipsum </div>
That's causing this problem
What I want is that text that's supposed to take up 1 line (or 2 lines) take up just 1 line (or 2 lines).
.grid article {
position: relative;
}
.students {
position: absolute;
}
.person {
display: flex;
align-items: center;
margin,
padding: 0px;
}
.name {
letter-spacing: 2.2px;
flex: 1;
margin-left: 20px;
}
.picture {
flex: 0 0 28px;
margin: 0;
}
.person img {
margin-top: 0;
margin-bottom: 0;
vertical-align: middle;
}
#media (min-width: 1312px) {
.students {
top: 565px;
}
.name {
font-size: 15px;
letter-spacing: 2.2px;
margin-left: 20px;
}
.person img {
width: 28px;
}
.person {
margin-bottom: -1px;
}
}
#media (max-width: 599px) {
.students {
top: 375px;
}
.name {
font-size: 11px;
letter-spacing: 2.2px;
margin-left: 6px;
}
.person img {
width: 21px;
}
.person {
margin-bottom: 5px;
}
}
<article>
<a href="#">
<div class="photo" style="background-image: url(img/image.png);"></div>
</a>
<div class="text">
<div class="one">Lorem Ipsum</div>
<div class="two">Lorem Ipsum</div>
</div>
<div class="students">
<div class="person">
<div class="picture"><img src="img/sample.jpg"></div>
<div class="name">Lorem Ipsum </div>
</div>
</div>
</article>
Grid Layout CSS
.grid {
display: -ms-flexbox;
-ms-flex-wrap: wrap;
display: grid;
justify-content: start;
margin-top: -20px;
justify-content: space-between;
}
.grid article {
-ms-flex: 0 0 250px;
}
.grid .photo {
width: 100%;
background-size: contain;
background-position: center;
}
.grid .photo:after {
content: "";
display: block;
padding-bottom: 100%;
}
.grid a {
display: block;
text-decoration: none;
color: inherit;
}
.grid a:hover {
opacity: 0.8;
}
.grid .text {
margin: 0px;
}
#media (max-width: 599px) {
.grid {
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
grid-row-gap: 140px;
grid-column-gap: 20px;
margin-top: 18px;
}
}
Do you need the image to be next to the text on mobile?
If you want them to stay aligned then just add flex-direction: row; to your person class in the #media (max-width: 599px) and set a max-width on your picture and name div that doesn't exceed 599px.
If you want them to stack then just add flex-direction: column; to your person class in the #media (max-width: 599px)
Note: You may need to adjust width depending on how much text you want next to your image so it doesn't wrap when it hits the width threshold on mobile.
See snippet below.
.grid article {
position: relative;
}
.students {
position: absolute;
}
.person {
display: flex;
align-items: center;
margin,
padding: 0px;
}
.name {
letter-spacing: 2.2px;
flex: 1;
margin-left: 20px;
}
.picture {
flex: 0 0 28px;
margin: 0;
}
.person img {
margin-top: 0;
margin-bottom: 0;
vertical-align: middle;
}
#media (min-width: 1312px) {
.students {
top: 565px;
}
.name {
font-size: 15px;
letter-spacing: 2.2px;
margin-left: 20px;
}
.person img {
width: 28px;
}
.person {
margin-bottom: -1px;
}
}
#media (max-width: 599px) {
.students {
top: 375px;
}
.name {
font-size: 11px;
letter-spacing: 2.2px;
margin-left: 6px;
display: block;
width: 100%;
}
.picture {
width: 21px;
max-width: 21px;
}
.person {
flex-direction: row;
max-width: 200px;
}
}
<article>
<a href="#">
<div class="photo" style="background-image: url(img/image.png);"></div>
</a>
<div class="text">
<div class="one">Lorem Ipsum</div>
<div class="two">Lorem Ipsum</div>
</div>
<div class="students">
<div class="person">
<div class="picture"><img src="https://cdn.pixabay.com/photo/2020/10/28/11/08/castle-5693094__340.jpg"></div>
<div class="name">Lorem Ipsum </div>
</div>
</div>
</article>
I'm trying to create a scrolling element without a fixed height parent. I want #SECTION1 of my code to be scrollable to show the rest of the images. I can't seem to find a way to do this. I've attempted to set #SECTION1 to a fixed height but it forces my images to be squashed. Any help would be appreciated. Thank you.
Here is my code:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
text-decoration: none;
}
::-webkit-scrollbar {
width: 15px;
}
/* Track */
::-webkit-scrollbar-track {
background: #f1f1f1;
}
/* Handle */
::-webkit-scrollbar-thumb {
background: #888;
}
/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
background: #555;
}
html,
body {
margin: 0;
padding: 0;
height: 100vh;
overflow: hidden;
}
/*----------SECTION 1----------*/
header {
height: 80px;
background-color: black;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
#header-wrapper {
display: flex;
width: 55%;
justify-content: space-between;
align-items: center;
}
#logo {
width: 70px;
}
nav a {
color: white;
padding: 20px;
font-family: 'Roboto';
font-size: 0.8em;
font-weight: bold;
}
#media only screen and (max-width: 750px) {
nav {
display: none;
}
}
#mobile-menu {
color: white;
font-size: 1.3em;
}
#media only screen and (min-width: 750px) {
#mobile-menu {
display: none;
}
}
#body-wrapper {
display: flex;
height: 100%;
}
aside {
width: 300px;
height: 889px;
background-color: #0c0c0c;
display: flex;
align-items: center;
padding-top: 50px;
flex-direction: column;
}
#aside-wrap {
width: 70%;
}
#user-info {
display: flex;
margin: 10px;
margin-left: 0;
margin-bottom: 50px;
justify-content: center;
align-items: center;
font-family: 'Roboto';
font-weight: 400;
}
#user {
font-size: 40px;
color: white;
margin-right: 20px;
}
aside h3 {
color: white;
font-size: 1.2em;
}
#hello {
color: white;
font-size: 20px;
}
#box-1 {
color: #808080;
margin-bottom: 60px;
}
#box-1 p {
margin: 20px;
margin-left: 0;
font-family: 'Roboto';
font-size: 0.9em;
}
#box-2 {
color: #808080;
}
#box-2 p {
margin: 20px;
margin-left: 0;
font-family: 'Roboto';
font-size: 0.9em;
}
#section1 {
background-color: #191919;
/*background: linear-gradient(rgba(0,0,0,0.3),rgba(0,0,0,0.3)),
url("listen_background.jpg");*/
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 100%;
overflow: auto;
}
#section1-wrapper {
width: 80%;
display: flex;
font-family: 'Roboto';
padding-top: 50px;
padding-bottom: 50px;
align-items: center;
flex-direction: column;
}
#section1 h1 {
color: white;
font-size: 3em;
margin-bottom: 50px;
text-align: center;
}
.image-box {
max-width: 280px;
margin: 20px;
}
img {
max-width: 100%;
}
#image-row-1,
#image-row-2,
#image-row-3,
#image-row-4 {
width: 100%;
margin-bottom: 50px;
display: flex;
justify-content: space-between;
}
#media only screen and (max-width: 1080px) {
#image-row-1,
#image-row-2,
#image-row-3,
#image-row-4 {
flex-direction: column;
align-items: center;
}
}
/*----------------SECTION 2--------------*/
#pusher {
height: 889px;
width: 300px;
}
#player {
height: 80px;
width: 100%;
position: fixed;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
background-color: black;
}
#media only screen and (max-width: 750px) {
#player {
height: auto;
}
}
#player-wrapper {
display: flex;
justify-content: space-between;
align-items: center;
width: 80%;
}
#media only screen and (max-width: 750px) {
#player-wrapper {
flex-direction: column;
}
}
.button-controls {
color: white;
margin: 20px;
}
#player-bar {
width: 100%;
height: 3px;
background-color: white;
}
#player-filler {
width: 50%;
height: 100%;
background-color: #2A4B5A;
}
#timeline {
width: 50%;
display: flex;
justify-content: center;
align-items: center;
}
#media only screen and (max-width: 750px) {
#timeline {
width: 100%;
}
}
#timeline p {
color: white;
margin: 20px;
}
#share,
#phone {
color: white;
margin: 20px;
}
#media only screen and (max-width: 750px) {
#share,
#phone {
display: none;
}
}
<head>
<meta charset="utf-8">
<title>Flo Music</title>
<link rel="stylesheet" type="text/css" href="listen.css">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
</head>
<body>
<div id="test">
<div id="body-wrapper">
<aside>
<div id="aside-wrap">
<div id="user-info">
<i class="far fa-user-circle" id="user"></i>
<h3>Emmanuel</h3>
</div>
<div id="box-1">
<p>Your Library</p>
<p>Recently Played</p>
<p>Songs</p>
<p>Playlist</p>
</div>
<div id="box-2">
<p>Your Library</p>
<p>Recently Played</p>
<p>Songs</p>
<p>Playlist</p>
</div>
<p>HOME</p>
</div>
</aside>
<section id="section1">
<div id="section1-wrapper">
<h1>New Releases</h1>
<div id="image-row-1">
<div class="image-box"><img src="album1.jpg"></div>
<div class="image-box"><img src="album2.jpg"></div>
<div class="image-box"><img src="album3.jpg"></div>
<div class="image-box"><img src="album4.jpg"></div>
</div>
<div id="image-row-2">
<div class="image-box"><img src="album5.jpg"></div>
<div class="image-box"><img src="album6.jpg"></div>
<div class="image-box"><img src="album7.jpg"></div>
<div class="image-box"><img src="album8.png"></div>
</div>
<div id="image-row-3">
<div class="image-box"><img src="album9.jpg"></div>
<div class="image-box"><img src="album10.jpg"></div>
<div class="image-box"><img src="album11.jpg"></div>
<div class="image-box"><img src="album12.jpg"></div>
</div>
<div id="image-row-4">
<div class="image-box"><img src="album13.jpg"></div>
<div class="image-box"><img src="album14.jpg"></div>
<div class="image-box"><img src="album15.jpg"></div>
<div class="image-box"><img src="album16.jpg"></div>
</div>
</div>
</section>
</div>
<div id="player">
<div id="player-wrapper">
<div id="controls">
<i class="fas fa-backward button-controls"></i>
<i class="fas fa-play button-controls"></i>
<i class="fas fa-forward button-controls"></i>
</div>
<div id="timeline">
<p>0:00</p>
<div id="player-bar">
<div id="player-filler"></div>
</div>
<p>0:00</p>
</div>
<div id="icon-right">
<i class="fas fa-share-square" id="share"></i>
<i class="fas fa-mobile" id="phone"></i>
</div>
Flex items are set to flex-shrink: 1 by default. This means they can shrink to prevent an overflow of the container. In your case, you may need to disable this feature (flex-shrink: 0).
Also, consider using height: 100vh, instead of height: 100% on your flex container. Percentage heights are tricky and often require the parent to have a defined height.
See this post for details: Working with the CSS height property and percentage values
Lastly, remove justify-content: center from your flex container. It makes content inaccessible via scroll in some cases.
See this post for details: Can't scroll to top of flex item that is overflowing container
Make these adjustments to your code:
#body-wrapper {
display: flex;
/* height: 100%; */
height: calc(100vh - 80px); /* subtract footer height */
}
#section1 {
background-color: #191919;
display: flex;
align-items: center;
/* justify-content: center; */ /* remove to avoid scrolling problems */
flex-direction: column;
width: 100%;
overflow: auto;
}
#section1-wrapper {
width: 80%;
display: flex;
font-family: 'Roboto';
padding-top: 50px;
padding-bottom: 50px;
align-items: center;
flex-direction: column;
flex-shrink: 0; /* add to disable flex default shrinking feature */
}
jsFiddle demo
You should change your overflow property in the #section1
#section1-wrapper {
overflow: scroll;
}