When you write this: (in chrome)
<div style="font-size: 32px">txt</div>
its height will be 37px.
but this one:
<div style="font-size: 16px">txt</div>
its height will be 18px.
So my question is: is there any formula to calculate how much will be a text's height when rendered (based on font-size)?
The height is not based on the font-size but the line-height and the default value is normal
Depends on the user agent. Desktop browsers (including Firefox) use a default value of roughly 1.2, depending on the element's font-family.ref
Basically, we don't know exactly the value of line-height but if we explicitely define it then we can know the exact height.
An example where I am setting the height to be 1.5 x font-size
$('div').each(function(){
console.log($(this).height());
})
div {
line-height:1.5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="font-size: 32px">txt</div>
<div style="font-size: 16px">txt</div>
Another one where the height is 35px for all of them:
$('div').each(function(){
console.log($(this).height());
})
div {
line-height:35px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="font-size: 32px">txt</div>
<div style="font-size: 16px">txt</div>
Worth to note that the result is different if you consider an inline element:
$('div').each(function(){
console.log("div "+$(this).height());
})
$('span').each(function(){
console.log("span "+$(this).height());
})
div,span {
line-height:1.5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="font-size: 32px">txt</div>
<div style="font-size: 16px">txt</div>
<span style="font-size: 32px">txt</span>
<span style="font-size: 16px">txt</span>
Or if you have different font-size inside a div or different alignment:
$('div').each(function() {
console.log("div " + $(this).height());
})
div {
line-height: 1.5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<span style="font-size: 32px">txt</span>
<span style="font-size: 16px">txt</span>
</div>
<div>
<span style="font-size: 32px;vertical-align:text-top">txt</span>
<span style="font-size: 32px">txt</span>
</div>
In the first case, the height will be defined only based on the font properties (line-height play no role here).
the height of the content area should be based on the font ref
On a non-replaced inline element, 'line-height' specifies the height that is used in the calculation of the line box height.
In the second case, we have a more realistic example where the height is not only based on the line-height but also consider the alignment of the different elements to find the final height we need in order to place all of them.
More details here: https://www.w3.org/TR/CSS2/visudet.html#line-height
Related
I have an HTML input[type='text'] with a placeholder. The input size is already determined by its container.
I would like to find a way to fit the placeholder size with the input width and add a minimum size.
<div id="container" style="width: 50px">
<input type='text' width='100%' placeholder='A placeholder which should be seen totaly'/>
</div>
I tried :
input::placeholder {
font-size: Xvw; //<-- I don't find how it works in this case
font-size: 100%; //<-- Does not work
}
Someone has an idea ?
When the browser size is changed/on different sized devices, I need a set of html elements that are all semantically related to remain together and move in a block. That is, if one of the elements move to the next "row" due to their not being enough width to contain the whole grouping, ALL of it should move down.
IOW, this is sort of like the "keep together" attribute that some groupings of items in a word processing document have.
To be a little more specific, say that I have collections of the following elements:
1) an anchor tag, filling out a first "column"
2) a collection of tags, to the right of the anchor tag, consisting of:
(a) a div, followed by a <br/>
(b) a cite, followed by a <br/>
(c) another div, followed by a <br/>
(d) two or three anchor tags that are aligned side-by-side at the bottom of the second "column"
So to sum up, if there is not enough room for the second "column" in a "row," rather than keep the in the first "column" and moving the elements in the second column down to the next "row," the in the first column should adhere to its siblings and always remain on the same "row" with them (I'm putting "row" and "column" in quotes because I'm not using an html table, and those exist only in a virtual sense).
If you're finding this a little hard to visualize (I don't blame you), check out the fiddle: http://jsfiddle.net/W7CYC/8/
Note: wrapping the groupings into html5 s did not help.
Here's the code:
HTML:
<div class="yearBanner">2013</div>
<section>
<a id="mainImage" class="floatLeft" href="https://rads.stackoverflow.com/amzn/click/com/0299186342" rel="nofollow noreferrer"><img height="240" width="160" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg"></a>
<div id="prizeCategory" class="category">BIOGRAPHY</div>
<br/>
<cite id="prizeTitle" class="title">Son of the Wilderness: The Life of John Muir</cite>
<br/>
<div id="prizeArtist" class="author">Linnie Marsh Wolfe</div>
<br/>
<img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
<img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
<img class="floatLeft" height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
</section>
<section>
<a class="floatLeft" href="https://rads.stackoverflow.com/amzn/click/com/0299186342" rel="nofollow noreferrer"><img height="240" width="160" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg"></a>
<div class="category">BIOGRAPHY</div>
<br/>
<cite class="title">Son of the Wilderness: The Life of John Muir</cite>
<br/>
<div class="author">Linnie Marsh Wolfe</div>
<br/>
<img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
<img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
<img height="60" width="40" src="http://ecx.images-amazon.com/images/I/51usxIl4vML._SY346_.jpg">
</section>
CSS:
body {
background-color: black;
}
.floatLeft {
float: left;
padding-right: 20px;
padding-left: 5px;
}
.yearBanner {
font-size: 3em;
color: white;
font-family: Verdana, Arial, Helvetica, sans-serif;
float: left;
padding-top: 64px;
}
.category {
display: inline-block;
font-family: Consolas, sans-serif;
font-size: 2em;
color: Orange;
width: 160px;
}
.title {
display: inline-block;
font-family: Calibri, Candara, serif;
color: Yellow;
width: 160px;
}
.author {
display: inline-block;
font-family: Courier, sans-serif;
font-size: 0.8em;
color: White;
width: 160px;
}
jQuery:
$('#prizeCategory').text("Changed Category");
$('#prizeTitle').text("Changed Title that spans two rows");
$('#prizeArtist').text("Changed Author and co-author");
$('#mainImage img').attr("src", "http://ecx.images-amazon.com/images/I/61l0rZz6mdL._SY300_.jpg");
$('#mainImage img').attr("height", "200");
You group items simply with div (or if you want to use section, it is okay too). With a little hint of CSS you can group item inside wrapper. Unfortunately, there is no such attribute than keep together but you can do following:
section.wrapper {
min-width: 400px; /* Minimum width of your wrapper element */
overflow: hidden;
display: inline-block;
}
min-width helps you to keep elements inside wrapper in order. Select a value that best suits your situation.
overflow with value hidden lets your wrapper to understand and add width and height values of floated elements inside.
display with value inline-block let all of wrappers to order next to each other as long as there is enough space, if not, wrapper jumps to other row.
http://www.w3schools.com/ serves great sources to understand and learn CSS, HTML and web technologies in generally. Very useful.
EDIT
As I edited, min-width or width suits better in that situation than max-width
This is not an HTML issue, learn about using CSS absolute position inside of relative positioning:
http://css-tricks.com/absolute-positioning-inside-relative-positioning/
Bootstrap grids
Bootstrap includes a powerful mobile-first flexbox grid system for
building layouts of all shapes and sizes. It’s based on a 12 column
layout and has multiple tiers, one for each media query range. You can
use it with Sass mixins or our predefined classes.
code example :
<div class="row">
<div class="col-4">.col-4</div>
<div class="col-4">.col-4</div>
<div class="col-4">.col-4</div>
</div>
<div class="row">
<div class="col-sm-4">.col-sm-4</div>
<div class="col-sm-4">.col-sm-4</div>
<div class="col-sm-4">.col-sm-4</div>
</div>
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4">.col-md-4</div>
</div>
<div class="row">
<div class="col-lg-4">.col-lg-4</div>
<div class="col-lg-4">.col-lg-4</div>
<div class="col-lg-4">.col-lg-4</div>
</div>
<div class="row">
<div class="col-xl-4">.col-xl-4</div>
<div class="col-xl-4">.col-xl-4</div>
<div class="col-xl-4">.col-xl-4</div>
</div>
Output:
This is default output:
and when size of my browser reduce to less than 1200px
Here we have four row and the first row coumns will always at the same tow.
the second row will remain when max width is >=768px
the third row will remain when max width is >=992px and so on.I have inserted this details in G rids options
Here is a gif demo of output
you can mix this classes together.for example
<div class="row">
<div class="col-sm-4 col-xs-6">first-col</div>
<div class="col-sm-4 col-xs-3">second-col</div>
<div class="col-sm-4 col-xs-3">third-col</div>
</div>
mean if small devices I want three column with same with but in very small device I want to first-col be half of space and every two other column be 25% of with.
Now as you can see in every size you can set what behavior every row and every column should do and if you want to always move together you can use col-* classes without any prefix.
Grids Options
(<768px) (≥768px) (≥992px) (≥1200px)
Grid behavior Horizontal at all times Collapsed to start, horizontal above breakpoints
Container width None (auto) 750px 970px 1170px
Class prefix .col-xs- .col-sm- .col-md- .col-lg-
# of columns 12
Column width Auto ~62px ~81px ~97px
Gutter width 30px (15px on each side of a column)
Nestable Yes
Offsets Yes
Column ordering Yes
Here is more detail if you need
I am trying to limit the horizontal size of whatever I load in a blockquote (I do not control that content in the blockquote so I know it's going to screw up my layout). Here is a reduced testcase of what the page layout looks like:
<html>
<body>
<div style="width: 800px; background-color: #ff0000;">
<div style="display: table;">
<div style="display: table-row;">
<blockquote style="overflow:hidden;">
<div style="width: 1000px; height: 400px;background-color: #00ff00;"></div>
</blockquote>
</div>
</div>
</div>
</body>
</html>
The size of the blockquote is implicitely set through a width on one of its parent elements and a table container is used to layout the blockquote itself.
Now, if you try to copy/paste the above html into a test.html file, you will see the entire inner div displayed as blue, beyond the boundaries of the red background. I would like to make sure it gets cliped at the background boundaries.
The question is then: is there a way to do this without changing the structure of the html layout and without having to set again an explicit width on the blockquote itself (I cannot do the latter because I do not know the real size of the blockquote in the real layout because there are other elements within the outer div that take an unknown amount of horizontal space) ?
EDIT
Earlier, I naively tried the following. I added an extra column in the table to illustrate the fact that I really do not know how much space the other elements in the table will suck up.
<html>
<body>
<div style="width: 800px; background-color: #ff0000;">
<div style="display: table;">
<div style="display: table-row;">
<div style="display: table-cell;">
<blockquote style="overflow:hidden;" id="inner">
<div style="width: 1000px; height: 400px;background-color: #00ff00;"></div>
</blockquote>
</div>
<div style="display: table-cell;">
<div style="width:100px; background-color:#0000ff; height: 300px;"></div>
</div>
</div>
</div>
</div>
<script>
var width = document.getElementById('inner').parentNode.offsetWidth;
console.log(width);
</script>
</body>
</html>
In order for overflow: hidden; to work you have to set the dimensions of the element explicitly. So, yes, sorry, you have to specify the width on blockquote.
If you are allowed to, you can use javascript to determine the width of the parent element which has a set width and then set the width of the blockquote accordingly.
Here's an example of how that might work, using your current markup:
var root = document.getElementsByTagName('div')[0],
block = document.getElementsByTagName('blockquote'),
i;
for (i = 0; i < block.length; i += 1) {
block[i].style.maxWidth = root.style.width;
}
Demo
However, this would be vastly improved if you were to give the div that's got the set width a class name or id. Check out this fiddle to see how that would work.
Please consider the following code:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style type="text/css">
.section {
display: block;
width: 200px;
border: 1px dashed blue;
margin-bottom: 10px;
}
.element-left,
.element-right-a,
.element-right-b {
display: inline-block;
background-color: #ccc;
vertical-align: top;
}
.element-right-a,
.element-right-b {
max-width: 100px;
}
.element-right-b {
position: absolute;
left: 100px;
}
</style>
<title>test</title>
</head>
<body>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-a">some text</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-a">some more text to force line wrapping</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some text</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some more text to force line wrapping</span>
</div>
<div class="section">
<span class="element-left">some text</span>
<span class="element-right-b">some more text to force line wrapping</span>
</div>
</body>
</html>
The element with absolute positioning apparantly makes the containing box "forget" which height he needs.
I need the absolute positioning inside the "section" box, is there another solution for this?
edit
Using tables is not really an option, I need some sort of "multi-level"/"nested" layout, where the second col is always on the same position:
| some text in first column | some text in 2nd column
| some indented text | 2nd column
| also indented | 2nd col
| even more indent | 2nd column with a lot of text that
| makes it wrap
| text | ...
| blah blah | ...
(of course whithout the "|"s)
When you use position:absolute;, the element is taken out of the normal page flow. Therefore it no longer affects the layout of its container element. So the container element does not take into account its height, so if there's nothing else to set the height, then the container will be zero height.
Additionally, setting display:inline-block; does not make any sense for an element that is position:absolute;. Again, this is because absolute positioning takes the element out of the page flow. This is at odds with inline-block, which only exists to affect how the element fits into the page flow. All elements that are position:absolute; are automatically treated as display:block, since that's the only logical display mode for absolute positioning.
If you need absolute positioning, then the only solution to your height problem is to set the height of the container box.
However, I suspect that you could do without the absolute positioning.
It looks like what you're trying to do is position the second <span> in each block to a fixed position in the block, so that they line up.
This is a classic CSS problem. In the "bad-old-days", web designers would have done it using a table, with fixed widths on the table cells. This obviously isn't the answer for today's web designers, but it is something that causes a lot of questions.
There are a number of ways to do it using CSS.
The easiest is to set both the <span> elements to display:inline-block;, and give them both a fixed width.
eg:
<div class='section'>
<span class="element-left">some text</span>
<span class="element-right">some text</span>
</div>
with the following CSS:
.section span {display:inline-block;}
.element-left {width:200px;}
.element-right {width:150px;}
[EDIT]after question has been updated
Here's how I would achieve what you're asking now:
<div class='section'>
<span class="element-left"><span class='indent-0'>some text</span></span>
<span class="element-right">some text</span>
</div>
<div class='section'>
<span class="element-left"><span class='indent-1'>some text</span></span>
<span class="element-right">some text</span>
</div>
<div class='section'>
<span class="element-left"><span class='indent-2'>some text</span></span>
<span class="element-right">some text</span>
</div>
with the following CSS:
.section span {display:inline-block;}
.element-left {width:200px;}
.indent-1 {padding:10px;}
.indent-2 {padding:20px;}
.element-right {width:150px;}
A small amount of extra markup, but it does achieve the effect you want.
No.
You could position absolutely then have a copy of the element inside the section box with visible: none but absolute positioning by definition makes it a "floating" element that doesn't interact with elements above it.
Assuming your page layout is fixed you could use padding-left: #px; to achieve your goal, as I don't think relative positioning is what you want.
Alternatively, you could use display: table-* to force it to retain a stricter form without affecting the document structure as shown here
However depending on whether you want the cells to be fluid you may need to alter the .section divs into display: table-row if you don't like a predetermined width setup and want the separate .section's to line up.
This can in fact be done easily and simply with divs. You just need to place a div with position:relative inside the inline or block display div. Set its width and height to the same as the containing div. You will then find you can position another div absolutely inside it.
I have several elements with already set fonts - like
<div style="font-size: 10px">
some text
</div>
<div style="font-size: 20p">
some text
</div>
I want to increment the font size proprtionally, eg
<div style="font-size: 15px">
..........................
<div style="font-size: 30px">
is that possible?
div {font-size: whatever} simply overwrites the values
Your question is unclear:
<div id="fred" style="font-size: 10px">hello</div>
<div id="walt" style="font-size: 20px">there</dev>
States absolute sizes which are absolute and there isn't anything that could alter those absolute measures except changing the literals. This is why many texts recommend establishing a body font and sizing relative to that:
<body style"font-size: 10px">
<div id="joan", style="font-size: 100%">hello</div>
<div id="mary", style="font-size: 200%">world</div>
</body>
Would make "joan" 10px and "mary" 20px. Changing the body size to 15px would make "joan" 15px and "mary" 30px.That forms an attribute "cascade". Of course all this should be done in a style block rather than in the div attributes, but it would make this answer less direct.
Using relative units in font sizes will scale them relative to the font size of the parent element, but the cascade doesn't provide a way to say "relative to the font size that this overrides" (and it would usually turn into a complete mess if there was a way).