html - 如何设置 <div> s inline : text, img, text, text

标签 html css image inline

起初我必须阅读很多教程,但我仍然不知道我做错了什么......

我想内联使用 4 个 div。在我想放置的那些 div 中:文本、图像、文本、文本。我希望中间文本自动设置为最大宽度。

我写了一个简单的代码,只是为了说明我的问题:

<div>
    <div style="float: right; width: 100px; background-color: red">right</div>
    <div style="margin-right: 100px; background-color: blue">
        <div style="width: 100px; background-color: green">left</div>
        <div style="width: 100px; margin-left: 100px; background-color: pink">
            <img src="../zdjecia_przedmiotow/1_small.jpg" />

        </div>
        <div style="margin-left: 200px; background-color: green">center</div>
    </div>
</div>

它看起来像这样: enter image description here

我想用 div 来做! 我错过了什么?

最佳答案

首先,您需要将这些 div float 到内部,以便它们彼此相邻对齐。然后你可以使用 calc() 让最后一个容器占据剩余的宽度;

FLOAT EXAMPLE

您可以在父级上使用 display: table 并将子级设置为 display: table-cell ,如下所示:

TABLE EXAMPLE

我还对它进行了一些重组,因为其中有一些不必要的元素/样式:

HTML

<div class="wrapper">
   <div class="box box1">left</div>
   <div class="box box2">
      <img src="../zdjecia_przedmiotow/1_small.jpg" />
   </div>
   <div class="box box3">center</div>
   <div class="box box4">right</div>
</div>

CSS

.wrapper{
    overflow:hidden;
    width:100%;
}

.box{
    float: left;
} 

.box1{
  width: 100px; 
  background-color: green;
}

.box2{
    width: 100px; 
    background-color: pink;
}

.box3{
    background-color: green;
    width:calc(100% - 300px);
}

.box4{
    width:100px;
    background-color: blue;
}

CLEANER FIDDLE

关于html - 如何设置 <div> s inline : text, img, text, text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27893650/

相关文章:

html - 订单栏 CSS

css - 日本标准网络字体

python - 感兴趣区域 opencv python

jquery - CSS 框位置 float

javascript - 从垂直滚动到水平滚动,然后再回到垂直滚动

html - css 更改输入光标/插入符号颜色

css - 如何为文本添加一些边框?

image - 这个 EMF 图像 A4 大小如何?

jQuery 同位素与图像

html - 如何在图像悬停时创建深色淡入淡出+文字?