Pergunta

I want a fixed width image in the middle.
That is no problem.
But i want the left and right div beside it to be 2 lines.

Example.

I have a 100px * 100px image in the middle and want the 2 divs to be 50% - 50px in width without using calc. The 2 divs should have a line which is also no problem.

I just use

 { background: white; margin-top: 48px; margin-bottom: 48px; }

Now the problem is to make those 2 divs fill the left and right space of the image. Needless to say the width of the entire screen is dynamic.

Is there anyway to do this only with CSS?

Foi útil?

Solução

Here is my solution using a wrapper for the left and right div, which have a width of 50% and contain other divs with margins of half the image's width. The image itself is positioned in the wrapper using absolute positioning.

HTML

<div class="wrapper">
    <div class="left"><div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.</div></div>
    <div class="right"><div>Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</div></div>
    <img src="your_image.png" alt="" />
</div>

CSS

.wrapper {
    position: relative;
    overflow: hidden;
    height: auto; /* match the height of the floating divs */
}

.wrapper > div > div {
    min-height: 100px; /* at least as high as the image */
}

.wrapper > .left {
    float: left;
    width: 50%;
}

.wrapper > .left > div {
    margin-right: 50px;
    background: blue;
}

.wrapper > .right {
    float: right;
    width: 50%;
}

.wrapper > .right > div {
    margin-left: 50px;
    background: red;
}

.wrapper > img {
    position: absolute;
    width: 100px;
    height: 100px;
    top: 0;
    left: 50%;
    margin-left: -50px;
}

Here is a JSFiddle: http://jsfiddle.net/j84LB/

Outras dicas

You could try warpping it, and giving the divs on the sides 25% and the image 50%.
This is the same as the divs having values 50% of the images width.

CSS

.wrapper {width: 200px;}
.left {width: 25%;}
.right {width:25%;}
.image {width: 50%;}

HTML

<div class="wrapper">
    <div class="left"></div>
    <div class="image">
        <img src="" />
    </div>
    <div class="right"></div>
</div>

Seems easy using enough display:table/display:table-cell

No need to extra divs etc.

JSfiddle Demo

HTML

<body>
    <div class="wrapper">
        <div class="left">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor
        </div>
        <div><img src="http://lorempixel.com/output/abstract-q-c-100-100-6.jpg" alt="" /></div>
        <div class="right">Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
        </div>

    </div>
</body>

CSS

.wrapper {
    position: relative;
    overflow: hidden;
    height: auto;
    display:table;
}

.wrapper > div {
    display:table-cell;
    vertical-align: middle;
    padding: 1em;
}

.wrapper > div:nth-child(2) {
    min-height: 100px;
    width:100px;
    padding:0;

}

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top