I have written a simple piece of code that creates a bottom navigation bar for a responsive version of a website am working on. The problem am facing I guess is the width of the bar conflicting on my home page but when I navigate to other pages the width of the navigation bar is okay.
below is my code:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.mobile-nav {
display: none;
width: 100%;
height: 25%;
justify-content: center;
align-items: center;
position: fixed;
bottom: 0;
border-top: 1px solid rgb(230, 230, 230);
z-index: 99;
background: fixed;
background-color: aliceblue;
}
.mobile-nav-header {
width: 25%;
height: 20%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
flex-wrap: wrap;
}
.menu__text {
color: black;
border: none;
text-decoration: none;
}
.nav-text {
list-style-type: none;
}
.nav__label {
font-weight: 500;
}
#media only screen and (max-width: 768px) {
.mobile-nav {
display: flex !important;
height: 9% !important;
width: 100% !important;
left: 0;
right: 0;
z-index: 10;
}
}
<div className="mobile-nav">
<div className="mobile-nav-header">
<ul className="nav-text">
<li>
<IoHomeSharp size={25} className="mobile-nav-header-icon" />
</li>
<li>
<span className="nav_label">Home</span>
</li>
</ul>
</div>
<div className="mobile-nav-header">
<ul className="nav-text">
<li>
<IoChatbubblesSharp size={25} className="mobile-nav-header-icon" />
</li>
<li>
<span className="nav__label">Option B</span>
</li>
</ul>
</div>
<div className="mobile-nav-header">
<ul className="nav-text">
<li>
<RiMotorbikeFill className="mobile-nav-header-icon" size={25} />
</li>
<li>
<span className="nav__label">Option C</span>
</li>
</ul>
</div>
<div className="mobile-nav-header">
<ul className="nav-text">
<li>
<GrRestaurant className="mobile-nav-header-icon" size={25} fill="#0000FF" />
</li>
<li>
<span className="nav__label">Option D</span>
</li>
</ul>
</div>
</div>
My problem is when at the first page the navigation bar stretches past the width I gave it but when I move to another page it takes up the width I gave it.
Someone, please help me out on this.
The reason this is happening is because the content of your homepage is exceeding the usual width , this has happened before to me with images that got outside my containers .
Inspect your page with devtools (if you are using chrome) and point your cursor as right as you can to find the element that exceeds your container.
Related
I've been trying to write a menubar that has two groupings in the same row across the top of a webpage: on the left is the site name and in the center should be the menu options (a ul/li). So far, following similar issues, I've written the following, which appears on first glance to do exactly what I'm seeking.
HTML:
<div class="menubar">
SITE NAME
<ul class="ul">
<li>MENU 0</li>
<li>MENU 1</li>
<li>MENU 2</li>
<li>MENU 3</li>
</ul>
</div>
CSS:
.menubar {
width: 100%;
height: 50px;
line-height: 50px;
vertical-align: middle;
background-color: #fff;
border-bottom: 1px solid #f5f5f5;
position: fixed;
top: 0;
display: flex;
flex-flow: row nowrap;
}
.logo {
width: 33.33%;
float: left;
margin-left: 20px;
font-size: 24px;
}
.ul {
font-size: 18px;
list-style: none;
padding: 0;
margin: 0;
}
.ul li {
margin-left: 15px;
margin-right: 15px;
display: inline-block;
}
However, if you look carefully in the JSFiddle (more apparent when widening browser windows or shrinking the window down just before the items begin wrapping), the 'centered' ul/li is not actually centered—it's closer to the left side of the browser window than the right. How do I fix this so that the ul/li remains truly centered in the menubar (as if the site name doesn't exist) with the left-aligned site name, regardless of what the browser window's width is? (I'm assuming within non-wrapping reason, since I plan to adjust sizes and behavior for smaller devices.)
JSFiddle
You're using a lot of margins, width and stuff. Check out flex here and you can get the same thing, properly aligned using flex and directions.
<!-- NEW CODE -->
<nav>
<div class="logo">
<span>Your Company</span>
</div>
<ul class="nav-items">
<li class="nav-item"> Menu 1 </li>
<li class="nav-item"> Menu 2 </li>
<li class="nav-item"> Menu 3 </li>
<li class="nav-item"> Menu 4 </li>
</ul>
</nav>
<!-- OLD CODE -->
<nav>
<div class="logo">
<img src="https://placehold.it/200x200" alt="logo">
</div>
<div class="menu-items">
<div class="menu-item"> Menu 0 </div>
<div class="menu-item"> Menu 1 </div>
<div class="menu-item"> Menu 2 </div>
<div class="menu-item"> Menu 3 </div>
</div>
</nav>
and the css
// MORE PROPERTIES
nav {
align-items: center;
}
nav div.logo {
position: absolute;
}
// OLD-NEW CSS
nav {
display: flex;
border: 1px solid pink;
}
nav div.logo {
border: 1px solid green;
display: flex;
align-items: center;
}
nav div.logo span {
padding: 0 0.5rem;
}
ul.nav-items {
border: 1px solid red;
display: flex;
flex-direction: row;
list-style-type: none;
margin: 0 auto;
padding: 0;
}
ul.nav-items li {
margin: 0 0.25rem;
padding: 0.5rem;
border: 1px solid blue;
}
// OLD CSS
nav {
display: flex;
}
nav div.menu-items {
display: flex;
flex-direction: row;
margin: 0 auto;
}
nav div.menu-items div.menu-item {
margin-left: 0.25rem;
margin-right: 0.25rem;
}
Fiddle:
NEW: https://jsfiddle.net/vzgn0Lju/1/
OLD: https://jsfiddle.net/kp9nsmah/1/
I added some margins between menu options and you can tweak a little bit more but flex is way easier than using lists and lots of things. You could use spans instead of div.menu items, can remove the container for items and such. But the general idea is there.
I am trying to get the second navbar to align on the right, I have tried adding float:right and have tried adding a margin on the right to space out the words but does not work. Tried removing the nav tag and replacing it with section but it does not work.
/* NAV ON TOP*/
.pagetopTop{
position: fixed;
top: 0;
right: 0;
left: 0;
z-index: 500;
transition: ease-in-out 0.3s;
padding-bottom: 20px;
}
.topNav{
display: flex;
float: right;
}
.Topspan{
margin: var(--size-10);
}
/*navbar*/
.navbar .container{
top: 0;
right: 0;
left: 0;
z-index: 500;
transition: ease-in-out 0.3s;
display:flex
}
.navbar-brand{
height: var(--size-50);
margin-top: var(--size-40);
cursor: pointer;
}
.navbar-nav{
display: flex;
align-items: center;
}
.navbar-nav li{
align-items: center;
position: absolute;
}
body{
font-family: "Source Sans Pro", sans-serif;
font-weight: 400;
font-size: 0.875rem;
line-height: 1.6;
letter-spacing: 0;
color: var(--font-color-base);
margin: 0;
padding: 0;
}
*,
::after,
::before{
box-sizing: border-box;
}
ul{
margin: 0;
padding: 0;
list-style: none;
}
a{ text-decoration: none;}
p{ margin-top: 0;}
h2{
font-size: 1.75rem;
font-weight: 500;
line-height: 1.2;
}
img{
max-width: 100%;
vertical-align: middle;
border-style: none;
}
.container{
width: 100%;
padding-left: 40px;
padding-right: 40px;
margin-left: auto;
margin-right: auto;
}
<body>
<section id="pagetopTop" class="pagetopTop">
<div class="container">
<i class="fas fa-angle-left"></i>
<span class="Topspan">Main Demos</span>
<ul class="topNav">
<li class="nav-link">
<span class="Topspan">Demos</span>
<i class="fas fa-angle-down"></i>
</li>
<li class="nav-link">
<span class="Topspan">Docs</span>
<i class="fas fa-angle-down"></i>
</li>
</ul>
</div>
</section>
<nav id="Nav" class="navbar nav">
<div class="container">
<a href="" class="">
<img src="Week5saasappassets-210323-142515 (1)/Week-5-saas-app-assets/project_logo/logo.svg" alt="Company logo" class="navbar-brand">
</a>
<ul class="navbar-nav">
<li class="nav-link">
Home
</li>
<li class="nav-link">
Features
</li>
<li class="nav-link">
Learn
</li>
<li class="nav-link">
Price
</li>
<li class="nav-link">
Hire us
</li>
</ul>
</div>
</nav>
</body>
Maybe because of the margins or parent elements overlapping each other.
want to move the ul list to the other side beneath demos and space it out
There are a few issues here, in terms of HTML semantics, it doesn't make a lot of sense to use <section> for a nav menu. The <section> represents a generic standalone section of a document, which doesn't have a more specific semantic element to represent it. A page without content and only a navigation menu wouldn't make very much sense.
Use a more semantic element like <nav> which represents a section of a page whose purpose is to provide navigation links, either within the current document or to other documents. At worse, a division element <div> which doesn't have semantic meaning would make more sense than a <section> for a navigation menu. Also, you have display: flex and float: right on the .topNav container. Flexbox is the "newer" way to distribute space for items along a single axis so use one or the other, not both.
Instead of using a float to create layouts by floating elements to the left or the right, flexbox allows you to create layouts by aligning items to a single axis. The axis can be horizontal or vertical. It is best used for distributing space for items in the same axis
Your using a bunch of FontAwesome icons but didn't include the <script> tag in your example so none of them rendered. Next, the position: fixed on the <section> container paired with absolute positioning of the <li> elements, this is causing some of the menu items to overlap since your taking the content out of the "normal flow". Unless that was intended, I'm not sure what the exact "expected output" is since you didn't include a picture.
For positioning two elements at opposite sides in a row, just use a Flexbox layout to distribute "space between" the two nav menus in the <body> using justify-content: space-between. I added a nested <div> to the <body> that will act as the flex container containing the two <nav> menus. You can also vertically align flex items (e.g. the two nav menus) along the cross-axis with align-items: center. I will let you take it from here to style the nav menus on each side of the viewport how you would like.
/* NAV ON TOP*/
.flexbox {
display: flex;
justify-content: space-between;
align-items: center;
}
.pagetopTop{
/*position: fixed;
top: 0;
right: 0;
left: 0;*/
z-index: 500;
transition: ease-in-out 0.3s;
padding-bottom: 20px;
}
.topNav{
display: flex;
justify-content: space-between;
align-items: center;
}
.Topspan{
margin: var(--size-10);
}
/*navbar*/
.navbar .container{
/*top: 0;
right: 0;
left: 0;*/
z-index: 500;
transition: ease-in-out 0.3s;
display:flex;
align-items: center;
margin-top: 1rem;
}
.navbar-brand{
height: var(--size-50);
margin-top: var(--size-40);
cursor: pointer;
}
.navbar-nav{
display: flex;
flex-direction: column;
align-items: center;
}
.navbar-nav li{
align-items: center;
/*position: absolute;*/
}
body{
font-family: "Source Sans Pro", sans-serif;
font-weight: 400;
font-size: 0.875rem;
line-height: 1.6;
letter-spacing: 0;
color: var(--font-color-base);
margin: 0;
padding: 0;
}
*,
::after,
::before{
box-sizing: border-box;
}
ul{
margin: 0;
padding: 0;
list-style: none;
}
a{ text-decoration: none;}
p{ margin-top: 0;}
h2{
font-size: 1.75rem;
font-weight: 500;
line-height: 1.2;
}
img{
max-width: 100%;
vertical-align: middle;
border-style: none;
}
.container{
width: 100%;
padding-left: 40px;
padding-right: 40px;
margin-left: auto;
margin-right: auto;
}
<body>
<div class="flexbox">
<nav id="pagetopTop" class="pagetopTop">
<div class="container">
<i class="fas fa-angle-left"></i>
<span class="Topspan">Main Demos</span>
<ul class="topNav">
<li class="nav-link">
<span class="Topspan">Demos</span>
<i class="fas fa-angle-down"></i>
</li>
<li class="nav-link">
<span class="Topspan">Docs</span>
<i class="fas fa-angle-down"></i>
</li>
</ul>
</div>
</nav>
<nav id="Nav" class="navbar nav">
<div class="container">
<a href="" class="">
<img src="https://dynamic.brandcrowd.com/asset/logo/8cb54db0-7219-4bf6-bba3-1c9e94078516/logo?v=4" width="60" height="60" alt="Company logo" class="navbar-brand">
</a>
<ul class="navbar-nav">
<li class="nav-link">
Home
</li>
<li class="nav-link">
Features
</li>
<li class="nav-link">
Learn
</li>
<li class="nav-link">
Price
</li>
<li class="nav-link">
Hire us
</li>
</ul>
</div>
</nav>
</div>
<!-- Font Awesome Script -->
<script src="https://kit.fontawesome.com/e03d7ac5cf.js" crossorigin="anonymous"></script>
</body>
My gallery on mobile phones looks way worse in vertical position than in horizontal.
.order-gallery ul li {
display: inline;
}
.order-gallery ul {
display: flex;
justify-content: flex-start;
align-items: center;
list-style-type: none;
text-align: center;
padding: 0;
}
.order-gallery li {
flex: 1 33%;
padding: 0 20px;
}
.order-gallery li img {
max-width: 100%;
}
.panel-body {
background: rgba(21, 21, 21, 0.5);
padding: 15px;
<div class="panel-body order-gallery">
<ul>
<li>
<img pp-type="imagemodal" pp-imagemodal-src="https://placehold.it/600x400" src="https://placehold.it/200x100">
</li>
<li>
<img pp-type="imagemodal" pp-imagemodal-src="https://placehold.it/600x400" src="https://placehold.it/200x100">
</li>
<li>
<img pp-type="imagemodal" pp-imagemodal-src="https://placehold.it/600x400" src="https://placehold.it/200x100">
</li>
<li>
<img pp-type="imagemodal" pp-imagemodal-src="https://placehold.it/600x400" src="https://placehold.it/200x100">
</li>
</ul>
</div>
Horizontal view: https://imgur.com/a/1XrZ07K
Vertical view: https://imgur.com/a/YRD7siT
I am trying to build a simple navigation where some parts of the navigation items are hidden depending on the available space:
.container {
display: flex;
border: 1px solid red;
width: 100%;
max-width: 600px
}
ul {
margin: 0 30px 0px 0px;
display: flex;
list-style: none;
padding: 0;
flex-grow: 1;
}
li {
margin: 0;
padding: 0px 5px;
border: 1px solid green;
margin-left: 20px;
}
li:first-child{
padding-left: 0px;
margin-left: 0px;
}
li a {
line-height: 18px;
height: 18px;
overflow: hidden;
display: block;
}
.important {
color: red;
}
<div class=container>
<ul>
<li>
<a href=#>
<span class=important>my</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>my</span>
<span class=important>elem</span>
</a>
</li>
<li>
<a href=#>
<span class=important>my</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>my</span>
<span class="important">2nd</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>longerfirst</span>
<span class="important">elem</span>
</a>
</li>
</ul>
</div>
I successfully made some words disappear with using a fixed height and overflow:hidden on the a tag.
Now I'm looking for a solution to keep the red words (.important) visible and hide the others. Is there a way to do this?
If the word on the invisible line is longer than the visible one, the visible has too much whitespace, is there also a solution to this?
With Javascript it would be fairly easy, but I'm looking for a CSS only solution.
JSFiddle: http://jsfiddle.net/86qjexz3/
I am using the rem to create a mobile web project.Now I am facing a problem.
My code is below:
<head>
<style type="text/css">
body {
background-color: green;
}
#top {
/*height: .9rem;*/
}
#top::after,
#top::before {
/*content: '';
display: block;
clear: both;
visibility: hidden;*/
}
ul {
list-style: none;
padding-left: 0;
}
#data-item {
margin-top: 4rem;
}
#data-item .image-list {
width: 4.5rem;
display: flex;
flex-flow: row wrap;
background-color: yellow;
}
#data-item .image-list .image-list .div-image {
width: 1.4rem;
height: 1.4rem;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-color: #6641E2;
}
</style>
</head>
<body>
<script src="js/mui.min.js"></script>
<script type="text/javascript">
mui.init()
</script>
<section id="top">
<div id="addImage" style="height: .90rem; width: 1.60rem; float:right ; padding-top: .20rem; position: relative;background-color: red; z-index: 999;">
<span style="display: inline-block; margin-right: .20rem;">ADD</span>
</div>
</section>
<section id="data-item">
<ul class="mui-table-view">
<li class="mui-table-view-cell">
<div class="title"><img src="images/vip.png" alt="" /><span class="date">6月1日</span></div>
<p class="item-description"></p>
<ul class="image-list">
<li>
<div class="div-image"></div>
</li>
<li>
<div class="div-image"></div>
</li>
<li>
<div class="div-image"></div>
</li>
<li>
<div class="div-image"></div>
</li>
</ul>
</li>
<li class="mui-table-view-cell">Item 2</li>
<li class="mui-table-view-cell">Item 3</li>
</ul>
</section>
</body>
I look the result in the chrome console, I choose the mobile device iPhone5.
The screen is like this:
I check the element in the Elements tab.
I see that the #top section is 320*0.
My question is why the #addImage div has the margin-top too. I set the margin-top for the #data-item section?
Why does not the red div at the top-right corner?
add this style to the parent:
#top{
overflow: auto;
}