Text in button with icon is not centered vertically - html

I am trying to create a button that has an icon, but when I add the icon the text isnt centered vertically. How can I fix this?
This is the code in HTML & CSS:
<a href="#">
<button class=" account signUp"><span class="icon-profile</span>button</button>
</a>
.signUp {
background-image: var(--orange-background);
border-image: var(--orange-background);
font-family: poppins;
font-weight: 600;
color: white;
}

but when I add the icon the text isnt centered vertically
Put the following two properties on its parent
.parent-of-icon-and-text {
display: grid;
place-content: center;
}
Please don't use a button and a link, choose one that best fits your scenario.
To use a button as a link, you can put it in a form.
.signUp {
background-image: var(--orange-background);
border-image: var(--orange-background);
font-family: poppins;
font-weight: 600;
color: white;
}
<form onsubmit="#">
<button type="submit" class="account signUp"><span class="icon-profile"></span>button</button>
</form>

Corrections
It is invalid HTML to place a <button> inside an <a>nchor. They are both interactive content and should never have inertactive content as a descendant node. <a>nchor has been removed. For more details refer to Can I nest a <button> element inside an <a> using HTML5?.
Typo in HTML, "> missing:
<span class="icon-profile"></span>
In CSS the font-family value of Poppins was misspelt as poppins (font-family values are case-sensitive).
Solution
The OP was incomplete so what is suggested in the example is as generic as possible. In the OP, span.icon-profile needs these two styles:
display: inline-block;
vertical-align: middle
vertical-align will set the tag's contents to a vertical position by either a pre-set value or a legnth value.
display: inline-block or table-cell is required by vertical-align
Further details are commented in the example below
/*
The actual CSS to resolve alignment issues explianed by OP is marked with a ✼ which are `display: inline-block` and `vertical-align: middle`
*/
#import url('https://fonts.googleapis.com/css2?family=Poppins:wght#300&display=swap');
/*
Global default for font
*/
:root {
font: 2ch/1 Poppins;
}
/*
Any rem unit measurements will reference 1rem to 2ch
*/
body {
font-size: 2ch;
}
button,
b {
display: inline-block; /*✼*/
font-weight: 300;
}
.sign-up {
font: inherit;
border-radius: 4px;
color: white;
background: #333;
}
.btn-link:hover {
outline: 1px solid cyan;
color: cyan;
cursor: pointer;
}
.btn-link:active {
outline: 2px solid cyan;
color: black;
background: white;
}
.icon-profile {
font-size: 1rem;
vertical-align: middle; /*✼*/
}
/*
content: '⚙️'
in HTML it's ⚙️
*/
.icon-profile::before {
content: '\002699\00fe0f';
}
<button class="account sign-up btn-link"><b class="icon-profile"></b> Profile</button>

Related

Why inside form button size and outside form button size is different after same style?

Style is same but inside form button size and outside form button size is not same. Outside form button text-content apply extra padding around it. Same issue with a tag. Why this is happening? How to solve it? Also for button user agent stylesheet override my font. How to fix it?
#import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght#400;700&display=swap');
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body,
html {
font-family: 'Noto Sans JP', sans-serif;
font-size: 16px;
color: #333;
}
.container {
margin: 30px auto;
padding: 25px;
border: 1.5px solid #e6e6e6;
box-shadow: 0 0 2px 0 rgba(0, 0, 0, 0.15);
border-radius: 6px;
}
a {
text-decoration: none;
}
.btn {
background-color: #fff;
font-size: 0.9em;
font-weight: 700;
padding: 10px 22px;
border-radius: 5px;
}
.btn--orange {
color: #e99d0f;
border: 1px solid #e99d0f;
}
.btn--red {
color: #ff2727;
border: 1px solid #ff2727;
}
.section-info {
width: 60%;
}
.section-info img {
width: 100%;
margin-top: 10px;
}
.section-info__nav {
display: -webkit-box;
display: flex;
margin-top: 10px;
}
.section-info a {
margin-right: 10px;
}
<section class="container section-info">
<div class="section-info__nav">
Edit
<button class="btn btn--red">Delete</button>
<form action="#" method="POST">
<button class="btn btn--red">Delete</button>
</form>
</div>
</section>
In the first look, it could be a bit confusing but if you look at the style inheritance with more attention you will find out a little difference between them.
Lets get into it step by step
As we can see there is display: flex; attribute within the provided style.
.section-info__nav {
display: -webkit-box;
display: flex;
margin-top: 10px;
}
As we know flex will only affect the direct children of a div, so here what we got:
Edit
<button class="btn btn--red">Delete</button>
<form action="#" method="POST">
...
</form>
There are three direct children to the provided div (a, button, form). The other button within the form won't take effect of the flex display since the form itself got display block by default.
Why this is happening at all?
As we know flex display in the default situation will stretch the content to match the exact height (There is 44px available in section-info__nav, so each button height with display flex will be 44px). But when we got a display block, all items with this kind of display will put in the document just by their normal form and size, so since the button class is:
.btn {
background-color: #fff;
font-size: 0.9em;
font-weight: 700;
padding: 10px 22px;
border-radius: 5px;
}
the sum of the padding, border, and font-size will be 34px (10px lower than actually available height in the div). So the button will add at the beginning of div and in comparison with other buttons, it will look likes a dumb.
NOTE: In order to prevent items from fitting the entire available space in your div you can control them by align-items attribute. But in your particular case, since <a> don't have a default line-height attribute you should add specific line-height attribute to your .btn class in order to align all of your items properly.
How to fix this?
Simply add flex display to your form like this:
form {
display: flex;
}
because your form is not flex.you should just add cod below in your form css:
display: flex;

Silently hiding icon if the image can't be loaded

On the website that I'm working on, there is a menubar which includes several options with icons next to them. These icons are purely decorative, and don't need alt text as a result.
Unfortunately, the images have styles - a margin set which correctly aligns them in the menubar, which is 30px high.
This is the code for the button itself (the icon is 32x32px, but is resized down to 16x16px):
<a class="button" href="#">
<img src="images/crosshair.png" alt="">
<span>Track and Trace</span>
</a>
The CSS:
.button {
display: block;
padding: 3px 8px;
background: #343434;
text-decoration: none;
color: #ececec;
font-size: 0; // used by inline-block
}
.button img {
display: inline-block;
vertical-align: top; // needed to align the image properly; I couldn't find a better way.
height: 16px;
border: 0;
font-size: 16px;
margin: 4px 6px 4px 0;
}
.button span {
display: inline-block;
vertical-align: top;
font-size: 16px;
}
.button:hover {
background: #2E2E2E;
}
When the image doesn't load, this has the effect of leaving a space because of the margin.
What do other people do when they want to have an icon disappear completely if it can't be loaded?
You could make use of the "onerror" attribute of the the "img" element to call a function that sets display: none on the outer "a" element.
See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img

Button display inline CSS

I have the following CSS and HTML: http://jsfiddle.net/47w0h73r/6/
.one {
padding: 20px;
background: #f00;
}
.two {
padding: 20px;
background: #00f;
}
a,
button {
font-family: Arial, Helvetica;
font-size: 16px;
padding: 10px;
background: #fff;
color: #000;
display: inline;
border: 0;
text-decoration: none;
}
<div class="one"></div>
<div class="two">
Link
<button>Button</button>
</div>
As you will notice, the button doesn't appear as inline. Why is this? How can I make this button inline, just like its sibling a?
Issue
By changing the button to an a you will notice that the display: inline makes the padding of the parent element to ignore the padding of both child elements, making them really display inline. The problem, is that the button tag doesn't really appear inline, which makes the parent element's padding push both elements down. How can I fix this?
Trying to set a button to display:inline seems to cause some confusion. The inability to get display:inline behaviour is often attributed to it being a replaced element, but that is incorrect. <button> is not a replaced element.
In fact, the HTML5 specification, Section 10.5.2 The button element makes this requirement:
When the button binding applies to a button element, the element is
expected to render as an 'inline-block' box rendered as a button whose
contents are the contents of the element.
The language is a little unusual, but the button binding does apply, and the effect is that the binding takes precedence over the specified value of the display property. The effect is that the button is always rendered as display:inline-block, no matter what its specified value is. There is nothing you can do about it.
Add line-height:17px; to a, button and that should make them the same:
.one {
padding: 20px;
background: #f00;
}
.two {
padding: 20px;
background: #00f;
}
a,
button {
font-family: Arial, Helvetica;
font-size: 16px;
padding: 10px;
background: #fff;
color: #000;
display: inline;
border: 0;
text-decoration: none;
line-height: 17px;
}
<div class="one"></div>
<div class="two">
Link
<button>Button</button>
</div>

CSS input styling and overflow issues

I would love to style my input field very similar to the divs I am building. However, I am unable to solve sizing issues.
Here is an example
http://codepen.io/anon/pen/kLwlm
And here is one more (with overflow:visible and fixed height)
http://codepen.io/anon/pen/Fxjzf
As you can see, it looks very different than the divs, and no matter what I tried, I could not make them look similar. First of all, I would love to make the input in a way that the text will pop put (overflow: visible? not working).
Secondly, the height should be similar to the divs. Setting the height and line-height properties does seem to effect the temporary text, but when it's clicked (and started to type) it breaks. (check second example)
Shortly, open to suggestions.
Try this solution here:
#import url(http://fonts.googleapis.com/css?family=Playfair+Display:400,700,900,400italic,700italic,900italic);
body {
margin: 100px;
background-color: #f7f7f7;
}
input{
border:0;
}
div, input{
font-family: 'Playfair Display', serif;
font-size: 40px;
background-color: #ff44ff;
width: 100%;
margin-top: 20px;
line-height: 40px;
}
div {
padding: 1px 0px 13px 2px;
color: #999;
}
I tried placing the input in div and then making the input background to transparent. YOu can play with the spacing to you liking, but it works http://codepen.io/anon/pen/Brcpl
I came up with this JSFiddle. I removed the line-height and positioned text using padding instead (that fixed the aligning of the input text).I also styled the placeholder. Here is a part of your CSS which I changed (do read the notes in it).
div, input{
font-family: 'Playfair Display', serif;
font-size: 40px;
background-color: #ff44ff;
width: 100%;
margin-top: 20px;
padding: 5px 0px 5px 0px;/*use padding to adapt the size*/
}
/*Change placeholder properties*/
#s::-webkit-input-placeholder {
color: black;
}
#s:-moz-placeholder { /* Firefox 18- */
color: black;
}
#s::-moz-placeholder { /* Firefox 19+ */
color: black;
}
#s:-ms-input-placeholder {
color: black;
}
PS: I do suggest styling the input-box differently so the visitors of your website notice it is actually a input-box.
What about this one: http://codepen.io/anon/pen/lcgAD
css
div input {
border: none;
font-size: 40px;
width: 100%;
background: transparent;
color: #000;
font-family: 'Playfair Display', serif;
}
div input:hover {
}
div {
color: #000;
background-color: #892;
height: 41px;
}
html
<div>
<input placeholder="Enter E-Mail ayxml#gmail.com" value="Enter E-Mail ayxml#gmail.com"/>
</div>

Make a link use all the space

I have a button class working like this :
<p class="button">Rejoindre</p>
The CSS is :
p.button
{
background-color: #e74c3c;
line-height: 30px;
width: 200px;
text-align: center;
}
.button a
{
font-family: Montserrat, sans-serif;
color: white;
font-size: 0.9em;
text-transform: uppercase;
}
.button a:hover
{
text-decoration: none;
}
How can I make the entire button (represented by the paragraph tag) a link instead of just the text ?
You can put the link tag on the outside to make anything inside it be contained in the link:
<p class="button">Rejoindre</p>
However, you probably want to use something other than a p tag for your button, maybe a button element instead?
More info on HTML buttons.
Add display: block to the .button a ruleset.
http://jsfiddle.net/ExplosionPIlls/UvrKx/
You can add display:block; to you anchor tag.
display: block means that the element is displayed as a block, as
paragraphs and headers have always been. A block has some whitespace
above and below it and tolerates no HTML elements next to it, except
when ordered otherwise (by adding a float declaration to another
element, for instance).
Fiddle: http://jsfiddle.net/akx3p/
CSS:
p.button
{
background-color: #e74c3c;
line-height: 30px;
width: 200px;
text-align: center;
}
.button a
{
font-family: Montserrat, sans-serif;
color: white;
font-size: 0.9em;
text-transform: uppercase;
display: block;
}
.button a:hover
{
text-decoration: none;}
<p> are block elements, meaning that they naturally are at 100% width. If you just added display: block; to the anchor tag, you can make it behave the same way. Here's a fiddle
. That way allows you to get rid of the p tag all together.