Vertical tabs with flex box - css

I would like to accomplish a vertical tabs layout, so on the left would be the tabs and on the right the tab contents. The challenge is that I have to adhere to a specific markup which makes things complicated.
Below is my last attempt, I cant make the tabs fill in the space left by the content:
ul{
list-style-type: none;
padding: 0;
margin: 0;
}
.tabs-container{
display: flex;
flex-direction: column;
max-width: 400px;
}
.tab{
width: 100px;
background-color: gray;
}
.tab-content{
display: none;
}
.tab-content.active{
display: block;
height: 100px;
order: -1;
align-self: flex-end;
}
<ul class='tabs-container'>
<li class='tab'>tab one</li>
<li class='tab-content active'>contents tab one</li>
<li class='tab'>tab two</li>
<li class='tab-content '>contents tab two</li>
<li class='tab'>tab three</li>
<li class='tab-content'>contents tab three</li>
</ul>
Is there a solution for this problem using css flex box?
Thanks

<div class="container">
<div class="obj"><div class="tab">Tab1</div><div class="tab-c">TabContent1</div></div>
<div class="obj"><div class="tab">Tab2</div><div class="tab-c">TabContent2</div></div>
</div>
<style>
.container {
display: flex;
flex-direction: column;
background-color: tomato;
}
.obj {
display: flex;
/*no need of specify the flex direction here it's row by default*/
background-color: orange;
margin: 10px;
}
.tab {
width: 50px;
background-color: yellow;
}
.tab-c {
width: 150px;
background-color: white;
}
</style>
This is probably not the most elegant way to do it but it's work for my needs

Related

Center-align horizontal inline-block list of page numbers

I am trying to center-align a block of page numbers at the bottom of this page. The HMTL and CSS looks like this:
.pt-cv-pagination-wrapper {
position: relative;
display: block;
text-align: center;
margin: 20px 0;
padding: 0;
}
.pt-cv-pagination {
position: static;
display: inline-block;
text-align: center;
margin: 20px 0;
padding: 0;
}
li {
position: relative;
display: inline;
text-align: center;
margin: 0;
padding: 0;
}
.pt-cv-pagination a {
position: relative;
display: block;
float: left;
padding: 6px 12px;
margin: 1em;
}
<div class="pt-cv-pagination-wrapper">
<ul class="pt-cv-pagination pt-cv-ajax pagination" data-totalpages="3" data-currentpage="1" data-sid="98f4b5c3fg" data-unid="">
<li class="cv-pageitem-prev">
<a title="Go to previous page">‹</a>
</li>
<li class="cv-pageitem-number">
<a title="Go to page 1">1</a>
</li>
<li class="cv-pageitem-number">
<a title="Go to page 2">2</a>
</li>
<li class="cv-pageitem-number active">
<a title="Current page is 3">3</a>
</li>
<li class="cv-pageitem-next active">
<a title="Go to next page">›</a>
</li>
</ul>
<div class="clear pt-cv-clear-pagination"></div>
</div>
If you look on a narrow screen (on the live site), it will be easier to see that the page numbers are very slightly off centre. I've read several articles on here that all make sense but seem to have no further effect on my outcome. It's driving me crazy trying figure out why it's not working. Any help appreciated!
Ah, the art of centering elements in CSS. Good thing we have flexbox to help us all out.
This should do the trick:
.pt-cv-pagination-wrapper .pt-cv-pagination.pagination {
display: flex;
justify-content: center;
}
The centering looks fine. If you want to have a border on the <li> then change the inline of the li in an inline-block.
.pt-cv-pagination li {
position: relative;
display: inline-block;
text-align: center;
margin: 0;
padding: 0;
}
Remove the float from the <a> to have the block on this anchor working the correct way.
.pt-cv-pagination a {
position: relative;
display: block;
padding: 6px 12px;
margin: 1em;
}

problem with creating dropdown within flexbox navigation bar, the dropdown content can't be displayed normally

I've been having trouble positioning and displaying dropdown menu below the navigation bar which is styled with flexbox, it appeas that the dropdown menu that overflows out of the flex container is hidden, even though it was given position: absolute and z-index. dropdown menu hidden vertically display please help me and take a look at my code.
<ul class="topNav" id="myTopNav" role="navigation">
<li>shopping bag</li>
<li>sign in</li>
<li class="all_categories">
<ul class="categories">
<li class="category-women">
women
<ul class="sub-category">
<li> New in!</li>
<li> Tops</li>
<li> Coats & Jackets</li>
</ul>
</li>
<li class="category-men">men</li>
<li class="category-home">home & gift</li>
</ul>
</li>
</ul>
The .topNav is the flex container is styled like this:
.topNav {
width: 100%;
padding-left: 2%;
padding-right: 2%;
padding-top: 0.6%;
padding-bottom: 0.6%;
border-bottom: #c9c8c7 0.02em solid;
position: sticky;
top: 0;
overflow: hidden;
display: flex;
flex-direction: row;
align-items: center;
}
The links inside navigation bar are displayed as block;
here's my code for the drop down menu:
.category-women{
border: 1px dotted red;
}
.sub-category {
display: none;
position: absolute;
background-color: green;
min-width: 100px;
z-index: 2;
border: 1px solid yellow;
}
.sub-category a {
font-size: 1em;
display: block;
padding: 0;
margin: 0 8px;
background-color: green;
}
.category-women:hover .sub-category {
display: flex;
flex-direction: column;
justify-content: space-between;
}

Spread elements evenly using Flexbox

I'm starting with Flexbox and I'm trying to center a row with three columns to the center of the document. I've tried the following code (Written in Sass) but as you can see in the screenshot the three elements are not perfectly centered (they don't have the same space between them):
HTML for the header:
<header>
<nav>
<ul>
<li>ELE 1</li>
<li>ELEMENT 2</li>
<li>ELEMENT 3</li>
</ul>
</nav>
</header>
SCSS for the header:
header {
height: 80px;
width: 100%;
background-color: $dark-grey;
}
nav {
height: inherit;
max-width: 800px;
margin: auto;
text-align: center;
display: flex;
align-items: center;
& ul {
width: 100%;
display: flex;
& li {
flex: 1 0 auto;
& a, a:visited {
color: $white-alpha;
}
& a.active {
color: $white;
}
}
}
}
I've recreated the problem in this CodePen, as you can see the real example. What I'm doing wrong? Thanks.

Problems with a header menu

I am trying to make a header menu work as I like. I have only minor experience with CSS.
I have only two main menu items, each with a dropdown. I want for it to look something like this when hover on first menu item:
And something like this when hovering on second menu item:
Note: I want the "Menu 1" and "Menu 2" to be positioned as they are in the images, including this: Dropdown for Menu 1 is to the right of Menu 1, dropdown of Menu 2 is to the left of Menu 2. I forgot to include "Logo" in the images, but it is centered in the middle.
I have a fiddle of what I have managed so far (check it out here).
I have two problems:
The width is not 100%. The blue background is 100%, but I tried for the two menu items to be 25% each and then there is a logo-div in the middle that should fill out the rest (with margin: auto), so it is 100% all together.
The active main menu item gets pushed down on hover
How to fix it to work as intended?
body {
margin: 0px;
}
.header {
background-color: royalblue;
width: 100%;
margin-bottom: 1em;
height: 3.15em;
color: yellow;
box-shadow: 0px 0px 5px black;
}
.nav h3 {
display: inline-block;
margin-left: 0.3em;
}
section {
display: inline-block;
vertical-align: text-top;
}
.nav:first-of-type h3 {
text-align: right;
}
.nav ul {
display: inline-block;
padding: 0;
top:36px;
list-style-type: none;
width: 15em;
margin-top: 4em;
display: none;
}
.nav:nth-of-type(2) ul {
text-align: right;
}
.nav:hover ul {
display: inline-block;
}
.width-25 {
width: 25%;
}
.fill-width {
margin: auto;
}
<div class="header">
<section class="nav">
<h3 class="width-25">Menu item 1</h3>
<ul>
<li>Dropdown 1 item 1</li>
<li>Dropdown 1/2</li>
<li>item 1 # 3</li>
<li>Dropdown 1 item 4 long</li>
<li>DD 1-5</li>
<li>Dropdown 1 i. 6</li>
</ul>
</section>
<span class="fill-width">Logo</span>
<section class="nav">
<ul>
<li>Dropdown 2 item 1</li>
<li>Dropdown 2/2</li>
<li>item 2 # 3</li>
<li>Dropdown 2 item 4 long</li>
<li>DD 2-5</li>
<li>Dropdown 2 i. 6</li>
</ul>
<h3 class="width-25">Menu item 2</h3>
</section>
</div>
I updated your fiddle with a way of how you could do this.
JSFiddle
It probably isn't the best or the cleanest way, but I did this in about 10 mins quickly. It should give you a base on how to go further.
The main problem was pretty much not giving your h3 seperate classes like so:
<h3 class="right">
I hope this helps you with your problem :).
Some few things:
Add position: relative to .nav.
Give position: absolute to .nav ul.
Now check this out:
body {
margin: 0px;
}
.header {
background-color: royalblue;
width: 100%;
margin-bottom: 1em;
height: 3.15em;
color: green;
box-shadow: 0px 0px 5px black;
}
.nav h3 {
display: inline-block;
margin-left: 0.3em;
}
section {
display: inline-block;
body {
margin: 0px;
}
.header {
background-color: royalblue;
width: 100%;
margin-bottom: 1em;
height: 3.15em;
color: green;
box-shadow: 0px 0px 5px black;
}
.nav h3 {
display: inline-block;
margin-left: 0.3em;
}
section {
display: inline-block;
}
.nav:first-of-type h3 {
text-align: right;
}
.nav ul {
display: inline-block;
padding: 0;
top:36px;
list-style-type: none;
width: 15em;
margin-top: 4em;
display: none;
}
.nav:nth-of-type(2) ul {
text-align: right;
}
.nav:hover ul {
display: inline-block;
}
.width-25 {
width: 25%;
}
.fill-width {
margin: auto;
}
}
.nav:first-of-type h3 {
text-align: right;
}
.nav ul {
display: inline-block;
padding: 0;
top: 36px;
list-style-type: none;
width: 15em;
margin-top: 4em;
display: none;
}
.nav:nth-of-type(2) ul {
text-align: right;
}
.nav:hover ul {
display: inline-block;
}
.width-25 {
width: 25%;
}
.fill-width {
margin: auto;
}
.nav {
position: relative;
}
.nav ul {
position: absolute;
}
<div class="header">
<section class="nav">
<h3 class="width-25">Toolbox</h3>
<ul>
<li>ECMAScript 2015 (ES6)</li>
<li>TypeScript/ES7</li>
<li>Observables</li>
<li>Building/Deploying</li>
<li>Components</li>
<li>View/Input Data</li>
</ul>
</section>
<span class="fill-width"></span>
<section class="nav">
<ul>
<li>Forms</li>
<li>Pipes</li>
<li>Services</li>
<li>Directives</li>
<li>Dependency Injection</li>
<li>Testing</li>
</ul>
<h3 class="width-25">Angular 2</h3>
</section>
</div>
<div>
<h1>Title</h1>
<p>Content</p>
</div>
You need to set the position to absolute on the <ul> element to fix the issue of active menu getting pushed on hover.
position: absolute;
Check out this updated jsFiddle.
Also, what do you mean by the width is not 100%?

Oneline horizontal list with middle item that fits its content width

I have a horizontal list of items, arranged with display:table-cell.
The middle item can vary in content length, and it should expand accordingly.
And obviously its siblings should shrink to make space to it.
Here is a sample of what I did until now:
http://codepen.io/Pictor13/pen/mJoyXw
But the middle item still overflows over the right sibling, when the content grows.
Is it possible to achieve what I want? Are display: table-cell and white-space: nowrap the right approach?
Note: the width, if specified, should always be generic and not fixed to a specific px/em.
Wellll....flexbox can do that.
.container {
background-color: yellow;
padding: 1px;
}
li {
text-align: center;
border: 2px solid #ccc;
background-color: green;
color: red;
list-style: none;
}
ul {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
padding: 0;
margin: 3px;
background-color: brown;
}
li {
-webkit-box-flex: 1;
-webkit-flex: 1 0 auto;
-ms-flex: 1 0 auto;
flex: 1 0 auto;
}
input {
width: 270px;
width: inherit;
}
form {
background-color: blue;
margin: 0;
padding: 0;
}
<div class="container">
<ul class="list">
<li>B</li>
<li>C</li>
<li class="middle">
<form>
<input type="number" value="123123418190238901390812903823" min="1" max="123012301232138192738798127398712983" />
<span>to page and with some long content</span>
<button>go</button>
</form>
</li>
<li>E</li>
<li>A</li>
</ul>
</div>
Codepen Demo

Resources