In HTML, I have two elements, one with text and one containing a couple of buttons.
Instead of touching in the middle, I want them spaced to align to the left and right margins. So I want my text aligned to the left margin, and my buttons aligned to the right margin, without losing their vertical alignment as is.
I also happen to be using Bootstrap 3.
When I use pull-left/pull-right, float:left/float:right, or align="left"/align="right", I get a change in the vertical alignment of these two elements.
Paired down code looks like this essentially:
<div style="width:200px;height:100px">
Delete dashboard?
<button class="btn btn-secondary cancel">
Cancel
</button>
<button class="btn btn-danger">
Delete
</button>
</div>
What is the the most straightforward way to align these items to the left and right margins? I can do it with a table, but is that not the ideal way?
[![A modal example][2]][2]
Without seeing your code, I would use a flex layout. The key in a flex row is to use justify-content: space-between to push 2 children in the row to the far edges. Then align-items on the parent will set the vertical alignment.
.flex {
display: flex;
}
.col {
flex-direction: column;
max-width: 250px;
border: 1px solid black;
}
.bot {
border-top: 1px solid #ccc;
justify-content: space-between;
align-items: center;
}
<div class="flex col">
<div>
<h1>confirm</h1>
</div>
<div class="flex bot">
<p>delete dashboard</p>
<div>
<button>cancel</button>
<button>delete</button>
</div>
</div>
</div>
What about this?
<div class="row">
<div class="col-md-6">Dashboard</div>
<div class="col-md-6 text-right">Cancel Delete</div>
</div>
This will split your area in two columns, where all content of the right column will be aligned right, without affecting the CSS of the elements itself.
Related
I am new to Bootstrap, and have been able to find only one other person who has asked this question. They didn't get a valid answer, so I'm asking it again.
I have two button groups. On larger screen sizes, they should appear on the same line, one left aligned and one right aligned. When the screen becomes small enough, the right aligned element drops down to the next line. This is currently working as intended. What isn't currently functional is I want the right aligned element to become left aligned when this shift takes place. The issue I (and the above post) are having is that one of the elements is dynamically sized, so simply using Bootstrap breakpoints isn't an option. The shift needs to be dynamically based on the size available to the right aligned button group, not the absolute size of the window in question.
My current HTML is below.
<div class = "btn-group grouped-buttons btn-group-toggle" data-toggle="buttons" style = "margin: 25px">
<!-- Dyanimcally sized button group left aligned-->
<button *ngFor = "let type of typeList" class = "btn btn-default" type = "button" (click) = "catagoryFilter(type.name)" [ngClass] = "{'active': typeSelect == type.name}">{{type.name}}</button>
</div>
<div class = "btn-group grouped-buttons btn-group-toggle float-right" data-toggle="buttons" style = "margin-top: 25px">
<!-- Fixed size button group which should only float-right when not on its own line -->
<button class="btn btn-red" type="button" (click)="newSpeciesModal.show()"><fa-icon [icon]="addIcon"></fa-icon> New Species</button>
<button class="btn btn-blue" type="button" (click)="newSpeciesTypeModal.show()"><fa-icon [icon]="addIcon"></fa-icon> New Species Type</button>
</div>
You could put the elements into a container which has display: flex.
If you give it justify-content: space-between that will ensure the second element is to the far right when both fit on the same line.
Adding also flex-wrap: wrap ensures that if there isn't enough space the second element will move down (and to the left).
Here's a simple example:
body {
width: 100vw;
height: 100vh;
}
.container {
width: 100%;
height: 20%;
margin-bottom: 10px;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.container :nth-child(1) {
height: 100%;
background-color: red;
}
.container :nth-child(2) {
width: 20%;
height: 100%;
background-color: blue;
}
<div class="container">
<div style="width:30%;"></div>
<div></div>
</div>
<div class="container">
<div style="width:90%;"></div>
<div></div>
</div>
I currently have a simple one page website which displays a logo, 4 short lines of text and 2 buttons which are side by side. I'm using Bootstrap 4.
I have already tried many solutions posted on stack overflow but haven't found a fix.
I am trying to align all of the content horizontally and vertically whilst still being responsive so it is in the middle of the screen on all devices. Would be good to have no scroll however i'm guessing scroll may be required especially on smaller devices such as iPhone SE so that's ok.
The closest I have got is with the code below. This centers correctly however for example, on iPhone SE the logo and buttons are cut off at the top and bottom of the page rather than resizing like responsive should do.
css:
html, body {
height: 100%;
font-family: Arial, sans-serif !important;
overflow:auto;
}
body {
margin: 0;
}
.container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
}
html:
<div class="container">
<div style="padding:15px;" class="row">
<div class="col text-center">
<center>
<img src="assets/img/logo.png" class="logo" />
<br>
<br>
<p style="margin-top:1rem;" class="big">text</p>
<p>text<br>text<br>text</p><p>text<br>text<br>text</p>
<p class="no-margin">PURCHASE TICKET INSTANTLY ONLINE</p>
<p class="big">OR</p>
<p>RESERVE AND PAY ON ENTRY</p>
<br>
<div class="row">
<div class="col-sm-12 text-center">
<a href="purchase">
<button style="background-color: #db0e0e !important;border:none; line-height: 130%;" class="btn btn-danger btn-md center-block">PURCHASE
<br>TICKET ONLINE</button>
</a>
<a href="purchase"><button style="background-color: #ffffff !important;color: #db0e0e !important;border:none; line-height: 130%;" class="btn btn-danger btn-md center-block">RESERVE
<br>PAY ON ENTRY</button></a>
</div>
</div>
</center>
</div>
</div>
</div>
Flex. See my jsfiddle.
The key is to center both axes with nested flex <div>'s one with a flex-direction of column and one of row, both with justify-content: center. The key "gotcha" with flex is that at least one of the first flex div has to have height: 100%, since flex elements' default height are determined by their contents, not their parent height.
<div>Mandatory Documents</div>
<div>
<button type="text" (click)="delete()" pButton icon="pi pi-times" label="Delete">
</button>
</div>
I have two div tags. The word Mandatory Documents is coming in two lines. I need the whole word to come in only one line and the Delete button beside to it
What CSS should I apply?
you just need to use "display: inline-block" by default div elements are block. or you could also span i guess which are inline by default. I dont know why you say don't break the two words? anyway you could add between the words so it wouldnt break
<div style="display:inline-block">Mandatory Documents</div>
<div style="display:inline-block">
<button type="text" (click)="delete()" pButton icon="pi pi-times" label="Delete">test</button>
</div>
I just don't understand why it would split up the words? can you provide a screen shot if it doesnt work just to show the full layout?
Use a non-breakable space between your words:
To keep the two div on the same line, use display: flex on their parent with flex-direction: row. Here is an example with a parent container which is too small yet the children are correctly placed on the same row.
.group {
display: flex;
flex-direction: row;
border: 1px solid black;
max-width: 100px;
}
.group > div {
border: 1px solid green;
margin: 2px;
}
<div class="group">
<div>Mandatory Documents</div>
<div><button>Button</button></div>
</div>
<br>
<div class="group">
<div>Mandatory Documents</div>
<div><button>Button</button></div>
</div>
I have a very trivial problem. I just needed two simple arrows (left and right) vertically aligned to the middle to the image. Arrows have to be responsive and become smaller with screen size.
My code now is just:
<div class="row">
<div class="offset-md-1 col-md-1">
<i class="fa fa-arrow-left" style="position:relative; top:50%"></i>
</div>
<div class="col-md-8 detail_pic">
<img src="#image" style="width:80%;">
<h2 style="text-align:center;">#Title</h2>
</div>
<div class="col-md-1">
<i class="fa fa-arrow-right" style="position:relative; top:50%"></i></div>
</div>
</div>
This looks like the image below. But it doesn't work well when I size down the screen as the cols get stacked over each other. How do I get the arrows and the image in one col and make them responsive?
For the image size...you can have automatically resizing arrow icons by using viewport units like this img{ width: 2vmin} which listens to the shorter of the width/height dimensions in your viewport, and uses a percentage of this dimension... css tricks link for more info css-tricks.com/fun-viewport-units
For positioning the arrows in the center of the div use
img{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
a helpful article on flexbox vertical centering phillipwalton.com
I have this jsfiddle: Sample
As you can see 2 divs are align together side by side, what I want is to vertically aligning them. Also I want to be able to add another div to float to the right which is also vertical aligned.
How can I able to aligned them without using absolute positioning?
<div style="background-color: blue; ">
<!-- Global Header -->
<div class="header">
<div class="floatLeft">
WritePub
</div>
<div id="pcontainer" class="inner-header-search floatLeft">
<input type="text"/>
</div>
</div>
</div>
Your fiddle is too noisy. If you want to vertical align the contents of a div without touching its height you can add "display: table-cell" to the div to simulate a table row column which gives you alignment.
http://jsfiddle.net/5peh12th/2/
<div class="container">
Hello: <input type="text-box"/>
</div>
.container {
display: table-cell;
width: 800px;
background-color: red;
height: 50px;
vertical-align: middle;
}
or you can just not give the div a height and give top and bottom padding of equal numbers