Keep the aside column fixed width in this flexbox layout - html

I have a simple flexbox layout for a new website.
It has a header, a nav, a cover that is out of container in full screen and the a content and an aside.
#container {
margin: 0 auto;
max-width: 1080px;
}
#columns {
display: -ms-flex;
display: -webkit-flex;
display: flex;
flex-flow: row wrap;
}
header {
padding: 3%;
background-color: #6495ED;
}
#cover {
max-width: 1350px;
margin: 0 -135px;
padding: 3%;
background-color: #6495ED;
}
header #title {
font-size: 50px;
color: #fff;
}
nav {
width: 97%;
background-color: #6495ED;
padding: 0 1.5% 0 1.5%;
}
nav ul li {
display: inline-block;
padding: 15px 1.5% 15px 1.5%;
}
nav ul li a {
text-decoration: none;
color: #ffffff;
font-size: 1.2em;
}
nav ul li a:hover {
color: #000000;
text-decoration: none;
}
/* Content After
--------------------------------------------- */
#columns>#content {
padding: 3%;
width: 64%;
}
#columns>aside {
padding: 3%;
min-width: 24%;
background-color: #eee;
}
footer {
width: 94%;
padding: 3%;
background-color: #6495ED;
border-top: 5px solid black;
color: #fff;
text-align: center;
}
#media all and (max-width: 1350px) {
#cover {
max-width: 1080px;
margin: 0;
}
}
#media all and (max-width: 768px) {
header {
text-align: center;
}
nav {
text-align: center;
}
#columns {
-webkit-flex-flow: column wrap;
flex-flow: column wrap;
}
#columns>#content {
width: 94%;
padding: 3%;
}
#columns>#sidebar {
width: 94%;
padding: 3%;
border-top: 3px solid #E64A19;
}
}
#media all and (max-width: 330px) {
nav ul li {
display: block;
width: 94%;
}
}
<div id="container">
<!-- header -->
<header id="header">
<h1 id="title">Two Column</h1>
</header>
<!-- Navigation -->
<nav id="menu" class="clearfix">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
</nav>
<section id="cover">
</section>
<section id="columns">
<!-- Main Content area -->
<section id="content">
<!-- your content here -->
</section>
<!-- Sidebar -->
<aside id="sidebar">
<!-- your content here -->
</aside>
</section>
<footer id="footer" class="clearfix">
Copyright &copy Booo
</footer>
</div>
https://codepen.io/anon/pen/YQzWNe
The width of the aside column fixed for desktop and tablet, 360px, then when I go under 768 px of viewport the column disappear.
I try to replace the width in percentage with min-width:360px but I broke the layout.
How can I fix it?

You mention the aside "disappearing" but I see it re-flowing below #content on smaller viewports.
If you need aside to be 360px and have #content fill the remaining space set flex-grow: 1; for #content and tell #sidebar to be 360px.
#container {
margin: 0 auto;
max-width: 1080px;
}
#columns {
display: -ms-flex;
display: -webkit-flex;
display: flex;
}
#header,
#cover,
#footer {
padding: 3%;
background-color: #6495ED;
}
#cover {
max-width: 1350px;
margin: 0 -135px;
}
#title {
font-size: 50px;
color: #fff;
}
#menu {
background-color: #6495ED;
padding: 0 1.5%;
}
#menu ul {
margin: 1rem;
padding: 0;
}
#menu li {
display: inline-block;
padding: 15px 1.5%;
}
#menu a {
text-decoration: none;
color: #fff;
font-size: 1.2rem;
}
#menu a:hover {
color: #000;
}
/* Content After
--------------------------------------------- */
#content,
#sidebar {
padding: 3%;
}
#content {
flex-grow: 1;
}
#sidebar {
min-width: 360px;
background-color: #eee;
}
#footer {
border-top: 5px solid black;
color: #fff;
text-align: center;
}
#media all and (max-width: 1350px) {
#cover {
max-width: 1080px;
margin: 0;
}
}
#media all and (max-width: 768px) {
#header,
#menu {
text-align: center;
}
#columns {
-webkit-flex-direction: column;
flex-direction: column;
}
#sidebar {
border-top: 3px solid #E64A19;
}
}
#media all and (max-width: 330px) {
#menu li {
display: block;
padding: 0 3%;
}
}
<div id="container">
<!-- header -->
<header id="header">
<h1 id="title">Two Column</h1>
</header>
<!-- Navigation -->
<nav id="menu" class="clearfix">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
</nav>
<section id="cover">
</section>
<section id="columns">
<!-- Main Content area -->
<section id="content">
<!-- your content here -->
</section>
<!-- Sidebar -->
<aside id="sidebar">
<!-- your content here -->
</aside>
</section>
<footer id="footer" class="clearfix">
Copyright &copy Booo
</footer>
</div>
Note: Improved the CSS a little.

Related

My current header is does not work on mobile. How can I make my header responsive?

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

How to center a sidebar menu elements

I found this code example of a responsive sidebar menu in this link.
This example is excellent because is totally responsive but
the menu bar elements (Home, News, Contact, About) are always positioned in left part of the menu, i am looking for to position these elements at the top-center in big screen of the page, just like this example
but i was not able to succeed, i am a beginner in css, so i ask for help here, any help is appreciated, thank you very much, here is the code :
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
font-family: "Lato", sans-serif;
}
.sidebar {
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
}
.sidebar a {
display: block;
color: black;
padding: 16px;
text-decoration: none;
}
.sidebar a.active {
background-color: #04AA6D;
color: white;
}
.sidebar a:hover:not(.active) {
background-color: #555;
color: white;
}
div.content {
margin-left: 200px;
padding: 1px 16px;
height: 1000px;
}
#media screen and (max-width: 700px) {
.sidebar {
width: 100%;
height: auto;
position: relative;
}
.sidebar a {float: left;}
div.content {margin-left: 0;}
}
#media screen and (max-width: 400px) {
.sidebar a {
text-align: center;
float: none;
}
}
</style>
</head>
<body>
<div class="sidebar">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
<div class="content">
<h2>Responsive Sidebar Example</h2>
<p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
<p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
<h3>Resize the browser window to see the effect.</h3>
</div>
</body>
</html>
First, you have to change the html "sidebar" div, Add another div "a-holder"
<div class="sidebar">
<div class="a-holder">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
then in the css, add a-holder with parameters like these:
#media screen and (max-width: 700px) {
.sidebar {
width: 100%;
height: auto;
position: relative;
}
.sidebar a {float: left;}
div.content {margin-left: 0;}
.a-holder {
margin: auto;
align-self: center;
width: 80%;
}
}
Then the full code will be
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
font-family: "Lato", sans-serif;
}
.sidebar {
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
}
.sidebar a {
display: block;
color: black;
padding: 16px;
text-decoration: none;
}
.sidebar a.active {
background-color: #04AA6D;
color: white;
}
.sidebar a:hover:not(.active) {
background-color: #555;
color: white;
}
div.content {
margin-left: 200px;
padding: 1px 16px;
height: 1000px;
}
#media screen and (max-width: 700px) {
.sidebar {
width: 100%;
height: auto;
position: relative;
}
.sidebar a {float: left;}
div.content {margin-left: 0;}
.a-holder {
margin: auto;
align-self: center;
width: 80%;
}
}
#media screen and (max-width: 400px) {
.sidebar a {
text-align: center;
float: none;
}
}
</style>
</head>
<body>
<div class="sidebar">
<div class="a-holder">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
<div class="content">
<h2>Responsive Sidebar Example</h2>
<p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
<p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
<h3>Resize the browser window to see the effect.</h3>
</div>
</body>
</html>
As of the image provided in the question and the link provided to the template. I believe you have to add an image at the top of side navigations. As of solution to the link provided in the question.
#media screen and (min-width: 767) {
.sidebar{
text-align:center;
margin: 0;
padding: 0;
background-color: #f1f1f1;
height: 100%;
overflow: auto;
}
.sidebar a {
display: inline-block;
color: black;
padding: 16px;
text-decoration: none;
}
}
Simply swap the properties of sidebar from #media screen and (max-width: 700px) to regular one
Here is a sample
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
font-family: "Lato", sans-serif;
}
.sidebar {
margin: 0;
padding: 0;
background-color: #f1f1f1;
overflow: auto;
width: 100%;
height: auto;
position: relative;
}
*{box-sizing: border-box;}
.sidebar::after {content: ''; clear: both; display: table;}
.sidebar .logo {float: left; width: 120px; padding-left: 10px; padding-top: 15px;}
.sidebar .social_links{float: right; width: 100px;}
.sidebar .social_links ul li{list-style:none; display: inline-block;}
.sidebar .social_links ul{margin: 0;}
.sidebar .social_links ul li a {padding: 15px 5px}
.sidebar .menu {float: left; width: calc(100% - 220px); text-align:center;}
.sidebar a {
display: inline-block;
color: black;
padding: 16px;
text-decoration: none;
}
.sidebar a.active {
background-color: #04AA6D;
color: white;
}
.sidebar a:hover:not(.active) {
background-color: #555;
color: white;
}
div.content {
padding: 1px 16px;
}
#media screen and (max-width: 700px) {
.sidebar {
width: 200px;
position: fixed;
height: 100%;
}
.sidebar a {float: none;}
.sidebar .logo {float: none; display: block; width: 100%; text-align: center; margin-bottom: 20px;}
.sidebar .menu, .sidebar .social_links {float: none; width: 100%;}
.sidebar .menu a{display: block; }
.sidebar .social_links li {float: none; text-align:center;}
.sidebar .social_links ul{padding-left: 0; width: 100%; text-align:center;}
div.content {margin-left: 0; height: 1000px; margin-left: 200px;}
}
#media screen and (max-width: 400px) {
.sidebar a {
text-align: center;
float: none;
}
}
</style>
</head>
<body>
<div class="sidebar">
<div class="logo">LOGO HERE</div>
<div class="menu">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
<div class="social_links">
<ul>
<li>fb</li>
<li>in</li>
</ul>
</div>
</div>
<div class="content">
<h2>Responsive Sidebar Example</h2>
<p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
<p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
<h3>Resize the browser window to see the effect.</h3>
</div>
</body>
</html>
This code is also responsive when screen size shrinks below 700px
PS: Run the code in full page to see how it looks like in desktop view
body {
margin: 0;
font-family: "Lato", sans-serif;
}
.sidebar {
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
display: flex;
justify-content: center;
}
.sidebar a {
display: block;
color: black;
padding: 16px;
text-decoration: none;
}
.sidebar a.active {
background-color: #04AA6D;
color: white;
}
.sidebar a:hover:not(.active) {
background-color: #555;
color: white;
}
div.content {
margin-left: 200px;
padding: 1px 16px;
height: 1000px;
}
#media screen and (max-width: 700px) {
.sidebar {
width: 100%;
height: auto;
position: relative;
}
.sidebar a {float: left;}
div.content {margin-left: 0;}
}
#media screen and (max-width: 400px) {
.sidebar a {
text-align: center;
float: none;
}
}
<div class="sidebar">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
<div class="content">
<h2>Responsive Sidebar Example</h2>
<p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
<p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
<h3>Resize the browser window to see the effect.</h3>
</div>

Flexbox nav elements overflow on their own in the most weird manner

While testing responsiveness, I encountered the most weird problem. I have these codes for aligning the header as a flexbox and when I resize it over 900px width, the navbar gets out of the header and shows on top of the next element.
Like this:
This thing happens up until 1205px width and then it gets the way I expect it.
Like this:
The anomaly in the first image shouldn't happen because of the media query I have added for Desktop screens.
This is the main code for that part:
HTML:
<header>
<img class="logo" src="images/img-tea-cozy-logo.webp" alt="The Tea Cozy Logo">
<nav>
<ul class="bar">
<li>Mission</li>
<li>Featured Tea</li>
<li>Locations</li>
</ul>
</nav>
</header>
CSS:
/* Header */
header {
max-width: 100%;
height: 4.54em;
border-bottom: 1px solid seashell;
display: flex;
align-items: center;
flex-direction: column;
background-color: black;
}
header a img {
height: 2.18em;
margin-top: 0.45em;
}
nav ul li {
display: inline-flex;
text-decoration: underline;
padding: 0.13em 0.25rem 0 0.25rem;
}
nav ul li a {
color: seashell;
}
And this is the media query for Desktop:
/* Header for Desktop Screens */
#media only screen and (min-width: 900px) {
header {
height: 3.13em;
justify-content: space-between;
}
header a img {
height: 2.27em;
margin: 0.30em 0 0 0.45em;
}
nav ul li {
padding: 0;
margin: 0.45em 0.45em;
font-size: 1em;
}
}
The weirdest thing happened when I inserted "and" after the condition, like this:
#media only screen and (min-width: 900px) and {
...
}
Then it got fixed while it shouldn't have because that's just not the right syntax:
Can anyone help me understand what's happening here and how to fix the problem in the first image?
You forgot to define flex-direction: row for the media query:
/* Header */
header {
max-width: 100%;
height: 4.54em;
border-bottom: 1px solid seashell;
display: flex;
align-items: center;
flex-direction: column;
background-color: black;
}
header a img {
height: 2.18em;
margin-top: 0.45em;
}
nav ul li {
display: inline-flex;
text-decoration: underline;
padding: 0.13em 0.25rem 0 0.25rem;
}
nav ul li a {
color: seashell;
}
/* Header for Desktop Screens */
#media only screen and (min-width: 900px) {
header {
height: 3.13em;
justify-content: space-between;
flex-direction: row;
}
header a img {
height: 2.27em;
margin: 0.30em 0 0 0.45em;
}
nav ul li {
padding: 0;
margin: 0.45em 0.45em;
font-size: 1em;
}
}
<header>
<img class="logo" src="images/img-tea-cozy-logo.webp" alt="The Tea Cozy Logo">
<nav>
<ul class="bar">
<li>Mission</li>
<li>Featured Tea</li>
<li>Locations</li>
</ul>
</nav>
</header>

Background image not showing up in div

My background image won't show up in the div I want to display in. However, it shows up if I put it in other elements. For example, I put it in the body element and it showed up just fine, but it won't display in the div I want it to. Can you check my code and see what I'm doing wrong. I double checked my file path and also the file type, and it's all fine. So, I'm not being able to figure out what I'm doing wrong.
HTML:
<body>
<header>
<div class = "container container-flex">
<div class = "title">
<h1>PRODUCE</h1>
</div>
<nav>
<ul>
<li>Home</li>
<li>Groups</li>
<li>Profiles</li>
<li>Gallery</li>
</ul>
</nav>
</div>
</header>
<div class = "container container-flex">
<main role = "main">
<div class = "image">
</div>
</main>
</div>
</body>
CSS:
body{
margin: 0;
font-size: 1.125rem;
font-weight: 400;
}
.container{
width: 90%;
max-width: 900px;
margin: 0 auto;
}
.container-flex{
display: flex;
flex-direction: column;
justify-content: space-between;
}
header{
padding: 1em 0;
text-align: center;
}
#media (min-width: 675px){
.container-flex{
flex-direction: row;
}
}
nav ul{
list-style: none;
padding: 0;
display: flex;
justify-content: center;
}
nav li{
margin-left: 2em;
}
nav a{
text-decoration: none;
padding: .25em 0;
font-family: 'Ubuntu', sans-serif;
color: black;
font-size: 1.3rem;
color: #575252;
}
h1{
font-size: 2.75rem;
margin: 0.1em;
font-family: 'Merriweather', serif;
color: #FF344B;
}
#media (max-width: 675px){
nav ul{
flex-direction: column;
}
nav li{
margin: .5em 0;
}
}
.image{
background-image: url('logo.jpg');
}
This is because you are trying set background image in an empty element, which has 0 height.
Set specific width/height of the element.
body{
margin: 0;
font-size: 1.125rem;
font-weight: 400;
}
.container{
width: 90%;
max-width: 900px;
margin: 0 auto;
}
.container-flex{
display: flex;
flex-direction: column;
justify-content: space-between;
}
header{
padding: 1em 0;
text-align: center;
}
#media (min-width: 675px){
.container-flex{
flex-direction: row;
}
}
nav ul{
list-style: none;
padding: 0;
display: flex;
justify-content: center;
}
nav li{
margin-left: 2em;
}
nav a{
text-decoration: none;
padding: .25em 0;
font-family: 'Ubuntu', sans-serif;
color: black;
font-size: 1.3rem;
color: #575252;
}
h1{
font-size: 2.75rem;
margin: 0.1em;
font-family: 'Merriweather', serif;
color: #FF344B;
}
#media (max-width: 675px){
nav ul{
flex-direction: column;
}
nav li{
margin: .5em 0;
}
}
.image{
background-image: url('https://lh3.googleusercontent.com/taykG37GWDgY-FGkdogDvsHSJMUGRMvkuVRT6yR-5UNkKvGRKeRlpGYXlslocOcS0txlfUdGW59JGtzADknxbMqnh6AtVCv9EXyB8nHp80YsRNA0Yw=w1024-h683-n-l50-sg-rj');
height: 10em;
background-size: contain;
background-repeat: no-repeat;
}
<header>
<div class = "container container-flex">
<div class = "title">
<h1>PRODUCE</h1>
</div>
<nav>
<ul>
<li>Home</li>
<li>Groups</li>
<li>Profiles</li>
<li>Gallery</li>
</ul>
</nav>
</div>
</header>
<div class = "container container-flex">
<main role = "main">
<div class = "image">
</div>
</main>
</div>
Your div has no content. Either give some content or if that does not agree with your current design, give explicit height and width.
.image{
width: 200px;
height: 200px;
background-image : url('https://image.shutterstock.com/image-vector/sample-stamp-grunge-texture-vector-260nw-1389188336.jpg');
}
.image2{
background-image : url('https://image.shutterstock.com/image-vector/sample-stamp-grunge-texture-vector-260nw-1389188336.jpg');
}
<div class="image"></div>
<div class="image2"></div>
You need to add width, height, and background-size properties to the .image.
body {
margin: 0;
font-size: 1.125rem;
font-weight: 400;
min-height: 100vh;
}
.container {
width: 90%;
max-width: 900px;
margin: 0 auto;
}
.container-flex {
display: flex;
flex-direction: column;
justify-content: space-between;
}
header {
padding: 1em 0;
text-align: center;
}
#media (min-width: 675px) {
.container-flex {
flex-direction: row;
}
}
nav ul {
list-style: none;
padding: 0;
display: flex;
justify-content: center;
}
nav li {
margin-left: 2em;
}
nav a {
text-decoration: none;
padding: .25em 0;
font-family: 'Ubuntu', sans-serif;
color: black;
font-size: 1.3rem;
color: #575252;
}
h1 {
font-size: 2.75rem;
margin: 0.1em;
font-family: 'Merriweather', serif;
color: #FF344B;
}
#media (max-width: 675px) {
nav ul {
flex-direction: column;
}
nav li {
margin: .5em 0;
}
}
.container:nth-child(2){
height: 250px; /* set the height here */
}
main{
width: 100%;
height: 100%;
}
.image {
height: 30%;
width: 30%;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Disk_pack1.svg/1200px-Disk_pack1.svg.png');
background-size: cover;
}
<body>
<header>
<div class="container container-flex">
<div class="title">
<h1>PRODUCE</h1>
</div>
<nav>
<ul>
<li>Home</li>
<li>Groups</li>
<li>Profiles</li>
<li>Gallery</li>
</ul>
</nav>
</div>
</header>
<div class="container container-flex">
<main role="main">
<div class="image">
</div>
</main>
</div>
</body>
Edit: You need to set the height on the second .container class. Because of no height, the relative units didn't work. Also, you need to set the height and width on parent (main) element of .image.
in your code if you setting an image in div tag it is should have a height and weight. This is another way to add images in your application with responsive.
body{
margin: 0;
font-size: 1.125rem;
font-weight: 400;
}
.container{
width: 90%;
max-width: 900px;
margin: 0 auto;
}
.container-flex{
display: flex;
flex-direction: column;
justify-content: space-between;
}
header{
padding: 1em 0;
text-align: center;
}
#media (min-width: 675px){
.container-flex{
flex-direction: row;
}
}
nav ul{
list-style: none;
padding: 0;
display: flex;
justify-content: center;
}
nav li{
margin-left: 2em;
}
nav a{
text-decoration: none;
padding: .25em 0;
font-family: 'Ubuntu', sans-serif;
color: black;
font-size: 1.3rem;
color: #575252;
}
h1{
font-size: 2.75rem;
margin: 0.1em;
font-family: 'Merriweather', serif;
color: #FF344B;
}
#media (max-width: 675px){
nav ul{
flex-direction: column;
}
nav li{
margin: .5em 0;
}
}
.image img{
width: 100%;
height: auto;
}
<html lang="en">
<head>
<meta charset="utf-8">
<title>Page Title Goes Here</title>
<meta name="description" content="Description Goes Here">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<div class = "container container-flex">
<div class = "title">
<h1>PRODUCE</h1>
</div>
<nav>
<ul>
<li>Home</li>
<li>Groups</li>
<li>Profiles</li>
<li>Gallery</li>
</ul>
</nav>
</div>
</header>
<div class = "container container-flex">
<main role = "main">
<div class = "image">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1200px-Image_created_with_a_mobile_phone.png" alt="Smiley face">
</div>
</main>
</div>
<script src="js/scripts.js"></script>
</body>
</html>
As mentioned in answers by other users, your div element is empty and will sit on your page with effectively height: 0px; and width: 0px; properties.
To combat this, you should add a width property to your .image div in your stylesheets.
You can either then control the height of the image div with the height property, or alternatively you could use padding-bottom to give a more responsive twist to your styling, since padding-bottom: x% is a percentage of the element's width.
For example, adding:
.image {
background-size: cover;
background-position: center;
background-repeat: no-repeat;
padding-bottom: 100%;
width: 100%;
}
would create a square image that will stay responsively square as the page width, and therefore the width of the div.iamge element shrinks. The padding value can be played around with to get the right height for the element that you desire.
UPDATE
body{
margin: 0;
font-size: 1.125rem;
font-weight: 400;
}
.container{
background-color: red;
width: 90%;
max-width: 900px;
margin: 0 auto;
}
.container-flex{
display: flex;
flex-direction: column;
justify-content: space-between;
}
.image-wrapper {
width: 75%;
}
.image {
background-color: green;
height: 0;
padding-bottom: 100%;
}
.container {
color: white;
padding-bottom: 16px;
}
.center-image {
margin: 0 auto;
}
<body>
<header>
</header>
<div class="container">
container
<main role="main">
<div class="image-wrapper center-image">
<div class="image">
<span>image</span>
</div>
</div>
</main>
</div>
</body>

Why can't I center an inline flex-item?

I'm creating navigation bars using flexbox and ran into this particular issue when trying to create buttons out of anchor tags (i.e. link in the centre of a box).
I have the li set to display flex so I can centre the a, but as soon as I give the a some height, the a aligns to the top-left. Is there any way of getting the a centred?
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
font: sans-serif;
}
#top-nav {
border-bottom: 4px solid blue;
}
#center-section {
display: flex;
width: 1000px;
margin: auto;
}
#center-section>* {
flex: 1 1 0;
}
header section {
background: blue;
height: 90px;
width: 500px;
}
.navbar {
display: flex;
}
.navbar ul {
list-style-type: none;
padding: 0;
display: flex;
flex-direction: row;
width: 100%;
height: 100%;
align-self: center;
border: 1px solid red;
}
.navbar ul li {
display: flex;
flex: 1 1 100%;
}
.navbar ul li a {
background-color: yellow;
align-self: center;
width: 100%;
height: 100%;
}
.navbar ul li a:hover {
background-color: green;
}
<header>
<div id="top-nav">
<div id="center-section">
<section id="logo">
Home
</section>
<div class="navbar">
<ul>
<li>About</li>
<li>Store</li>
<li>Contact</li>
</ul>
</div>
</div>
</div>
</header>
When you set height:100% to a you make it stretch all the li and thus you can no more control it as all the alignment are equivalent in this case. What you need is to align the content inside the a. For this you can make it a flex container and use align-items and/or justify-content:
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
font: sans-serif;
}
#top-nav {
border-bottom: 4px solid blue;
}
#center-section {
display: flex;
width: 1000px;
margin: auto;
}
#center-section>* {
flex: 1 1 0;
}
header section {
background: blue;
height: 90px;
width: 500px;
}
.navbar {
display: flex;
}
.navbar ul {
list-style-type: none;
padding: 0;
display: flex;
flex-direction: row;
width: 100%;
height: 100%;
align-self: center;
border: 1px solid red;
}
.navbar ul li {
display: flex;
flex: 1 1 100%;
}
.navbar ul li a {
background-color: yellow;
display:inline-flex;
align-items: center;
justify-content:center;
width: 100%;
height: 100%;
}
.navbar ul li a:hover {
background-color: green;
}
<header>
<div id="top-nav">
<div id="center-section">
<section id="logo">
Home
</section>
<div class="navbar">
<ul>
<li>About</li>
<li>Store</li>
<li>Contact</li>
</ul>
</div>
</div>
</div>
</header>