Flexbox align-self is not working in my layout - css

Im trying to center #view-ctrls-cntnr horizontally inside of .menubar. When I use align-self: center it doesnt appear to do anything. What am I doing wrong, is there a better approach? JSFiddle.
HTML
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<section class="analysis">
<div class="menubar">
<div class="dropdown" id="file-btn">
Dropdown <span class="caret"></span>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
<li role="separator" class="divider"></li>
<li>One more separated link</li>
</ul>
</div>
<div id="view-ctrls-cntnr">
<div class="btn-group" role="group">
<button class="btn btn-default" ng-class="{'active-view': active_views[0]}" ng-click="toggleView(0)">R-Theta</button>
<button class="btn btn-default" ng-class="{'active-view': active_views[1]}" ng-click="toggleView(1)">Cartesian</button>
<button class="btn btn-default" ng-class="{'active-view': active_views[2]}" ng-click="toggleView(2)">Longitudinal</button>
<button class="btn btn-default" ng-class="{'active-view': active_views[3]}" ng-click="">Console</button>
</div>
</div>
</div>
<div id="views-cntnr">
<div id="r1" class="view-row">
<div id="v1" class="view">V1</div>
<div id="v2" class="view">V2</div>
<div id="v3" class="view">V3</div>
</div>
<div id="r2" class="view-row">
<div id="v4" class="view">V4</div>
</div>
</div>
<div id="frame-ctrl-cntnr">
<div id="frame-num" class="frame-ctrl"># X</div>
<div id="frame-range-cntnr" class="frame-ctrl">
<input type="range">
</div>
</div>
</section>
CSS
html,
body {
height: 100%;
}
.analysis {
display: flex;
flex-direction: column;
height: 100%;
}
/* MENUBAR */
.menubar {
padding: 4px 0 4px;
background-color: #eee;
border: hsl(0, 0%, 75%) solid 1px;
border-right: none;
border-left: none;
display: flex;
}
#view-ctrls-cntnr {
align-self: center;
}
#file-btn a {
color: black;
text-decoration:none
}
/* menubar */
/* VIEWS */
#views-cntnr {
display: flex;
flex-direction: column;
flex-grow: 1;
}
/* ROWS */
/* ROW 1 */
#r1 {
display: flex;
flex-grow: 4;
}
#r1 .view {
flex-grow: 1;
border: black 1px solid;
border-top: none;
border-right: none;
}
#r1 .view:last-child {
border-right: black 1px solid;
}
/* row 1 */
/* ROW 2 */
#r2 .view {
border: black 1px solid;
border-top: none;
flex-grow: 1;
}
#r2 {
display: flex;
flex-grow: 1;
}
/* row 2 */
/* rows */
/* views */
/* FRAME CTRL */
#frame-ctrl-cntnr {
display: flex;
}
.frame-ctrl {
border: black 1px solid;
border-top: none;
border-right: none;
}
.frame-ctrl:last-child {
border-right: black 1px solid;
}
#frame-num {
width: 50px;
}
#frame-range-cntnr {
flex-grow: 1;
padding: 4px;
}
/* frame ctrl */

You can use nested flexbox to center the inner element instead.
jsFiddle
#view-ctrls-cntnr {
flex: 1;
display: flex;
justify-content: center;
}
Or you could use the margin tricks:
jsFiddle
#view-ctrls-cntnr {
margin-left: auto;
margin-right: auto;
}
Note, it's #view-ctrls-cntnr not .view-ctrls-cntnr.
Edit: to center it in the entire viewport width, you can set the sibling element to absolute position: #file-btn {position: absolute;} This may cause overlapping in small viewport width.
jsFiddle
Other than that, you can give the left sibling a fixed width, i.e. 80px and add a pseudo :after element on the container with same width set. So that there will be equal space on the left and right.
.menubar:after {
content: "";
width: 80px;
}
#file-btn {
width: 80px;
}
jsFiddle

Related

How to align one flex child as flex-start and other in center?

I have a sidebar that is set to flex with direction column. I am trying to get my menu ul to be vertically centered, and my .logo-container to be on the top of the page.
Is there any way to get one child to flex-start and another one centered?
Code:
<aside class="side-bar">
<nav class="navigation">
<div class="logo-container">
<a href="index.html" class="link">
<img src="http://unsplash.it/30/30" class="logoimg" alt="">
<h6 class="logoname">My<span class="lastname">Name</span></h6>
</a>
</div>
<ul class="nav-list">
<li class="item">Menuitem1</li>
<li class="item">Menuitem2</li>
<li class="item">Menuitem3</li>
<li class="item">Menuitem4</li>
</ul>
</nav>
</aside>
CSS:
html {
box-sizing: border-box;
}
* {
margin: 0;
padding: 0;
}
.side-bar {
width: 35%;
height: 100vh;
background-color: blue;
}
.navigation {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
}
.logoname {
display: inline-block;
}
* {
color: black;
}
ul {
list-style: none;
}
Codepen
Many thanks!
What you can do is to create an empty/invisible element as a third flex item inside the flex parent (in my example below it's the divwith class xxx) and apply justify-content: space-between to the flex parent (instead of center).
Depending on your actual code and content you should make sure that that additional element has the same height as the nav element (30px in your and my example). And again, depending on the situation you might want to add visibility: hidden; to the additional element (xxx) to make it invisible but still have its height included in the flex position calculations:
html {
box-sizing: border-box;
}
* {
margin: 0;
padding: 0;
}
.side-bar {
width: 35%;
height: 100vh;
background-color: blue;
}
.navigation {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
height: 100%;
}
.logoname {
display: inline-block;
}
* {
color: black;
}
ul {
list-style: none;
}
.xxx {
height: 30px;
visibility: hidden;
}
<aside class="side-bar">
<nav class="navigation">
<div class="logo-container">
<a href="index.html" class="link">
<img src="http://unsplash.it/30/30" class="logoimg" alt="">
<h6 class="logoname">My<span class="lastname">Name</span></h6>
</a>
</div>
<ul class="nav-list">
<li class="item">Menuitem1</li>
<li class="item">Menuitem2</li>
<li class="item">Menuitem3</li>
<li class="item">Menuitem4</li>
</ul>
<div class="xxx"></div>
</nav>
</aside>
You can try this approach.
html {
box-sizing: border-box;
}
* {
margin: 0;
padding: 0;
}
.side-bar {
width: 35%;
height: 100vh;
background-color: blue;
}
.navigation {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
}
* {
color: black;
}
ul {
list-style: none;
}
.logo-container {
display:grid;
justify-content:space-around;
margin:0 auto;
padding-top: 20px;
}
.logo-container img {
text-align:center;
padding:5px;
}
<aside class="side-bar">
<div class="logo-container">
<a href="index.html" class="link">
<img src="http://unsplash.it/30/30" class="logoimg" alt="">
<h6 class="logoname">My<span class="lastname">Name</span></h6>
</a>
</div>
<nav class="navigation">
<ul class="nav-list">
<li class="item">Menuitem1</li>
<li class="item">Menuitem2</li>
<li class="item">Menuitem3</li>
<li class="item">Menuitem4</li>
</ul>
</nav>
</aside>
All you have to do is to have the logo and ul in separate divs within the parent div that has the column direction styling, apply flex-shrink:0 to the div containing the logo and flex-grow: 1 to the other div.
That will allow the logo to be at the top and the other div to take the rest of the space - then you can apply flex styling in the navigation -container to center the ul within that div.
UPDATE - the OP wanted the ul centered into the height of the viewport - as noted in the comments this is as simple as offsetting the position of the ul in the bottom div by half the height of the top div - so in this case - moving it up by 20px) because the top div is 40px in height. This allows centering of the ul into the viewport height without resorting to adding empty divs just to get the alignment.
html {
box-sizing: border-box;
color: white;
}
* {
margin: 0;
padding: 0;
}
.side-bar {
width: 35%;
height: 100vh;
background-color: blue;
padding: 8px;
}
.navigation {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
align-items: center;
}
.logo-container {
flex-shrink:0
}
.logoname {
display: inline-block;
padding : 8px;
color: lime;
}
.navigation-container {
flex-grow:1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
ul {
list-style: none;
position: relative;
top: -20px
}
li a{ color: white; }
<aside class="side-bar">
<nav class="navigation">
<div class="logo-container">
<a href="index.html" class="link">
<img src="http://unsplash.it/30/30" class="logoimg" alt="">
<h6 class="logoname">My<span class="lastname">Name</span></h6>
</a>
</div>
<div class="navigation-container">
<ul class="nav-list">
<li class="item">Menuitem1</li>
<li class="item">Menuitem2</li>
<li class="item">Menuitem3</li>
<li class="item">Menuitem4</li>
</ul>
</div>
</nav>
</aside>

CSS submenu parent color on hover and position of submenus

this might be an easy one.
This is how my pure css menu currently looks like:
html, body {
margin:0;
padding: 0;
font-family: arial;
}
/* Menu */
.menu__wrapper {
background: #fff;
z-index: 8000;
min-height: 30px;
box-shadow: 0px 1px 5px 0px rgba(0,0,0,0.75);
}
/* The dropdown container */
.dropdown {
float: left;
overflow: hidden;
}
/* Main links */
.menu__mainlink {
cursor: pointer;
border: none;
color: $dark-color;
padding: 5px 16px;
display: inline-block;
}
/* Sublinks */
.menu__sublink {
font-size: 16px;
padding: 5px 16px;
text-decoration: none;
display: block;
}
.menu__mainlink, .menu__sublink:hover {
color: red;
text-decoration: none;
}
/* Dropdown content (hidden by default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #fff;
width: 100%;
left: 0;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Sticky header */
.menu__wrapper.sticky {
position: fixed;
background-color: #fff;
width: 100%;
top: 0px;
}
/* Dropdown button */
.sticky .dropdown .dropbtn, .sticky a {
color: #000;
}
<div class="menu__wrapper padding-left-large sticky">
<div class="dropdown no-padding-left">
<a class="menu__mainlink" href="#">Main</a>
<i class="fa fa-caret-down"></i>
<div class="dropdown-content">
<a class="menu__sublink" href="#">Information</a>
<a class="menu__sublink" href="#">Archiv</a>
<a class="menu__sublink" href="#">Kontakt</a>
<a class="menu__sublink" href="#">Impressum</a></div>
</div>
<div class="dropdown no-padding-left">
<a class="menu__mainlink" href="#">Program</a>
<i class="fa fa-caret-down"></i>
<div class="dropdown-content show">
<a class="menu__sublink" href="#">This</a>
<a class="menu__sublink" href="#">That</a>
<a class="menu__sublink" href="#">Really_long_menu_item</a>
<a class="menu__sublink" href="#">Calendar</a></div>
</div>
<div class="dropdown no-padding-left">
<a class="menu__mainlink" href="#">Found</a>
<i class="fa fa-caret-down"></i>
<div class="dropdown-content">
<a class="menu__sublink" href="#">Videos</a>
<a class="menu__sublink" href="#">Image</a>
<a class="menu__sublink" href="#">Sound</a>
<a class="menu__sublink" href="#">Text</a>
</div>
</div>
</div>
Pen: https://codepen.io/t-book/pen/yLNwRba?editors=1100
Question 1: How can I color red the parent menu item like "main" when hovering over its subitems (like Archiv or Kontakt)?
Question 2: How could I push the absolute positioned submenu left to align it under its parent? The moment I position the submenu relative it will keep the x of its parent but unfortunately, in case of really long submenu names, it pushes the next floated parent item right.
Answer 1:
.dropdown:hover> a{color:red;}
Answer 2:
remove overflow:hidden; to .dropdown
add position:relative; to .dropdown
remove width: 100%; from .dropdown-content

size and vertical center navbar items relative to logo image

I am trying to size a navbar logo on the left side of the horizontal navbar and then have the rest of the navbar items take the same vertical space and be vertically centered in the space. The navbar items currently do not use the full vertical space. Every sizing attribute that I've tried has created another problem. Thanks for all help.
Note: Can't use bootstrap. Wish I could.
/****************************************/
/* Fixed top navbar with dropdowns */
/****************************************/
.m-navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
}
/*.m-navbar li {
float: left;
}*/
.m-navbar-right {
float: right;
}
.m-navbar-left {
float: left;
}
.m-navbar li a, .m-menu-form {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
border: none;
font-size: 100%;
font-family: "Lato", sans-serif;
}
.m-navbar li a:hover:not(.active), .m-dropdown:hover, .m-menu-form:hover {
/*background-color: #ddd;*/
background-color: #9dd0f0;
}
.m-navbar-logo-img {
padding: 3px 0px 4px 4px;
width: auto;
max-height: 33px;
position: relative;
}
.m-navbar li a.active {
color: white;
background-color: #00719c;
}
.m-navbar li.m-dropdown {
display: inline-block;
}
.m-dropdown-content {
display: none;
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="navbar.css">
<title>Navbar Problem</title>
</head>
<body>
<div class="m-outer">
<div class="m-fixed-header">
<div class="m-navbar">
<ul>
<li class="m-navbar-left"><img class="m-navbar-logo-img" src="bugicon.png"></img></li>
<li class="m-navbar-left">
<form name="idForm" method="post" action="">
<input type="submit" name="action" class="m-menu-form" value="Home">
</form>
</li>
<li class="m-dropdown m-navbar-left">
Menu 1
<div class="m-dropdown-content">
Drop 1<br>
Drop 2<br>
Drop 3<br>
</div>
</li>
<li class="m-dropdown m-navbar-left">
Menu 2
<div class="m-dropdown-content">
Drop 1<br>
Drop 2<br>
Drop 3<br>
Drop 4<br>
Drop 5<br>
</div>
</li>
<li class="m-navbar-right"><a href='#'>Logoff </a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
did you try it with display:flex;
/****************************************/
/* Fixed top navbar with dropdowns */
/****************************************/
.m-navbar ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
display: flex;
flex-shrink: 1;
display: -webkit-flex;
-webkit-justify-content: space-around;
justify-content: space-around;
}
/*.m-navbar li {
float: left;
}*/
.m-navbar-right {
float: right;
}
.m-navbar-left {
float: left;
}
.m-navbar li a, .m-menu-form {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
border: none;
font-size: 100%;
font-family: "Lato", sans-serif;
}
.m-navbar li a:hover:not(.active), .m-dropdown:hover, .m-menu-form:hover {
/*background-color: #ddd;*/
background-color: #9dd0f0;
}
.m-navbar-logo-img {
padding: 3px 0px 4px 4px;
width: auto;
max-height: 33px;
position: relative;
}
.m-navbar li a.active {
color: white;
background-color: #00719c;
}
.m-navbar li.m-dropdown {
display: inline-block;
}
.m-dropdown-content {
display: none;}
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="navbar.css">
<title>Navbar Problem</title>
</head>
<body>
<div class="m-outer">
<div class="m-fixed-header">
<div class="m-navbar">
<ul>
<li class="m-navbar-left"><img class="m-navbar-logo-img" src="bugicon.png"></img></li>
<li class="m-navbar-left">
<form name="idForm" method="post" action="">
<input type="submit" name="action" class="m-menu-form" value="Home">
</form>
</li>
<li class="m-dropdown m-navbar-left">
Menu 1
<div class="m-dropdown-content">
Drop 1<br>
Drop 2<br>
Drop 3<br>
</div>
</li>
<li class="m-dropdown m-navbar-left">
Menu 2
<div class="m-dropdown-content">
Drop 1<br>
Drop 2<br>
Drop 3<br>
Drop 4<br>
Drop 5<br>
</div>
</li>
<li class="m-navbar-right"><a href='#'>Logoff </a></li>
</ul>
</div>
</div>
</div>
</body>
</html>

How do I evenly space these links within my navbar div?

I would like to evenly space the 3 links ('About', 'Hours', 'Contact') within the containing 'banLinks' div. I do not want to use a list of any kind.
I would like each link to be evenly spaced, taking up 1/3 of their container. I am very new to HTML and CSS and I'm not sure how to do this.
I think one way of doing it may be by dividing the width of the div container in pixels by 3, account for the font size, then set the margins somehow around this figure. But to me this seems a bit unseemly, I'n not sure if this is the done thing.
<body>
<div id="wrapper">
<div class="bruceBanner">
<a href="#">
<img border="0" alt="XYZ Banner" src="http://bit.ly/1QSpdbq" width="553" height="172">
</a>
</div>
<nav>
<div class="banLinks">
<a id="about" href="#">About</a>
<a id="hours" href="#">Hours</a>
<a id="contact" href="#">Contact</a>
</div> </nav>
</div><!-- .wrapper-->
</body>
CSS:
#wrapper {
}
.bruceBanner img {
border: 2px solid black;
height: 172px;
width: 553px;
display: block;
margin: 0 auto;
}
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
}
#about, #hours, #contact {
font-size: 20px;
border: 2px solid blue;
}
Here is a jsfiddle. https://jsfiddle.net/yuy84gmq/6/
you can do this using flexbox. Do as followed:
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
display: flex;
justify-content: space-around; //or space-between whatever you like best
}
JSfiddle: https://jsfiddle.net/yuy84gmq/10/
flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Add this to style:
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
padding: 0;
}
.banLinks a{
width: calc(33% - 4px);
display: inline-block;
margin: 0;
}
Use a display table
.banLinks {
display:table;
table-layout:fixed;
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
}
.banLinks a {
display:table-cell;
}
Here is the fiddle: https://jsfiddle.net/yuy84gmq/8/
A couple of options here...both of which work regardless of the number of list items...assuming there is enough width.
Display:Table-cell
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
display: table;
}
.banLinks a {
display: table-cell;
border: 1px solid grey
}
<div class="banLinks">
<a id="about" href="#">About</a>
<a id="hours" href="#">Hours</a>
<a id="contact" href="#">Contact</a>
</div>
Flexbox
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
display: flex;
}
.banLinks a {
flex: 1;
border: 1px solid grey
}
<div class="banLinks">
<a id="about" href="#">About</a>
<a id="hours" href="#">Hours</a>
<a id="contact" href="#">Contact</a>
</div>
Instead of usual a links, put them into a list, and set the list to be inline. Then you can apply margin to the list items to space them out.
HTML
<nav>
<ul class="banLinks">
<li><a id="about" href="#">About</a></li>
<li><a id="hours" href="#">Hours</a></li>
<li><a id="contact" href="#">Contact</a></li>
</ul>
</nav>
CSS
.banLinks li { display:inline-block;margin:0 10px;} /* Adjust left/right margin as appropriate */

scrollable ul element without scrollbar

I'm trying to use angular to create list of elements. The page will be an app on mobile phone.
The list itself can have many elements so what I expect is that the ul element should be scrollable ("swipable"?). I tried to follow some example like http://jsfiddle.net/sirrocco/9z48t/ and http://jsfiddle.net/qcv5Q/1/..
This is the html code:
<div class="container">
<div id="spinner-icon" style="display:none">
<span class = "icon-spinner"></span>
</div>
<div class="col-xs-12 indentation push-down bg-white" ng-repeat="cei in completeElementInfo">
<div class="clearfix">
<div class="pull-left">
<h4>{{cei.description}}</h4>
<p>{{cei.name}}</p>
</div>
</div>
<div class="log-widget-list">
<ul class="list scroller clearfix" id="elements-list">
<li class="pull-left" ng-repeat="tinfo in cei.techInfo | orderBy: 'tinfo.sentTime'">
<h4 class="align-center">{{tinfo.elementShortCode}}</h4>
<div class="clearfix">
<span class="icon-clock pull-left"></span>
<span class="pull-right"> {{tinfo.sentTime}}min</span>
</div>
</li>
</ul>
</div>
</div>
</div>
and this is the css code:
.list {
margin: 0;
padding: 0;
}
.log-widget-list {
height:100px;
width: 720px;
overflow: hidden;
}
.log-widget-list .scroller{
overflow-x: scroll;
list-style-type: none;
width: 1500px; /* combined width of all LI's */
}
#elements-list li {
width: 100px;
list-style: none;
box-sizing: border-box;
border-top: none!important;
background-color: #0accf8;
padding: 4px;
}
#elements-list li:not(:last-of-type) {
border-right: 3px solid #ffffff;
}
#elements-list [class^="icon-"], #elements-list [class*=" icon-"] {
margin-top: 4px;
font-size: 12px;
}
Now the problem is that i don't want that the horizontal scrollbar appears, but it appears and i don't understand why... Any idea?
add overflow:hidden in #wrapper css.
CSS:
#wrapper {
background: transparent;
width: 550px;
color: white;
height:100%;
overflow:hidden;
}
Demo: http://jsfiddle.net/lotusgodkk/9z48t/5/
DEMO
http://jsfiddle.net/FTUrF/6/
Changed some CSS here:
.log-widget-list {
width: 200px;
height: 300px;
border: 1px solid #000;
overflow: hidden;
}
.log-widget-list .scroller {
width: 215px;
height: 300px;
overflow: scroll;
padding-bottom: 15px;
list-style-type: none;
}
Added height and padding-bottom in .scroller and border in .log-widget-list
and added some more of these:
<span class="pull-right"> {{tinfo.sentTime}}min</span>

Resources