I'm trying to create simple page header with a logo on the left-hand side and links divided evenly on the right-hand side. Like this render from Adobe Xd presenting expected header design. To do this I have written this code (snippet is missing the image and is a bit too narrow for this margin):
header {
margin: 0 200px;
height: 75px;
border-bottom: #707070 solid 2px;
display: flex;
flex-direction: row;
}
header .logo {
flex-grow: 2;
flex-basis: 0;
padding: 5px 0;
}
header nav {
flex-grow: 1;
flex-basis: 0;
padding-bottom: 20px;
display: flex;
justify-content: space-between;
}
header nav a {
align-self: flex-end;
}
<header>
<div class="logo">
<a href="home.html">
<img alt="Logo Hufca" src="logo_hufca_zlote.svg" />
</a>
</div>
<nav>
Działalność
Jednostki
Dokumenty
Kontakt
</nav>
</header>
This solution works as expected in certain cases on Chrome and Opera. The layout is correct after the first load of page (screenshot from Chrome after the first page load). It also scales adequately to viewport resizing. Then, after clicking some links, <nav> become narrower and flex is not flexible anymore - doesn't change size when viewport does (screenshot from Chrome after the bug occures).
On Firefox and Edge this solution doesn't work at all - bug occurs all the time.
You can test this page here. Even more confusing is fact that the footer (made with flexbox) works propoerly on the same page. How can I repair my code to make header looks as expected?
You are working with a static margin. Its better to use a relative CSS rule to achieve the space between content and borders. Also you dont need to set flex-grow & flex-basis.
But the main issue was that the img { height: 100% } caused that the img element rise to its full height. You have to give the header .logo a max-height attribute.
Also its better to achieve the space between the nav elements with padding.
Here is a possible solution:
header {
margin: 0 auto;
max-width: 90%;
height: 75px;
border-bottom: #707070 solid 2px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
header .logo {
padding: 5px 0;
height: 100%;
max-height: 65px;
}
img {
height: 100%;
}
header nav {
padding-bottom: 20px;
display: flex;
justify-content: space-between;
}
header nav a {
align-self: flex-end;
padding-right: 10px;
}
header nav a:last-child {
padding-right: 0;
}
<header>
<div class="logo">
<a href="home.html">
<img alt="Logo Hufca" src="https://wip.733mdh.pl/user/themes/motyw-hufca/images/logo_hufca_zlote.svg">
</a>
</div>
<nav>
Działalność
Jednostki
Dokumenty
Kontakt
</nav>
</header>
header {
height: 75px;
border-bottom: #707070 solid 2px;
display: flex;
flex-direction: row;
justify-content: space-between;
}
header .logo {
flex-grow: 2;
flex-basis: auto;
padding: 5px 0;
}
header nav {
flex-grow: 1;
flex-basis: auto;
padding-bottom: 20px;
display: flex;
justify-content: space-between;
}
header nav a {
align-self: flex-end;
}
<header>
<div class="logo">
<a href="home.html">
<img alt="Logo Hufca" src="https://picsum.photos/200/50" />
</a>
</div>
<nav>
Działalność
Jednostki
Dokumenty
Kontakt
</nav>
</header>
Related
I have a HTML file and CSS file with code:
body {
border: 0px;
padding: 0px;
margin: 0px;
}
/* HEADER STYLE */
header .top-nav {
padding: 10px;
margin: 0;
position: fixed;
width: 100%;
background-color: aqua;
display: flex;
flex: 1;
align-items: center;
justify-content: space-between;
}
header .top-nav-right-block {
display: flex;
}
<header>
<nav class="top-nav">
Home
<div class="top-nav-right-block">
Portfolio
Contacts
</div>
</nav>
</header>
The problem is when I set a 10px padding of .top-nav I expect to have a 100% width navbar with 10px of padding from each side but what I get instead is just a content pushed out of screen from right side. Inspecting the code in a browser indicates that I have a position-right: -20px.
I understand that paddings applied from left side only and pushes content to the right on respective value, but I don't understand why.
Change the box-sizing on your nav bar to border-box;
See: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing
body {
border: 0px;
padding: 0px;
margin: 0px;
}
/* HEADER STYLE */
header .top-nav {
padding: 10px;
margin: 0;
position: fixed;
width: 100%;
background-color: aqua;
display: flex;
flex: 1;
align-items: center;
justify-content: space-between;
box-sizing: border-box;
}
header .top-nav-right-block {
display: flex;
}
<header>
<nav class="top-nav">
Home
<div class="top-nav-right-block">
Portfolio
Contacts
</div>
</nav>
</header>
Alternately, removing the width and fixed position would resolve the issue, but I'm assuming you want to keep the fixed positioning.
Try setting the box-sizing property to border-box for the .top-nav element, like this:
body {
border: 0px;
padding: 0px;
margin: 0px;
}
/* HEADER STYLE */
header .top-nav {
box-sizing: border-box;
padding: 10px;
margin: 0;
position: fixed;
width: 100%;
background-color: aqua;
display: flex;
flex: 1;
align-items: center;
justify-content: space-between;
}
header .top-nav-right-block {
display: flex;
}
<header>
<nav class="top-nav">
Home
<div class="top-nav-right-block">
Portfolio
Contacts
</div>
</nav>
</header>
box-sizing defaults to content-box if not specified which means that the padding and border are added to the width and height of the element, rather than being included within the specified width and height.
You can add to css:
* {
box-sizing: border-box;
}
https://codepen.io/rachkovartem/pen/ZEMbVJE
https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing
I'm trying to build a product landing page as a certificate project for freeCodeCamp.
I can't understand how to fix the icons to the left, while fixing the feature and price to the center. And I can't understand why my li items are overflowing over their container.
I tried all the overflow and wrap tags I know but I can't work it out.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-family: 'Montserrat', sans-serif;
}
#logo {
position: absolute;
width: 25%;
left: 0;
height: auto;
image-resolution: 100%;
}
header {
width: 100%;
display: flex;
justify-content: space-evenly;
}
.navspace {
justify-content: end;
position: relative;
right: -15%;
}
nav {
positio: relative;
}
ul {
display: flex;
list-style: none;
}
li {
margin: 40px;
}
.product {
position: absolute;
top: 15%;
}
#leads {
width: 100vw;
display: flex;
flex-direction: column;
text-align: center;
justify-content: center;
align-items: center;
}
.title {
padding: 2%;
}
#logo-green {
width: 5vw;
height: auto;
margin-right: 5vw;
}
#Features {
margin-left: 27%;
display: flex;
align-items: left;
justify-content: left;
text-align: left;
padding: 5%;
}
#Price {
display: flex;
align-items: center;
justify-content: left;
text-align: center;
padding: 3%;
margin-left: 27%;
}
.price {
display: flex;
flex-direction: column;
text-align: left;
}
.pricelist {
width: 100%;
max-height: 400px;
text-align: center;
position: absolute;
display: inline-flex;
justify-content: center;
}
.class {
width: 300px;
height: 300px;
border: 1px solid black;
margin: 20px;
}
.class>h1 {
width: 100%;
height: 10%;
background-color: blanchedalmond;
padding: 2%;
font-size: large;
}
#medium,
#pika,
#base {
display: flex;
flex-direction: column;
}
.class>h2 {
margin: 5% 0 5% 0;
}
.class>ul {
display: grid;
display: flex;
justify-content: center;
flex-direction: column;
gap: 2px;
}
.class>li {
position: relative;
}
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet'>
<!-- tabella nav -->
<header>
<div>
<img id="logo" src="img/LIGHTSPEED.png" alt="" />
</div>
<!-- bhr -->
<div class="navspace">
<nav id="nav-link">
<ul>
<li>Features</li>
<li>Price</li>
<li>Contacts</li>
</ul>
</nav>
</div>
</header>
<main class="product">
<!-- form -->
<section id="leads">
<h2 class="title">Most efficient way to light your life</h2>
<form action="">
<input class="email" type="email" required placeholder="Enter your email" columns="10">
<input class="submit" type="submit" value="Get Shocked">
</form>
</section>
<!-- features -->
<section>
<div id="Features">
<div id="green">
<img id="logo-green" src="img/294432.png" alt="">
</div>
<div class="feature">
<h2>Only from renovable energy</h2>
<p>Coming from water and earth termal energy</p>
</div>
</div>
</section>
<!-- price -->
<section>
<div id="Price">
<div id="cheap">
<img id="logo-green" src="img/low-price.png" alt="">
</div>
<div class="price">
<h2>Prices you have never seen</h2>
<p>With our funding system you might get some solar panels</p>
<p>and who knows... we might pay you your energy.</p>
</div>
</div>
</section>
<div class="pricelist">
<div class="class" id="base">
<h1>BASE LEVEL</h1>
<h2>49€</h2>
<ul>
<li>Standart power transmission</li>
<li>Change power output by your personal profile</li>
<li>Client Support 10am-20am</li>
</ul>
</div>
<div class="class" id="medium">
<h1>MEDIUM LEVEL</h1>
<h2>59€</h2>
</div>
<div class="class" id="pika">
<h1>PIKACHU LEVEL</h1>
<h2>149€</h2>
</div>
</div>
</main>
A few things right off the bat, i've added some comments to your CSS. There's a lot going on here, and I think a lot of your styling is just working against you.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-family: 'Montserrat', sans-serif;
}
#logo {
position: absolute;
width: 25%;
left: 0;
height: auto;
image-resolution: 100%;
}
header {
width: 100%;
display: flex;
justify-content: space-evenly;
}
.navspace {
justify-content: end;
position: relative;
right: -15%;
}
nav {
position: relative; /* <- mispelled positio[n] */
}
ul {
display: flex;
list-style: none;
}
li {
margin: 40px;
}
.product {
position: absolute; /* I hate position absolute. Hate it deeply. Basically refuse to use it. It never works the way I want it to. More on this in response below */
top: 15%;
}
#leads {
width: 100vw;
display: flex;
flex-direction: column;
text-align: center;
justify-content: center;
align-items: center;
}
.title {
padding: 2%;
}
#logo-green {
width: 5vw;
height: auto;
margin-right: 5vw;
}
#Features {
margin-left: 27%;
display: flex;
align-items: left;
justify-content: left;
text-align: left;
padding: 5%;
}
#Price { /* be careful, classes and Ids are case sensitive*/
display: flex;
align-items: center;
justify-content: left;
text-align: center;
padding: 3%;
margin-left: 27%;
}
.price {
display: flex;
flex-direction: column;
text-align: left;
}
.pricelist {
width: 100%;
max-height: 400px;
text-align: center;
position: absolute;
display: inline-flex; /* technically, unless this object has a sibling, making it inline-flex while it is position: absolute; won't really change anything... though it could in unexpected ways. More below*/
justify-content: center;
}
.class {
width: 300px;
height: 300px;
border: 1px solid black;
margin: 20px;
}
.class>h1 {
width: 100%;
height: 10%;
background-color: blanchedalmond;
padding: 2%;
font-size: large;
}
#medium,
#pika,
#base {
display: flex;
flex-direction: column;
}
.class>h2 {
margin: 5% 0 5% 0;
}
.class>ul {
display: grid;
display: flex;/* display grid is just being overridden by display flex here, so there's no point in keeping it */
justify-content: center;
flex-direction: column;
gap: 2px;
}
.class>li {
position: relative;
}
<link href='https://fonts.googleapis.com/css?family=Montserrat' rel='stylesheet'>
<!-- tabella nav -->
<header>
<div>
<img id="logo" src="img/LIGHTSPEED.png" alt="" />
</div>
<!-- bhr -->
<div class="navspace">
<nav id="nav-link">
<ul>
<li>Features</li>
<li>Price</li>
<li>Contacts</li>
</ul>
</nav>
</div>
</header>
<main class="product">
<!-- form -->
<section id="leads">
<h2 class="title">Most efficient way to light your life</h2>
<form action="">
<input class="email" type="email" required placeholder="Enter your email" columns="10">
<input class="submit" type="submit" value="Get Shocked">
</form>
</section>
<!-- features -->
<section>
<div id="Features">
<div id="green">
<img id="logo-green" src="img/294432.png" alt="">
</div>
<div class="feature">
<h2>Only from renovable energy</h2>
<p>Coming from water and earth termal energy</p>
</div>
</div>
</section>
<!-- price -->
<section>
<div id="Price">
<div id="cheap">
<img id="logo-green" src="img/low-price.png" alt="">
</div>
<div class="price">
<h2>Prices you have never seen</h2>
<p>With our funding system you might get some solar panels</p>
<p>and who knows... we might pay you your energy.</p>
</div>
</div>
</section>
<div class="pricelist">
<div class="class" id="base">
<h1>BASE LEVEL</h1>
<h2>49€</h2>
<ul>
<li>Standart power transmission</li>
<li>Change power output by your personal profile</li>
<li>Client Support 10am-20am</li>
</ul>
</div>
<div class="class" id="medium">
<h1>MEDIUM LEVEL</h1>
<h2>59€</h2>
</div>
<div class="class" id="pika">
<h1>PIKACHU LEVEL</h1>
<h2>149€</h2>
</div>
</div>
</main>
First off, while it is somewhat a personal choice and somewhat just the way most people use CSS these days: I hate position: absolute. It never acts the way you want it to, it's very difficult to get it to be responsive, and there are better ways to do it these days. The problem with absolute is that it simultaneously breaks an object out of the standard flow of the document, but it also gets rid of the object's ability to affect other objects in the document. This means that a div with position:absolute will no longer push down its sibling objects (things next to it), hold space within the parent object (the thing holding it will act like it has nothing inside it). While it's more complicated, using grid to break objects out of the normal document flow is more predictable.
Let's take a look at your code simply without position: absolute:
https://jsfiddle.net/slingtruchoice/sye5wbmu/
already, things aren't breaking outside of your boxes.
Your best bet to get things to align is to use use margin and display:block. Remember, inline items don't hold their own line in a document, they allow other inline items to butt up next to them in the same line, so they won't be affected by margin and padding in the same way. display:inline-block fixes some of that, but not everything.
i literally have this as a poster in my office. I've been doing this for ten years and still use it daily: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
if an item is overflowing out of the container, it's because the container isn't recognizing that it needs to be affected by the child's size. This either means that the parent has a fixed size that overrides the child's desire to make the parent bigger (like a pregnant lady wearing a corset, as my teacher always said... such a weird image).
It may also mean that the child's size isn't affecting things around it, such as a position:absolute or display:inline item that is acting weird. If you have display:flex and flex-wrap: nowrap on the parent, but the children are set to have a size larger than the parent object, then they will overflow.
I'm sure about what icons you are talking about, I would be happy to help you If can clarify the issue. Now about the why your li items are overflowing over their container.
In your CSS on the line number 94 and 103 you set max-height: 400px; and height: 300px; because of that your li items are overflowing.
One way you can fix this bye removing fix height and set the value height: auto; that will make the parent container automatically grow based on their child element, or you can give your ul element a class or ID and then set overflow-y: scroll; to that ID or class.
This is the code freecodecamp is using to design the middle section with the icon.
.grid {
display: flex;
}
#features .icon {
display: flex;
align-items: center;
justify-content: center;
height: 125px;
width: 20vw;
color: darkorange;
}
#features .desc {
display: flex;
flex-direction: column;
justify-content: center;
height: 125px;
width: 80vw;
padding: 5px;
}
<!-- Wrapper -->
<div class="grid">
<!-- Icon -->
<div class="icon">
<i class="fa fa-3x fa-fire"></i>
</div>
<!-- /Icon -->
<!-- Description -->
<div class="desc">
<h2>Premium Materials</h2>
<p> Our trombones use the shiniest brass which is sourced locally. This will increase the longevity of your purchase. </p>
</div>
<!-- /Description -->
</div>
<!-- /Wrapper -->
Now let me explain what is going on here. The div with the class name grid is the parent element for both icon and the desc So they set display of grid to display: flex; and give both child element fix width width: 20vw; and width: 80vw;
And I like to add one little note, That If you are using position: absolute; to any element then It's a good idea to set Its parent to position: relative; That would make working with position a lot easier.
I'm trying to create a navigation bar but can't get rid of the extra space to the left of the logo container. How can I remove this space?
I've tried setting the margin and padding to 0 but neither worked.
Here's an image of what i'm talking about. I added red borders to make it easier to understand where the containers are:
The output should have the logo all the way to the left of the menu container. There should be no extra space.
.center-container{
max-width: 960px;
margin: 0 auto;
margin-top: 1rem;
}
menu{
background-color: cadetblue;
display: flex;
justify-content: space-between;
}
.logo{
height: 1.5rem;
margin-right: 1rem;
}
.logo-container{
display: flex;
align-items: center;
}
<header>
<menu class="center-container">
<div class="logo-container">
<img class="logo" src="https://s3.amazonaws.com/codecademy-content/courses/freelance-1/unit-5/tortoiseshell-optics/resources/images/logo-white.png">
<span>Tortoiseshell Optics</span>
</div>
</menu>
</header>
<menu> elements are usually styled like <ul> elements by the browser which have a left padding. Remove it and you get rid of the space:
.center-container {
max-width: 960px;
margin: 0 auto;
margin-top: 1rem;
}
menu {
background-color: cadetblue;
display: flex;
justify-content: space-between;
padding-left: 0;
}
.logo {
height: 1.5rem;
margin-right: 1rem;
}
.logo-container {
display: flex;
align-items: center;
}
<header>
<menu class="center-container">
<div class="logo-container">
<img class="logo" src="https://s3.amazonaws.com/codecademy-content/courses/freelance-1/unit-5/tortoiseshell-optics/resources/images/logo-white.png">
<span>Tortoiseshell Optics</span>
</div>
</menu>
</header>
Also note that support for <menu> is quite limited.
.center-container {
max-width: 960px;
margin: 0 auto;
margin-top: 1rem;
padding: 0;
}
menu {
background-color: cadetblue;
display: flex;
justify-content: space-between;
}
.logo {
height: 1.5rem;
margin-right: 1rem;
}
.logo-container {
display: flex;
align-items: center;
}
<header>
<menu class="center-container">
<div class="logo-container">
<img src="https://s3.amazonaws.com/codecademy-content/courses/freelance-1/unit-5/tortoiseshell-optics/resources/images/logo-white.png" class=logo>
<span>Tortoiseshell Optics</span>
</div>
</menu>
</header>
I am writing a landing page using css/flexbox and I'm struggling to get some images to be equal size. The logo images I have are all different sizes and I want them to be equal size, clickable links. I was able to achieve this by creating a css table, but would really like to use flexbox, if possible.
Here is the code in question:
.how-to-buy {
/*rgba used to add opacity*/
background-color: rgba(255,255,255,0.4);
border: hidden;
border-radius: 25px;
/* make sure there is space between buttons and bottom of page */
margin-bottom: 30px;
padding: 10px;
max-width: 90%;
text-align: center;
}
.ebook-links img {
border-radius: 15px;
}
.ebook-links img:hover {
}
.how-to-buy{
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.ebook-links{
max-width: 80%;
display: flex;
justify-content: center;
align-items: center;
}
.logo1{ flex: 2.07;}
.logo2{ flex: 2.4;}
.logo3{ flex: 3;}
.logo4{ flex: 2.8;}
.logo5{ flex: 2.43;}
<div class="how-to-buy">
<p>This collection includes nineteen tales of faeries and magic at Midwinter. Available Now online:</p>
<div class="ebook-links">
<a href=#>
<img class="logo1" src="https://tinypandabird.wpengine.com/wp-content/uploads/2017/12/amazon-logo_black-300x145.png" /></a>
<a href=#>
<img class="logo4" src="https://tinypandabird.wpengine.com/wp-content/uploads/2017/12/ibooks-300x108.png" /></a>
</div>
</div>
I thought that setting the logo flex values equal to their aspect ratios would work, but it doesn't seem to...
How about you put max-height to the img tag, if you set only 'width' or only 'height' to an img tag the image does not lose the aspect ratio
.how-to-buy {
/*rgba used to add opacity*/
background-color: rgba(255,255,255,0.4);
border: hidden;
border-radius: 25px;
/* make sure there is space between buttons and bottom of page */
margin-bottom: 30px;
padding: 10px;
max-width: 90%;
text-align: center;
}
.ebook-links img {
border-radius: 15px;
max-height:50px;
}
.ebook-links img:hover {
}
.how-to-buy{
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.ebook-links{
max-width: 80%;
display: flex;
justify-content: center;
align-items: center;
}
.logo1{ flex: 2.07;}
.logo2{ flex: 2.4;}
.logo3{ flex: 3;}
.logo4{ flex: 2.8;}
.logo5{ flex: 2.43;}
<div class="how-to-buy">
<p>This collection includes nineteen tales of faeries and magic at Midwinter. Available Now online:</p>
<div class="ebook-links">
<a href=#>
<img class="logo1" src="https://tinypandabird.wpengine.com/wp-content/uploads/2017/12/amazon-logo_black-300x145.png" /></a>
<a href=#>
<img class="logo4" src="https://tinypandabird.wpengine.com/wp-content/uploads/2017/12/ibooks-300x108.png" /></a>
</div>
</div>
Hope this is the result you are looking for
Need to fit img into dynamically calculated height of li due to flex. Currently it takes all height, and pull height content. Image should just fit into dynamically calculated height of LI, e.g. 50px and fit into it.
No hardcoded height is needed. Page should be 100% in any device. header/footer - flex: 1, main - flex: 3.
body,
ul {
margin: 0;
padding: 0;
}
html,
body,
.root {
height: 100%;
}
.root {
display: flex;
flex-direction: column;
}
main {
flex: 3;
background: lightyellow;
}
ul {
display: flex;
flex-direction: column;
height: 100%;
}
ul li {
flex: 1;
background: aliceblue;
outline: 1px solid #000;
display: flex;
/* align-items: center; */
justify-content: space-between;
}
img {
/* object-fit: contain; */
}
header,
footer {
flex: 1;
}
header {
background: lightgray;
}
footer {
background: lightblue;
}
<div class="root">
<header>
HEAD
</header>
<main>
<ul>
<li>
<img src="http://via.placeholder.com/200x200" />
<span>text</span>
</li>
<li>
<img src="http://via.placeholder.com/200x200" />
<span>text</span>
</li>
<li>
<img src="http://via.placeholder.com/200x200" />
<span>text</span>
</li>
</ul>
</main>
<footer>
FOOTER
</footer>
</div>
https://jsfiddle.net/sefb3o95/18/
The main thing to make this work is to use Flexbox all the way.
Simply put, one need to nest flex parent/child instead of using height: 100%, to make use of Flexbox's own properties making its flex children stretch/fill their parents, and the reason is that height: 100% is not gonna work properly.
Additionally, having the img as a flex item will cause you some cross browser issues, so wrap it and give it a max-height: 100%, and it will size properly.
See my notes in the CSS. The min-height: 0 fix for Firefox is well explained here:
Why don't flex items shrink past content size?
Updated fiddle
Stack snippet
body,
ul {
margin: 0;
padding: 0;
}
html,
body,
.root {
height: 100%;
}
.root {
display: flex;
flex-direction: column;
}
main {
flex: 3;
background: lightyellow;
display: flex; /* added */
flex-direction: column; /* added */
min-height: 0; /* added, Firefox need this */
}
ul {
display: flex;
flex-direction: column;
min-height: 0; /* added, Firefox need this */
}
ul li {
flex: 1;
background: aliceblue;
outline: 1px solid #000;
display: flex;
/* align-items: center; */
justify-content: space-between;
min-height: 0; /* added, Firefox need this */
}
img {
max-height: 100%; /* added */
}
header,
footer {
flex: 1;
}
header {
background: lightgray;
}
footer {
background: lightblue;
}
<div class="root">
<header>
HEAD
</header>
<main>
<ul>
<li>
<span>
<img src="http://via.placeholder.com/200x200" />
</span>
<span>text</span>
</li>
<li>
<span>
<img src="http://via.placeholder.com/200x200" />
</span>
<span>text</span>
</li>
<li>
<span>
<img src="http://via.placeholder.com/200x200" />
</span>
<span>text</span>
</li>
</ul>
</main>
<footer>
FOOTER
</footer>
</div>