-1

Is there a possibility to put an image on the middle of a page?
BUT: On the right side and the left side of the image, there are 2 areas that can grow according to the screen resolution.
These areas are "1 pixel repeat-x" images.
Please note: the image on the right side and the left side aren't the same picture!

Below a picture with a sketch that (I hope) will explain my problem:

sketch

Ephy L
  • 69
  • 5
  • You can apply multiple background-images using `background-image` property and position them as you want using `background-position`. – Mr_Green Jun 17 '14 at 10:56
  • possible duplicate of [Can I have multiple background images using CSS?](http://stackoverflow.com/questions/423172/can-i-have-multiple-background-images-using-css) – Mr_Green Jun 17 '14 at 10:57
  • 2
    Yes, there are many possibilities. Try one and come back here with some code if you get stuck. – Laurent S. Jun 17 '14 at 10:58
  • [What have you tried..?](http://mattgemmell.com/what-have-you-tried/) – T J Jun 17 '14 at 10:59
  • @TJ, I have tried solution that didn't work!
    It's why I didn't attempt to add some example that is not ok!
    – Ephy L Jun 17 '14 at 12:16
  • @EphyEphraimLevy if what you tried works, there's no need to post it over here. hope you got the point. – T J Jun 17 '14 at 12:19
  • @TJ, What I tried DIDN'T work! Believe me, I don't have time to post some question if I know the answer! – Ephy L Jun 17 '14 at 12:21
  • @EphyEphraimLevy i know it didn't work. that's why you're posting the question. so share the code that didn't work, so that we can work on it to make it work, and make sure you've done your part. – T J Jun 17 '14 at 12:24

5 Answers5

1

You can do it like this

Set the image in .container where for now I have added sample text.

CSS

.rightArea,
.leftArea {
  position: absolute;
  width: 50%;
  background: yellow;
  top:0;
  left:0;
  height: 100%;
}
.rightArea {
  background: red;
  right: 0;
  left: auto;
}
Tushar
  • 4,280
  • 5
  • 24
  • 39
1

Here is one way of building this layout using CSS table cells.

Start with this HTML:

<div class="wrapper">
    <div class="left"></div>
    <div class="center">
        <img src="http://placekitten.com/400/200">
    </div>
    <div class="right"></div>    
</div>

and apply the following CSS:

.wrapper {
    display: table;
    width: 100%;
    border: 1px solid blue;
}
.left, .center, .right {
    display: table-cell;
    border: 1px dotted blue;
    vertical-align: top;
}
.center {
    width: 1%;
}
.left, .right {
    width: 50%;
}
.left {
    background-image: url(http://placekitten.com/4/100);
    background-repeat: repeat-x;
    background-position: left center;
}
.right {
    background-image: url(http://placekitten.com/10/100);
    background-repeat: repeat-x;
    background-position: left center;
}
.center img {
    display: block;
}

The .wrapper has a width of 100%, so it fills the width of the page.

The child elements .left, .center and .right are table cells.

.center is forced to shrink-to-fit the image by setting the width to some small value, for example, 1%.

The .left and .right elements are set to the same width, 50%, which forces them to take up the remaining space equally.

You can apply background images as needed to any of the child elements.

See demo at: http://jsfiddle.net/audetwebdesign/pG2v3/

Note: Most modern browsers support CSS table cells.

Marc Audet
  • 46,011
  • 11
  • 63
  • 83
1

You can relay on a single container and pseudo-elements.DEMO

display:table/table-cell -properties will make this easy to manage: (update test with your image name/path)

HTML

<div>
    <img src="middle.jpg" />
</div>

CSS

img {
    display:block;/* avoid gap underneath*/
    margin:auto;/*optionnal*/
}
div {
    display:table;
    width:100%;
    background:#7E858F;
}
div:before, div:after {
    content:' ';
    display:table-cell;
    width:50%;/* will shrink to leave room for image */
    background-repeat:repeat-x;
}
div:before {
    background-image:url(left.jpg);/* slice of 1px */
}
div:after {
    background-image:url(right.jpg);/* slice of 1px */
}

DEMO

G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
  • Table cells are the way to go for this layout problem. The pseudo-elements offer a very concise approach if the left and right elements are not needed to hold any other content. – Marc Audet Jun 17 '14 at 11:52
  • @MarcAudet He doesn't say, but another similar answer uses divs, so , at least he learnt or was reminded of some method today :) – G-Cyrillus Jun 17 '14 at 11:55
0

To freely grow sided placed divs, keeping a content in the middle, use a wrapper display: table; element, and set the inner divs to display:table-cell, so the center element will adjust according to the width of the right and left divs:

http://jsfiddle.net/4uHm8/

HTML:

<div class='wrapper'>
    <div class='left'></div>
    <div class='center'>test</div>
    <div class='right'></div>
</div>

CSS:

.wrapper {
    display: table;
    width: 100%;
    height: 200px;
}

.wrapper > div {
    display: table-cell;
}

.left {
    width: 30px;
    background: red;
}

.right {
    width: 50px;
    background: blue;
}

EDIT:

In the same line of thought, if you set only the central div's width, the left and right divs will adjust themselves equally using the remaining width... even zero.

http://jsfiddle.net/4uHm8/1/

.wrapper {
    display: table;
    width: 100%;
    height: 200px;
}

.wrapper > div {
    display: table-cell;
}

.center {
    background: blue;
    width: 100px;
}

.left, .right {
    background: red;
}
LcSalazar
  • 16,524
  • 3
  • 37
  • 69
  • This solution may be ok, but the right and left widths may be 0 In case of the central image size equals the screen resolution. – Ephy L Jun 17 '14 at 12:18
  • Are the right and left width always equals? I mean, is the content always centered? – LcSalazar Jun 17 '14 at 12:26
0

HTML

<div class="main">
    <div class="left">&nbsp;</div>
    <div class="image">
        <img src="http://www.ricoh.com/r_dc/r/r8/img/sample_10.jpg" height="150" width="120" alt="image" />
    </div>
    <div class="right">&nbsp;</div>
</div>

CSS

.main {
    width: 100%;
    margin: 0 auto;
    position: relative;
}
.left {
    float: left;
    width: 50%;
    background: #FF0000;
}
.right {
    float: right;
    width: 50%;
    background: #FFFF00;
}
.image {
    position: absolute;
}

jQuery

$(document).ready(function () {
    $(".image").css('right', ($(".main").width() / 2) - ($(".image").width() / 2));
    $(window).resize(function() {
        $(".image").css('right', ($(".main").width() / 2) - ($(".image").width() / 2));
    });
});

Example

http://jsfiddle.net/wphn9/

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Srinivasa Reddy
  • 114
  • 1
  • 7
  • When you resize the browser width,it doesn't work. Thanks anyway. – Ephy L Jun 17 '14 at 12:23
  • Just change the jQrey to this $(document).ready(function (){ $(".image").css('right',($(".main").width()/2)-($(".image").width()/2)); $(window).resize(function(){ $(".image").css('right',($(".main").width()/2)-($(".image").width()/2)); }); }); – Srinivasa Reddy Jun 17 '14 at 12:31
  • Thanks for update. I update the jQuery. Now it is working fine in browser resize. – Srinivasa Reddy Jun 17 '14 at 12:38