Flexbox children don't always expand as wide as they could/should - css

Two of the link buttons in my nav have long-ish names, and they're text-wrapping and becoming two lines tall even though (as far as I can tell) there's nothing stopping them or the nav from growing wider. Have I just missed something?
The code is quite long, so you'll probably be happier viewing the codepen than reading it here. It's so long because I'm afraid something I've overlooked in my global styling or my media query changes is to blame for this bug.
EDIT: This one was an easy answer. I misunderstood selector precedence. I thought media queries took precedence over everything (except inline). So I didn't realize the nav a rule in my media query was not overwriting the nav > section > a in my base CSS. A good lesson about always respecting CSS selector precedence.
HTML
<section>
<nav>
<a id="NSFW_deactivateFilter" onClick="worksafeOff()" href="#">Worksafe Mode: On</a>
<h2 onClick="showMenu()">Category Filters</h2>
<div></div>
<section id="nav_Orientation">
<a id="Horizontals" href="#">Horizontals</a>
<a id="Verticals" href="#">Verticals</a>
</section>
<div></div>
<section id="nav_Category">
<a id="Biltmore" href="#">Biltmore</a>
<a id="Commercial" href="#">Commercial / Product</a>
<a id="Fashion" href="#">Fashion & Glamour</a>
<a id="Invocation" href="#">Invocation</a>
<a id="NewOrleans" href="#">New Orleans</a>
</section>
</nav>
</section>
CSS
/** GLOBAL **/
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
color: inherit;
font-size: 16px;
font-style: inherit;
font-weight: 400;
line-height: 1em;
margin: 0 auto;
padding: 0;
position: relative;
text-align: center;
text-decoration: none;
z-index: 1;
}
a {
text-decoration: underline;
}
body {
background-color: #444;
color: #ccc;
font-family: Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif;
font-style: normal;
}
body > section {
background-color: #333;
width: 100%;
max-width: 1440px;
}
/** NAV **/
nav {
align-items: center;
background-color: #222;
display: flex;
flex-flow: column nowrap;
margin-bottom: 10px;
width: 95%;
max-width: 480px;
}
nav a, nav > h2 {
border: 1px solid #666;
margin: 0 0 5px 0;
padding: 5px;
}
nav a {
text-decoration: none;
}
nav > a, nav > h2 {
width: 100%;
}
nav > a {
background-color: #666;
}
nav > div {
display: none;
}
nav > section {
align-items: center;
display: none;
flex-flow: row wrap;
justify-content: space-between;
margin: 0;
width: 95%;
}
nav > section.nav {
display: flex;
}
nav > section > a {
display: inline-block;
width: 48%;
}
/** MEDIA QUERIES **/
#media (min-width: 1000px) {
nav {
display: inline-flex;
flex-flow: row nowrap;
width: auto;
max-width: none;
}
nav a {
margin: 0 5px;
width: auto;
}
nav > h2 {
display: none;
}
nav > div {
background-color: #666;
display: block;
height: 20px;
margin: 0 5px;
width:1px;
}
nav > section {
display: flex;
flex-flow: row nowrap;
width: auto;
}
}

Remove width: 48%; rule from nav > section > a.
See codepen.

Related

CSS Unable to use hover effect on list ID item

I have a Navbar that has a <li> and one of those list items have a ID of Navbar-login-btn. I am able to modify just that ID in the css, however whenever I try to use the :hover function on it; it does not work.
Is there something I am doing wrong here, because I am not sure. Any help would be nice, thank you!
Navbar.jsx:
import '../App.css';
import myAvatar from '../images/avataaars.png'
function Navbar() {
return(
<div className='Navbar-container'>
<img src={myAvatar} className='Nav-logo'/>
<ul>
<li>Home</li>
<li>About</li>
<li>Skills</li>
<li>Projects</li>
<li id='Navbar-login-btn'>Login</li>
</ul>
</div>
)
}
export default Navbar;
App.css:
.Navbar-container{
margin: 0;
width: 100%;
display: flex;
align-items: center;
position: fixed;
background-color: #fff;
box-sizing: border-box;
padding: 10px 40px 10px 40px;
}
.Nav-logo {
width: 75px;
height: auto;
}
#Navbar-login-btn {
border: 2px solid rgb(101, 201, 255);
border-radius: 20px;
color: rgb(101, 201, 255);
cursor: pointer;
}
#Navbar-login-btn:hover {
color: #fff;
background-color: rgb(101, 201, 255);
}
.Navbar-container ul{
width: 100%;
display: flex;
justify-content: space-evenly;
font-family: Russo One;
list-style: none;
gap: 40px;
}
.Navbar-container li{
padding: 10px;
}
First, I recommend you use a kebab-case for the CSS property.
example - navbar-container. lowercase with a hyphen.
By the way, you don't have to use an ID to grab the element you want.
you can use last-child method.
In your case:
If the Login is the last Element in the list. use this:
// for the navbar
.navbar-container{
margin: 0;
width: 100%;
display: flex;
align-items: center;
position: fixed;
background-color: #fff;
box-sizing: border-box;
padding: 10px 40px 10px 40px;
}
// for ul in the navbar
.navbar-container ul{
width: 100%;
display: flex;
justify-content: space-evenly;
font-family: Russo One;
list-style: none;
gap: 40px;
}
// for li ul in the navbar
.navbar-container ul li{
padding: 10px;
}
// for last li ul in the navbar
.navbar-container ul li:last-child {
border: 2px solid orange;
}
You have same background-color on #Navbar-login-btn & #Navbar-login-btn:hover
Try replacing different color on one of them
Example:
#Navbar-login-btn:hover {
color: #fff;
background-color: rgb(15 98 143);//place this new color
}
Playcode: Sample result here

Why are these navbar links overflowing on top of section outside the header?

I am trying to get the links on the navbar to take full width and NOT take any of the area of the background image placed on the following section when the page is displayed on a smaller viewport #media (max-width: 900px).
Why is the hamburger menu icon not displaying?
I am trying to get navmenu items stacked and then a background image to display in full on a smaller viewport.
header {
position: fixed;
z-index: 100;
width: 100%;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
background: var(--secondary-dark);
color: var(--main-white);
padding: 5px 30px;
height: 90px;
}
.logo {
font-size: var(--fs-600);
margin: 0.5rem;
text-decoration: none;
font-family: var(--ff-nav);
font-weight: var(--fw-400);
letter-spacing: 1px;
}
.navbar-items ul {
margin: 0;
padding: 0;
display: flex;
}
.navbar-items li a {
text-decoration: none;
color: var(--main-white);
padding: 1rem;
display: block;
}
.navbar-items li:hover a {
color: var(--blue-primary);
}
.toggle-button a {
position: absolute;
top: 0.75rem;
right: 1rem;
display: flex;
flex-direction: column;
justify-content: space-between;
width: 30px;
height: 21px;
}
.toggle-button a .bar {
height: 3px;
width: 100%;
background-color: var(--main-white);
border-radius: 10px;
}
#media (max-width: 900px) {
.toggle-button {
display: flex;
}
.navbar-items {
/* display: none; */
width: 100%;
background-color: var(--secondary-dark);
}
.navbar {
flex-direction: column;
align-items: flex-start;
}
.navbar-items ul {
flex-direction: column;
width: 100%;
}
.navbar-items li {
text-align: center;
}
.navbar-items li a {
padding: .5rem 1rem;
width: 100;
}
.navbar-items.active {
display: flex;
}
}
/* //////////////////////
Main
/////////////////////// */
.welcome {
position: relative;
outline: 2px solid red;
padding-top: 70px;
height: 700px;
background-image: url("/img/background.png");
background-position: center;
background-size: cover;
}
.welcome p {
position: absolute;
display: block;
bottom: 0;
right: 0;
margin: 20px 50px;
font-size: 3em;
color: var(--clr-section-background);
letter-spacing: 0.5px;
}
<header>
<nav class="navbar">
<div class="logo">
<span>Brand</span>
</div>
<div>
<a href="#">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
</div>
<div class="navbar-items">
<ul>
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>
Blog
</li>
<li>Newsletter</li>
</ul>
</div>
</nav>
</header>
<!--mission statement------------------------------------------>
<section class="welcome">
<div>
<p>#OnPoint</p>
</div>
</section>
Problem resolved, I just needed to add a z-index on navbar and remove padding on ul in order to have the hamburger menu displaying on full width on smaller viewports.

How to remove corner spacing in css?

I am learning CSS while writing CSS I got some issue.
I need to remove this space from my displaying web page.
.grouping:before,
.grouping:after {
content: " ";
display: table;
}
.grouping:after {
clear: both;
}
nav {
background-color: white;
position: fixed;
top: 0px;
right: 0px;
left: 0px;
}
nav figure {
position: absolute;
}
img {
width: 100px;
}
.primary-nav {
float: right;
}
.primary-nav>li {
display: block;
float: left;
}
.primary-nav>li>a {
float: left;
padding: 25px 0;
width: 100px;
border-left: 1px solid;
}
nav li a {
font-family: Arial, Helvetica, sans-serif;
color: black;
text-transform: uppercase;
font-size: 15px;
text-align: center;
}
nav li:first-child a {
border-left: none;
}
nav li a:focus,
nav li a:hover {
background: red;
background-color: red;
}
body {
background-color: grey;
}
<nav class="grouping">
<figure>
<img src="images/logo.png" alt="LOGO">
<ul class="primary-nav">
<li>Home</li>
<li>Home2</li>
<li>Home3</li>
<li>Home4</li>
<li>Home5</li>
</ul>
</figure>
</nav>
Using this code resultant output must be like this:
Expected Result Image
But getting this result with spaces at corner indicated as an arrow in next image:
Generated Result Image
1) There is some margin on ul element rendered by the browser defaults in general, to fix this do:
ul {
margin: 0
}
2) Also figure element has default margins
figure {
margin: 0
}
Try to inspect the output of the browser to see exactly how the browser defaults renders that margin to see it better.
https://jsfiddle.net/t8netg8j/4/
I would use display: inline-block for the image and the li elements, erase some of the floats and reset some of the margins to 0 as shown below:
(note: I reduced the width of the elements from 100 to 80 px to fit them into the narrow snippet window, but of course you don't have to do that for a wider viewport)
(note #2: i added empty HTML comments at line ends and beginnings for all li elements to avoid the whitespace resulting from the use of inline-blocks which would cause the hover background not to fill the full space between two vertical borders)
html, body {
margin: 0;
}
nav figure {
position: absolute;
width: 100%;
margin: 0;
}
img {
width: 80px;
display: inline-block;
}
.primary-nav {
float: right;
margin: 0;
}
.primary-nav>li {
display: inline-block;
}
.primary-nav>li>a {
float: left;
padding: 25px 0;
width: 80px;
border-left: 1px solid;
}
nav li a {
font-family: Arial, Helvetica, sans-serif;
color: black;
text-transform: uppercase;
font-size: 15px;
text-align: center;
}
nav li:first-child a {
border-left: none;
}
nav li a:focus,
nav li a:hover {
background: red;
background-color: red;
}
body {
background-color: grey;
}
<nav class="grouping">
<figure>
<img src="images/logo.png" alt="LOGO">
<ul class="primary-nav">
<li>Home</li><!--
--><li>Home2</li><!--
--><li>Home3</li><!--
--><li>Home4</li><!--
--><li>Home5</li><!--
--></ul>
</figure>
</nav>

Align long text in tab header

I'm trying to do a tab header, its a list of the titles, sometimes the titles are too long and has "-" in between. So to save space I add br to breakline.
1/The problem is the distance up & down between the "-" symbols is too big, is there any way I can fix that?
is this a correct way to do it by set br tag or should I set max-width for each li for the breakline?
This is my codepen
<div>
<ul>
<li>Real Estate, <br> Building House</li>
<li>Distribution <br>–<br> Manufacturing</li>
<li>Media <br>–<br> Broadway theater</li>
<li>Singer <br>–<br> dancer</li>
<li>Real Estate</li>
<li>Construction</li>
</ul>
div {width: 80%; margin: 0 auto;}
ul {
list-style: none;
/* display: table; */
width: 100%;
padding: 0;
margin: 0;
}
ul li {
position: relative;
font-size: 1.4rem;
/* display: table-cell; */
color: blue;
text-align: center;
display: inline-block;
margin-right: 20px;
}
Hope this helps you:
ul li {
font-size: 1.4rem;
color: red;
max-width: 120px;
padding: 10px;
vertical-align: middle;
text-align: center;
/* padding: 0 5px; */
display: inline-block;
margin-right: 14px;
}
Updated codepen

Margins work fine on chrome but not on firefox

I'm learning basic CSS currently, I'm trying to create a little mockup product page that uses a grid system with 4 columns. It looks and works fine on chrome, but on Firefox it breaks like such. Here's how it looks like on Firefox:
I can't seem to figure out how to fix it, I've tried changing the margins/padding from various locations but to no avail, it's still messing up on Firefox.
Here's my CSS:
html {
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
background: #232526; /* fallback for old browsers */
background: -webkit-linear-gradient(to left, #232526 , #414345); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to left, #232526 , #414345); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
* {
box-sizing: border-box;
}
.menu {
background-color: #17534b;
position: absolute;
width: 99.23%;
height: 8vh;
}
ul.nav {
width: 100%;
}
ul.nav li {
list-style: none outside none;
display: inline-block;
width: 19%;
text-align: center;
}
ul.nav li a {
display: block;
text-align: center;
padding: 1.5em;
color: #ffffff;
letter-spacing: 1px;
font-size: 15px;
text-transform: uppercase;
text-decoration: none;
}
ul.nav li a:hover {
background-color: #000000;
color: white;
}
ul.nav li.li-ultimate{
text-transform: uppercase;
color: #eef2ff;
font-size: 25px;
}
section {
width: 100%;
padding: 0 7%;
display: table;
margin: 0;
max-width: none;
height: 100vh;
}
.welcome {
height: 100vh;
background-color: #ffffff;
}
.content {
display: table-cell;
vertical-align: middle;
}
.welcome h1{
font-size: 3em;
display: block;
color: black;
font-weight: 300;
}
.welcome h2 {
font-size: 1.5em;
display: block;
color: black;
font-weight: 200;
}
.productsheader {
color: #ffffff;
}
.productsheader h1{
text-align: center;
color: #ffffff;
font-size: 3em;
}
.products{
height:100vh;
}
ul.rig {
padding: 5%;
list-style: none;
font-size: 0px;
margin-left: -2.5%;
}
ul.rig li {
display: inline-block;
padding: 10px;
margin: 0 0 2.5% 2.5%;
background: #ffffff;
border: 1px solid #ddd;
font-size: 16px;
vertical-align: top;
box-shadow: 0 0 5px #ddd;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
ul.rig li img {
max-width: 100%;
height: auto;
margin: 0 0 10px;
}
ul.rig li h3 {
margin: 0 0 5px;
text-align: center;
}
ul.rig li p {
font-size: .9em;
line-height: 1.5em;
color: #999;
text-align: center;
}
ul.rig.columns-4 li {
width: 22.5%;
}
Along with the relevant HTML:
<section class="products">
<div class="content">
<div class="productsheader">
<h1>
Our Proudly Presented Products
</h1>
</div>
<ul class="rig columns-4">
<li>
<img src="images/1.jpg"/>
<h3>Is-it-even-a-cup Cup - $100</h3>
<p>Life's true questions.</p>
</li>
<li>
<img src="images/2.jpg"/>
<h3>Flute Looking Cup - $150</h3>
<p>Looks like a flute.</p>
</li>
<li>
<img src="images/3.jpg"/>
<h3>Uterus Looking Cup - $350</h3>
<p>Modelled after a woman's uterus. Probably.</p>
</li>
<li>
<img src="images/4.jpg"/>
<h3>Artistic Cup - $1</h3>
<p>Artsy...ish...</p>
</li>
<li>
<img src="images/5.jpeg"/>
<h3>Ancient Cup - $500</h3>
<p>Or at least we think its old. Sure does look it.</p>
</li>
<li>
<img src="images/6.jpg"/>
<h3>Chinese Poetic Cup - $250</h3>
<p>The cup feels empty when you drink, but you're empty when you don't.</p>
</li>
<li>
<img src="images/7.jpg"/>
<h3>Dragonfly Cup - $25</h3>
<p>Pretty little dragonfly...On a cup.</p>
</li>
<li>
<img src="images/8.png"/>
<h3>FIFA World Cup Brazil - $50</h3>
<p>Technically a cup...</p>
</li>
</ul>
</div>
</section>
Are there any tips to also prevent this from happening in the future when I work on another css project?
ul.rig {
padding: 5%;
list-style: none;
font-size: 0px;
margin-left: -2.5%;
width: 960px; // choose your width like 960px
margin: auto; // auto work with display : block for centering
display: block; // block work with margin : auto for centering
}

Resources