Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

html - Set image to same line with text that doesn't have default line height

If my text doesn't have default line-height value and I have images that are floated either left or right it looks like the image doesn't start from the same line as the text. Is there anything I can do about it? I know I could add some top-margin for the image but it is manual work. I have hundreds of images and text chapters and this problem is everywhere. My problem is demonstrated here: https://codepen.io/shnigi/pen/VwKmYPb It looks like the image starts way above the text.

body {
  max-width: 900px;
  margin: auto;
}

p {
  line-height: 4em;
}

img {
  float: left;
  margin-right: 20px;
}
<p><img src="https://via.placeholder.com/350">Messages are used for personal, family, business and social purposes. Governmental and non-governmental organizations use text messaging for communication between colleagues. In the 2010s, the sending of short
  informal messages became an accepted part of many cultures, as happened earlier with emailing.[1] This makes texting a quick and easy way to communicate with friends, family and colleagues, including in contexts.</p>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One approximation is to reset the line-height of the first line but this will make the gap with the next line smaller than the others:

body {
  max-width: 900px;
  margin: auto;
}

p {
  line-height: 4em;
}

p::first-line {
  line-height: initial;
}

img {
  float: left;
  margin-right: 20px;
}
<p><img src="https://via.placeholder.com/350">Messages are used for personal, family, business and social purposes. Governmental and non-governmental organizations use text messaging for communication between colleagues. In the 2010s, the sending of short
  informal messages became an accepted part of many cultures, as happened earlier with emailing.[1] This makes texting a quick and easy way to communicate with friends, family and colleagues, including in contexts.</p>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...