I have an input box on my page <input class='cv-input cv-dashboard-input'>, with the following rules defined:
input.cv-dashboard-input {
font-family: Roboto;
font-weight: 300;
color: #004d6f;
border: 1px solid #adadad;
border-radius: 0px;
font-size: 14px;
transition: all 0.3s;
margin: 0;
box-sizing: border-box;
}
input.cv-input {
height: 42px;
width: 100%;
padding: 5px;
font-family: Roboto;
font-weight: 300;
}
When I inspect this element in Firefox, and check the computed sizes of it, it states, that my border width is 0.583333px. I tought that the minimum render size is 1 px, so how is this happening?
I am using Firefox 54.0.1 (32-bit)
It is indeed possible to render 0.5px when using CSS. The answer to the question is simply that a px in CSS does not always translate to one pixel on the computed properties because it can be impacted by the screen density. You can read more about it here: https://www.w3.org/TR/CSS21/syndata.html#length-units
I would like to mention that I am using a hi-dpi screen which may explain why my computed properties are very accurate.
The top one has a border-top-width of 0.5 pixels.
Related
I want to have one of those "i" icons appear next to a name on my site so people can click on it and look up more information. I have this HTML
<div id="personName"><h2>PersonA</h2> <div id="moreInfo">i</div></div>
and the below style
#personName {
display: block;
}
#moreInfo {
border-radius: 50%;
behavior: url(PIE.htc); /* remove if you don't care about IE8 */
width: 36px;
height: 36px;
padding: 8px;
background: #fff;
border: 2px solid #666;
color: #666;
text-align: center;
font: 32px Arial, sans-serif;
font-weight: bold;
font-style: italic;
display: inline-block;
}
The problem is I also have this style
* {
box-sizing: border-box;
}
which I need for a lot of other elements on my site and it seems to be throwing off the way my "i" graphic is appearing -- https://jsfiddle.net/ds9sqr0y/ . It also doesn't seem to be appearing next to the name, but maybe that's a separate issue.
That's because box-sizing: border-box includes both the border and the padding in the height computations.
Which means that if you create an element with height: 30px and padding-top: 5px, it will be 35px tall (height + padding) but with setting box-sizing: border-box, it will be 30px tall.
In your specific case, you can increase the height and width to the following to make it look like you want to:
width: 57px;
height: 57px;
As per Jesse de Bruijne's answer, you can set the padding property within the #moreInfo selector to 0. If you can, try and reduce the font size of the i, to better position it (I'm using Chrome). Setting it to 30px seems to show it better.
#moreInfo {
...
padding: 0;
font: 30px Arial, sans-serif;
...
}
How do I vertically align the characters/text inside an input without changing the height of the input (it has to be exactly 28px)? The input has this CSS, so I don't understand why it has some padding-top (?):
input {
font-family: arial;
font-size: 28px;
line-height: 28px;
height: 28px;
padding: 0;
border: none;
outline: none;
background-color:#cdcdcd;
}
<input value="asdg">
Some letters like g, p and q get cut off
Removing the margin-bottom doesn't help.
https://jsfiddle.net/4rtL6415/
There is no padding top, it's about font size. I've changed your snippet input with a special char that fit the whole height (I'll explain below):
input {
font-family: arial;
font-size: 28px;
line-height: 28px;
height: 28px;
padding: 0;
border: none;
outline: none;
background-color:#cdcdcd;
}
<input value="ᅡgs">
This image:
Explain how a font is construct. 99% of the time you'll see characters with Body < EM and that's why we may think that there is a sort of padding-top.
Sometimes, you'll cross characters for which Body == EM, that's the case of ᅡ (and a lot of others).
What you are seeing is not a bug but a feature. From here you have 3 choices:
Changing the font-size;
Changing the input height;
Changing the font-family for one that doesn't "overflow".
The choice is all yours.
The Problem
In some fonts, characters with descenders, like g, p, q, and y, "overflow" the vertical space defined by the font-size property. Normally, that's not a problem, because the line-height property provides enough extra space to accommodate the descenders. However, if the characters are placed in a container element with a fixed height that's less than the line-height, the descenders may get clipped if that's how the container handles overflow (text inputs being one example of such).
If you were hoping to bump the text up a few notches to avoid the clipping, then you'll be disappointed to know that there is currently no way to reposition text within its own line-height. (vertical-align, in case you were wondering, positions an inline element relative to its parent.) However, there are a few CSS tricks that we can use to achieve the same visual effect...
Solution 1 (Webkit only)
This one works by giving the input a large enough height to fit the font's lower extremities, and then using clip-path to trim it back down to 28px. This is probably the most elegant solution, but unfortunately, clip-path isn't well supported outside of Webkit browsers (Chrome, Safari, Opera).
input {
display: inline-block;
padding: 0;
border: none;
height: 32px;
font-size: 28px;
line-height: 32px;
font-family: arial;
background: #cdcdcd;
vertical-align: baseline;
-webkit-clip-path: inset(4px 0px 0px 0px);
clip-path: inset(4px 0px 0px 0px);
}
input: <input value="asdg">
Solution 2
This one was inspired by DebRaj's answer, but mine uses an inline-block wrapper instead of a block (not sure how you would use it otherwise). Like the previous solution, it increases the height of the input, but instead of using clip-path to trim it back down, it uses a container element with overflow: hidden;. This is probably the most practical approach until support for clip-path improves.
.text {
display: inline-block;
vertical-align: baseline;
overflow: hidden;
margin: 7px 0 -7px 0;
height: 28px;
}
.text > input {
margin-top: -4px;
border: none;
padding: 0;
background: #cdcdcd;
font-size: 28px;
line-height: 32px;
font-family: arial;
}
input:<span class="text"><input value="asdg"></span>
Solution 3
Although you can't reposition text within its own line-height, this may be the next best thing. If you set the line-height to something less than the font-size, the text will indeed move upward relative to its normal baseline. That means you can bring the clipped parts into view without changing the container height. Unfortunately, if you try this with a text input, you'll discover a strange quirk: line-height is completely ignored if it's less than the input's height. So we'll have to substitute a different element, and turn it into an editable textbox somehow. That can be accomplished with the contenteditable attribute.
.fauxTextInput {
display: inline-block;
vertical-align: baseline;
margin: 6px 0 -6px 0;
padding: 0 3px 0 3px;
width: 9em;
height: 28px;
overflow: hidden;
font-size: 28px;
line-height: 23px;
font-family: arial;
background: #cdcdcd;
}
Faux input: <span class="fauxTextInput" contenteditable>asdg</span>
As #Thomas mentioned there is a default spacing as per font construction rules. If we concentrate the output you want to achieve is make font exact same height at the input area, you can wrap your input into a div and give that a height to adjust the input into it using as a mask.
Here is the code:
<div class="input-wrapper">
<input value="asdg">
</div>
CSS:
.input-wrapper{
position: relative;
font-family: arial;
width: 100%;
height: 90%;
padding: 0;
background-color: #fff;
overflow: hidden;
}
.input-wrapper input {
display: block;
width: 100%;
height: 124%;
margin-top: -0.19em;
margin-bottom: 0em;
font-size: 28px;
padding: 0;
outline-offset: 0;
border: none;
}
.input-wrapper input:focus{
outline-offset: 0;
outline: 0;
border: none;
box-shadow: none;
}
fiddle: https://jsfiddle.net/x8jmLp8m/12/
Hope that helps.
Although there have been plenty of answers. I thought I'd add my solution to the bunch.
In this Fiddle, you can see how I managed to create an input field with a span tag, and the contenteditable attribute. The pros of taking this route are that the input field can stretch and wrap and that we can make it exactly 28px high.
In the CSS, I've added the following rules that are important:
span{
display: inline-block;
font-size: 25px; /*higher than 25px simply doesn't fit a 28px container*/
line-height: 1;
padding: calc(-.5em + 14px) 0;
}
display, of course, to style the bunch
font-size to declare the height of the font
line-height of 1 to make sure the text actually takes up 25px by default.
a padding of calc(-.5em + 14px) 0. And that's the tricky part
Because of this padding, the element will stay 28px high, while still centering the text. See the table below to see how the calculation works. The font-size and output * 2 always add up to a minimum of 28.
font-size | calculation | output |
--------------------------------------
50px | calc( -25px + 14px) | -11px | a negative padding translates to a padding of 0
25px | calc(-12.5px + 14px) | 1.5px |
20px | calc( -10px + 14px) | 4px |
15px | calc( -7.5px + 14px) | 6.5px |
10px | calc( -5px + 14px) | 9px |
With this code, you can edit the span's height by editing the 14px part in the calc, and edit the font-size without having to recalculate yourself
Hope this helps
Edited your fiddle here
The problem is your font is larger than the height of the element enclosing it. So you just need to set both the height and line-height to a couple of px larger than the font size you're using.
Hope this helps.
Just decrease the font-size:
input {
font-family: arial;
font-size: 20px;
line-height: 28px;
height: 28px;
padding: 0;
border: none;
outline: none;
background-color:#cdcdcd;
}
<input value="asdg">
I hope this will help you know what you want to achieve
In CSS, the line-height
property sets the height of
an entire line of text, so the
difference between the font-
size and the line-height is
equivalent to the leading (as
shown in the diagram above).
And our css is this
input {
font-family: arial;
font-size: 28px;
line-height: 28px;
height: 28px;
padding: 0;
border: none;
outline: none;
background-color:#cdcdcd;
}
Here we have set line-height and font size equal and because of that decent is getting cut.So you either need to decrease font-size or increase line-height.
input {
height:34px;
}
Just change height and line-height to 40px or more.
https://jsfiddle.net/525raf3L/
input {
font-family: arial;
font-size: 28px;
line-height: 40px;
height: 40px;
padding: 0 12px;
border: none;
outline: none;
background: yellow;
}
<input value="asdg">
I'm currently working on custom mixins to work with in SASS.
What I'm trying to achieve is working out the pixel height from a font-size and the line height. For example, I know 16px font size with a line height of 1 comes out at 22px high. The font family is Open Sans.
I've tried working out formulas to see how this comes about, but I'm struggling.
Is there a simple way to work out the literal pixel height from these two inputs?
EDIT
Yeah I was completely and utterly wrong. ~ I've changed the details.
Image added to show height shown.
input {
font-size: 16px;
line-height: 1;
font-family: "Open Sans",sans-serif;
-webkit-appearance: none;
-webkit-box-shadow: none;
box-shadow: none;
border: 1px solid #7B7B7B;
border-radius: 2px;
width: 100%;
background: #fff;
font-family: "Open Sans",sans-serif;
font-weight: 300;
padding: 8px;
margin: 0 0 12px;
cursor: pointer;
text-transform: none;
font-size: 16px;
font-size: 1em;
}
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,700,900">
<input type="search" class="search_field" placeholder="Search..." id="search" name="s" title="Search for:" aria-required="true" aria-invalid="false" required="">
First time putting a chunk of code into this, so hopefully this is correct.
You are wrong.
A font size of 16 pixels with a line height of 2, the line height is 32 pixels.
span {
display:inline-block;
background:yellow;
font-size: 16px;
line-height: 2;
}
<span>Hello!</span>
Actually, when font size is 16px and line height is 2, pixel height is 32px. The formula is very simple: font-size * line-height.
alert($("p").height());
p {
font-size: 16px;
line-height: 2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>test</p>
CSS:
h1 {
width: 25%;
margin: 11rem auto;
font-family: 'Baron Neue';
font-size: 4vw;
color: white;
text-align: center;
border-top: solid 2px white;
border-bottom: solid 2px white;
}
HTML:
<h1>POTJESMARKT</h1>
The h1 tag has <body> </body> as it's parent.
The problem is that the border on the top isn't attached to h1, while there's no padding there. What I would like eventually is about a 1rem padding between h1 and the border, at the top and bottom. I could solve this by adding padding at the bottom of h1, but if I then resize my browser window, the distance from h1 to border isn't equal anymore on top and bottom.
Solved it in an 'ugly' way:
I added padding-bottom: 1vw;
This way, the padding scales together with h1, which makes it acceptable.
Seeing as (technically), it's working here:
html,body{background:red;}
h1 {
width: 25%;
margin: 11rem auto;
font-family: 'Baron Neue';
font-size: 4vw;
color: white;
text-align: center;
border-top: solid 2px white;
border-bottom: solid 2px white;
}
<h1>POTJESMARKT</h1>
In which since I haven't actually got your font style installed, I believe your problem is with the font itself. In order to 'fix' this you could;
A: Find out the font's baseline margin (in px) and add the extra 'top' padding to your h1 element
B: Use a different/more common font
C: 'Guess' by adding some padding-bottom to your element.
Just some points I would like to note:
Ensure you import your font (so that everyone can 'use' it (i.e. google fonts))
I wouldn't really be that keen on using vw and vh unless actually needed
Again, personal preference would stop me from using 'rem' values, and either stick to em, px, or otherwise.
Here is a semantic answer:Header fiddle
html:
<h1><strong>Potjes market</strong><h1>
css:
h1 {
font-size:4em;
border:3px solid blacK;
}
h1 > strong {
position:relative;
bottom:0.1em;
}`
This pushes the text slightly up so that the border looks even above and below
How can i increase the round corner button width dynamically depends on the text size in CSS ?
i have 10 round corner buttons with different text size base, i want the solution is buttons size should increase dynamically based on text size....
Is any solution for this using HTML and CSS, or any other way to solve using scripting language.
Help..,
jsfiddle to see it in action: http://jsfiddle.net/XAU8V/
HTML:
<div class="container">
Small
Medium
Large
</div>
CSS:
.button {
border: 1px solid #96d1f8;
background: #65a9d7;
padding: 5px 30px;
color: white;
font-family: Georgia, serif;
text-decoration: none;
vertical-align: middle;
-webkit-border-radius: 1em;
-moz-border-radius: 1em;
border-radius: 1em;
}
.small {
font-size: 14px;
}
.medium {
font-size: 20px;
}
.large {
font-size: 30px;
}
The border radius is in "em", so it is in relation to the parent value (even though it's not really a parent in this example). Em's can get a little tricky due to inheritance.