why aren't my html buttons aligning properly - html

I cant get the 4 buttons to align in two rows. I've tried using flexbox yet I can only get them to align in two rows of three buttons at the top and one at the bottom. Ideally the buttons would be in the centre of the page, equally distant from each other.
HTML code
CSS code
Result

You can do it this way using flexbox note that you have to make two containers as a parent to hold your buttons.
.button_tense{
width:300px;
height: 300px;
}
.container{
display: flex;
flex-direction: row;
justify-content: space-around;
}
<div class="container">
<button class="button_tense"><a >present</a></button>
<button class="button_tense"><a >past</a></button>
</div>
<div class="container">
<button class="button_tense"><a >future</a></button>
<button class="button_tense"><a >imperfect</a></button>
</div>
And using grid with this
.button_tense{
width:300px;
height: 300px;
}
.container{
display: grid;
grid-template-columns: 1fr 1fr;
justify-items: center;
}
<div class="container">
<button class="button_tense"><a >present</a></button>
<button class="button_tense"><a >past</a></button>
<button class="button_tense"><a >future</a></button>
<button class="button_tense"><a >imperfect</a></button>
</div>
I would recommend for you to see this
Guide To Grid
and this
Guide To Flexbox
And welcome to stackoverflow :)

You have to add divs around each of the 2 buttons you want in the same row. Code is below.
button{
margin:5px;
}
<div>
<button class="button_tense"><a >present</a></button>
<button class="button_tense"><a >past</a></button>
</div>
<div>
<button class="button_tense"><a >future</a></button>
<button class="button_tense"><a >imperfect</a></button>
</div>

Related

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>

Fitting elements into a grid in a responsive way

Consider a <div> element that contains <button> elements:
<div id="box">
<button class="button">1</button>
<button class="button">2</button>
<button class="button">3</button>
...
</div>
I want to fit these elements into a grid pattern, so that
buttons are of equal dimensions and have a fixed minimum width,
a maximal amount of buttons is stacked on each row,
button width is adjusted so that each row is filled completely,
and this behavior persists when #box is resized.
Is there a way to do so using native HTML/CSS features (without using JavaScript)? I tried using the new CSS Flexbox layout:
#box {
display: flex;
width: 500px;
height: 500px;
}
.button {
flex: 1 1 auto;
min-width: 40px;
height: 40px;
}
But this doesn't work..
Thanks for your help!
You can easily use CSS grid layout.
I prefer it over flexbox.
<div id='grid'>
<button class='button'>1</button>
<button class='button'>2</button>
<button class='button'>3</button>
<button class='button'>4</button>
<button class='button'>5</button>
<button class='button'>6</button>
<button class='button'>7</button>
<button class='button'>8</button>
<button class='button'>9</button>
<button class='button'>10</button>
<button class='button'>11</button>
<button class='button'>12</button>
<button class='button'>13</button>
<button class='button'>14</button>
<button class='button'>15</button>
<button class='button'>16</button>
</div>
<style>
#grid {
display: grid;
grid: 50px / auto-flow;
}
</style>
Here is the codepen.
https://codepen.io/kenanbalija/pen/bYYarj
You can use CSS flex for this, as you suspected.
flex-wrap is what you were missing, as well as using percentages for responsiveness.
#box {
display: flex;
flex-wrap: wrap;
}
#box > * {
flex: 1 0 100px; /* each button should be at least 100px wide */
}
<div id='box'>
<button class='button'>1</button>
<button class='button'>2</button>
<button class='button'>3</button>
<button class='button'>4</button>
<button class='button'>5</button>
<button class='button'>6</button>
<button class='button'>7</button>
<button class='button'>8</button>
<button class='button'>9</button>
<button class='button'>10</button>
<button class='button'>11</button>
<button class='button'>12</button>
<button class='button'>13</button>
<button class='button'>14</button>
<button class='button'>15</button>
<button class='button'>16</button>
</div>

Right aligned button is misaligned vertically

I have a row with header text and a button on the right.
<div class="page-header">
<div class="pull-right">
<button type="button" class="btn btn-success pull-right">Set All Values</button>
</div>
<h4>Points Earned for Each Chapter</h4>
</div>
The code above is what I have currently but I have tried many variations. In all of them, the button is not vertically aligned with the text. (The image below includes the overlay when I hover over the element in the Elements pane of Chrome, in order to show vertical alignment.)
Searching existing questions on stackoverflow, I've found this question posted a number of times; however, I found no knowledgeable answers.
Has anyone figured this out?
This happens because the line-height of the heading and the height of the button differ.
With a line-height: 1.9; for the heading it should work:
.page-header h4 {
line-height: 1.9;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="page-header">
<div class="pull-right">
<button type="button" class="btn btn-success pull-right">Set All Values</button>
</div>
<h4>Points Earned for Each Chapter</h4>
</div>
You can try using the grid to be mobile friendly and make everything inside vertically aligned.
<div class="page-header vertical-center">
<div class="row">
<div class="col-md-8">
<h1>Text on the left</h1>
</div>
<div class="col-md-4">
<button type="button" class="btn btn-success pull-right">Set All Values</button>
</div>
</div>
</div>
Add this CSS
.row > div {
border: 1px solid black;
}
.vertical-center .row {
display: flex;
align-items: center;
}
/*maybe create an id instead of h1*/
h1 {
margin: 0;
}

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

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>

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