HTML: What css attribute makes content in button "center"? - html

It's a weird and not that important question:
if I create a button tag in html, then type some text like "click me" or something
<button> Click me! </button>
then use css to give it width and height, like:
.myBtn{
width:100px;
height:100px;
}
The words "Click me!" will automatically be right in the center of my button.
I tried text-align:left;, the words will move to the left side,
which means I disable its horizon center
but I don't know how to make it not vertical center ?
put elements vertical center always has some solution to make it(like after/before, flex, margin...etc), but today I wanna make the contents not vertical center in button,
I totally have no idea how to do that execpt changing its top and left.
does any body know what attributes should I change to make it not vertical center?
btw, I just wanna know what basic attributes the button has to make its contents center, not the solution of put the content in button to the left-top, so please don't tell me use top, left, calc or something calculate stuff about changing contents' position, that will be great.

.myBtn{
width:100px;
height: auto;
padding-bottom:82px;
min-height:100px;
}
<button class="myBtn"> Click me! </button>
You can use min-height and padding-bottom

you can did this by adding a <span> tag to your <button> tag .I'm added the snippet below
button{
width:100px;
height:100px;
}
button > span {
display: inline-block;
vertical-align: top;
}
button:after {
content: "";
display: inline-block;
vertical-align: top;
height: 100%;
}
<button>
<span> <!-- Added wrapper -->
Click Me...!
</span>
</button>

Related

Trying to justify icons left while keeping text center in HTML

I'm trying to achieve a look where I have all of the icons justified to the left while keeping the text centered and have the text wrap onto another line if necessary. However, I want the icon to be inline with the title. Right now it looks like this:enter image description here
I'm not sure why some of the boxes are stretching vertically and why the icons are floating on the line above the text. Also, some of the text is shifting outside of the grey boxes they are supposed to be in. How do I get every icon/title to look like the top example (ie eye glyphicon with "Look Inside This Chapter" text)?
Here is the HTML code I'm using:
<div class="text-left"><span class="glyphicon glyphicon-download">
</span> <div class="text-center">Teacher Pre-Assessment</div></div>
I also tried this code to see if that would work but I'm not having any luck:
<div style="float: left; text-align: left"><span class="glyphicon
glyphicon-download"></span> <div class="text-center"> Teacher Post-
Assessment </div></div>
In that scenario, you can set the icon position to absolute and then left:0. However, that can make the icon just go outside the .text-left div. So you need to set its position to relative, because absolute elements are positioned according to the closest ancestor that has position relative.
.text-left {
position: relative
}
.text-left .glyphicon {
position: absolute;
left: 0;
}
Then you're free to position the other elements as you want.
You could put the icons into a pseudo class, so they don't clutter up your html:
.text-center {
padding-left: 25px;
position: relative;
}
.text-center:before {
position: absolute;
content:"";
top:0;
left:0;
width: 20px;
height: 20px;
background-image: url(http://icons.iconarchive.com/icons/paomedia/small-n-flat/1024/sign-check-icon.png) ;
background-size:100% 100% ;
}
this basically creates an absolute positioned div in .text-center , thats why .text-center needs some padding so the text doesn't run below the icon.
Example: https://jsfiddle.net/6mjscdxL/
More Details about pseudo classes: https://www.smashingmagazine.com/2011/07/learning-to-use-the-before-and-after-pseudo-elements-in-css/

How to perfectly vertical align navigation buttons with responsive image

I want to make the button always aligns vertically on the middle of the image responsively. I can make the image responsive using .img-responsive, however I can't make the arrow to be always on the middle of the image. I suspect the issue is because I can't make the height of the arrow's div to be equal the height of the image. Any way to do so?
Here is my jsFiddle..
PS: for those who can come up with better words please change the title.. ^^
CSS only solution. Using the display table and table-cell combo, you can achieve what you are looking for. I had never really tried it before, as far as I know, but searched around a bit and found a solution which gave me a good starting point to achieve what I needed.
The trick is to have a container which will possess the display table property. Inside that wrapper, you will have all your other elements, which will possess the table-cell property, in order to have them behave properly and stack themselves next to each other, as table-cell would to do.
By giving your table-cells a 100% height, they will adapt themselves to the height of the wrapper, giving you the chance to use the handy little table property going by the name: vertical align. Use the middle vertical align property to center perfectly your nav buttons.
Give your image the max-width 100% property for proper responsive behavior. But don't use bootstrap's own image responsive class because it contains css properties we don't want and that messes up our layout.
I reworked the html a bit, so that each element align perfectly, in the correct order.
WORKING EXAMPLE JSFIDDLE
HTML:
<div class="row">
<div class="col-xs-12">
<div class="image-container">
<div class="prev-btn nav-btn"> < </div>
<div class="inner-container">
<img src="http://farm9.staticflickr.com/8072/8346734966_f9cd7d0941_z.jpg" class="center-block">
</div>
<div class="next-btn nav-btn"> > </div>
</div>
</div>
</div>
CSS:
.image-container{
display:table;
height: 100%;
text-align:center;
}
.inner-container{
display:table-cell;
width: 100%;
height: 100%;
padding: 0 15px;
vertical-align: middle;
text-align: center;
}
.inner-container img{
width: 100%;
height: auto;
max-width: 100%;
}
.nav-btn{
font-size:50px;
font-weight:700;
font-family: monospace;
color: #000;
cursor:pointer;
}
.nav-btn:hover{
color: #B6B6B6;
}
.prev-btn{
position: relative;
height: 100%;
display: table-cell;
vertical-align: middle;
}
.next-btn{
position: relative;
height: 100%;
display: table-cell;
vertical-align: middle;
}
Here's a simple solution in Javascript/Jquery. The trick is to adjust the position of each NAV buttons according to the height of the image each time the browser id resized. Dividing the image height by 2 will get you the center of the image, aka the position where you will want your buttons to be. Using only this value will no be enough, you also need to get the center value of your nav buttons. Substracting each values will give you the real position value for your buttons. The ScreenResize function will then update the position each time the image is scaled responsively.
$(function(){
//Call On Resize event on load
screenResize();
//Bind On Resize event to window
window.onresize = screenResize;
});
function screenResize() {
//Adjust Nav buttons position according to image height
$('.nav_btn').css({
'top': (($('.center-block').height() / 2)-($('.nav_btn').height() / 2))
});
}
Also, change the line-height of your buttons to this, it will help:
.nav_btn p{
line-height: 1.25;
}
Finally, use Media-Queries to change buttons font-size and line-height if necessary. Also, like user Valentin said, using images for the nav buttons could also be easier, you wouldn't have to use media-queries.
Example JSFIDDLE

Positioning a button within an image in CSS

I would want to insert a button within an image using CSS; the image has a fixed width and height, centered in the page.
The button must remain stationary within the image at different resolution.
CSS doesn't allow other elements inside an <img> tag. My common go-to in this situation is put both your image and button inside two wrapper <div> elements that act like a table (vertically and horizontally centered). Like so;
HTML
<div class="wrapper">
<div>
<img src="[your image src]">
<a class="button" href="[go to page]">Button</a>
</div>
</div>
CSS
.wrapper{
display: table;
width: 100%; /* Add or remove this depending on your layout */
}
.wrapper > div{
display: table-cell;
vertical-align: middle;
text-align: center;
}
As long as the image has no size constraints this should sit flush in the center of the image :). Enjoy!
EDIT ---
Alternatively, remove the <img> tag and put it as a background-image of .wrapper and use background-size: cover; to center it :). Then you can control the height and width of the image really easily.
Remember to use vendor prefixes too. I have a handy list of them here although they are in Sass.
in your CSS:
myImage {
background-image:url('image.jpg');
background-size:cover;
}
be sure to give your button an ID
in your HTML
<button id='myImage' type='submit'> </button>
To put the button in the position where you want it, simply give it some margins:
.wrapper button {
margin-left:81%; margin-top:3%;
}
But I believe you want the div with the background image to never be larger than the window, right? In that case, you will need some more CSS: set a max-width and a max-height on the div, and then you will also need to set both html and body to a fixed width and height, otherwise max-width and max-height won't know what they ought to be relative to.
So, that's
html, body {width:100%; height:100%; margin:0; padding:0;}
.wrapper {
(..)
margin:0 auto;
max-width:100%; max-height:100%;
}
See updated fiddle.

Top-align text within button element?

I have some buttons that have varying numbers of lines of text, all with a fixed width and height. It seems that anything I put between the <button> </button> tags are vertically aligned to the middle by default. I want to vertically align the text within the button to the top, so that the first line of each button starts at the same line.
One workaround I found was this, but as you can see in this example (http://jsfiddle.net/d27NA/1/), with this approach, I have to manually designate the top property for buttons that contain texts of different lengths, else buttons that contain longer text will overflow the top.
I'm wondering if there is an alternate approach that will solve this problem in a more elegant way. Thanks.
html:
<div>
<button class="test">
This text is vertically center-aligned. I want this to be top-aligned.
</button>
</div>
css:
.test{
font-size:12px;
line-height:14px;
text-align:center;
height:100px;
width:100px;
}
Yup, just define the button as position:relative; and the span as position:absolute;top:0;left:0; and you're in business.
jsFiddle updated
UPDATE
So in the three years since this answer, flexbox has become more prominent. As such, you can do this now:
.test {
display: inline-flex; /* keep the inline nature of buttons */
align-items: flex-start; /* this is default */
}
This should give you what you're looking for. You may need to apply appearance: none; (with appropriate browser prefixes), but that should do it.
You just need to change the CSS of Button and Span. Separate both the CSS and make following changes:
button {
display: block;
position: relative;
}
span {
display: block;
position: absolute; //<-- Make it absolute
top: 0px; //<-- Set the top property accordingly. In this case 0px;
}
Set the position of span as absolute and set top property accordingly
Like PlantTheIdea's answer, this one adds a <span>, but is simpler and works across browsers.
<div>
<button class="test">
<span>
This text is vertically center-aligned. I want this to be top-aligned.
</span>
</button>
</div>
.test > span {
display: block;
height: 100%;
}
I would replace the BUTTON tag with A or DIV, and then style it to look like a button.

trying to align html button at the center of the my page [duplicate]

This question already has answers here:
How to center a button within a div?
(16 answers)
Closed 3 years ago.
I'm trying to align an HTML button exactly at the centre of the page irrespective of the browser used. It is either floating to the left while still being at the vertical centre or being somewhere on the page like at the top of the page etc..
I want it to be both vertically and horizontally be centered. Here is what I have written right now:
<button type="button" style="background-color:yellow;margin-left:auto;margin-right:auto;display:block;margin-top:22%;margin-bottom:0%">
mybuttonname
</button>
Here's your solution: JsFiddle
Basically, place your button into a div with centred text:
<div class="wrapper">
<button class="button">Button</button>
</div>
With the following styles:
.wrapper {
text-align: center;
}
.button {
position: absolute;
top: 50%;
}
There are many ways to skin a cat, and this is just one.
You can center a button without using text-align on the parent div by simple using margin:auto; display:block;
For example:
HTML
<div>
<button>Submit</button>
</div>
CSS
button {
margin:auto;
display:block;
}
SEE IT IN ACTION: CodePen
Edit by author: This is a really out of date answer! Flexbox or grid are much better.
I've really taken recently to display: table to give things a fixed size such as to enable margin: 0 auto to work. Has made my life a lot easier. You just need to get past the fact that 'table' display doesn't mean table html.
It's especially useful for responsive design where things just get hairy and crazy 50% left this and -50% that just become unmanageable.
style
{
display: table;
margin: 0 auto
}
JsFiddle
In addition if you've got two buttons and you want them the same width you don't even need to know the size of each to get them to be the same width - because the table will magically collapse them for you.
JsFiddle
(this also works if they're inline and you want to center two buttons side to side - try doing that with percentages!).
try this it is quite simple and give you cant make changes to your .css file this should work
<p align="center">
<button type="button" style="background-color:yellow;margin-left:auto;margin-right:auto;display:block;margin-top:22%;margin-bottom:0%"> mybuttonname</button>
</p>
Here is the solution as asked
<button type="button" style="background-color:yellow;margin:auto;display:block">mybuttonname</button>
There are multiple ways to fix the same. PFB two of them -
1st Way using position: fixed - position: fixed; positions relative to the viewport, which means it always stays in the same place even if the page is scrolled. Adding the left and top value to 50% will place it into the middle of the screen.
button {
position: fixed;
left: 50%;
top:50%;
}
2nd Way using margin: auto -margin: 0 auto; for horizontal centering, but margin: auto; has refused to work for vertical centering… until now! But actually absolute centering only requires a declared height and these styles:
button {
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
height: 40px;
}
For me it worked using flexbox.
Add a css class around the parent div / element with :
.parent {
display: flex;
}
and for the button use:
.button {
justify-content: center;
}
You should use a parent div, otherwise the button doesn't 'know' what the middle of the page / element is.
Place it inside another div, use CSS to move the button to the middle:
<div style="width:100%;height:100%;position:absolute;vertical-align:middle;text-align:center;">
<button type="button" style="background-color:yellow;margin-left:auto;margin-right:auto;display:block;margin-top:22%;margin-bottom:0%">
mybuttonname</button>
</div>​
Here is an example: JsFiddle
Make all parent element with 100% width and 100% height and use display: table; and display:table-cell;, check the working sample.
Working Git Version
<!DOCTYPE html>
<html>
<head>
<style>
html,body{height: 100%;}
body{width: 100%;}
</style>
</head>
<body style="display: table; background-color: #ff0000; ">
<div style="display: table-cell; vertical-align: middle; text-align: center;">
<button type="button" style="text-align: center;" class="btn btn-info">
Discover More
</button>
</div>
</body>
</html>