How to get rid of unwanted space in a Chromium Flexbox? - html

I'm quite new to html 5 and css 3 but I wanted to make project page with flexbox and ran into a problem:
The site looks good in desktop mode but when it switches to the mobile view and "flex-flow: column" there is way too much space between the items as you can see in the pictures below.
Desktop version
Smartphones
The problem only occurs in chromium based browsers (Google Chrome, Vivaldi, Iron, Opera) Firefox, IE and Edge work well.
I'm not allowed to post images yet so I just put the links here.
Down there you can see my CSS code. I hope some can help me with this!
body {
display: flex;
flex-flow: row wrap;
margin: 0 auto;
background-color: f4f4f4;
font-family: Hind, Sans-serif;
justify-content: space-between;
}
header,
nav,
nav a,
article,
aside,
footer {
border-radius: 0px 0,5em 0em;
padding: 15px;
margin: 0.5em;
flex: 1 100%;
}
header {
background: var(--color-primary);
display: flex;
flex-flow: column;
align-items: center;
}
header * {
flex: 1 1 0%;
}
header nav {
flex: 1 1 100%;
}
nav,
nav ul,
nav li {
margin: 0;
padding: 0;
border: none;
}
nav ul {
display: flex;
flex-direction: column;
}
nav li {
list-style-type: none;
margin: 1.3em 0;
flex: 1 1 100%;
}
nav a {
display: inline-block;
width: 95%;
/*background: #fffbf0;*/
border-bottom: solid 0.1em;
border-color: var(--color-primary);
margin: 0;
font-weight: bold;
text-decoration: none;
text-align: center;
color: var(--color-secondary-2);
}
#media all and (min-width: 35em) {
header {
flex-flow: row wrap;
}
nav ul {
flex-direction: row;
}
nav li {
margin: 0 0.5em;
flex: 1 1 0%;
}
Now for the essential HTML part:
<header>
<nav>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
<li>item6</li>
<li>item7</li>
</ul>
</nav>
</header>

It will work if you remove align-items: center; from the header rule
header {
background: var(--color-primary);
display: flex;
flex-flow: column;
/* align-items: center; commented out */
}
and change to flex: 1 1 0%; instead of flex: 1 1 100%; in your nav li rule
nav li {
list-style-type: none;
margin: 1.3em 0;
flex: 1 1 0%; /* change basis to 0% */
}
You also need to close your media query properly by adding its missing end bracket }

Related

CSS border bottom on Navigation bar

I have a navigation bar and I added a red line on the bottom when hovering any item of the list, but I want to move that red line under the header (something like "Services"), any idea how to achieve this?
I added an small sample in codepen so you can easily check the HTML and CSS code
header {
background-color: lightblue;
padding-top: 1rem;
position: sticky;
top: 0;
display: flex;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
}
header li:hover {
height: 100%;
border-bottom: 2px solid red;
}
<header>
<a href="/">
<p>Whatever logo</p>
</a>
<nav>
<ul>
<li>About us</li>
<li>Services</li>
<li>Pricing</li>
<li>Blog</li>
</ul>
</nav>
CONTACT
</header>
Link to check the code
You can fix the header height and also fix the height of navbar items.
Also, you had one issue where on hover li elements are moving. You can also fix that with always adding border with transparent color to the element, so the overall height of the element won't change on hover state.
Here is the fixed CSS
header {
background-color: lightblue;
position: sticky;
display: flex;
height: 60px;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
height: 60px;
}
header li {
display: flex;
align-items: center;
border-bottom: 2px solid transparent;
height: 60px;
}
header li:hover {
border-bottom: 2px solid red;
}
https://codepen.io/swarajgk/pen/JjZewPo?editors=1100
I think just giving height to all list elements the same as the header will work.
Like this:-
header {
background-color: lightblue;
padding-top: 1rem;
height: 3rem;
position: sticky;
top: 0;
display: flex;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
height : 100%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
}
header li{
height: inherit;
}
header li:hover {
border-bottom: 2px solid red;
}
<body>
<header>
<a href="/"
><p>Whatever logo</p></a>
<nav>
<ul>
<li>About us</li>
<li>Services</li>
<li>Pricing</li>
<li>Blog</li>
</ul>
</nav>
CONTACT
</header>
</body>
Hope this solves the issue.
header {
background-color: lightblue;
padding-top: 1rem;
height: 3rem;
position: sticky;
top: 0;
display: flex;
align-items: center;
justify-content: space-around;
}
header nav {
min-width: 50%;
height : 100%;
}
header nav ul {
margin: 0;
height: 100%;
list-style: none;
padding-left: 0;
display: flex;
align-items: center;
justify-content: space-between;
}
header li{
height: inherit;
}
header li:hover {
border-bottom: 2px solid red;
}
I'd suggest the following approach, with explanatory comments in the CSS:
/* removing default padding and margin from all
elements, and forcing the browser to use the
same sizing algorithm - border-box - to calculate
element sizes, including the padding and border
widths in the declared size: */
*, ::before, ::after {
box-sizing: border-box;
padding: 0;
margin: 0;
}
/* setting common properties for the two element
groups: */
header,
header nav ul {
/* using display: flex layout: */
display: flex;
/* forcing the flex-items within the flex parent
to take the full height of that parent: */
align-items: stretch;
}
header {
background-color: lightblue;
block-size: 3em;
position: sticky;
justify-content: space-around;
}
/* using :is() to combine the two selectors
header a,
header li
into one selector: */
header :is(a, li) {
/* using grid layout: */
display: grid;
/* positioning the - including text - content
at the center of the element: */
place-items: center;
}
header nav {
min-width: 50%;
}
header nav ul {
/* the <ul> isn't a flex-item so we have to specify
that we want it to take all available space on
the block-axis (equivalent to 'height' in left-to-right
languages such as English): */
block-size: 100%;
list-style: none;
justify-content: space-between;
}
header li {
/* to prevent the jumping content: */
border-bottom: 2px solid transparent;
}
header li:hover {
/* to style the color of the bottom border: */
border-bottom-color: red;
}
<header>
<a href="/">
<p>Whatever logo</p>
</a>
<nav>
<ul>
<li>About us</li>
<li>Services</li>
<li>Pricing</li>
<li>Blog</li>
</ul>
</nav>
CONTACT
</header>
JS Fiddle demo.
References:
align-items.
display.
justify-content.
place-items.
Bibliography:
"Aligning items in a flex container," MDN.
"Basic concepts of flexbox," MDN.
"Box alignment in grid layout," MDN.

I cannot apply display: none to ul

I have an ul element (one of three on the page) that I don't want to be shown on mobile devices. I'm trying to apply display: none; to it but it doesn't work.
The code isn't mine, I just used a template from the internet, so go easy on me, plz
Html part
<section id="banner" class="major">
<div class="inner">
<header class="major">
<h1>This the title</h1>
</header>
<div class="content">
<p> and some text here </p>
<ul class="actions">
<li>A button</li>
</ul>
</div>
</div>
</section>
What I have in CSS
ul.actions {
display: -moz-flex;
display: -webkit-flex;
display: -ms-flex;
display: flex;
cursor: default;
list-style: none;
margin-left: -1em;
padding-left: 0;
}
ul.actions li {
padding: 0 0 0 1em;
vertical-align: middle;
}
ul.actions.special {
-moz-justify-content: center;
-webkit-justify-content: center;
-ms-justify-content: center;
justify-content: center;
width: 100%;
margin-left: 0;
}
ul.actions.special li:first-child {
padding-left: 0;
}
ul.actions.stacked {
-moz-flex-direction: column;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
margin-left: 0;
}
ul.actions.stacked li {
padding: 1.3em 0 0 0;
}
ul.actions.stacked li:first-child {
padding-top: 0;
}
ul.actions.fit {
width: calc(100% + 1em);
}
ul.actions.fit li {
-moz-flex-grow: 1;
-webkit-flex-grow: 1;
-ms-flex-grow: 1;
flex-grow: 1;
-moz-flex-shrink: 1;
-webkit-flex-shrink: 1;
-ms-flex-shrink: 1;
flex-shrink: 1;
width: 100%;
}
ul.actions.fit li > * {
width: 100%;
}
ul.actions.fit.stacked {
width: 100%;
}
ul.actions are used for different buttons throughout the page: one that I showed you in the html part and two other buttons to fill out a form.
So, I'm adding this bit of code but it just won't work
#media screen and (max-width: 480px) {
#banner .actions {
display: none;
}
}
You have used this code in wrong CSS file.
Please use right approch:
Your mentioned URL have this CSS
https://html5up.net/uploads/demos/forty/assets/css/main.css
The media query #media screen and (max-width: 480px) is mentioned in this css. Please go there
Please use this bit of code in this media query
#banner .actions {
display: none;
}
The code seems to work fine, I am not sure if you opened the page on the mentioned browsers before doing some code editing. In that case, clearing the caches could help.
That code works fine.
If this is not work fine on you site It seems like any other declaration overriding this declarations.
When an important rule is used on a style declaration, this declaration overrides any other declarations.
Please use CSS rule with !important and check after Clear your cache.
#media screen and (max-width: 480px) {
#banner .actions {
display: none !important;
}
}

flexbox mobile first menu issue

I have tried to emulate the excellent flexbox tutorials by Wes Bos. I wanted to convert one specific tutorial he has on responsive flexbox menu. But I wanted my menu to be done with mobile first so I did my media queries with min-width.
But I am not able to make it work properly on the default mobile layout. In the menu created by Wes, the li items are stacked upon each other and the social icons at the bottom have flex:1 1 25%. But my social icons are also stacked.
On the other breakpoints my layout follows the one that Wes created.
I have set up a codepen for my code.
.flex-nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
.flex-nav .social {
flex: 1 1 25%;
}
#media all and (min-width:500px) {
.flex-nav li {
flex: 1 1 50%;
}
.flex-nav ul {
flex-wrap: wrap;
flex-direction: row;
}
.flex-nav ul {
border: 1px solid black;
}
}
#media all and (min-width:800px) {
.flex-nav li {
flex: 3;
}
.flex-nav .social {
flex: 1;
}
}
This is your default code (no media queries applied):
.flex-nav ul {
display: flex;
flex-direction: column;
}
.flex-nav .social {
flex: 1 1 25%;
}
Yes, you've given each social media icon a flex-basis: 25%.
BUT, your container is flex-direction: column.
So the flex rule applied to your social media icons works vertically, not horizontally.
Consider this method instead:
.flex-nav ul {
display: flex;
flex-wrap: wrap;
}
li {
flex: 0 0 100%; /* sized to fit one item per row */
}
.flex-nav .social {
flex: 0 0 25%; /* sized to fit four items per row */
}
revised demo
I've created a container for the social links so it's more easy (at least for me) structure the menu.
SEE IN CODEPEN
Here the html:
<div class="wrapper">
<nav class="flex-nav">
<ul>
<li>item01</li>
<li>item02</li>
<li>item03</li>
<li>item04</li>
<li>item05</li>
<li>item06</li>
<div class="social-container">
<li class="social"><i class="fa fa-gift"></i></li>
<li class="social"><i class=" fa fa-glass"></i></li>
<li class="social"><i class=" fa fa-calendar"></i></li>
<li class="social"><i class=" fa fa-cutlery"></i></li>
</div>
</ul>
</nav>
</div>
CSS:
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
font-family: sans-serif;
margin: 0;
}
a {
color: white;
font-weight: 100;
letter-spacing: 2px;
text-decoration: none;
background: rgba(0, 0, 0, 0.2);
padding: 20px 5px;
display: inline-block;
width: 100%;
text-align: center;
transition: all 0.5s;
}
a:hover {
background: rgba(0, 0, 0, 0.3);
}
.wrapper {
max-width: 1000px;
margin: 0 auto;
padding: 50px;
}
.flex-nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100%; /* ADDED */
}
.flex-nav .social {
flex: 1 1 25%;
}
.social-container { //just make it flex container
display: flex;
width: 100%;
}
#media all and (min-width:500px) {
.flex-nav li {
flex: 1 1 50%;
}
.flex-nav ul {
flex-wrap: wrap;
flex-direction: row;
}
.flex-nav ul {
border: 1px solid black;
}
}
#media all and (min-width:800px) {
.flex-nav li {
flex: 1;
}
.flex-nav .social {
/*flex: 1;*/
}
.social-container {
flex: 2; /* set the value as many as you want */
}
.flex-nav ul { //change the direction
flex-direction: row;
flex-wrap: no-wrap;
}
}

why is the text in the h1 wrapping?

I'm building a simple website and cant figure out my the h1 title is wrapping, the nav doesnt touch it and nothing seems to be interfering with it, causing it to wrap. I have declared white-space: nowrap; but its still wrapping, does anyone know what is causing this and how I can fix it?
https://jsfiddle.net/xsqk3xk4/1/
#media screen and (min-width: 400px){
header{
height: 120px;
display: flex;
justify-content: space-between;
}
header h1{
margin: 0 0 0 8%;
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
}
header nav{
display: block;
align-self: flex-end;
}
nav ul{
display: flex;
justify-content: flex-end;
margin: 0 8% 0 0;
}
nav li{
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
}
footer nav{
display: none;
}
}
Working fine for me in the fiddle. Anyways if you remove the margin of the header h1 then it appears properly.
header h1{
margin:0;
...
}

Changing position of navbar at mobile view

I was wondering if there was a way to change the position of a navigation bar in mobile view by only using CSS3. For example instead of having the navigation bar underneath the top container in mobile view, I would like to have it fixed above the top container. I am using a wordpress theme, Thanks in advanced.
The website is www.capcar.com.au
You may try "transform" property with media rule.
Add css :
#media only screen and (max-width: 767px) {
.navigation_menu {
background: none repeat scroll 0 0 #ffffff;
position: absolute;
top: 0;
width: 100%;
}
.header_section {
background-color: #ffffff;
color: #99ca00;
padding-top: 51px;
width: 100%;
}
}
Flexbox can solve this issue pretty easily. Just add display: flex and flex-direction to #wrapper. Then, inside of a media query, add order: 0 to .header_section and order: -1 to .navigation_menu. This will switch those two around, but leave everything else in the order it was before.
You should be able to just add this to your css:
#wrapper {
display: flex;
flex-direction: column;
}
#media only screen and (max-width:640px) {
.header_section {
order: 0;
}
.navigation_menu {
order: -1;
}
}
var el = document.getElementById("toggle");
el.addEventListener("click", function(){
var menu = document.getElementById('menu');
menu.classList.toggle("show")
}, false);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#wrapper {
display: flex;
flex-direction: column;
}
header,
nav ul {
display: flex;
max-width: 800px;
width: 100%;
margin: 0 auto;
justify-content: space-between;
align-items: flex-end;
padding: 1rem 2rem;
}
header {
margin-bottom: 1rem;
}
#logo {
width: 40%;
}
.awards {
display: flex;
align-items: flex-end;
width: 40%
}
#award1 {
width: 25%
}
#award2 {
width: 75%;
}
nav {
background: linear-gradient(to bottom, #e3f5ab 0%, #99ca00 100%);
display: flex;
flex-direction: column;
min-height: 3rem;
}
#toggle {
display: none;
align-self: flex-end;
background: transparent;
border: none;
width: 2rem;
height: 2rem;
margin: .5rem 1rem;
outline: none;
cursor: pointer;
}
nav ul {
list-style: none;
padding: 0 2rem;
}
li {
text-transform: uppercase;
font-family: 'Open Sans', sans-serif;
width: 20%;
text-align: center;
}
li a {
display: block;
height: 3rem;
line-height: 3rem;
width: 100%;
text-decoration: none;
color: #02006d;
}
li:hover {
background-color: #ffffff;
}
.caret {
font-size: .6rem;
}
#media screen and (max-width:700px) {
#logo {
width: 70%;
display: block;
margin: 0 auto;
}
#award1 {
display: none;
}
#award2 {
display: none;
}
header {
order: 0;
display: block;
}
nav {
order: -1;
}
#toggle {
display: block;
}
#toggle img {
width: 100%;
}
nav ul {
display: none;
}
nav ul.show {
display: flex;
flex-direction: column;
align-items: center;
}
nav ul li {
width: 100vw;
}
}
<div id="wrapper">
<header>
<img id="logo" src="http://www.capcar.com.au/wp/wp-content/uploads/2015/05/LOGO-FOR-WEB23.png" />
<div class="awards">
<img id="award1" src="http://www.capcar.com.au/wp/wp-content/uploads/2015/05/HIA_Logo1.png" alt="" />
<img id="award2" src="http://www.capcar.com.au/wp/wp-content/uploads/2015/05/MBA_Logo.png" alt="" />
</div>
</header>
<nav>
<button id="toggle"><img src="http://www.tax-consulting-group-san-diego.com/images/taxes-menu-icon.png" alt="" /></button>
<ul id="menu">
<li>Home</li>
<li>About Us</li>
<li>Projects <span class="caret">▼</span></li>
<li>Testimonials</li>
<li>Contact Us</li>
</ul>
</nav>
</div>
A simple clean demo, and a demo with your site (codepen doesn't like your site, so the header looks weird, but I can't get all the css to load properly—hence the simple demo).
For more info on flexbox here's a snippet from css-tricks and the support tables.
Hope this helps!
Try using the flex property to build your menus. I have great success with this feature and it allows for a number of simple order methods via CSS and media queries. Try CSS-Tricks.com as a reference