CSS - 3 Links - Left Align 1, Right Align 2 - html

Suppose I have three buttons
<div class='button-wrap>
<a class='button'>First Button</a>
<a class='button'>Second Button</a>
<a class='button'>Third Button</a>
</div>
What is the best way to left align the first, and right align the second two? Preferably without using float.
I have tried wrapping the second two in a seperate div and using text-align:right but the extra div causes the buttons to go out of alignment with one another.

Try this.
.buttons {
display: flex;
justify-content: space-around;
}
.buttons .button:first-child {
flex-grow: 1;
}
<div class="buttons">
<div class="button">
<button>First button</button>
</div>
<div class="button">
<button>Second button</button>
</div>
<div class="button">
<button>Third button</button>
</div>
</div>

You can achieve it with display:flex - no need to change your markup
.button-wrap {
display: flex;
}
.button:nth-child(2) {
/* margin left the second button */
margin-left: auto;
}
<div class="button-wrap">
<a class="button">First Button</a>
<a class="button">Second Button</a>
<a class="button">Third Button</a>
</div>

With float, you have to wrap your links into divs like this :
.left {
float: left;
}
.right {
float: right;
}
<div class='button-wrap'>
<div class="left">
<a class='button'>First Button</a>
</div>
<div class="right">
<a class='button'>Second Button</a>
<a class='button'>Third Button</a>
</div>
</div>

Use flexbox with justify-content: space-between on the wrapper, plus put margin-left: auto on the second button. This will align everything from there on to the right, while everything before that stys left-aligned.
EDIT: I simplyfied the previous code - less is more efficient...
.wrap {
display: flex;
justify-content: space-between;
}
button:nth-child(2) {
margin-left: auto;
}
<div class="wrap">
<button>Button 1</button>
<button>Button 2</button>
<button>Button 3</button>
</div>

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>

Align 2 buttons to the right of text

I am trying to make my two buttons appear on the far right of a piece of text. I want the text and two buttons to all be inline with each other but the text is on the left and the two buttons are on the right. I have seen multiple solutions and I could get it to work with only 1 button but when I have two buttons it does not work.
I run a for loop over 'weaknesses' and print out each weakness. Each weakness will have an update and delete action.
I tried to do margin-left but each weakness is a different length so my buttons would not line up
I also tried using some solutions from this post but could not seem to get the expected outcome: Place a button right aligned
Any help would be appreciated
HTML
<div *ngFor="let weakness of weaknesses | async">
<div class="flex-box">
<p>{{ weakness }}</p>
<a class="btn btn-light btn-sm pull-right" href="#">Update</a>
<button type="button" class="btn btn-danger btn-sm">Delete</button>
</div>
</div>
Try this:
#btnHolder {
position:fixed;
top:10px;
right:10px;
}
<p>Text is here</p>
<div id="btnHolder">
<button>Update</button>
<button>Delete</button>
</div>
I dont know why it was so hard. It should have been easier to fix this problem.
One solution is auto margin with flex
nav {
display: flex;
align-items: center;
}
nav button:first-of-type {
/* Key here is margin-left needs to be auto */
margin: 0 10px 0 auto
}
<nav>
<span>Text</span>
<button>Button 1</button>
<button>Button 2</button>
</nav>
You should do it like this:
.flex-box {
display: flex;
align-items: center;
justify-content: start;
}
#buttons{
margin-left: 10px;
}
<div class="flex-box">
<p>Text is here</p>
<div id="buttons">
<a>Update</a>
<button>Delete</button>
</div>
</div>

Position div in navbar vertically center and to the right

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

Button and ceneterd text in one line

How can I make button to be on the left and a text to be in the center? They should be in one line. I tired this:
<html>
<body>
<div style="display: inline">Back</div>
<div style="display: inline"><center><h1>Welcome</h1></center></div>
</body>
</html>
But button is above the text.
You can use a flex container to center elements:
.container {
display: flex;
align-items: center;
}
h1 {
display: inline;
margin: 10px;
}
<div class='container'>
<button>
Back
</button>
<h1>
Welcome
</h1>
</div>
you can give margin-top to 20px; or something
<a href="/back" class="btn btn-default pull-left" role="button" style="margin-
top:20px;">Back</a>
<center><h1>Welcome</h1></center>

How to center an element in a column vertically in relation to another column

How can I center 2 buttons vertically in my view?
For example:
<div class="container">
<div class="row">
<div class="col-md-5">
<h4>My List</h4>
<div style="height:400px; overflow-y:auto">
<ul>
// List of checkboxes
</ul>
</div>
</div>
<div class="col-md-2">
<input type="button" id="btnAddField" class="btn btn-primary" value="Add" />
<input type="button" id="btnRemoveField" class="btn btn-primary" value="Remove" />
</div>
<div class="col-md-5">
<h4>Selected Fields</h4>
// This will contain the list selected from the checkboxes.
</div>
</div>
</div>
I want the buttons in column 2 to be centered in relation to column 1 (which in this case I made height of 400px.
Try css Line Height. Hope it will help.
vertical-align: middle;
line-height: 400px;
If you can use flexbox, something like this would do what you want (using your existing classes):
.row {
display: flex;
[class^="col"] {
float: none;
flex-shrink: 0;
margin: auto 0;
}
}
You can add new class on row that will use Flexbox and vertically center items with align-items: center when width is > 992px which is breakpoint of md DEMO
#media(min-width: 992px) {
.flex-row {
display: flex;
align-items: center;
}
}