I've a slightly odd requirement here. I'm trying to create a HTML + CSS transcript of a seventeenth century book which has a peculiar layout. The pages have a column of text with a generous right-hand margin that contains various floats (many of which are tables of numbers). These floating diagrams line up with the paragraphs of text so that the first line of the float lines up with the last line of the paragraph. (The text makes reference to this fact, so it's important to preserve it.)
Mostly this is easy, as in this example:
<main>
<h1>Test</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua —
<span class="float">123<br/>456</span></p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua —
<span class="float">789</span></p>
</main>
main {
max-width: 475px;
padding: 0.5rem;
border: thin solid black;
margin: 0 auto;
}
h1 {
text-align: center;
}
p {
text-align: justify;
text-indent: 1.5em;
padding-right: 0.5rem;
border-right: thin solid black;
margin: 0 calc(3ch + 0.5rem) 0 0;
}
span.float {
float: right;
clear: right;
text-align: right;
text-indent: 0;
font-variant-numeric: tabular-nums;
width: calc(3ch);
margin-right: calc(-3ch - 1rem);
}
This produces something like the following, which is exactly as I want:
The 123 lines up with the end of the first paragraph, the 789 lines up with the end of the second paragraph, and the first float (123, 456) flows alongside the start of the second paragraph.
However, very occasionally, I have a large float and the following paragraph is very short, e.g.
<main>
<h1>Test</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua —
<span class="float">123<br/>456<br/>789</span></p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit
sed do eiusmod tempor incididunt — <span class="float">321</span></p>
<p>Lorem ipsum dolor sit amet.</p>
</main>
(CSS as before.) If this happens, the book adds space above the second paragraph, and I want to replicate this so that it looks a bit like the following (which I've mocked up by inserting <p><br/></p> before the second paragraph):
Is there a way of achieving this in HTML and CSS? I'm not adverse to changing the way I mark up the document, however I'm catering for a variety of screen widths and fonts, so I cannot hard-code spacing based on how much I need in my browser. I'm happy with a solution that won't work in ancient browsers.
Update: To help illustrate what I'm after, here is a quick and dirty solution using jQuery, though I would prefer to avoid using Javascript if at all possible and do it in CSS:
$(function() {
$('p').each( function() {
var floats = $(this).find('.float');
var nextp = $(this).next('p:has(.float)');
if (floats.length && nextp.length) {
var f = $(floats[floats.length-1]), n = $(nextp[0]);
if (f.height() > n.height()) {
f.css('float', 'none');
n.css('padding-top', (f.height() - n.height()) + 'px');
f.css('float', 'right');
}
}
});
});
Related
I searched but I couldn't figure out this !!
as you know img tag is an inline-block as default but when I want to wrap text around it, I can't.
I know that inline-block elements behave like inline elements to take the screen, I mean they take the screen just the content dimensions, but my question is why img element doesn't let text wrapped around it?
I mean why do we need to use float?
<html lang="fa-IR">
<head>
<meta charset="utf-8">
<title>Html toturial</title>
</head>
<body>
<img src="download.png"/>
hello<br>
hello<br>
hello<br>
</body>
</html>
**NOTE: ** download.png file is on my local PC.
but I can show you the result!
IMAGE OF MY CODE
Inline and inline-block elements render on the same line (imagine lines of text in the book for example). And since img is inline-block element, it renders on the same line as the text next to it. So what you see in your example are three lines. First one contains image and text, the other two only contain text.
However two things happen here:
Inline-block also has height (by definition), which makes the first line higher.
All the elements in the line are (by default) vertically aligned to the bottom. That is why the text in the first line is aligned with lower border of the image.
In case you add float to the image, the concept of line is broken, which lets the subsequent content floating around.
When you wrap the text in <span> elements it works as you described because <span>s are inline elements.
<img> is a replaced element; it has a display value of inline by default, but its default dimensions are defined by the embedded image's intrinsic values, like it were inline-block. You can set properties like border/border-radius, padding/margin, width, height, etc. on an image.
MDN
<html lang="fa-IR">
<head>
<meta charset="utf-8">
<title>Html toturial</title>
</head>
<body>
<img src="download.png"/>
<span>hello</span>
<span>hello</span>
<span>hello</span>
</body>
</html>
inline-block elements have some properties of block elements and some properties of inline elements … and neither of them have the property of "content wraps around them".
An image is rendered a lot like a single character. A large character.
So you might compare these two paragraphs:
img { display: inline-block; }
span { display: inline-block; font-size: 50px; }
<p>A <img src="http://placekitten.com/50/50"> that Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad</p>
<p>A <span>k</span>itten that Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad</p>
The inline block box generated by the image is a lot like the inline box generated by a letter of the same size.
Wrapping of text around images in HTML / CSS is achieved with the float property;
img { float: left; }
p.foo::first-letter { float: left; font-size: 50px; }
<p><img src="http://placekitten.com/50/50"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad</p>
<p class="foo">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad</p>
this is what i have
and this is what i have to do
i know how to create "hover" but don't understand how to add this numbers which are pulled to the left OUT of the card!
this is my html
<li>
<div uk-parallax="opacity: 0,1; y: 100,0; viewport: 0.3"
class="uk-card uk-card-hover uk-card-small uk-card-body">
<h3 class="uk-card-title">HOW THE PRICE CALCULATED</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua.</p>
</div>
</li>
<li>
<div uk-parallax="opacity: 0,1; y: 100,0; viewport: 0.5"
class="uk-card uk-card-hover uk-card-small uk-card-body">
<h3 class="uk-card-title">HOW THE PRICE CALCULATED</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua.</p>
</div>
</li>
By the way i am using css framework named UIkit 3, maybe it can help somehow...
Thank you!
You'll have to do some custom CSS, but it can be accomplished with CSS counters + before pseudo element, like so:
ol {
list-style: none;
counter-reset: mycounter;
padding-left: 50px;
}
li {
counter-increment: mycounter;
position: relative;
}
li:hover {
background-color: #efefef;
}
li::before {
content: counter(mycounter);
position: absolute;
left: -30px;
font-size: 72px;
}
li:hover::before {
color: rgb(0,100,255);
font-size: 96px;
left: -40px;
top: -5px;
}
Here it is on codepen (with a little more styling to get it closer to your mockup):
https://codepen.io/jcatt/pen/jeOQaO?editors=1100
And here's a guide to doing this kind of thing:
https://css-tricks.com/custom-list-number-styling/
I have found how to make non-rectangular shaped text from here (Unusual shape of a textarea?) and it works like a charm.
I need to limit this to 2 lines so I used overflow: hidden; but the area somehow returns to rectangular shape.
This is my html code.
.descTitle {
height: 20px;
width: 70px;
float: left;
border: none;
}
.descContent {
height: 40px;
line-height: 20px;
/*overflow: hidden; /* This breaks the form*/
}
.descOut {
height: 40px;
width: 100%;
}
<tr>
<td colspan=1>
<div class='descOut'>
<div class='descTitle'>
<label for='txt9'><b>Description: </b></label>
</div>
<div class='descContent' contenteditable='true' style='font-color:black; font-size: 11px; font-family:arial'/>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam
</div>
</td>
</tr>
It works great when I comment out the overflow line but somehow it breaks the form when being used.
Can anyone help me to find out how to limit the contents to 2 lines without breaking unusual shaped text area?
This question already has answers here:
Floating elements within a div, floats outside of div. Why?
(11 answers)
Closed 6 years ago.
I have lists of data need to display like this
.mycontent-bottom {
border-bottom: 1px solid #ccc;
}
#float-right{
float: right;
}
<div class="mycontent-bottom">
Title
<span id="float-right">50000</span>
</div>
<div class="mycontent-bottom">
lorem ipsum yang lazim digunakan adalah: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
<span id="float-right">50000</span>
</div>
The problem is the second one, if the text is too long, it will push the number outside the bottom border. Any ideas how to hide long text so the number will stay at right and dont get push outside the border ?
You could try this:
https://jsfiddle.net/Lboxddh9/5/
.mycontent-bottom {
border-bottom: 1px solid #ccc;
display: inline-block;
width: 100%;
}
#float-right{
float: right;
}
Also, you should not use identical ids for multiple elements in a one page.
That's why, this would be correct, while your original markup is invalid.
<div class="mycontent-bottom">
Title
<span class="float-right">...</span>
</div>
<div class="mycontent-bottom">
...
<span class="float-right">...</span>
</div>
Updated fiddle with class instead of multiple ids:
https://jsfiddle.net/Lboxddh9/7/
I want the byline to appear just below the image.
I am trying to use the right, left, etc properties in relation to the relative property, but the span moves left of the image.
What is the mistake in my code?
<section id="manchanabele">
<img id="club" alt="club" src="images/club.jpg">
<p id="lorem">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. nisi ut aliquip ex ea commodo consequat.
<span id="byline">by: Lorem Ipsum</span>
</p>
</section>
section#manchanabele {
background: #C8C8C8;
}
#club {
float: right;
width: 75px;
height: 75px;
}
p#lorem {
background: #A0A0A0;
}
span#byline {
position: relative;
float: right;
}
You are structuring your DOM in a wrong way, you should wrap the elements you want to float in a single container. I will provide you the code which will result you in something like below
Here, in the code below, I will explain you related to the image above, the black border container is .wrap, the one which is having green border is the paragraph, which is p, the red on is the container which you are floating to the right which is .right_float and the nested elements inside red element is your img and span respectively.
For example
<div class="wrap">
<p>Hello</p>
<div class="right_float">
<img src="#" />
<span>Hello</span>
</div>
</div>
.wrap {
overflow: hidden; /* Clears float */
}
.wrap p {
float: left;
width: /*Some fixed width*/
}
.wrap .right_float {
float: right;
width: /* Some fixed width */
}
.wrap .right_float span {
display: block;
}
Note, if you don't care about the older versions, especially IE, I would recommend you to use a self clearing parent class
.clear:after {
clear: both;
display: table;
content: "";
}
Now, you can call the above class on your parent element holding floated elements, and you don't have to use overflow: hidden;
You could keep the byline aligned with the image by wrapping the elements in a container such as a DIV.
HTML:
<section id="manchanabele">
<div id="align">
<img id="club" alt="club" src="images/club.jpg">
<span id="byline">by: Lorem Ipsum</span>
</div>
<p id="lorem">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. nisi ut aliquip ex ea commodo consequat.
</p>
</section>
CSS:
section#manchanabele {
background: #C8C8C8;
}
#align {
float:right;
width:75px;
}
#club {
width: 75px;
height: 75px;
}
p#lorem {
background: #A0A0A0;
}
N.B. You may want to consider using classes rather the IDs if you need to use this layout several times for similar content.
Use this markup:
<article>
<div class="clearfix">
<img src="http://lorempixel.com/70/70" alt="a random image" class="thumb" >
<p>The quick brown fox jumps over all the messy markup and writes a new one.</p>
</div>
<footer>By The Fox</footer>
</article>
Fiddle: http://jsfiddle.net/C5GkH/1/
or if you need the image and the byline always one below the other keeping a blank sidebar on the right follow the advice of #Mr. Alien
Try to clear:both; after the image.
Like so
<section id="manchanabele">
<img id="club" alt="club" src="images/club.jpg">
<div style="clear:both;></div>
<p id="lorem">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. nisi ut aliquip ex ea commodo consequat.
<span id="byline">by: Lorem Ipsum</span>
</p>
</section>
Also avoid floating inline elements. Better if you wrapped that image with a div and then floated the div.