简体   繁体   English

显示图像和文本并排显示HTML

[英]Display Image and Text next to each other HTML

I'm trying to display an image and some text on my webpage floating next to each other as you can see below. 我正试图在我的网页上显示一个图像和一些文字,它们彼此相邻,如下所示。

在此输入图像描述

I've tried basically all the methods suggested in these two previous SO questions I found on this topic: 我基本上已经尝试过在此主题中找到的前两个SO问题中建议的所有方法:

However, no matter what combinations I try, this is the result that I obtain: 但是,无论我尝试什么组合,这都是我获得的结果:

在此输入图像描述

This is the HTML code for the first example (which seems not to work at all): 这是第一个示例的HTML代码(似乎根本不起作用):

<div class="cf">
    <img src="//upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Balzac.jpg/220px-Balzac.jpg" width=100>
    <div>some text here</div>
</div>

This is the HTML code for the second example, which differs cause the text is not wrapped into the <div> container (but seems to work only for a limited amount of text): 这是第二个示例的HTML代码,这有所不同,原因是文本未包装到<div>容器中(但似乎仅适用于有限数量的文本):

<div class="cf">
    <img src="//upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Balzac.jpg/220px-Balzac.jpg" width=300>
    some text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text heresome text here
</div>

The css file is from Nicholas Gallagher's micro clearfix: css文件来自Nicholas Gallagher的 micro clearfix:

/**
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    contenteditable attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that are clearfixed.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */
.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.cf {
    *zoom: 1;
}

Can you please tell me what is going wrong and how to fix this? 你能告诉我出了什么问题以及如何解决这个问题吗?

Demo 演示

css CSS

img {
    display: inline-block;
    vertical-align: middle; /* or top or bottom */
}
.text {
    display: inline-block;
    vertical-align: middle; /* or top or bottom */
}

html HTML

<div class="cf">
    <img src="//upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Balzac.jpg/220px-Balzac.jpg" width="100px" />
    <div class="text">some text here</div>
</div>

Final Demo 最终演示

css CSS

img {
    display: inline-block;
    vertical-align: middle;
    width: 100px;
}
.text {
    display: inline-block;
    vertical-align: middle;
    width: calc(100% - 100px);    
}

Your problem is that you have applied the clearfix but there is no float applied! 您的问题是您已经应用了clearfix,但是没有应用浮点数! Try adding the following 尝试添加以下内容

.cf img {float:left;}
.cf div  {float:left;}

Demo 演示

Clearfix .cf does nothing to float. Clearfix .cf无需浮动。 It's purpose is to ensure the "parent" element of floated elements "expand" to contain the floated elements. 目的是确保浮动元素的“父”元素“扩展”包含浮动元素。 Adding a backgound-color demonstrates this: http://jsfiddle.net/kxpur7z3/1/ 添加backgound-color对此进行了演示: http : //jsfiddle.net/kxpur7z3/1/

My code in the answer floats each of the elements to the left. 我在答案中的代码将每个元素浮动到左侧。 Note that "floating" removes the elements from the "natural flow" of the document. 请注意,“浮动”会从文档的“自然流”中删除元素。

Clearfix Demo Clearfix演示

Here are a couple of good references to continue with: 以下是继续使用的几个很好的参考:

So you want lots of text. 所以你想要很多文本。 Well as block and inline-block elements expand to fit their content you need to apply some width attributes. 当块和内联块元素扩展以适合其内容时,您需要应用一些宽度属性。 YOu have some options here. 你在这里有一些选择。

  • Apply a specific width to the text: width:80% , width:300px etc 将特定的宽度应用于文本: width:80%width:300px
  • Applying a calculated width to the text (thanks @ 4dgaurav for reminding me of this): width:calc(100% - 100px) 将计算出的宽度应用于文本(感谢@ 4dgaurav提醒我): width:calc(100% - 100px)
  • Go dynamic on both image and text with complimentay percentages: img {width:20%;} div {width:80%;} 使用补充百分比在图像和文本上动态: img {width:20%;} div {width:80%;}

Demo of various options 演示各种选项

I have just update your html like below. 我刚才更新你的html,如下所示。

<div class="cf">
   <div><img src="//upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Balzac.jpg/220px-Balzac.jpg" width="100px" /> </div>
   <div>some text here</div>
</div>

I have added only one additional CSS like below. 我只添加了一个额外的CSS,如下所示。

.cf > div
{
display:table-cell;
vertical-align:top;
}

DEMO DEMO

Make use of bootstrap classes which makes it very easy to achieve this 使用bootstrap类可以很容易地实现这一点

Example: 例:

<div class="row">
  <div class="col-md-6">
    <img src="//upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Balzac.jpg/220px-Balzac.jpg"      width=100>
  </div>
  <div class="col-md-3">
    <h5> some text here </h5>
  </div>
</div>

Make use of bootstrap.min.css library 使用bootstrap.min.css库

-Thanks -谢谢

use this html 使用这个HTML

<div class="cf">
<div class="leftcol">
<img src="//upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Balzac.jpg/220px-Balzac.jpg" width=100></div>
<div class="rightcol">some text here</div>
</div>
<div class="clear"></div>

then CSS 然后CSS

.leftcol{float:left; width:50%}
.rightcol{float:left; width:50%}
.clear{clear:both; display:block;}

This answer is maybe coming a bit late, but since I had that this problem too, I created a working model on fiddle (without float and purely with inline-block). 这个答案也许来得有点晚,但是由于我也有这个问题,所以我在小提琴上创建了一个工作模型(没有浮点数,并且只使用内联块)。

I didn't want to use float, because it messes up the heights and backgrounds of the parent container. 我不想使用float,因为它弄乱了父容器的高度和背景。

HTML HTML

<div class="box">
  <div class="box-content">

    <div class="box-image">
      <div class="wrap">
        <img src="https://pbs.twimg.com/media/DQckroiVwAAPw8Y.jpg" />
      </div>
    </div>

    <div class="box-text">
      <div class="wrap">
        Hier komt de tekst en wel een hele lange tekst, eigenlijk te lange tekst. Well this could be even longer than the Dutch text if it would be in English. Und es könnte auch noch etwas an Deutschen Text beinhalten, aber das würde schon ein sehr lange Text werden. Dus houden we het maar gewoon bij Nederlandse tekst.
      </div>
    </div>

  </div>
  <div class="box-footer">
    <div class="wrap">
      hier komt de bijlage
    </div>
  </div>
</div>

CSS CSS

.box {
  width: 600px;
  border: 5px solid #a1a1a1;
  border-radius: 10px;
  background: #fff;
  position: relative;
}

.box-content {
  padding: 10px;
}

.box-image {
  width: 30%;
  display: inline-block;
}

.box-image .wrap {
  padding-top: 10px;
  width: 100%;
}

.box-image img {
  width: 100%;
}

.box-text {
  display: inline-block;
  text-align: justify;
  font-size: 20px;
  vertical-align: top;
  width: 68%;
}

.box-text .wrap {
  padding: 5px;
}

.box-footer {
  padding: 10px;
  border-top: 4px solid #a1a1a1;
  text-align: center;
  background: #f1f1f1;
}

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM