How to align divs horizontally with html? - css

I need to do a toolbar that consists of a Left-arrow button, a set of image buttons then a Right-arrow button
My requirements are:
the toolbar needs to be on one row
the toolbar needs to be centered horizontally
the set can have many image buttons (N is unknown)
when the window is too small in width the middle div should hide the image buttons that don't fit
To center everything horizontally I found this to work well on chrome:
<div style="display:-webkit-box; -webkit-box-pack:center; -webkit-box-align:center;" class="toolbar">
<button>LEFT</button>
<div style="overflow:hidden;">
<button style="height:72px">1</button>
<button style="height:72px">2</button>
<button style="height:72px">3</button>
<button style="height:72px">4</button>
<button style="height:72px">5</button>
<button style="height:72px">6</button>
<button style="height:72px">7</button>
<button style="height:72px">8</button>
<button style="height:72px">9</button>
</div>
<button>RIGHT</button>
</div>
But unfortunately the box parameters breaks the overflow:hidden style that is necessary if the middle set becomes too big to fit everything.
So is there another way of centering everything horizontally ?

As long as the elements are not floated, you can use text-align: center, along with display: inline block.
<div style="text-align:center;" class="toolbar">
<button style="display: inline-block;">LEFT</button>
<div style="overflow:hidden; display:inline-block;">
<button style="height:72px">1</button>
<button style="height:72px">2</button>
<button style="height:72px">3</button>
<button style="height:72px">4</button>
<button style="height:72px">5</button>
<button style="height:72px">6</button>
<button style="height:72px">7</button>
<button style="height:72px">8</button>
<button style="height:72px">9</button>
</div>
<button style="display: inline-block;">RIGHT</button>
</div>
Check this JSFiddle

There are a few ways to do this I'm not sure exactly what is best for you.
First be sure to set the width of your div element that contains the buttons. other wise it will default to 100% and the margin:0 auto won't work;
something like this
<style>
.horizantalAlign{
width:900px;
margin:0 auto;
}
</style>
<div class="horizantalAlign">
<button style="height:72px">1</button>
<button style="height:72px">2</button>
<button style="height:72px">3</button>
<button style="height:72px">4</button>
<button style="height:72px">5</button>
<button style="height:72px">6</button>
<button style="height:72px">7</button>
<button style="height:72px">8</button>
<button style="height:72px">9</button>
</div>

The overflow:hidden will only work if you give it a set width and height.
<div style="overflow:hidden; width:213px; height:73px;">
View the JSFiddle

Above answers not met the condition
when the window is too small in width the middle div should hide the image buttons that don't fit
overflow:hidden requires size
<div style="overflow:hidden; width:10%; max-width:10%; white-space:nowrap;
max-height:72px; height:72px">
http://jsfiddle.net/VzyRm/

The "margin: 0 auto" inspired me to find a solution. thanks
I am using a table instead of a div and it's then working well
http://jsfiddle.net/G3Vu6/4/
<div style="" class="toolbar">
<table style="margin: 0 auto; max-width: 100%">
<tr>
<td style=""><button>LEFT</button></td>
<td style="" >
<div style="overflow:hidden;max-height:72px">
<button style="height:72px">1</button>
<button style="height:72px">2</button>
<button style="height:72px">3</button>
<button style="height:72px">4</button>
<button style="height:72px">5</button>
<button style="height:72px">6</button>
<button style="height:72px">7</button>
<button style="height:72px">8</button>
<button style="height:72px">9</button>
<button style="height:72px">1</button>
<button style="height:72px">2</button>
<button style="height:72px">3</button>
<button style="height:72px">4</button>
<button style="height:72px">5</button>
<button style="height:72px">6</button>
<button style="height:72px">7</button>
<button style="height:72px">8</button>
<button style="height:72px">9</button>
<button style="height:72px">1</button>
<button style="height:72px">2</button>
<button style="height:72px">3</button>
<button style="height:72px">4</button>
<button style="height:72px">5</button>
<button style="height:72px">6</button>
<button style="height:72px">7</button>
<button style="height:72px">8</button>
<button style="height:72px">9</button>
</div>
</td>
<td><button style="">RIGHT</button></td>
</tr>
</table>
Try playing with the splitters in fiddler and you will see how it reacts beautifully.

Related

Angular Material: Prevent Slider from Overlapping with Menu

I am just getting started with Angular Material and experiencing the problem that the slider component overlaps with my menu.
How can I make sure the slider is displayed below the menu - is there some non-overlapping flag I can set to accomplish this?
May html code looks like this:
<button mat-button [matMenuTriggerFor]="aboveMenu">Above</button>
<mat-menu #aboveMenu="matMenu" yPosition="above">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
</mat-menu>
<button mat-button [matMenuTriggerFor]="belowMenu">Below</button>
<mat-menu #belowMenu="matMenu" [overlapTrigger]="false" yPosition="below">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
</mat-menu>
<button mat-button [matMenuTriggerFor]="beforeMenu">Before</button>
<mat-menu #beforeMenu="matMenu" xPosition="before">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
</mat-menu>
<button mat-button [matMenuTriggerFor]="afterMenu">After</button>
<mat-menu #afterMenu="matMenu" xPosition="after">
<button mat-menu-item>Item 1</button>
<button mat-menu-item>Item 2</button>
</mat-menu>
<mat-slider min="1" max="100" step="1" value="1"></mat-slider>
Thank you very much for your help!
Best regards, Sam
You could try to use flex-wrap in your styles this way:
display: flex; flex-wrap: wrap;
Here is a simple snippet adding it as an inline style but I do suggest that you add them in a separate file and reference the divs with classes.
I hope It helps you!
<div style="display: flex; flex-wrap: wrap;">
<div style="width: 100%;">
<button mat-button [matMenuTriggerFor]="aboveMenu">Above</button>
<button mat-button [matMenuTriggerFor]="belowMenu">Below</button>
<button mat-button [matMenuTriggerFor]="beforeMenu">Before</button>
<button mat-button [matMenuTriggerFor]="afterMenu">After</button>
</div>
<div style="width: 100%;">
<input type="range" min="1" max="100" value="50">
</div>
</div>
More information about flex and flex-wrap:
https://developer.mozilla.org/en-US/docs/Web/CSS/flex-wrap

modal vertical position center

1.How to put a modal in the center?
2.Using Angular 2/4 with html5,JS and css!
<ng-template #content let-c="close">
<div class="modal-header">
<h4 class="modal-title">Deseja excluir definitivamente?</h4>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" title="Confirmar Ação" (click)="c('Close click')" (click)="deleteUser(user)">Confirmar</button>
<button type="button" class="btn btn-secondary" title="Cancelar Ação" (click)="c('Close click')">Cancelar</button>
</div>
</ng-template>
<ng-template #content let-c="close">
<div class="modal-header">
<h4 class="modal-title">Deseja excluir definitivamente?</h4>
</div>
<!--add this in start here--><div class="modal-body">
<p>Some text in the modal.</p><!--end here-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" title="Confirmar Ação" (click)="c('Close click')" (click)="deleteUser(user)">Confirmar</button>
<button type="button" class="btn btn-secondary" title="Cancelar Ação" (click)="c('Close click')">Cancelar</button>
</div>
</ng-template>
AS per your requirement your modal should be in fixed height, so based on that you can set top margin and other css.
OR
You have to calculate modal height after DOM/HTML render in your modal body and then again apply some calculation logic and set top-margin from back end.

Spacing between Bootstrap buttons

How do I make sure that there is spacing between the buttons when i use the following piece of code
<div class="container">
<div class="jumbotron">
<button ng-repeat="char in alphabet" ng-click="filterByAlphabet(char)">{{char}}</button>
<br><br>
<span ng-repeat="genre in genres">
<button class="btn btn-xs" ng-click="filterByGenre(genre)">{{genre}}</button>
</span>
</div>
'btn-toolbar' does the trick in one fell swoop. No manual css'ing required
<div class="btn-toolbar">
<button id="button1" class="btn btn-primary">Button 1</button>
<button id="button2" class="btn btn-primary">Button 2</button>
<button id="button3" class="btn btn-primary">Button 3</button>
</div>
You can use Bootstrap spacing, docs here --> https://getbootstrap.com/docs/4.0/utilities/spacing/
This example uses the class 'mr-2' to add right margins to buttons
<button id="button1" class="btn btn-primary mr-2">Button 1</button>
<button id="button2" class="btn btn-primary mr-2">Button 2</button>
<button id="button3" class="btn btn-primary">Button 3</button>
Use css property margin
button { margin: 5px 5px 5px 5px } //change the value to see the effect and choose for yourself which value to keep
margin: margin-top margin-right margin-bottom margin-left ;
Set right margin on buttons:
<div class="container">
<div class="jumbotron">
<button ng-repeat="char in alphabet" ng-click="filterByAlphabet(char)" style="margin-right: 10px;">{{char}}</button>
<br><br>
<span ng-repeat="genre in genres">
<button class="btn btn-xs" ng-click="filterByGenre(genre)" style="margin-right: 10px;">{{genre}}</button>
</span>
</div>
Hope, this is what you're looking for.
.capsule{
padding: 0 30px;
}
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<div class="container">
<div class="jumbotron">
<span class="col-xs-4"> </span>
<span class="capsule col-xs-2">
<button ng-repeat="char in alphabet" ng-click="filterByAlphabet(char)">{{char}}</button>
</span>
<span class="capsule col-xs-2" ng-repeat="genre in genres">
<button class="btn btn-xs" ng-click="filterByGenre(genre)">{{genre}}</button>
</span>
</div>
i added one empty span to center the jumbo in the screen, also added "capsule" span to get space between the buttons.
Hope it helps!

How to add space between right align bootstrap button?

I would like to add 3 spaces between two right align button. Is there any bootstrap solution for it without adding custom css?
I do this :
<div class="text-right">
<button class="btn-info btn">Log in</button>
<!-- Add 3 spaces between button -->
<button class="btn-info btn">Log Out </button>
</div>
Fiddle demo : http://jsfiddle.net/6816gq84/1/
You can add btn-toolbar class to the container div.
<div class="text-right btn-toolbar">
<button class="btn-info btn">Log in</button>
<button class="btn-info btn">Log Out </button>
</div>
Have a look here.
I do this rudimentary solution: How to add space between bootstrap buttons in same div?.
<div class="btn-group pull-right row">
<div class="col-md-3"><button class="btn btn-default" type="button">Clôturer</button></div>
<div class="col-md-1"></div>
<div class="col-md-3"><button class="btn btn-primary" type="button">Soumettre</button></div>
</div>
This also work fine for me:
<div style="display: flex ; justify-content: flex-end">
<button class="btn-info btn mr-3">Log in</button>
<button class="btn-info btn">Log Out </button>
</div>
I'm not sure any bootstrap solution already exists for what you want to do, but there are other way, depending what you mean by "custom css".
You can add a span balise for exemple and add an inline css defining the width to 3em (3 spaces). It's HTML/CSS but you don't modify the css of the button's class.
<div class="text-right">
<button class="btn-info btn">Log in</button>
<span style="width:3em;"> </span>
<button class="btn-info btn">Log Out </button>
</div>

Making button go full-width?

I want a button to take up the full width of the column, but having difficulties...
<div class="span9 btn-block">
<button class="btn btn-large btn-block btn-primary" type="button">Block level button</button>
</div>
How do I make the button as wide as the column?
Bootstrap v3 & v4
Use btn-block class on your button/element
Bootstrap v2
Use input-block-level class on your button/element
Why not use the Bootstrap predefined class input-block-level that does the job?
Full-Width Button <!-- BS2 -->
Full-Width Button <!-- BS3 -->
<!-- And let's join both for BS# :) -->
Full-Width Button
Learn more here in the Control Sizing^ section.
Bootstrap / CSS
Use col-12, btn-block, w-100, form-control or width:100%
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<button class="btn btn-success col-12">
class="col-12"
</button>
<button class="btn btn-primary w-100">
class="w-100"
</button>
<button class="btn btn-secondary btn-block">
class="btn-block"
</button>
<button class="btn btn-success form-control">
class="form-control"
</button>
<button class="btn btn-danger" style="width:100%">
style="width:100%"
</button>
In case some are noticing btn-block not working anymore in bootstrap 5.1+:
It was dropped in bootstrap 5.1 (changelog):
Breaking Dropped .btn-block for utilities. Instead of using .btn-block
on the .btn, wrap your buttons with .d-grid and a .gap-* utility to
space them as needed. Switch to responsive classes for even more
control over them. Read the docs for some examples.
You can now make a button full width by adding the class w-100, as the authors do in their official Pricing example page:
<button type="button" class="w-100 btn btn-lg btn-outline-primary">
Sign up for free
</button>
Source: https://getbootstrap.com/docs/5.1/examples/pricing/
If you want to make a stack of block buttons, the official docs recommend the following :
<div class="d-grid gap-2">
<button class="btn btn-primary" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
Source: https://getbootstrap.com/docs/5.1/components/buttons/#block-buttons
I simply used this:
<div class="col-md-4 col-sm-4 col-xs-4">
<button type="button" class="btn btn-primary btn-block">Sign In</button>
</div>
use
<div class="btn-group btn-group-justified">
...
</div>
but it only works for <a> elements and not <button> elements.
see: http://getbootstrap.com/components/#btn-groups-justified
You should add these styles to a CSS sheet
div .no-padding {
padding:0;
}
button .full-width{
width:100%;
//display:block; //only if you're having issues
}
Then change add the classes to your code
<div class="span9 btn-block no-padding">
<button class="btn btn-large btn-block btn-primary full-width" type="button">Block level button</button>
</div>
I haven't tested this and I'm not 100% sure what you want, but I think this will get you close.
In Bootstrap 5, the class btn-block doesn't work anymore. But instead, you can add the class w-100 to the button to make it as wide as its container.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="row"><div class=" col-5 ">
<a class="btn btn-outline-primary w-100 " href="# "><span>Button 1</span></a>
</div><div class="col-5 ">
<a class=" btn btn-primary w-100 weiss " href="# "><span>Button 2</span></a>
</div></div>
I would have thought this would be the most bootstrap-esque way of doing things:
<button type='button' class='btn btn-success col-xs-12'> First buttton baby </button>
All I'm doing is adding the class col-xs-12 to the button.
<div class="col-md-9">
<button class="btn btn-block btn-primary" type="button">Block level button</button>
</div>
In Bootstrap 3, this should be all you need. I believe btn-large was overriding the width of btn-block.
Update 2022:
You have multiple ways to make button full width.
You can add the w-100 to the class.
You can add d-grid and gap-2to a container above the button:
<div class="d-grid gap-2"> <!-- Here -->
<button class="btn btn-primary" type="button">Button</button>
</div>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<button class="btn btn-success col-12">
class="col-12"
</button>
<button class="btn btn-primary w-100">
class="w-100"
</button>
<button class="btn btn-secondary btn-block">
class="btn-block"
</button>
<button class="btn btn-success form-control">
class="form-control"
</button>
<button class="btn btn-danger" style="width:100%">
style="width:100%"
</button>
We can Use a Block Level Full-Width Button
Create block level buttons—those that span the full width of a parent—by adding .btn-block.
<button type="button" class="btn btn-primary btn-lg btn-block">
Block level button
</button>
<button type="button" class="btn btn-secondary btn-lg btn-block">
Block level button
</button>

Resources