bloodopal
bloodopal

Reputation: 154

Let a floated element overflow to a certain extent

As the image on the link below shows, I want an image to float next to a text as usual, but allow it to flow out of it's parent, to the extent of the parent's container. The dimensions of the image are variable, everything else is allowed to be a fixed width.

enter image description here

With what structure and styling can I achieve this, using only HTML and CSS?

Upvotes: 0

Views: 64

Answers (2)

Weafs.py
Weafs.py

Reputation: 22992

You could put your images in a container give them min-width and max-width and a negative margin-right. Apply float: left to the images inside the container.

body {
  background-color: #7F7F7F;
}
#main-container {
  position: relative;
  background-color: #337CFF;
  width: 600px;
  height: 400px;
  margin: 0 auto;
}
#yellow-container {
  background-color: #FCE300;
  position: relative;
  width: 400px;
  height: 350px;
  margin: 0 auto;
  top: 25px;
}
#content {
  padding: 30px 20px 30px 20px;
}
.img-container {
  float: right;
  margin-right: -110px;
  height: 100px;
  min-width: 100px;
  max-width: 200px;
}
.img {
  float: left;
}
#img-1 {
  background: url(http://dummyimage.com/100x100/4d3c00/fff);
  height: 100px;
  width: 100px;
  margin-right: 100px;
  float: left;
}
#img-2 {
  background: url(http://dummyimage.com/200x100/4d3c00/fff);
  height: 100px;
  width: 200px;
}
p {
  word-break: break-all;
  padding-right: 5px;
}
<div id="main-container">
  <div id="yellow-container">
    <div id="content">

      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis<span class="img-container"><span class="img" id="img-1"></span></span>nostrud exercitation
        ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
        mollit anim id est laborum.Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explica.
        <span
        class="img-container"><span class="img" id="img-2"></span></span>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta
          sunt explica Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>
    </div>
  </div>
</div>

Upvotes: 0

austinthedeveloper
austinthedeveloper

Reputation: 2601

See the fiddle below:

http://jsfiddle.net/austinthedeveloper/vocju9n2/

The trick is to give negative margin to your image depending on which way it is floating:

img {
    float: right;
    margin-right: -10px;
}

Upvotes: 1

Related Questions