I am trying to align an ionicon icon and text to its side in a vertical way inside a button using Bootstrap 3.
I want the text to be vertically aligned to the center of the button with the icon also vertically aligned.
The default seems to align the icon and the text using their lowest points.
My HTML is:
<div class="col-xs-4 text-right">
<a href="/signout" class="btn btn-xs btn-danger sign-out">
<i class="ion-power ion-fw"></i>
<span class="menu-label">Sign Out</span>
</a>
</div>
My CSS is:
.main-header .navbar .dropdown-menu li a.sign-out {
color: #fff !important;
vertical-align: middle;
}
.ion-fw {
width: 1.28571429em;
text-align: center;
}
My CSS targets the pair OK as their colors are correct. I have tried padding and margins on .menu-label but that moves the text and icon.
How do I adjust the text to the middle of the icon's height? Thanks!
Example:
Change this
.ion-fw {
width: 1.28571429em;
text-align: center;
}
to this
.ion-fw {
width: 1.28571429em;
text-align: center;
vertical-align: middle;
}
vertical-align works on elements in the same container and so must be applied to the contents...not the parent element.
.ion-fw {
width: 1.28571429em;
text-align: center;
vertical-align: middle;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-xs-4 text-right">
<a href="/signout" class="btn btn-xs btn-danger sign-out">
<i class="ion-power ion-fw"></i>
<span class="menu-label">Sign Out</span>
</a>
</div>
This works for me:
.ion-fw {
display: inline-block;
vertical-align: bottom;
height: 1.4em;
font-size: 1.1em;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-xs-4 text-right">
<a href="/signout" class="btn btn-xs btn-danger sign-out">
<i class="ion-power ion-fw"></i>
<span class="menu-label">Sign Out</span>
</a>
</div>
Related
JS: http://jsfiddle.net/1cmtpnha/
I have this:
<div class="card-body">
<div class="padding_5">
<div class="icon-center">
<i class="fa fa-twitter" aria-hidden="true"></i>
</div>
</div>
</div>
padding_5 just contains padding css
card-body is bootstrap 4
I tried this:
[class*="icon-"] {
display: inline-block;
width: 100%;
}
.icon-center i {
text-align: center;
}
and this:
.icon-center {
margin: auto;
display: block;
}
and neither worked. Those were top checked responses i saw on SO.
What is wrong with my code?
I want it to be horizontally centered within a card-body.
It's not the bootstrap either. I tested it on a separate page with no css or card-body. Still same issue.
You have three options here
1. Parent: text-align: center
set text-align: center to body.
2. Child: display: block and text-align: center
set display: block to .icon-center.
3. Child display: block , set width and margin: auto
set display: block , width: 20px and margin: auto to .icon-center.
https://codepen.io/blackcityhenry/pen/WPexKp
.icon-center {
text-align:center;
}
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="card-body">
<div class="padding_5">
<div class="icon-center">
<i class="fa fa-twitter" aria-hidden="true"></i>
</div>
</div>
</div>
Just add text-align: center; to the parent card-body
.card-body{
border: 1px solid red;
text-align: center;
}
.padding_5 {
padding: 10px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" type="text/css" />
<div class="card-body">
<div class="padding_5">
<div class="icon-center">
<i class="fa fa-twitter" aria-hidden="true"></i>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.bundle.min.js"></script>
How to arrange nested divs next to each other using inline block or flex
I have 3 elements in my html.
One icon, and two text elements.
I want the icon to be on left and then the text on top of each other.
I am trying to do this using following html and css, which works partially. The icon is way down and I tried moving it up. More over , I think there is a better and right way of doing this.
body {
background-color: #D2D5DD;
}
.auto-complete-box {
display: inline;
margin-top: -20px;
}
.auto-complete-tag {
margin-top: 15px;
margin-left: 5px;
display: inline-block;
}
.tag-header {
text-transform: uppercase;
letter-spacing: 1px;
color: #0083cb;
}
.tag-description {
font-weight: 100;
font-size: 12px;
padding: 1px;
color: #798071;
margin-top: 3px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />
<div>
<div class="auto-complete-box">
<i class="fa fa-bars"></i>
</div>
<div class="auto-complete-tag">
<div class="tag-header">HeaderValue</div>
<div class="tag-description">Item description</div>
</div>
</div>
<div>
<div class="auto-complete-box">
<i class="fa fa-bars"></i>
</div>
<div class="auto-complete-tag">
<div class="tag-header">Header 2 </div>
<div class="tag-description">Item description</div>
</div>
</div>
Edit:
The divs are aligned and the icon also aligned to the div(s) on right side , probably by using stretch.
Use display:flex to wrap div and remove unnecessary code(inline/inline-block)
body {
background-color: #D2D5DD;
}
.wrap{
display:flex;
}
.tag-header {
text-transform: uppercase;
letter-spacing: 1px;
color: #0083cb;
}
.tag-description {
font-weight: 100;
font-size: 12px;
padding: 1px;
color: #798071;
margin-top: 3px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />
<div class="wrap">
<div class="auto-complete-box">
<i class="fa fa-bars"></i>
</div>
<div class="auto-complete-tag">
<div class="tag-header">HeaderValue</div>
<div class="tag-description">Item description</div>
</div>
</div>
<div class="wrap">
<div class="auto-complete-box">
<i class="fa fa-bars"></i>
</div>
<div class="auto-complete-tag">
<div class="tag-header">Header 2 </div>
<div class="tag-description">Item description</div>
</div>
</div>
Another simpler method to use when trying to align components in HTML is to make use of the table structure. Sometimes aligning of certain components don't work when using CSS because there might be a conflicting style element.
So a method I prefer using to ensure that my components are perfectly in line, is to encapsulate them in a table. In your case, put the icon and text element into 2 separate columns within a single row as shown in the code snippet below:
I hope this helps! :)
body {
background-color: #D2D5DD;
}
.auto-complete-box {
display: inline;
margin-top: -20px;
}
.auto-complete-tag {
margin-top: 15px;
margin-left: 5px;
display: inline-block;
}
.tag-header {
text-transform: uppercase;
letter-spacing: 1px;
color: #0083cb;
}
.tag-description {
font-weight: 100;
font-size: 12px;
padding: 1px;
color: #798071;
margin-top: 3px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />
<table>
<tr>
<td>
<div>
<div class="auto-complete-box">
<i class="fa fa-bars"></i>
</div></div>
</td>
<td>
<div class="auto-complete-tag">
<div class="tag-header">HeaderValue</div>
<div class="tag-description">Item description</div>
</div>
</td>
<tr>
<td>
<div>
<div class="auto-complete-box">
<i class="fa fa-bars"></i>
</div></div>
</td>
<td>
<div class="auto-complete-tag">
<div class="tag-header">Header 2 </div>
<div class="tag-description">Item description</div>
</div>
</td>
</tr>
</table>
Maybe this way:
.wrap{
display: flex;
align-items: center;
}
I'm creating a simple web page that will have three large buttons that should fill the entire page (ie, they span the width of the page, and should take up 33.3% of the height for each.) I have the width part working, but I am unsure how to get the heights part working. This is my code so far.
<html>
<link rel="stylesheet" href="./bootstrap.min.css"/>
<link rel="stylesheet" href="./bootstrap-theme.min.css"/>
<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"/>
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet" type="text/css">
<div class="container-fluid">
<div class="row">
<div class="jumbotron">
<span style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif">
<div class="col-md-4">
<i class = "fa fa-search fa-3x"></i>  <h1>Catalogue Search</h1>
</div>
<div class="col-md-4">
<i class = "fa fa-sign-in fa-3x"></i>  <h1>Library Account Login</h1></button>
</div>
<div class="col-md-4">
<i class = "fa fa-map-marker fa-3x"></i>  <h1>Hours and Locations</h1></button>
</div>
</span>
</div>
</div>
</div>
</html>
The following fiddle link may solve your problem.
Codes are as follows:
HTML:
<div class="container-fluid">
<div class="row">
<button id="catalogSearch" class="btn btn-default">Catalogue Search </button>
</div>
<br/><br/>
<div class="row">
<button type="button" id = "AccountLogin" class="btn btn-default">Library Account Login</button>
</div>
<br/><br/>
<div class="row">
<button id ="hrsLocations" class="btn btn-default btn-lg"> Hours and Locations</button>
</div>
</div>
CSS:
.btn
{
width:100%;
padding: 12%;
}
JavsScript
document.getElementById("catalogSearch").onclick = function () {
location.href = "https://encore.mpl.on.ca/iii/mobile/homepage?lang=eng";
};
document.getElementById("AccountLogin").onclick = function () {
location.href = "https://encore.mpl.on.ca/iii/mobile/homepage/Sdologin?lang=eng&loginReason=doDefault";
};
document.getElementById("hrsLocations").onclick = function () {
location.href = "http://www.mpl.on.ca/hours.php";
};
You haven't specified whether you are using Bootstrap v3 or v4.
You should only be using .col-*-* as children of .row elements or any other elements with a default margin: 0 -15px (such as .form-groups).
You are using .col-*-4 when you don't want your element to be 1/3 of their parent width.
You're using 3 <h1> tags on the same page, which is a very big no-no SEO-wise.
Probably the easiest way to achieve what you want, using Bootstrap v3 (which is what I assume you're using), would be using a .btn-block combined with flexbox. Please also note I've made significant changes to markup (yours is invalid).
body {
margin: 0;
}
div.jumbotron {
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
min-height: 100vh;
margin-bottom: 0;
}
.jumbotron > .btn-block {
display: flex;
flex-direction: column;
min-height: calc(100vh - 60px);
align-items: stretch;
}
.jumbotron > .btn-block > .btn {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#media (min-width: 768px) {
.jumbotron > .btn-block {
min-height: calc(100vh - 120px);
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="jumbotron">
<div class="btn-group-vertical btn-group btn-block">
<i class = "fa fa-search fa-3x"></i><h1>Catalogue Search</h1>
<i class = "fa fa-sign-in fa-3x"></i><h1>Library Account Login</h1>
<i class = "fa fa-map-marker fa-3x"></i><h1>Hours and Locations</h1>
</div>
</div>
</div>
</div>
Don't forget to autoprefix
Try this without bootstrap :)
// this will call the button and design
.button {
background-color: gray;
color: #FFFFFF;
padding: 10px;
border-radius: 10px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
margin:10px
}
// this will assign sizes of the button you want
.btnbig {
width: 99%;
height: 500px;
}
//btnbig is the class of your button you want to modify
<div class="button btnbig">A big button</div>
is this clear now ? im sorry (Im newbie)
If I understand the question correctly then you can use CSS calc to set the height as a percentage of the page.
.btn-block {
display: block;
width: 100%;
height: calc(100vh/3);
}
I'm trying to display the following code. It's working fine in Chrome and Safari, but not working properly in Firefox. The code link: http://jsfiddle.net/nvarun123/fv4jxo4t/26/
Html code:
<div>
<a>
<button (click)="decrement()" class="btn btn-primary" style="float: left;">
<span><i class="glyphicon glyphicon-minus" ></i></span>
</button>
</a>
<input type="text" style="width:45%;" [(ngModel)]="count">
<a>
<button (click)="increment()" class="btn btn-primary" style="float: right;">
<span><i class="glyphicon glyphicon-plus" ></i></span>
</button>
</a>
</div>
CSS code:
div {
position:relative;
border: 1px solid #CACEDB;
width: 162px;
height:38px;
overflow: hidden;
white-space: nowrap;
border-radius:3px;
}
input {
text-align: center;
height:100%;
width:100%;
border:none;
}
input:focus {
outline: none;
}
button {
height:100%;
border:none;
}
button:hover {
background-color: green;
}
button:active {
background-color: #015191;
}
Here a trick to vertically center content in an html element.
Vertical aligns are always tricky with CSS, but this snippet will allow you to do it easily for a single line of text. Basically we use the line-height property to put an equidistant space at the top and bottom of the text. To make this work, the line height value needs to be the same as the height of the html element containing the text.
Check this
p {
border: red dotted thin;
height: 100px;
text-align: center;
background: black;
color: white;
/* magic line */
line-height: 100px;
}
<p>
One Liner
</p>
div {
position: relative;
border: 1px solid #CACEDB;
width: 500px;
height: 500px;
overflow: hidden;
white-space: nowrap;
border-radius: 5px;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div>
<a>
<button type="button" (click)="decrement()" class="btn btn-primary" style="width:50px;height:50px;">
<span class="glyphicon glyphicon-minus"></span>
</button>
</a>
<span style="width:100%">1</span>
<a>
<button type="button" (click)="increment()" class="btn btn-primary" style="width:50px;height:50px;">
<span class="glyphicon glyphicon-plus"></span>
</button>
</a>
</div>
</body>
</html>
check out the snippet
Is this the way you want it?
You can try to style the input tag to set the value at the center by using text-align: center; this worked for me with your code.
div {
position:relative;
width: 160px;
height:38px;
overflow: hidden;
white-space: nowrap;
border-radius:5px;
}
input{
text-align: center;
}
input:focus {outline:none;}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
<div>
<a>
<button type="button" (click)="decrement()" class="btn btn-primary" style="height:100%; border:none;">
<span class="glyphicon glyphicon-minus"></span>
</button>
</a>
<input type="text" style="width:45%;border:none;">
<a>
<button type="button" (click)="increment()" class="btn btn-primary" style="height:100%;float: right; ">
<span class="glyphicon glyphicon-plus"></span>
</button>
</a>
</div>
</body>
</html>
I'm probably really sleepy and missing something easy here. I've tried every margin and padding available but I'm unable to lift a span (div or any other text containing element for that matter) that contains some text to the right level.
What's the best way to achieve this? In the fiddle below, i want to align it with the font-awesome icon.
.add-cohorts-button > a > i {
padding-top: 5px;
}
.add-cohorts-button > span {
padding-left: 8px;
/*any amount of bottom margin/padding doesn't work. Try it. Height didn't either */
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="col-xs-3 add-cohorts-button">
<a href="addcohort.form"><i class="fa fa-plus-square-o fa-2x" aria-hidden="true"></i>
<span>Hello from the other side </span></a>
</div>
Way out of this fix?
This Suits you?
Adjust the margin according to your icon height.
.add-cohorts-button > a > i {
padding-top: 5px;
}
.add-cohorts-button span {
position:absolute;
padding-left:8px;
margin-top:10px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="col-xs-3 add-cohorts-button">
<a href="addcohort.form"><i class="fa fa-plus-square-o fa-2x" aria-hidden="true"></i>
<span>Hello from the other side </span></a>
</div>
Use display: inline-block on your link so that it can fit its height to its contents, then use vertical-align: middle, like this:
.add-cohorts-button a {
display: inline-block;
}
.add-cohorts-button > * {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="col-xs-3 add-cohorts-button">
<i class="fa fa-plus-square-o fa-2x" aria-hidden="true"></i>
<span>Hello from the other side </span>
</div>
Version with the whole thing as a link:
.add-cohorts-button {
text-decoration: none;
}
.add-cohorts-button i {
display: inline-block;
}
.add-cohorts-button > * {
vertical-align: middle;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet"/>
<a class="col-xs-3 add-cohorts-button" href="addcohort.form">
<i class="fa fa-plus-square-o fa-2x" aria-hidden="true"></i>
<span>Hello from the other side </span>
</a>