Position div in navbar vertically center and to the right - html

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;
}

Related

How to style horizontally and vertically?

<div class="flex-container">
<div class="flex-row">
<li class="org-li" v-for="o in orgs">
<div class="flex-item">
<button v-b-modal.link_printer #click="click_org(o.id)" class="org-btn">
<img class="org-img" :src="`http://localhost:8080/orgs/org_pictures/${o.img}`" alt=""/>
</button>
</div>
<li class="printer-li" v-for="p in printers[o.id]">
<div class="flex-item">
<button variant="outline-primary" class="printer-btn" v-bind:org_id="`${o.id}`" v-bind:printer_id="`${p.id}`" #click="onClick(o.id, p.id, p.serial_number)" >
<img class="printer-img" :src="`http://localhost:8080/printers/printer_pictures/${p.img}`" alt=""/>
</button>
</div>
</li>
</li>
</div>
</div>
I want the org-img to be 55px x 55px and I want the printer-img to be 25px x 25px. Each org-btn and printer-btn should be centered vertically on the screen.
I have been having difficulties with the printer-img being slightly offset from being vertically aligned.
you can do it that way. This adds the CSS styles to your component's block. The .org-img and .printer-img styles will define the width and height of the images, and the .flex-item style will center the buttons within the div elements that contain them. And the .flex-container will wrap the content inside on overflow.
You can make adjustments as needed to match your component's specific structure and class naming.
<template>
<div class="flex-container">
<div class="flex-row">
<li class="org-li" v-for="o in orgs">
<div class="flex-item">
<button v-b-modal.link_printer #click="click_org(o.id)" class="org-btn">
<img class="org-img" :src="`http://localhost:8080/orgs/org_pictures/${o.img}`" alt=""/>
</button>
</div>
<li class="printer-li" v-for="p in printers[o.id]">
<div class="flex-item">
<button variant="outline-primary" class="printer-btn" v-bind:org_id="`${o.id}`" v-bind:printer_id="`${p.id}`" #click="onClick(o.id, p.id, p.serial_number)" >
<img class="printer-img" :src="`http://localhost:8080/printers/printer_pictures/${p.img}`" alt=""/>
</button>
</div>
</li>
</li>
</div>
</div>
</template>
<style>
.org-img {
width: 55px;
height: 55px;
}
.printer-img {
width: 25px;
height: 25px;
}
.flex-item {
display: flex;
align-items: center;
justify-content: center;
}
.flex-container {
display: flex;
flex-wrap: wrap;
}
</style>

margin: 0 auto; not centering div in parent

I have the following div arrangement, with the below css. Everywhere online seems to say you need margin:0 auto; for centering, but the text still goes to the top right,
Any help appreciated.
.menu_item {
margin: 0 auto;
}
<div style='position:absolute;width:110%;height:110%;left:-5%;top:-5%;background-color:white;z-index:7;overflow:none;'>
<div id='menu_but' style='width:36px;height:36px;'>
<div class='menu_line' />
<div class='menu_line' />
<div class='menu_line' />
</div>
<div id='menu'>
<div class='menu_item'>HOME</div>
<div class='menu_item'>ABOUT US</div>
<div class='menu_item'>CONTACT US</div>
<div class='menu_item'>PORTFOLIO</div>
</div>
</div>
First of all, <div> is not a self-closing tag so you have to write </div> instead of <div class='menu_line'/>
Then, you could simply do:
<style>
.menu_item{
display: inline-block;
}
#menu {
position: relative;
text-align: center;
}
</style>
<div id="menu">
<div class="menu_item">HOME</div>
<div class="menu_item">ABOUT US</div>
<div class="menu_item">CONTACT US</div>
<div class="menu_item">PORTFOLIO</div>
</div>
maybe use flexbox ?
.parent {
display:flex;
justify-content:center;
align-items:center;
}
All of the child elements will be centered

How to vertically center text adjacent to image in grid alignment?

Trying to vertically center align text next to an image in a three across grid alignment. Have tried many things but either one of two things happens:
- text vertically center aligns for one row of text, but wraps under the image if there is more than one row of text
- text wraps properly for multiple rows of text, but won't vertically center align, as shown in the provided code
Any suggestions?
.tile-third {
font-size: 18px;
text-transform: uppercase;
float: left;
width: 25%;
margin-right: 1%;
margin-bottom: 1%;
}
.tile-image {
vertical-align:middle;
float:left;
margin-right: 1%;
background-color:#000;
}
.tile-text {
}
<div class="tile-third">
<a href="#">
<img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-outside.png"/>
<div class="tile-text">Department Description One (DD1)</div>
</a>
</div>
<div class="tile-third">
<a href="#">
<img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-lowimpact.png"/>
<span class="tile-text">Department Description Two</span>
</a>
</div>
<div class="tile-third">
<a href="#">
<img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-fun.png"/>
<span class="tile-text">Department Description Three (DD3)</span>
</a>
</div>
the image and the text is inside in an anchor tag,
change the style of the anchor tag to achieve the required.
example,
.tile-third a {
height: 100%;
display: flex;
align-items: center;
}
Is this what you are looking for??
.tile-third {
display: inline-block;
font-size: 18px;
text-transform: uppercase;
width: 30%;
}
.tile-third > a {
align-items: center;
display: flex;
}
.tile-image {
background-color:#000;
margin-right: 2%;
}
.tile-text {
}
<div class="tile-third">
<a href="#">
<img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-outside.png"/>
<div class="tile-text">Department Description One (DD1)</div>
</a>
</div>
<div class="tile-third">
<a href="#">
<img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-lowimpact.png"/>
<span class="tile-text">Department Description Two</span>
</a>
</div>
<div class="tile-third">
<a href="#">
<img class="tile-image" src="http://acmebeachandbike.com/wp-content/uploads/2016/05/icon-fun.png"/>
<span class="tile-text">Department Description Three (DD3)</span>
</a>
</div>
I set the display value for the <a> tag under the <div class="tile-third"> to flex which is an easy way to get things to properly align.
This allows the <img> and <div> inside the <a> to properly align.

Dashboard header using CSS Flexbox

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

trying to resize list items so they are all equal size

I am trying to make my list items all the same width. I've tried setting the widths in the css and nothing changes. Can anyone figure this out?
Here is an image to show what I am talking about:
HTML:
<body>
<div class="content-wrapper" id="container">
<header>logo
<nav>navigation</nav>
</header>
<div class="clear-fix"></div>
<div id="body">
<section class="main-content">
<section class="leftPane">
<h2>Categories</h2>
</section>
<section class="rightPane">
<div class="checkout">
<h1>Checkout</h1>
<ul class="steps">
<li><a href="http://localhost:59213/Cart">
<div id="step0" class="arrow_box_grey">
Shopping Cart</div>
</a>
</li>
<li><a href="http://localhost:59213/Cart/Student">
<div id="step1" class="arrow_box_grey">
Student</div>
</a>
</li>
<li><a href="http://localhost:59213/Cart/Delivery">
<div id="step2" class="arrow_box_grey">
Delivery</div>
</a>
</li>
<li>
<div id="step3" class="arrow_box_green">Payment</div>
</li>
<li>
<div id="step4" class="arrow_box_blue">Finish</div>
</li>
</ul>
</div>
</section>
<div class="clear-fix"></div>
</section>
</div>
</div>
And here if my fiddle with the code: http://jsfiddle.net/M74Em/
You need to change this style:
.main-content .checkout ul.steps li div {
display: inline;
width: 1000px;
}
You can't set widths for inline elements so try this:
.main-content .checkout ul.steps li div {
display: inline-block;
width: 100px;
}
Example
From twBootstrap, applied to <ul>'s (demo):
.ul-justified {
display: table;
width: 100%;
table-layout: fixed;
border-collapse: collapse;
}
.ul-justified > li {
display: table-cell;
float: none;
width: 1%;
}
and slap .ul-justified to any ul-list you want aligned, and you get equal width <li>'s automatically fit parent ul's width.