I've two elements (one p
and another one img
) in a simple container without styling (except background). I've specifically styled both of the elements to be inline-block
and border-box
but when the total of their length is less than 100% even, 99% only then they appear on same line.
What would possibly be the reason.
Here is the link to jsfiddle.
Asked
Active
Viewed 318 times
0

NATS
- 21
- 1
-
Dear Friend . When you say "why they don't fit in the samw line" which line are you referring to? Bottom or top ro which one ? – Imran Rafiq Rather Jan 02 '21 at 06:21
-
I mean why don't they fit side by side – NATS Jan 02 '21 at 06:24
-
1They are side by side actually, with respect to the base line. If you open developer window and try to extend it, say to 2000 view port width..You will get to know... But it's the content of the para which keeps adjusting itself to accomodate its content. Over all w.r.t.base line of their parent element they are side by side. Thank You :) – Imran Rafiq Rather Jan 02 '21 at 06:27
-
Hope you are satisfied with my answer. :) – Imran Rafiq Rather Jan 02 '21 at 08:39
3 Answers
-1
Any whitespace between inline-block elements takes up space. Either:
- remove the whitespace
.content {
background: #eeeeee;
}
p {
display: inline-block;
width: 80%;
}
img {
display: inline-block;
width: 20%;
}
<div class="content">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dignissimos sequi fugiat sint nihil quo unde deleniti ratione laudantium dolorum illo odio recusandae est, ab fugit incidunt, molestiae, inventore quam soluta mollitia dolorem! Quaerat fuga,
repellendus quo, at adipisci nihil voluptate molestias asperiores, dolorem maiores amet rerum debitis ullam! Molestias quasi doloribus tempora ratione ipsum similique accusamus debitis voluptatibus deserunt autem consequatur, ut dolor repellendus
at repudiandae? Dolorum modi blanditiis atque consequatur nesciunt repellat doloremque eius quisquam ea dolores, voluptate earum adipisci sit tempora voluptates aspernatur expedita dolorem reiciendis animi nisi, quas mollitia iusto autem! Quisquam
explicabo molestias dignissimos. Similique iste beatae sed? Mollitia commodi aperiam provident cum alias sapiente esse?</p><img src="https://source.unsplash.com/random/200x200" alt="">
</div>
- Or set
font-size:0
on the parent element.
.content {
background: #eeeeee;
font-size: 0;
}
p {
display: inline-block;
width: 80%;
font-size: initial;
}
img {
display: inline-block;
width: 20%;
}
<div class="content">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dignissimos sequi fugiat sint nihil quo unde deleniti ratione laudantium dolorum illo odio recusandae est, ab fugit incidunt, molestiae, inventore quam soluta mollitia dolorem! Quaerat fuga,
repellendus quo, at adipisci nihil voluptate molestias asperiores, dolorem maiores amet rerum debitis ullam! Molestias quasi doloribus tempora ratione ipsum similique accusamus debitis voluptatibus deserunt autem consequatur, ut dolor repellendus
at repudiandae? Dolorum modi blanditiis atque consequatur nesciunt repellat doloremque eius quisquam ea dolores, voluptate earum adipisci sit tempora voluptates aspernatur expedita dolorem reiciendis animi nisi, quas mollitia iusto autem! Quisquam
explicabo molestias dignissimos. Similique iste beatae sed? Mollitia commodi aperiam provident cum alias sapiente esse?</p>
<img src="https://source.unsplash.com/random/200x200" alt="">
</div>

ksav
- 20,015
- 6
- 46
- 66
-
Sorry, but this is not the answer to my question, I need to know the reason why they don't fit in the same line – NATS Jan 02 '21 at 06:18
-1
use display:flex
to the parent container .content
,I added the snippet below.I'm added the Snippet with 100% width (80+20) .
.content {
background: #eeeeee;
display:flex;
}
p {
display: inline-block;
width: 80%;
box-sizing: border-box;
}
img {
box-sizing: border-box;
width: 20%;
display: inline-block;
}
<div class="content">
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Dignissimos sequi fugiat sint nihil quo unde deleniti ratione laudantium dolorum illo odio recusandae est, ab fugit incidunt, molestiae, inventore quam soluta mollitia dolorem! Quaerat
fuga, repellendus quo, at adipisci nihil voluptate molestias asperiores, dolorem maiores amet rerum debitis ullam! Molestias quasi doloribus tempora ratione ipsum similique accusamus debitis voluptatibus deserunt autem consequatur, ut dolor
repellendus at repudiandae? Dolorum modi blanditiis atque consequatur nesciunt repellat doloremque eius quisquam ea dolores, voluptate earum adipisci sit tempora voluptates aspernatur expedita dolorem reiciendis animi nisi, quas mollitia iusto
autem! Quisquam explicabo molestias dignissimos. Similique iste beatae sed? Mollitia commodi aperiam provident cum alias sapiente esse?</p>
<img src="https://source.unsplash.com/random/200x200" alt="">
</div>

Dharman
- 30,962
- 25
- 85
- 135

ADH - THE TECHIE GUY
- 4,125
- 3
- 31
- 54
-
I know the solution too, but I need to know the reason that why don't they fit in the same line – NATS Jan 02 '21 at 06:19
-1
They are side by side actually, with respect to the base line. If you open developer window and try to extend it, say to 2000 view port width..You will get to know... But it's the content of the paragraph <p>...</p>
which keeps adjusting itself to accommodate its content. Over all w.r.t base line of their parent element they are side by side. Thank You :)

Imran Rafiq Rather
- 7,677
- 1
- 16
- 35