I have this situation with Previous/Next Project buttons where I could either have both previous and next project buttons showing, or just one depending on what page the user is on. (First project does not have prev button, last project does not have next button). Same code for all situations.
I used flexbox and justify-content: space-between; to space them normally, and then margin-left: 10px; on the prev button for mobile and it was perfect.
For 1 button case however I added margin-left/right to shove them to the left/right side of page. This overrides margin: auto.
So you see my problem now is with the 2 button situation on mobile. When these buttons collide obviously it looks terrible, I need margin in between but I have margin: auto already on them.
I have added my current state and all 3 situations below:
.project-controls {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.project-controls a {
width: 100%;
max-width: 200px;
padding: 10px 5px;
border: 1px solid #2c2c2c;
font-weight: 600;
text-align: center;
text-decoration: none;
text-transform: uppercase;
color: #2c2c2c;
transition: all 0.3s ease;
}
.project-controls .prev-proj {
margin-right: auto;
}
.project-controls .next-proj {
margin-left: auto;
}
<h1>Two buttons</h1>
<section class="project-controls">
<a class="prev-proj" href="#">Previous Project</a>
<a class="next-proj" href="#">Next Project</a>
</section>
<h1>One button prev</h1>
<section class="project-controls">
<a class="prev-proj" href="#">Previous Project</a>
</section>
<h1>One button next</h1>
<section class="project-controls">
<a class="next-proj" href="#">Next Project</a>
</section>
How can I keep my existing structure and still get that space in between on mobile? My best guess is having some sort of invisible spacer div that I could display with a media query, but just wondering if anyone has a quicker/cleaner solution I'm not thinking of?
Thanks
No need for margin auto on both. You can set a fixed margin-right for the previous element. You also don't need to use justify-content.
.project-controls {
display: flex;
flex-direction: row;
/* no need this too
justify-content: space-between;
*/
}
.project-controls a {
width: 100%;
max-width: 200px;
padding: 10px 5px;
border: 1px solid #2c2c2c;
font-weight: 600;
text-align: center;
text-decoration: none;
text-transform: uppercase;
color: #2c2c2c;
transition: all 0.3s ease;
}
.project-controls .prev-proj {
margin-right: 20px; /* Changed this */
}
.project-controls .next-proj {
margin-left: auto;
}
<h1>Two buttons</h1>
<section class="project-controls">
<a class="prev-proj" href="#">Previous Project</a>
<a class="next-proj" href="#">Next Project</a>
</section>
<h1>One button prev</h1>
<section class="project-controls">
<a class="prev-proj" href="#">Previous Project</a>
</section>
<h1>One button next</h1>
<section class="project-controls">
<a class="next-proj" href="#">Next Project</a>
</section>
I also use previous and next buttons on my web pages.
I also use flexbox and justify-content: space-between or an auto margin, depending on the site (I've switched it up just for variety; ultimately they both have the same effect).
I encountered the same problem as you with having only one button on the first and last pages.
My solution was to add a hidden button on the first and last pages. This keeps the layout behavior consistent on all pages, without a lot of extra CSS (just one or two lines for the .hidden class).
Then on smaller screens, to avoid the "collision" issue altogether, a media query kicks in with flex-direction: column.
.hidden {
visibility: hidden;
width: 0;
}
nav {
display: flex;
justify-content: space-between;
border: 1px dashed blue;
}
#media ( max-width: 500px ) {
nav { flex-direction: column; align-items: center; }
}
<h3>first page</h3>
<nav>
next
</nav>
<h3>middle page</h3>
<nav>
previous
next
</nav>
<h3>last page</h3>
<nav>
previous
</nav>
jsFiddle demo
Related
Hobbyist who really sucks at css.
I have the following three divs:
The problem is, when I click on the middle one, the box grows, and so do the other two boxes:
How do I make boxes start off and stay the same size even after click. The reason the box is growing is do to adding the "arrow-icon"
Code looks like this:
HTML
<section class='modes-flex__options'>
<div class='options'>
<h2 class='options__title'>Options</h2>
<div class='options__item-container'id='1v1' onClick="selectedGameOption(this.id)">
<h3 class='options__item'>Player vs AI (1 v 1) </h3>
<div class='arrow-icon__div'>
<i></i>
</div>
</div>
<div class='options__item-container' id='1v1-tourny' onClick="selectedGameOption(this.id)">
<h3 class='options__item'>Player vs AI (Tournament)</h3>
<div class='arrow-icon__div'>
<i></i>
</div>
</div>
<div class='options__item-container' id='ai-v-ai-tourny' onClick="selectedGameOption(this.id)">
<h3 class='options__item' >AI vs AI (Tournament)</h3>
<div class='arrow-icon__div'>
<i></i>
</div>
</div>
</div>
</section>
CSS
.modes-flex{
display: flex;
margin-top: 3rem;
&__options{
flex:1;
display: flex;
justify-content: end;
}
&__description{
flex:1;
display: flex;
flex-direction: column;
}
}
.options{
margin-right: 5rem;
&__title{
font-size: 1.2rem;
padding-bottom:2rem;
}
&__item{
flex: 1;
padding-right: 5rem;
}
}
.description{
&__title{
font-size: 1.2rem;
padding-bottom:2rem;
}
}
.options__item-container {
padding: 1.5rem 1rem 1.5rem 1rem;
margin-bottom: 2rem;
box-shadow: 0 0 10px lightgrey;
border: 1px solid lightgrey;
display: flex;
align-items: center;
&:hover{
cursor: pointer;
}
}
.arrow-icon__div{
text-align: right;
}
.active-option{
background-color: $dark-navy;
color: white;
}
Tried to set min and max width and was still growing , just want them to stay even width after adding the icon.
You said it: The reason the box is growing is do to adding the "arrow-icon".
In my experience, in these situations, I always added to the default size of the boxes so that when another element (i.e. arrow-icon) is added, it doesn't change the size. (Because there is enough space in the box for arrow-icon to be added). With doing so, all the boxes remain the same through any actions.
I'm trying to make menu on top bar but I want to make a little bit of space between each element. How can I make some space between each menu item?
.TopMenu {
display: flex;
justify-content: center;
align-items: center;
width: 50%;
height: 40px
}
<div class="TopMenu">
<a class="active" href="#home">Inicio</a>
Tecnologias Que Trabajamos
Labs
Contacto
Legal
</div>
Since you are using a flexbox container you can use the gap attribute on your flex element as follows:
.TopMenu {
gap: 10px;
}
You should also add some styling to the a links. Adding :not(:first-of-type) makes sure this will only happen from the second item and onward
.TopMenu a:not(:first-of-type) {
padding-left: 8px;
}
Just switch justify-content to space-between
.TopMenu{
display: flex;
align-items: center;
width: 50%;
height: 40px;
justify-content: space-between;
}
<html>
<body>
<div class="TopMenu">
<a class="active" href="#home">Inicio</a>
Tecnologias Que Trabajamos
Labs
Contacto
Legal
</div>
</body>
</html>
See it in jsfiddle
I have a problem that I've been working on all day, unfortunately without success.
In Bootstrap I want a sticky footer that consists of only one row with several menu items which turns into one row for every item on screens with less then 1000px.
One Row Footer
More Row Footer
Unfortunately, I always fail because the footer is responsive and stays at the bottom of the page. I would be very happy if anyone. could send the appropriate HTML and CSS code.
Here's my code:
<style>
.footer {
position: fixed;
height: 100px;
bottom: 0;
width: 100%;
}
.footer-purple {
background-color: green;
}
.footer-content {
padding: 30px 50px 50px;
flex-direction: row;
max-width: 100%;
font-size: 14px;
display: flex;
}
.footer-navigation {
margin-left: 10px;
flex-direction: row;
display: inline;
}
.footer-link {
color: #fff;
}
</style>
<footer class="footer">
<div class="footer-content footer-purple">
<nav class="footer-navigation">
<a class="footer-link">
Test
</a>
<a class="footer-link">
Test 2
</a>
</nav>
</div>
</footer>
It looks like that: https://prnt.sc/sj02x6
I'm trying to use flexbox (justify-content: space-between;) to push the Motorola logo to the left and the red block (nav-bar) to the right. It actually works pretty well as long as there is no anchor tag involved. However, I need anchor tags so that visitors can actually click on each item of the nav-bar and get to the respective section of the website.
How can I make justify-content work without removing the anchor tags?
HTML
<div id="header">
<img id="header-img" src="https://upload.wikimedia.org/wikipedia/commons/2/22/Motorola_Logo_White.png" alt="This is Motorolas Logo">
<div id="nav-bar">
<div id="nav1" class="nav-link"><a href="#prices">Prices</div>
<div id="nav2" class="nav-link"><a href="#prices">Specs</div>
<div id="nav3" class="nav-link"><a href="#prices">Reviews</div>
</div>
</div>
CSS
#header {
background-color: gray;
position: fixed;
justify-content: space-between;
z-index: 1;
display: flex;
width: 100%;
height: 4rem;
}
#header-img {
background-color: orange;
height: 4rem;
width: 25%;
display: flex;
align-items: center;
justify-content: center;
}
#nav-bar {
background-color: red;
width: 25%;
height: 4rem;
display: flex;
justify-content: space-around;
align-items: center;
}
Here you can see it on Codepen
In cases where you have a flex container with left-aligned content - except one or more items you want to right-align, there is a shortcut using the margin property.
If you add the following rule to your existing styles:
#nav-bar {
margin-left: auto; /* Pushes the element right inside a flex container */
}
It should work as you want. You could even remove the justify-content: space-between; rule from your #header selector.
EDIT: I did manage to figure it out in flex! But the links still aren't quite taking up the entire top row:
enter image description here
there's still some extra space to the right and left. Is there any way to force the flex to use all the available space?
I'd like the nav bar to automatically adjust width based on how many links there are.
.top-container {
text-align: center;
display: flex;
}
.top-content-box {
width: 90%;
height: 30%;
margin: 30px auto;
background: #fff;
box-shadow: 0px 0px 10px #000;
justify-content: center;
display: flex;
flex-direction: column;
}
.news-and-twitter {
display: flex;
flex-direction: row;
flex-grow: 1;
}
.twitter-min {
width: 300px;
}
.news-box {
text-align: left;
vertical-align: middle;
padding: 30px;
flex-grow: 2;
}
.nav-bar {
text-align: center;
flex-grow: 1;
}
ul.link-list {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
display: inline-block;
}
<div class="top-container">
<div class="top-content-box">
<div class="nav-bar">
<ul class="link-list">
<li class="link-list"><a class="nav-list" href="default.asp">home</a></li>
<li class="link-list"><a class="nav-list" href="news.asp">about</a></li>
<li class="link-list"><a class="nav-list" href="contact.asp">projects</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">portfolio</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">commissions</a></li>
<li class="link-list"><a class="nav-list" href="about.asp">patreon</a></li>
</ul>
</div>
<div class="news-and-twitter">
<div class="news-box">
<h1>current project</h1>
Arena Circus ch 4 | Inserting voices into The Pretenders Guild
<h1>Current Events</h1>
<ul>
<li>The Pretenders Guild voice update is coming 7/15/19!
</li>
<li>Check the 1 year anniversary special offer here!</li>
</ul>
</div>
<div class="twitter-min">
<a class="twitter-timeline" data-width="300" data-height="450" data-theme="light" data-link-color="#343584" href="https://twitter.com/CapMinyan?ref_src=twsrc%5Etfw">Tweets by CapMinyan</a>
<script async src="https://platform.twitter.com/widgets.js"
charset="utf-8"></script>
</div>
</div>
</div>
</div>
screenshot
You have to make the parent element of the list (ul.link-list) a flex and make the children (li.link-list) have flex-grow of 1
.top-container{
text-align:center;
}
.top-content-box {
width: 90%;
height:30%;
margin:30px auto;
background: #fff;
box-shadow: 0px 0px 10px #000;
}
.news-and-twitter{
display:flex;
flex-direction:row;
flex-grow:1;
}
.twitter-min{
width:300px;
}
.news-box{
text-align:left;
vertical-align: middle;
padding:30px;
flex-grow:2;
}
Some may say "use flex box" but I really prefer not to do such methods!
So my suggestion would be position the nav bar left:50%;
And then give it a margin-left: -100px then change -100px to half of the nav's width, so lets say it has a width of 526px you put it to -263px.
And it may change it's width depending on the device's screen size, so I'd add some Javascript to change that when the page loads, so basically check what the width of the nav is then divide it by 2 and put a "-" in front of it then a "px" after it...
I understand if that sounds too complicated for such a simple task but I like using JavaScript to do my, things
Also many would say this is dumb to do, because the less Javascript the better but I strongly disagree, but that's my opinion... Other than the fact that a lot of Javascript will make the page slower, and a few other things...
Alternatively try doing margin: 0 auto could fix the problem, worth a shot!
Hope this helps!