How to vertically center a button in a container - css

How do I vertically center my view button within the box container.
I tried styling the button with vertical-align:center; but it didn't work.
I tried adding: position: relative; top: 50%; transform: perspective(1px) translateY(-50%);.
But this just makes half of the button appear on top of the container and the other half inside the container
View:
<div class="container signup2">
<div class="title container">
<h2> Enrolled Courses: </h2>
</div>
<div class="container box">
<div class="row col-centered">
<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 index">
<h3><%= course.name %></h3>
<p><%= course.description %></p>
</div>
<div class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
<%= link_to "View", course_path(course), class: "btn button buttonmargin align" %>
</div>
</div>
</div>
</div>
custom.scss:
.signup2 {
margin-top: 120px;
}
.title {
margin-bottom: 15px;
}
.box {
background-color: #f7f7f7;
margin-bottom: 20px;
}
.index {
text-align: left;
}
.align {
position: relative;
top: 50%;
transform: perspective(1px) translateY(-50%);
}
.button {
background-color: #20B2AA;
color: white;
}
.buttonmargin {
margin-left: 150px;
}
This is how it looks like

Try using flex.
<div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 align"> //moved align class here
<%= link_to "View", course_path(course), class: "btn button buttonmargin" %>
</div>
And css for align :
.align {
display: flex;
justify-content: center;
align-items: center;
}
Here is codesandbox link

Shubham's answer is almost correct. However, the css class that needs to have a display:flex property is actually on the row itself. Instead of modifying the CSS, Bootstrap 4.0 has a built-in class called .d-flex that you can pair with .align-items-center and it will work. See examples below:
With no defined height properties:
<div class="container">
<div class="row d-flex align-items-center">
<div class="col-12">
Anything in here will be vertically centered
</div>
</div>
</div>
With a defined height property on the container:
<div class="container d-flex align-items-center" style="height:500px">
<div class="row">
<div class="col-12">
Anything in here will be vertically centered
</div>
</div>
</div>

Related

Push the right aligned element down when the first left element is too long

I'm trying to achieve this, where brown gets pushed down when pink is too long, and pink is dynamically sized based on its text content.
I was only able to achieve this using javascript and two templates, if pink length is over X use second template, but I'm wondering if there's a way to achieve this using CSS only?
I tried meddling with grid auto-fill/auto-fit with minmax, float, flex with wrap, but I couldn't get to the desired result with these.
.parent {
width: 170px;
}
.flex {
display: flex;
}
div {
outline: 2px solid rgba(255, 0,0, 0.3);
}
<div>
<p>scenario A</p>
<div class="parent flex">
<div>
<div class="one">A short title</div>
<div class="three">Some text here</div>
<div class="three">some more text</div>
</div>
<div>
<button>A button</button>
</div>
</div>
</div>
<div>
<p>scenario B</p>
<div class="parent">
<div class="one">Testing a very long title</div>
<div class="flex">
<div>
<div class="three">Some text here</div>
<div class="three">some more text</div>
</div>
<div>
<button>A button</button>
</div>
</div>
</div>
</div>
<p>can a component achieve both a and b with only css?
I found a solution, it requires having fixed height in the first row, and the right side button will basically overflow when needed.
Will wait a bit before accepting my own solution in case someone came with a better one.
.container {
width: 300px;
display: flex;
gap: 10px;
padding: 16px;
font-size: 14px;
font-family: Arial;
}
.text {
flex: 1;
min-width: 0;
}
.first-row {
height: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.image {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: #444;
}
.title {
flex-grow: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<h2>Long title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Test title test title test title</div>
<button>Visit store</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>
<h2>Short title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Short title</div>
<button>Visit place</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>
<h2>Very long title</h2>
<div class="container">
<div class="image"></div>
<div class="text">
<div class="first-row">
<div class="title">Test title test titleTest title test titleTest title test title</div>
<button>Visit place</button>
</div>
<div class="three">in the stores</div>
<div class="three">sponsored</div>
</div>
</div>

Place Font-Awesome-5 Icon as Bootstrap-4 Card background

I want to use a Font Awesome 5 icon as card background image using Bootstrap-4!
I tried everything what I found from the internet but nothing worked.
I'm using Angular2 (v8) along with Bootstrap 4.
The following code worked for me. I've simplified what I use but you still get a font-awesome icon as background and you can put text on top of it:
HTML:
<div class="container-fluid">
<div class="row">
<div class="col-md-4 col-sm-6 d-flex">
<div class="class-box">
<div class="bg-primary">
<i class="fa fa-question big-centered-icon"></i>
</div>
<div class="box-content">
<h3 class="title-bg text-outline"><Strong>TITLE</Strong></h3>
<h4 class="title-bg text-outline">Subtitle</h4>
<h4 class="title-bg text-outline"><Strong> Subtitle</Strong></h4>
</div>
</div>
</div>
<div class="col-md-4 col-sm-6 d-flex">
<div class="class-box">
<div class="bg-primary">
<i class="fa fa-question big-centered-icon"></i>
</div>
<div class="box-content">
<h3 class="title-bg text-outline"><Strong>HELLO</Strong></h3>
<h3 class="title-bg text-outline"><Strong> 2nd HELLO</Strong></h3>
<h3 class="title-bg text-outline"><Strong> 3rd HELLO</Strong></h3>
</div>
</div>
</div>
</div>
</div>
CSS:
.big-centered-icon {
font-size: 144px;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: flex; /* add */
justify-content: center; /* add to align horizontal */
align-items: center; /* add to align vertical */
}
.class-box{ text-align:center;position:relative;margin:8px;width: 100%;padding-bottom: 75%}
div.class-box > div { position:absolute;top:0;bottom:0;left:0;right:0;overflow:hidden;padding-top:15%}
.class-box .title-bg{font-size:24px;color:#000;}
See the working fiddle.

How to make inline images responsive in smaller screens

I'm looping through some images in my rails app"
<% #attachments.each do |attachment| %>
<div class="img-thumbnail" style="display:inline-block; margin:auto; text-align: center; padding-top: 15px; width: 120px;">
<%=image_tag attachment.images_url(:thumb).to_s %>
<div class="desc">
<%= link_to "Remove", remove_image_path(attachment), data: { confirm: "Are you sure you want to delete this image?" }, :method => :delete, :style => "text-decoration: none !important" %>
</div>
</div>
<% end %>
The images appear inline and they start from the left side which is exactly what I wanted. Now I'm wondering if there is a way I can make the image take up the 100% width only when the screen is smaller?
I created a dummy snippet for help.
.form-border {
padding-bottom: 20px;
padding-top: 20px;
border: 1px solid lightgrey;
border-radius: 8px;
background-color: white;
}
div.desc {
padding: 15px;
text-align: center;
}
<div class="container">
<div class="row">
<div class="col-md-8 offset-md-2">
<div class="container form-border">
<div class="img-thumbnail" style="display:inline-block; margin:auto; text-align: center; padding-top: 15px; width: 120px;">
<img src="pic_trulli.jpg" alt="Italian Trulli">
<div class="desc">Remove</div>
<img src="pic_trulli.jpg" alt="Italian Trulli">
<div class="desc">Remove</div
</div>
</div>
</div>
</div>
</div>
Update A
I followed #agustin answer by doing the doing:
<div class="img-thumbnail" style="display:inline-block; margin:auto; text-align: center; padding-top: 15px; width: 120px;">
<div class="img-fluid">
<%=image_tag attachment.images_url(:thumb).to_s %>
</div>
</div
or
<div class="img-thumbnail" style="display:inline-block; margin:auto; text-align: center; padding-top: 15px; max-width: 100%;;">
<div class="img-fluid">
<%=image_tag attachment.images_url(:thumb).to_s %>
</div>
</div
but I still get the same result:
You have errors in your layout.
Remember that you shouldn't create another .container element inside a col or row element. The correct way is .container > .row> .col, and if you want columns inside a column, then you have to create another .row with .cols inside, but not another container.
The img-fluid class has to be applied to the img element. You will probably want to define the width of the image though.
Check the following snippet. I created two columns with the class col-md-6, which means that, on medium screens (≥768px), those columns will take 50% of the width of the row each. As I didn't define any class with col-sm or col-xs, those columns will take 100% of the width of the row on small screens.
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-8 offset-md-2">
<div class="row">
<div class="col-md-6">
<div class="img-thumbnail">
<img src="https://placehold.it/400x300/" class="img-fluid" alt="Italian Trulli">
<div class="desc">Remove</div>
</div>
</div>
<div class="col-md-6">
<div class="img-thumbnail">
<img src="https://placehold.it/400x300/" class="img-fluid" alt="Italian Trulli">
<div class="desc">Remove</div>
</div>
</div>
</div>
</div>
</div>
</div>

Align two buttons at the bottom of the page

I have to align two buttons at the bottom of the page, but I can not do it. One is the button "gallery" and the other is icon linkedin.
<div class="container fill">
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<div class="item">
<div class="fill" style="background-image:url('http://www.mysite/images/category/image.jpg');background-position: center;">
<div class="container">
<div class="carousel-caption">
<h1>TITLE</h1>
<p class="lead">Description</p>
</div>
</div>
<div class="pull-right">
<a class="btn btn-large btn-primary" href="categories.php?id=17">View Gallery</a>
</div>
</div>
</div>
</div>
<div class="pull-left">
<div class="social-caption">
<button class="btn btn-large btn-linkedin">
<i class="icon-linkedin"></i><span><a style="color:white" vhref="http://www.linkedin.com/"> | Linkedin</span></button>
</div>
css for the two buttons:
.social-caption {
background-color: transparent;
position: relative;
max-width: 100%;
padding: 0 20px;
bottom: 45px;
left: 5px;
}
.gallery-button {
background-color: transparent;
position: relative;
max-width: 100%;
padding: 0 20px;
bottom: 45px;
right: 5px;
}
The two buttons do not appear aligned. This problem is seen especially with mobile devices
Solutions? Thanks
You could do something like THIS, for example. It uses position:absolute to locate the buttons at the bottom of the page's content (bottom:0) - or relative to their parents positioning (in this case the body. The alternative would be to use position:fixed which would ensure the buttons appear at the bottom of the viewport at all times.
See the difference between absolute positioning and fixed positioning.
HTML
<button>Button 1</button>
<button>Button 2</button>
CSS
body{
position:relative;
}
button{
position:absolute;
bottom:0;
}
button:last-child{
left:100px;
}
May be you could use this:
<input type='button' style='position:fixed; top:94%; left:82%; width:100px' value='
this means the button will appear at the right corner in the bottom of your page.
//hope so this code helps you.
.center{
justify-content: center;
display:flex;
align-item: center
}
.right{
justify-content: flex-end;
display:flex;
}
//for center align
<div class="center">
<button>First Button</button>
<button>Second Button</button>
</div>
//for right align
<div class="right">
<button>First Button</button>
<button>Second Button</button>
</div>

Twitter Bootstrap Banner - How to render

This is probably a pretty basic question, but I have a banner with an image on the left and text on the right. Under the banner is just a block of color. When the page gets smaller, my expectation is that the bits in the banner would stack (maintaining the background color for both) and the block of color (class="blue-line") would fall beneath them.
Here is the mark-up:
<section>
<div class="row header">
<div class="col-sm-6">
<img src="../images/logo.png" height="100px" />
</div>
<div class="col-sm-6 title">
<h2>Some Title Text</h2>
</div>
</div>
<div class="row">
<div class="col-sm-12 blue-line"></div>
</div>
</section>
and the css
.header {
background-color: #F2EBCC;
border: 10px solid #F2EBCC;
height: 120px;
}
.row > .title {
text-align: right;
top: 45%;
}
Thanks in advance!
JSFiddle: http://jsfiddle.net/3n6Kd/
try this:
<section>
<div class="row header">
<div class="col-sm-6">
<img src="../images/logo.png" height="100px" />
</div>
<div class="col-sm-6 title">
<h2>Some Title Text</h2>
</div>
</div>
<div class="row">
<div class="col-sm-12 blue-line"></div>
</div>
and:
.header {
background-color: #F2EBCC;
height: 120px;
}
.title {
text-align: right;
display: block;
padding: 10px;
}
.blue-line {
margin-top:10px;
height: 15px;
background-color: blue;
}
the text go under the first column not the blue-line, but it seems to appear above the blue-line so try it in your computer because some time jsfiddle.net don't show code correctly.
hope it will help you.

Resources