Here is the pretty short fiddle which includes a 20px by 20px "ruler". I expect my p tag to be 20px high...10px text, and 10 px for the top and bottom padding (5px + 5px).
However it looks to be about 22px.
http://jsfiddle.net/BNnhp/30/
I have reset the body tag, the p tag and the div tag using margin 0px, padding 0px and line-height 100%.
Previously I had as a similar issue that was fixed by setting line-height to 100%....however this is not working here?
I want to know the exact cause - CSS attribute and value.
But for testing purposes I clicked the normalize box in jsfiddle and this had no effect either.
Added in more resets here:
http://jsfiddle.net/BNnhp/32/
I created a small test case, but it worked fine there. So, I went back to your answer, and found you were setting p's display value to inline, which was causing the 2px - 3px difference (fixed version). To fix, change your CSS as follows (look at comments):
/* ... */
#hold_name{
padding: 5px 5px 5px 5px;
/* position:relative; -- Don't need */
/* top: 0px; -- Don't need */
color:#000000;
/* display:inline; -- Don't need */
background: #ffffff;
font-size: 10px;
margin:0; /* Need to add */
line-height:100%;
}
#hold_name:hover{
}
#wrap{
position: absolute;
top: 20px; /* Change to 20px from 24px */
visibility: hidden;
margin: 0;
padding: 0;
border-left: 1px solid #007fa5;
border-bottom: 1px solid #007fa5;
border-right: 1px solid #007fa5;
}
/* ... */
The fact that your font-size is set to 10px doesn't promise you that the height would also be 10px. Some specific letters/symbols are higher than others.
If you change it to, say 8px like this:
#hold_name{
font-size: 9px;
}
then you will notice the container size changes as well.
This is because the tag re-sizes itself to contain the text inside.
You could set a specific height for your as well and it would change:
#hold_name{
font-size: 9px;
height: 10px;
}
and that would set the containing <p> element to a height of 10 pixels, no matter what the font-size is.
combining with the 5px padding on top and bottom, it would sum to 10px + 5px + 5px = 20px height.
While taking the other answers into consideration, you can also change the padding attribute of .menu-drop to match the following, then add a line-height of 20px:
.menu_drop{
position: relative;
margin: 0;
padding: 0 10px; /* modified padding value */
white-space: nowrap;
text-align: left;
text-decoration: none;
background: #bfddec;
color: #2875DE;
font: 11px arial;
line-height: 20px; /* new line-height property */
}
Related
I wanna create a responsive button so I cannot use "px" values for the border radius. If I try to use "%" values I never get to the format which I want. Basically the same button but instead of border-radius=100px; something responsive with "%".
Following a snippet.
.button-in-main-box{
background-color: #04AA6D;
border:1px solid ;
height: 15px;
padding: 15px 0;
width: 40%;
border-radius: 100px;
display: block;
text-align: center;
}
<span class = button-in-main-box>Löse Aufgabe</span>
This should work for you border-radius: 5%/100%;
There are more details about it in here: https://www.w3.org/TR/css-backgrounds-3/#the-border-radius
Try using rem. It is responsive tho.
.button-in-main-box{
background-color: #04AA6D;
border:1px solid ;
height: 15px;
padding: 15px 0;
width: 40%;
border-radius: 20rem;
display: block;
text-align: center;
}
<span class = button-in-main-box>Löse Aufgabe</span>
The border-radius property defines the radius of the element's corners and it sometimes take more than one value depending on what you want to achieve,
A simple rule
/* If one value is set, `radius` applies to all 4 corners. */
border-radius: 15px;
/* If two values are set, the first applies to top-left and
bottom-right corner, the second applies to top-right and
bottom-left corner. */
border-radius: 15px 15px;
/* Three values: The second value applies to top-right and
also bottom-left. */
border-radius: 15px 15px 15px;
/* Four values apply to the top-left, top-right, bottom-
right, bottom-left corner */
border-radius: 15px 15px 15px 15px;
Keep in mind that , percentages , refer to corresponding dimension of the border box.
You may specify the value of border-radius in percentages. This is particularly useful when wanting to create a circle or elipse shape,
/* create a circle */
border-radius: 50%;
Try using rem or em unit. Rem is relative to the root font size of your html document while em is relative to the font size of the element or nearest parent.
.button-in-main-box{
background-color: #04AA6D;
border:1px solid ;
height: 15px;
padding: 15px 0;
width: 40%;
border-radius: 6.25rem;
display: block;
text-align: center;
}
<span class = button-in-main-box>Löse Aufgabe</span>
It looks like the value of border-radius depends on the height of the button which includes height + padding.
You may set and use those two value within a css var(--myCssVar) and calc(). It will adapt itself the value to set to border-radius.
examples:
.button-in-main-box {
/* update using css var to calculate border-radius according to height + padding */
--height: 15px;
--padding: 15px;
box-sizing: content-box;/*keep it that way */
height: var(--height);
padding: var(--padding) 0;
border-radius: calc(var(--height) + var(--padding));
/* original op's rules */
background-color: #04AA6D;
border: 1px solid;
width: 40%;
display: block;
text-align: center;
}
<span class="button-in-main-box">Löse Aufgabe</span>
<hr> give it a different height and padding via a reset of CSS var() used
<span class="button-in-main-box" style="--height:30px;--padding:40px">Löse Aufgabe</span> what about font-size and em ?
<span class="button-in-main-box" style="font-size:10px;--height:1.5em;--padding:0.5em">Löse Aufgabe</span>
ressources:
CSS variable
https://developer.mozilla.org/en-US/docs/Web/CSS/var()
The var() CSS function can be used to insert the value of a custom property (sometimes called a "CSS variable") instead of any part of a value of another property.
CSS calc() function
https://developer.mozilla.org/en-US/docs/Web/CSS/calc()
The calc() CSS function lets you perform calculations when specifying CSS property values. It can be used anywhere a <length>, <frequency>, <angle>, <time>, <percentage>, <number>, or <integer> is allowed.
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;
...
}
I created multi-line-padded text based on Matthew Pennell's solution (codepen by CSS Tricks). In Chrome all looks fine, but in Firefox height of span elements bigger than height of their ancestor. If I adjust vertical padding for Firefox, in Chrome will be same problem, and vice versa.
Why it happens? What the real technical reasons of this problem?
HTML Code:
<div class="padded-multiline">
<h1>
<strong>How do I add padding to subsequent lines of an inline text element?</strong>
</h1>
</div>
CSS Code:
:root {
font-family: Arial, sans-serif;
font-size: 20px;
}
.padded-multiline {
line-height: 1.3;
padding: 2px 0;
border-left: 20px solid #c0c;
width: 400px;
margin: 20px auto;
}
.padded-multiline h1 {
background-color: #c0c;
padding: 4px 0;
color: #fff;
display: inline;
margin: 0;
}
.padded-multiline h1 strong {
position: relative;
left: -10px;
}
Setting a line-height: 1; on strong will fix the problem also read my comment.
Chrome and Firefox seems to use different text layout system.
In Chrome it will floor the line-height attribute and Firefox seems to use the correct one.
To achieve the same effect for title, just use only the outline.
H1 does not need strong.
.padded-multiline {
line-height: 1.3;
padding: 2px 0;
width: 400px;
margin: 20px auto;
}
.padded-multiline h1 {
background-color: #c0c;
padding:1px;
color: #fff;
display: inline;
outline: 10px solid #c0c;
margin: 0;
font-size:16px;
}
<div class="padded-multiline">
<h1>How do I add padding to subsequent lines of an inline text element?</h1>
</div>
Here is codepen: http://codepen.io/anon/pen/vgRvjM
If you need exactly visual (that means less purple space from top and bottom, you can use for example border from after and before):
.padded-multiline:before{
content:'';
display:block;
border:5px solid #fff;
position:relative;
left:-10px;
top:-3px;
}
.padded-multiline:after{
content:'';
display:block;
border:5px solid #fff;
position:relative;
left:-10px;
bottom:-3px;
}
Codepen for this solution: http://codepen.io/anon/pen/QdmzxK
Unfortunately, there isn't a full and clean crossbrowser workaround. Because different UAs render text different, height of each textline may be taller a bit (or vice verca). So, I create a solution based on SCSS calculations of required box' sizes, and hide artefacts via overflow property.
Here is my solution, if you meet the same problem: http://codepen.io/ifiri/pen/ygEeeL
HTML:
<p class="multiline-text">
<span class="multiline-text__wrapper multiline-text__wrapper--outer">
<span class="multiline-text__wrapper multiline-text__wrapper--left">
<span class="multiline-text__wrapper multiline-text__wrapper--right">Multiline Padded text, which looks great on all browsers. No artefacts, no hacks, all clear and flexy, all alignment support. Change SCSS variables for see how it works.</span>
</span>
</span>
</p>
SCSS:
/*
Variables
*/
$base-line-height: 1.75;
$base-font-size: 1.25em;
$multiline-padding-base: ($base-line-height / 2) * 1em;
$multiline-padding-horizontal: $multiline-padding-base;
$multiline-padding-vertical: $multiline-padding-base - (1em / 2);
$multiline-bg-color: #a5555a;
$multiline-font-color: #fff;
/*
= Snippet Styles
This code is required
*/
.multiline-text {
color: $multiline-font-color;
padding: 0px $multiline-padding-horizontal;
// hide line-height artefacts
overflow: hidden;
position: relative;
}
.multiline-text__wrapper {
background-color: $multiline-bg-color;
padding: $multiline-padding-vertical 0px;
}
.multiline-text__wrapper--outer {
// Inner padding between text lines
line-height: $base-line-height;
}
.multiline-text__wrapper--left {
position: relative;
left: -($multiline-padding-horizontal);
}
.multiline-text__wrapper--right {
position: relative;
right: -($multiline-padding-horizontal / 2);
}
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 have a problem positioning an inputfield with variable size and a post button with fixed size inside of a div with fixed "outer-margin".
I've provided a jfiddle example where you can see the wrong version.
Here you can see my problem:
Wrong version: Actual website
"Should-be" version: This is what it should look like
Textual description:
As you can see in the 2nd picture, it's important for me that the bottom-part always sits on the bottom...
.submitform {
display: block;
position: fixed;
bottom: 0;
...more on jfiddle...
}
... and the post-button always on the right. The bottom-part should have the same gap on the left and right as the upper-part. The difficult thing is, that the inputfield should have flexible size but should take all the space between left gap and post-button.
I hope you can help me because hours of trying and searching on the web didn't brought me the right solution.
Try this: http://jsfiddle.net/jgHAA/1/
.submitform {
background: white;
display: block;
position: fixed;
/* Instead of applying margin and width, simply set the bottom, left,
and right properties to 13px */
bottom: 13px;
left: 13px;
right: 13px;
/*box-shadow: 0 2px 7px rgba(0, 0, 0, 0.1);*/
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
padding: 7px;
overflow:hidden;
box-sizing:border-box;
}
to have the textbox fill the width, you can use the same technique: http://jsfiddle.net/jgHAA/2/
.submitform #post_input {
display: block;
font-size: 18px;
border: none;
outline: none;
padding: 3px;
/* set left and right, and z-index to make it appear behind the button.*/
left:7px;
right:7px;
position:absolute;
z-index:-1;
}