I want to create something like this using CSS Flexbox:
I usually achieve this using floats and lots of positioning tweaks, adding margins, padding, percentage values on top, bottom, left and right of the elements to adjust it to a certain viewport, but it seem to break-off in different viewports.
I know it is not right way, I end up writing too much CSS and still doesn't look consistent on different viewports.
Getting the Logo perfectly on the center and aligning them vertically is where I'm struggling.
Here is the HTML code.
<nav class="navbar">
<div class="sidebar-toggle">
<button class="sidebar-toggler" type="button">☰</button>
</div>
<div class="headerLogo">
Logo
</div>
<div class="headerLinks">
link1
link2
</div>
<div class="notifications">
<div class="mailIcon"></div>
</div>
</nav>
You can just use justify-content: space-between and get right spacing and align-items: center for vertical align.
.navbar,
.links {
display: flex;
align-items: center;
justify-content: space-between;
}
<nav class="navbar">
<div class="sidebar-toggle">
<button class="sidebar-toggler" type="button">☰</button>
</div>
<div class="headerLogo">
Logo
</div>
<div class="links">
<div class="headerLinks">
link1
link2
</div>
<div class="notifications">
<div class="mailIcon">Mail</div>
</div>
</div>
</nav>
If you want logo perfectly centered then you need to remove it from elements flow with position: absolute;
.navbar,
.links {
display: flex;
align-items: center;
justify-content: space-between;
position: relative;
}
.headerLogo {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<nav class="navbar">
<div class="sidebar-toggle">
<button class="sidebar-toggler" type="button">☰</button>
</div>
<div class="headerLogo">
Logo
</div>
<div class="links">
<div class="headerLinks">
link1
link2
</div>
<div class="notifications">
<div class="mailIcon">Mail</div>
</div>
</div>
</nav>
I've slightly modified your markup to achieve the logo centered. There is no need at all to use absolute positioning.
The idea is to end up with 3 containers of the same width equaly distributed along the main axis (row), so that the logo can fall just in the middle of the layout.
.navbar{
display:flex;
align-items:center;
}
.sidebar-toggle{
display:flex;
flex-basis:33.3%
}
.headerLogo{
display:flex;
justify-content:center;
flex-basis:33.3%
}
.headerLinks{
display:flex;
justify-content:space-around;
flex-basis:33.3%
}
I've included the .mailIcon div in the third div. You can distribute its content internally using similar styling if you need to.
<nav class="navbar">
<div class="sidebar-toggle">
<button class="sidebar-toggler" type="button">☰</button>
</div>
<div class="headerLogo">
Logo
</div>
<div class="headerLinks">
link1
link2
<div class="mailIcon"></div>
</div>
</nav>
You can play around with flexbox with this toy I've created:
http://algid.com/Flex-Designer
Related
I am looking at achieving the below image. I am not really sure on how to get the css working for the below structure. Should I be making DIV2 absolute?
I want the nav, div1 and div2 to occupy full height of the broswer.
My HTML skeletal is as follows.
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">
</a>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<div>
DIV 1
</div>
<div>
DIV 2
</div>
</div>
</div>
</div>
</body>
Set <html> and <body> to have 100% height, then set height on the divs to however large you want them to be (say 80%), as well as the container for the divs. Percentage height is based on the parent of the element.
I have provided a non-bootstrap answer, to help you understand the flexbox.
Setting the parent container to full height is the first step.
.container{
display: flex;
height: 100vw;
flex-direction: column;
}
Then by using flex: 1; we allow the children to take all the available space.
Last step is to limit the max-height of the second child by using max-height.
Working example here
Here you can find the CSS styles based on your HTML structure. The code is not responsive but it will give you an idea on how to go about solving your own problem.
body {
margin: 0;
}
.navbar {
background: blue;
height: 50px;
}
.container-fluid .row .col-md-6 {
max-width: 60%;
margin: 0 auto;
display: flex;
flex-direction: column;
// 50px is height of the navbar so subtracting it from total height
height: calc(100vh - 50px);
}
.col-md-6 > div {
display: flex;
justify-content: center;
align-content: center;
font-size: 30px;
}
.col-md-6 div:first-child {
flex: 1;
min-height: 400px;
/*Setting minimum height so height of div will not go below 400px otherwise it will get smaller than div2 due to flex:1
flex: 1 takes 100% of height - 50px - 100px
*/
background-color: red;
}
.col-md-6 div:last-child {
height: 100px;
background-color: green;
}
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">
</a>
</div>
</div>
</nav>
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<div>
DIV 1
</div>
<div>
DIV 2
</div>
</div>
</div>
</div>
Do not use bootstrap grid.
Use flexbox.
Parent div must have 100% height, display: flex and column direction. Think that way.
you have to use container class instead of container-fluid to have div centered on page
her the code
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">
</a>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-md-12">
<div>
DIV 1
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div>
DIV 2
</div>
</div>
</div>
</div>
for height you can use vh css unities OR use Felxbox and if you chose this second solution probably you can use V4 of BS
I have a lot of links in a navbar. Most of the links are positioned to the left. I want to position some of the links to the right (right-side-login-cart). The part that is giving me trouble is that I can get the links to the right, but then cannot get the links to vertically align.
I've tried a bunch of different things to get it to work. For instance, I have float:right;, used transform to translate Y, and tried to used vertical align.
Some different attempts:
HTML
<div class="links-at-top">
<div class="links-div">
<a href="/">
<img src="example.com/img">
</a>
Link1
Link2
</div>
<div class="right-side">
<div class="positioning">
<a id="right-link-1">RightLink1</a>
<a id="right-link-2">RightLink2</a>
</div>
</div>
</div>
Attempt 1:
CSS:
.right-side {
padding-right: 50px;
display: inline-block;
}
.positioning {
position: absolute;
right: 0;
transform: translateY(-50%);
}
Attempt 2:
CSS:
.right-side {
padding-right: 50px;
display: inline-block;
}
.positioning {
vertical-align: middle;
}
Add your .right-side div into the same div as your other links so that they'll be on the same line. Then you can just float right.
.links-div {
display: flex;
justify-content: space-between;
align-items: center;
}
<div class="links-at-top">
<div class="links-div">
<div>
Link1
Link2
</div>
<img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png" alt="" />
<div>
<a id="right-link-1">RightLink1</a>
<a id="right-link-2">RightLink2</a>
</div>
</div>
</div>
You can consider using flex-box
.links-at-top{
display: flex;
justify-content:space-between;
align-items:center;
}
<div class="links-at-top">
<div class="links-div">
Link1
Link2
</div>
<div class="right-side">
<div class="positioning">
<a id="right-link-1">RightLink1</a>
<a id="right-link-2">RightLink2</a>
</div>
</div>
</div>
make .links-div and .right-side both display:inline-block, and float the right-side to right.
<div class="links-at-top">
<div class="links-div">
Link1
Link2
</div>
<div class="right-side">
<ul class="positioning">
<li> <a id="right-link-1">RightLink1</a></li>
<li> <a id="right-link-2">RightLink2</a></li>
</ul>
</div>
.right-side {
display: inline-block;
float:right;
}
.links-div{
display:inline-block;
}
Maybe I don't have it setup right? I've been looking at code for the past 3 hours so it's possible I'm over looking something. Either way I have this h3 in a div and I want it to be centered both horizontally and vertically, however only horizontal is working.
<div class="col-md-4">
<div class="align-items-center" style="background-image:url({{ uni.u_homepage_pic.url }}); background-repeat:no-repeat; width: 350px; height: 225px;">
<h3 class="text-center" style="color:#ffffff;">{{ uni.u_name }</h3>
</div>
</div>
Update: Using Bootstrap V4
.align-items-center {
display: flex;
align-items: center; /*Aligns vertically center */
justify-content: center; /*Aligns horizontally center */
}
<div class="col-md-4">
<div class="align-items-center" style="background:red; width: 350px; height: 225px;">
<h3 class="text-center" style="">Hello</h3>
</div>
</div>
This answer is for bootstrap 4:
To vertically align the column content, you can use my-auto class in column itself:
<div class="col-md-4 my-auto">
<div style="background-image:url({{ uni.u_homepage_pic.url }});
background-repeat:no-repeat; width: 350px; height: 225px;">
<h3 class="text-center" style="color:#ffffff;">
{{ uni.u_name }
</h3>
</div>
</div>
Further, to align the items inside a column horizontally, you can use mx-auto and for both horizontal and vertical alignment, use m-auto. Horizontal align will work for single column.
To align all the columns, you need to supply the align-items-* classes inside the row element but not inside the col element:
<div class="container">
<div class="row align-items-start"><!-- align contents to top -->
<div class="col">
Top A
</div>
<div class="col">
Top B
</div>
<div class="col">
Top C
</div>
</div>
<div class="row align-items-center"><!-- align contents to center -->
<div class="col">
Mid A
</div>
<div class="col">
Mid B
</div>
<div class="col">
Mid C
</div>
</div>
<div class="row align-items-end"><!-- align contents to end -->
<div class="col">
Bottom A
</div>
<div class="col">
Bottom B
</div>
<div class="col">
Bottom C
</div>
</div>
</div>
Hope this helps!
try with flex and align-self-center:
<div class=”row”>
<div class=”col-6 align-self-center”>
<div class=”card card-block”>
Center
</div>
</div>
<div class=”col-6">
<div class=”card card-inverse card-danger”>
Taller
</div>
</div>
</div>
check this css
<div class="col-md-4">
<h3 class="centered">{{ uni.u_name }</h3>
</div>
and this is the css (put it in the head tag)
<style>
.centered {
display: flex;
flex-direction: row !important;
align-content: center !important;
justify-content: center !important;
align-self: center !important;
align-items: center !important;
margin:auto;
background-image:url({{ uni.u_homepage_pic.url }});
background-repeat:no-repeat;
width: 350px;
height: 225px;
color:#ffffff;
}
<style>
you can do this like in my example: https://jsfiddle.net/h5xofjqf/
or you just need set a fixed height to your container, then, the h1 would have a
h1 {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Property is a margin:auto; ...Do this
I'm very new to programming and I've been trying to figure this out for a while now but I'm still having trouble. I'm trying too center two different pieces of text so that they are equal distances from the left and right side and from the top and bottom of the page.
Here's my code:
<div class="entrance">
<div class="container">
<div class="row">
<div class="col-md-6">
<li> Example1 </li>
</div>
<div class="col-md-6">
<li><a href="#" style="text-decoration:none" > Example2 </a></li>
</div>
</div>
</div>
</div>
I just can't seem to figure out what I'm missing and I'm sure it's probably something very simple that I'm just not getting. Any help is appreciated, thanks!
You can use css text-align to achieve what you want. text-aligh: right on the left hand <div> and text-align: left on the right hand <div>.
As #Manish mentioned, I also removed the <li> wrapper. I also added col-xs-6 to the <div>'s so it works on smaller views too.
Example: http://jsfiddle.net/11cLvc2c/3/
In case you just want to center the text, try this:
Add "text-center" next to each "col-md-6" classes:
<div class="entrance">
<div class="container">
<div class="row">
<div class="col-md-6 text-center">
<li> Example1 </li>
</div>
<div class="col-md-6 text-center">
<li><a href="#" style="text-decoration:none" > Example2 </a></li>
</div>
</div>
</div>
</div>
In case you'd like to make full centering, I'd recommend to step a little outside from bootstrap and use flexboxes. Flexboxes are new to CSS3 and solve most of our problems. See here for more info:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Here's your modified code:
<style type="text/css">
.entrance{
display: flex;
justify-content:center;
align-items: center;
width: 800px; /*Put your desired width*/
height: 400px; /*Put your desired height */
}
.container div
{
width: 50%;
text-align: center;
}
</style>
<div class="entrance">
<div class="item-1">
Item 1
</div>
<div class="item-2">
Item 2
</div>
At the moment my html page has 2 divs that hold all the information on the page one underneath the other. Now I want there to be a side bar to the left of them spanning down the entire page.
<div class="container">
<div class="panel panel-primary">
<!-- Default panel contents -->
<div class="panel-heading">Group 1</div>
<div class="panel-body">
<div class='contentWrapper ng-cloak'>
<div class='content'>
<ul class="thumbnails">
<p>
content
</p>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="panel panel-primary">
<!-- Default panel contents -->
<div class="panel-heading">Group 2</div>
<div class="panel-body">
<div class='contentWrapper ng-cloak'>
<div class='content'>
<ul class="thumbnails">
<p>
content
</p>
</ul>
</div>
</div>
</div>
</div>
</div>
I would normally do this using the bootstrap grid template, however I am using an angular drag and drop library and using that (for some reason) messes up the animations when things are being moved around.
What would be the easiest way of adding in another div to act as a side menu always to the left of the two divs shown?
You can do something like this:
.sidebar {
background: #eee;
width: 100px;
height: 50px;
margin-right: -100%;
margin-bottom: 10px;
position: relative;
display: block;
overflow: visible;
float: left;
box-sizing: border-box;
}
.page-content {
background: #aaa;
margin-left: 100px;
}
<div class="container">
<div class="sidebar-wrapper">
<div class="sidebar">
SIDEBAR<br>
AT LEFT;
</div>
</div>
<div class="page-content-wrapper">
<div class="page-content">
<div class="panel panel-primary">
<!-- Default panel contents -->
<div class="panel-heading">Group 1</div>
<div class="panel-body">
<div class='contentWrapper ng-cloak'>
<div class='content'>
<ul class="thumbnails">
<p>
content
</p>
</ul>
</div>
</div>
</div>
</div>
<div class="panel panel-primary">
<!-- Default panel contents -->
<div class="panel-heading">Group 2</div>
<div class="panel-body">
<div class='contentWrapper ng-cloak'>
<div class='content'>
<ul class="thumbnails">
<p>
content
</p>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Trade-offs of this approach:
You need to put a fixed width to your sidebar (either by px, %, or anything)
You need either to have a fixed height or to let the sidebar has the height of the content (you can't put height: 100%;)
You can float a sidebar left, but to have it fill the page’s full height all its ancestor elements must have height: 100%. If .sidebar is directly under body, these styles will do it:
html, body, .sidebar { height: 100% }
.sidebar { float: left }
Sample, with tinted backgrounds to show block outlines.
I m not sure I understand entirely the question so I ll try to answer.
I would create a div with float left css to have a nav within for your left menu and if it has to be all along the page . And another div either float right or none to keep the 2 divs you created.
You can use flexbox (adjust your needs)
CSS
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row nowrap;
flex-flow: row nowrap;
}
.flex-item {
-webkit-flex: 1 auto;
flex: 1 auto;
}
DEMO HERE
Wrap it all in a container with
display: table;
width: 100%;
table-layout: fixed;
then create a sidebar div with
display:table-cell;
vertical-align:top;
width:35%;
and wrap your content in a container with
display:table-cell;
vertical-align:top;
width:100%;
make sure your side bare is above your content for it to be on the left.
and that's you a flexible grid with a sidebar.
You can use col-md-3 and col-md-9 for sidebar and content respectively. Fix the sidebar using position: fixed
BootPly Demo