How can I keep a button horizontally centered with an input if they have different font sizes? - css

I have an input box for text and directly next to it is a button. The problem is when the button font-size and input box font-size is different, the button won't be correctly aligned.
Ex - different font-sizes. Notice the bottom of the button extends past the input box much more than the top
input{
font-size:20px;
}
.btn {
padding: 8px 12px;
font-size: 14px;
border-radius: 2px;
}
<input>
<button class="btn btn-primary m-l">Add</button>
Ex: Same font sizes: button is aligned:
input{
font-size:20px;
}
.btn {
padding: 8px 12px;
font-size: 20px;
border-radius: 2px;
}
<input>
<button class="btn btn-primary m-l">Add</button>

I'm not sure this is the best solution, but you can achieve this by giving the input a height, and vertically aligning both elements to the middle. See snippet below.
input{
font-size:20px;
height: 30px;
vertical-align: middle;
}
.btn {
padding: 8px 12px;
font-size: 14px;
border-radius: 2px;
vertical-align: middle;
}
<input>
<button class="btn btn-primary m-l">Add</button>

Use CSS Flexbox. And apply align-items: center to make your child <div>s vertically centered. In my case I've used body as my parent element
Have a look at the snippet below:
input{
font-size:20px;
}
.btn {
padding: 8px 12px;
font-size: 14px;
border-radius: 2px;
margin-left: 5px;
}
body {
display: flex;
align-items: center;
}
<input>
<button class="btn btn-primary m-l">Add</button>
Hope this helps!

Simply wrap your content into a .container div and apply dispaly: flex to it and that should do the trick for you.
.container {
display: flex;
}
input{
font-size:20px;
margin-right: 5px;
}
.btn {
padding: 8px 12px;
font-size: 14px;
border-radius: 2px;
}
<div class="container">
<input>
<button class="btn btn-primary m-l">Add</button>
</div>
Hope helps (y).

Related

css - how to center the span text in rounded button [duplicate]

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 1 year ago.
CodeSandbox:
https://codesandbox.io/s/eloquent-haibt-1bnib?file=/src/main.js
I want to center the - text in the button, but I cannot find a way to do it.
html
<button class="round-button align-middle mr-1">
<span>-</span>
</button>
css
.round-button {
min-width: 20px;
max-height: 20px;
text-decoration: none;
display: inline-block;
outline: none;
cursor: pointer;
border-style: none;
color: white;
background-color: #3498db;
border-radius: 100%;
overflow: none;
text-align: center;
padding: 0;
}
.round-button:before {
content: "";
display: inline-block;
vertical-align: middle;
}
html
<button class="round-button align-middle mr-1">-</button>
css
.round-button {
min-width: 20px;
height: 20px;
border-style: none;
color: white;
background-color: #3498db;
border-radius: 50%;
text-align: center;
padding: 0;
line-height: 20px; // to center text vertically
}
You just need to add the same line-height as your button's height and don't need an extra span element to add text. I've also removed unnecessary styles.
Try setting line-height: 20px to that. If it still looks off, you might be using a custom font with non-standard line height. In this case play with the line-height property until it looks okay.
Add the following style properties to .round-button:
.round-button {
display: flex;
align-items: center;
justify-content: center;
}
And, remove style for .round-button:before.
Try this.
.round-button {
background-color: #3498db;
border-style: none;
border-radius: 100%;
color: #fff;
cursor: pointer;
aspect-ratio: 1 / 1;
width: 48px;
display: flex;
align-items: center;
justify-content: center;
}
<button class="round-button">
<span>-</span>
</button>
Try changing <span>-</span> to <span style="position:relative; left:0px; top:-3px">-</span>. If it doesn't look right you can play around with it.

Checkbox cross mark alignment fix

I need to display a cross mark inside unchecked checkbox. I have written a code like this:
HTML:
<input type="checkbox" class="cls-1"/>
CSS:
.cls-1:not(:checked):after {
content: '\00D7';
margin: 0px 0px 0px 2px;
}
The alignment of cross-mark is not correct though. There appears a space between checkbox top and cross mark top. Is there a way to make cross mark appear exactly at center.
Stackblitz for sample code can be found here: https://stackblitz.com/edit/js-ul1sgb
try this
h1, h2 {
font-family: Lato;
}
.cls-1:not(:checked):after {
content: '\00D7';
margin: 0px 0px 0px 2px;
position: absolute;
top:-2px;
}
.cls-1 {
position:relative
}
<div id="app1">
<input type="checkbox" class="cls-1"/>
</div>
can you try adding a line-height property to the input type checkbox,
say 10px;
eg:
input {
line-height: 10px;
}
Just add display: flex; align-items: center; in .cls-1. Thanks
.cls-1 {
display: flex;
align-items: center;
}
I've modified your css to center the cross. Use line-height of 0.5rem to achieve this.
h1, h2 {
font-family: Lato;
}
.cls-1:not(:checked):after {
content: '\00D7';
margin: 0px 0px 0px 2px;
display: inline-block;
vertical-align: text-top;
line-height: 0.5rem;
}
<div id="app1">
<input type="checkbox" class="cls-1"/>
</div>

css - auto strech buttons horizontally

I'm trying to figure out how to use this w3schools example
How do I modify it, so that buttons stretch to fill the entire page width?
I want to add buttons dynamically but don't know their width in advace.
Here it is in JSFiddle
The relevant clipping of the CSS code:
.tablink {
background-color: #555;
color: white;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
font-size: 17px;
width: 15%; // <------ hardcoded width
}
You could use flex. Add a wrapping div set to display: flex and add flex-basis: 100% to .tablink.
.tab-container {
display: flex;
width: 100%;
}
.tablink {
background-color: #555;
color: white;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
font-size: 17px;
flex-basis: 100%;
}
<div class="tab-container">
<button class="tablink" onclick="openPage('Home', this, 'red')">Home</button>
<button class="tablink" onclick="openPage('News', this, 'green')" id="defaultOpen">News</button>
<button class="tablink" onclick="openPage('Contact', this, 'blue')">Contact</button>
<button class="tablink" onclick="openPage('About', this, 'orange')">About</button>
</div>
Here's a great flexbox cheatsheet.
You can use flexbox to achieve this. Since the elements tabs getting used is adding dynamically you should remove the hardcoded width on it like mentioned in your comment and add a parent div to the buttons with property display:flex.
Replace your HTML part where the buttons are rendered with this
<div class="tabBar">
<button class="tablink" onclick="openPage('Home', this, 'red')">Home</button>
<button class="tablink" onclick="openPage('News', this, 'green')" id="defaultOpen">News</button>
<button class="tablink" onclick="openPage('Contact', this, 'blue')">Contact</button>
<button class="tablink" onclick="openPage('About', this, 'orange')">About</button>
</div>
And add this to css
.tabBar {
display: flex;
flex-wrap: wrap;
}
.tablink {
background-color: #555;
color: white;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
font-size: 17px;
flex-basis: 0;
flex-grow: 1;
max-width: 100%;
}
A working demo is here
Changing the width to 25% will do the trick.

Is there a way to center a text vertically regardless of font-family or font-size?

I am looking for a font-agnostic way of centering text in a div. I have a button style fixed height div. And i want to center the text in it (one line only) vertically. The problem is that when i change the font-family, some fonts do not center vertically. Is there a way to compute the desired lineheight based on font metrics?
Try this solution, hope it works for you.
.content{
align-items: center;
display: flex;
justify-content: center;
height: 150px;
}
<div class="content">
<span>Hello world</span>
</div>
You can set line-height equal to the height of the button.
div {
height: 50px;
line-height: 50px;
border: 1px solid;
}
<div>Button</div>
Try this, im not using line height:
button {
height: 40px;
display: table;
}
button>span {
display: table-cell;
text-align: center;
vertical-align: middle;
}
button.one {
font-family: "Helvetica Neue";
font-size: 30px;
}
button.two {
font-family: sans-serif;
font-size: 18px;
}
button.three {
font-family: Helvetica;
font-size: 18px;
}
button.four {
font-family: Arial;
font-size: 20px;
}
button.five {
font-family: Times New Roman;
font-size: 30px;
}
<button class="one"><span>text</span></button>
<br>
<br>
<button class="two"><span>text</span></button>
<br>
<br>
<button class="three"><span>text</span></button>
<br>
<br>
<button class="four"><span>text</span></button>
<br>
<br>
<button class="five"><span>text</span></button>
If the text size extends the height of the button then you wil get a failed result which should be obvios, since the button has a fixed height.

CSS layout - what is causing additional space

I have created the following jsFiddle to demonstrate my problem (or lack of understanding more like)
http://jsfiddle.net/gRCS6/
And Code here
<div id="scoreboard"></div>
<canvas id="game">
Your browser does not support canvas.
</canvas>
<div id="controls">
<button type="submit" id="newGame">New</button>
<button type="submit" id="pause">Pause</button>
<button type="submit" id="help">Help</button>
</div>
#game {
border: 1px solid #000000;
background-color:#333333;
width: 250px;
margin:0px;
}
#scoreboard {
border: 1px solid #000000;
background-color:#333333;
color: orange;
width: 250px;
height: 40px;
font:36px arial,sans-serif;
text-align: right;;
}
#controls {
margin-top: -5px;
padding:0px;
}
button {
border: 1px solid #000000;
margin-left:0px;
background-color:#333333;
color: orange;
width:82px;
height: 40px;
}
Why does the div with id "controls" need a margin-top of -5px to make it touch the canvas above it?
What is taking up that 5 pixels?
What is stopping the 3 buttons from being next to each other with no space between them?
"Why does the div with id "controls" need a margin-top of -5px to make it touch the canvas above it?"
Like ralph.m pointed out, can be fixed by adding
canvas {
display: block;
}
"What is stopping the 3 buttons from being next to each other with no space between them?"
Well, since there are spaces (the character ' ') between the button elements in the html code you will see those spaces between the buttons when the page is displayed. You can either remove the spaces:
<button type="submit" id="newGame">New</button><button type="submit" id="pause">Pause</button><button type="submit" id="help">Help</button>
Instead of
<button type="submit" id="newGame">New</button>
<button type="submit" id="pause">Pause</button>
<button type="submit" id="help">Help</button>
Or you can try to fix it with css styling, for example by adding float: left; to the button selector.
The canvas element is display: inline (or is it inline-block?) by default, which means by default there is a gap at the bottom so that it will align with the baseline of any text beside it.
You can change this by setting the canvas to display: block or vertical-align: bottom.
It's a similar problem with the buttons, which are display: inline-block, meaning that there is space between them (as there is a natural space between words). As mentioned in the chosen answer, removing the white space is an option, but a more elegant solution is as follows:
#controls {word-spacing: -2em; display: table; width: 100%;}
button {word-spacing:0;}
Answer to Q1: Check this topic. Different browsers have different algorythm, so you should some extra parameters for body css.
body
{
margin: 0;
padding: 0;
}
Answer to Q2: Avoid using to close button tab. is not necessary, if you remove it, the margin between buttons will disappear. http://jsfiddle.net/gRCS6/5/
<button type="submit" class="button">New
<button type="submit" class="button">Pause
<button type="submit" class="button">Help
Another way to fix the issue is to use absolute positioning to define the exact placement of your controls div. Then you have to ability to define the exact alignment of the buttons regardless of display: inline-block; or display: block; commands.
http://jsfiddle.net/gRCS6/34/
#game {
border: 1px solid #000000;
background-color:#333333;
width: 250px;
height: 120px;
margin:0px;
position: absolute;
}
#scoreboard {
border: 1px solid #000000;
background-color:#333333;
color: orange;
width: 250px;
height: 40px;
font:36px arial,sans-serif;
text-align: right;
}
#controls {
position: absolute;
top: 172px;
margin-top: 0px;
padding: 0px;
}
button {
border: 1px solid #000000;
margin:0px;
background-color:#333333;
color: orange;
width:81.5px;
height: 40px;
}

Resources