简体   繁体   English

浮动图像保留为背景-CSS

[英]float image left as background - css

I have this html: 我有这个HTML:

<style>
#todo{
display:table;
border:1px solid #000;
}

.divimg{
float: left; 
height: 100px; 
margin-right: 0px; 
margin-bottom: 0px;
}
</style>

<div id=todo>
<img class=divimg src=https://40.media.tumblr.com/2292c0583894cd27ef887ca431e4cc40/tumblr_mkjw8oi7vb1s5jt7zo1_500.jpg></img>
<img class=divimg src=http://36.media.tumblr.com/tumblr_m6pvhwHP2O1rp9ko0o1_500.jpg></img>
<img class=divimg src=http://data.whicdn.com/images/48891103/tumblr_mghlgh66It1r0i9t4o1_500_large.png></img>
<img class=divimg src=http://www.ladylaike.com.br/blog/wp-content/uploads/2014/01/beach-summer-tumblr.jpg></img>
<img class=divimg src=https://41.media.tumblr.com/bdc50942adb5463db541bc77fcecede2/tumblr_n5mkb0CXlV1s7s4cuo2_500.jpg></img>
<img class=divimg src=https://40.media.tumblr.com/2292c0583894cd27ef887ca431e4cc40/tumblr_mkjw8oi7vb1s5jt7zo1_500.jpg></img>
<img class=divimg src=http://36.media.tumblr.com/tumblr_m6pvhwHP2O1rp9ko0o1_500.jpg></img>
</div>

https://jsfiddle.net/v5wj21c5/ https://jsfiddle.net/v5wj21c5/

my problem is, I want the same result as I have in this, but with images in background, not as img tag. 我的问题是,我希望获得与此相同的结果,但是背景图像而不是img标签。 Is it possible? 可能吗? how can i do that? 我怎样才能做到这一点?

I don't think you can add multiple-image on background with floating attribute. 我认为您不能在具有浮动属性的背景上添加多个图像。

But you can create 2 div with position:absolute CSS attribute. 但是您可以使用position:absolute CSS属性创建2 div。 The background div with z-index:1; 带有z-index:1;的背景div z-index:1; and the content div with z-index:2 内容div的z-index:2

Like that : https://jsfiddle.net/qxo8q2hn/2/ 像这样: https : //jsfiddle.net/qxo8q2hn/2/

Use a div tag instead of an img tag. 使用div标签代替img标签。 Change the display to inline-block and control their size with background-size. 将显示更改为内联块并使用背景尺寸控制其尺寸。

<div id=todo>
    <div class='div-img1 divimg'></div>
    <div class='div-img1 divimg'></div>
    <div class='div-img1 divimg'></div>
    <div class='div-img1 divimg'></div>
    <div class='div-img2 divimg'></div>
</div>

... ...

<style>
#todo{


border:1px solid #000;
}

.divimg{
display:inline-block;
height: 100px; 
width:100px;
background-size:100px 100px;
margin-right: 0px; 
margin-bottom: 0px;
}

.div-img1 {
    background-image:url('https://40.media.tumblr.com/2292c0583894cd27ef887ca431e4cc40/tumblr_mkjw8oi7vb1s5jt7zo1_500.jpg');
}

.div-img2 {
    background-image:url('http://36.media.tumblr.com/tumblr_m6pvhwHP2O1rp9ko0o1_500.jpg');
}

</style>

https://jsfiddle.net/v5wj21c5/22/ https://jsfiddle.net/v5wj21c5/22/

Since you know how wide you want the images you could do multiple background images and just set the distance, but you'd have to give the div some height and width otherwise it will just collapse 由于您知道图像的宽度,因此可以制作多个背景图像并设置距离,但是您必须给div一些高度和宽度,否则它会折叠

div {
    height: 300px;
    width: 300px;
    background-image: url(“https://40.media.tumblr.com/2292c0583894cd27ef887ca431e4cc40/tumblr_mkjw8oi7vb1s5jt7zo1_500.jpg”), url(“http://36.media.tumblr.com/tumblr_m6pvhwHP2O1rp9ko0o1_500.jpg”), url(“http://data.whicdn.com/images/48891103/tumblr_mghlgh66It1r0i9t4o1_500_large.png”);
    background-size: 100px auto, 100px auto, 100px auto;
    background-position: 0 0, 100px 0, 200px 0;
    background-repeat: no-repeat;    
}

here's a fiddle 这是一个小提琴

You can use multiple background images as a css-property with background-image:url(...), url(...), ... . 您可以使用background-image:url(...), url(...), ...将多个背景图像用作CSS属性。

Example HTML: HTML示例:

<div id="todo">
    Example Text.
    Example Text.
    Example Text.
    Example Text.
</div>

With CSS (3 of your images): 使用CSS(您的3张图片):

#todo {
    margin:5px;
    padding:50px 0px 0px 0px;
    border:1px solid black;
    height:500px; 
    width:1000px;
    background-repeat:no-repeat;
    background-image:url("https://40.media.tumblr.com/2292c0583894cd27ef887ca431e4cc40/tumblr_mkjw8oi7vb1s5jt7zo1_500.jpg"),
        url("http://36.media.tumblr.com/tumblr_m6pvhwHP2O1rp9ko0o1_500.jpg"),
        url("http://data.whicdn.com/images/48891103/tumblr_mghlgh66It1r0i9t4o1_500_large.png");
    background-size:50px 50px;
    background-position:0px 0px, 50px 0px, 100px 0px;
}

See altered jsfiddle for the result. 有关结果,请参见更改的jsfiddle Works in Chrome and Firefox for me. 适用于Chrome和Firefox。

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

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