CSS Layout - Dynamic width DIV - css

I have a pretty common layout issue that I have traditionally used a table to solve, but would like some advice on getting it done with CSS. I have 3 images that makeup a 'container'. The left and right images are usually just shown using tags, and the center image is displayed as a 'background-image" with my content over it, so that the content appears to be in the container. I'm sure you've seen/used this a million times:
<table width="100" cellpadding="0"><tr>
<td width="50"><img src="myleftimage" /></td>
<td style="background: url('mymiddleimage');">Content goes here...</td>
<td width="50"><img src="myrightimage" /></td>
</tr></table>
The nice thing about this is that the width of the table is always the width of the browser (or parent) and the middle column where the content is dynamically sizes to take up the remaining space between the left/right images.
What I want to is recreate this using CSS, with as little hard coded info as possible. So something like this:
<div style="float:left; width:100%">
<div style="width: 50px;float:left;"><img src="myleftimage" /></div>
<div style="background: url('mymiddleimage');float:left;width:???">Content goes here...</div>
<div style="width: 50px;float:left;"><img src="myrightimage" /></div>
</div>
This works great accept for the middle div -how do I set the width? Right now I can hard-code it to be, say, 92%, etc. But want I want is for it to auto-fill the space. Can it be done using only CSS?

try
<div style="width:100%;">
<div style="width:50px; float: left;"><img src="myleftimage" /></div>
<div style="width:50px; float: right;"><img src="myrightimage" /></div>
<div style="display:block; margin-left:auto; margin-right: auto;">Content Goes Here</div>
</div>
or
<div style="width:100%; border:2px solid #dadada;">
<div style="width:50px; float: left;"><img src="myleftimage" /></div>
<div style="width:50px; float: right;"><img src="myrightimage" /></div>
<div style="display:block; margin-left:auto; margin-right: auto;">Content Goes Here</div>
<div style="clear:both"></div>
</div>

This will do what you want. Fixed sides with 50px-width, and the content fills the remaining area.
<div style="width:100%;">
<div style="width: 50px; float: left;">Left Side</div>
<div style="width: 50px; float: right;">Right Side</div>
<div style="margin-left: 50px; margin-right: 50px;">Content Goes Here</div>
</div>

Or, if you know the width of the two "side" images and don't want to deal with floats:
<div class="container">
<div class="left-panel"><img src="myleftimage" /></div>
<div class="center-panel">Content goes here...</div>
<div class="right-panel"><img src="myrightimage" /></div>
</div>
CSS:
.container {
position:relative;
padding-left:50px;
padding-right:50px;
}
.container .left-panel {
width: 50px;
position:absolute;
left:0px;
top:0px;
}
.container .right-panel {
width: 50px;
position:absolute;
right:0px;
top:0px;
}
.container .center-panel {
background: url('mymiddleimage');
}
Notes:
Position:relative on the parent div is used to make absolutely positioned children position themselves relative to that node.

making a dynamycal width with mobile devices support
http://www.codeography.com/2011/06/14/dynamic-fixed-width-layout-with-css.html

Related

How to make img fit a cell when it has a wrapper div

Say I have a div with fixed dimensions
<div class="cell" style="width:150px; height:150px"></div>
If I want the image to be not wider and not higher than the div, but keep its original aspect ratio I can set max-height and max-width to 100%
<div class="cell" style="width:150px; height:150px">
<img src="" style="max-height:100%; max-width:100%">
</div>
However, in my case I want the img to be wrapped in a div. The reason is, I want to place some elements on top of the image, and position them relatively to the image:
<div class="cell" style="width:150px; height:150px">
<div class="image-button-wrapper" style="position:relative">
<img src="" style="max-height:100%; max-width:100%">
<button style="position:absolute; top:3px; right:3px"></button>
</div>
</div>
However once I wrap the image in a div it only respects the max-width property, but not the max height, So the image (and the wrapper) overflow the cell in vertical direction.
possible solution:
I read that for max-height to work I have to set the parent's height explicitly. But how would I do that, since I want its dimensions to be identical to those of the image?
While playing around with in the browser I realized that setting the wrapper width and height to "fit-content" solves it. But this is quite uncommon value. Is that the simplest solution?
I'm adding a code snippet where you can see how the max-width is obeyed while the max-height isn't
<div class="cell" style="width:150px; height:50px;border: 3px solid red">
<div class="image-wrapper" style="position:relative">
<img src="https://images.pexels.com/photos/62321/kitten-cat-fluffy-cat-cute-62321.jpeg?auto=compress&cs=tinysrgb&h=350" style="max-height:100%; max-width:100%">
<button style="position:absolute; top:3px; right:3px; width: 10px; height:10px"></button>
</div>
</div>
I added expected result. The only difference is making the wrapper's width and height "fit-content".
<div class="cell" style="width:150px; height:50px;border: 3px solid red">
<div class="image-wrapper" style="position:relative; width:fit-content; height:fit-content">
<img src="https://images.pexels.com/photos/62321/kitten-cat-fluffy-cat-cute-62321.jpeg?auto=compress&cs=tinysrgb&h=350" style="max-height:100%; max-width:100%">
<button style="position:absolute; top:3px; right:3px; width: 10px; height:10px"></button>
</div>
</div>
You can use background-image:
.cell{
background-image: url("https://images.pexels.com/photos/62321/kitten-cat-fluffy-cat-cute-62321.jpeg?auto=compress&cs=tinysrgb&h=350");
background-repeat: no-repeat;
background-size: 100% 100%;
}
<div class="cell" style="width:150px; height:50px;border: 3px solid red">
<div class="image-wrapper" style="position:relative">
<button style="position:absolute; top:3px; right:3px; width: 10px; height:10px">
</button>
</div>
img{
width: 100%;
height: 100%;
}
.image-wrapper{
width: 100%;
height: 100%;
object-fit: contain;
}
<div class="cell" style="width:150px; height:50px;border: 3px solid red">
<div class="image-wrapper" style="position:relative;">
<img src="https://images.pexels.com/photos/62321/kitten-cat-fluffy-cat-cute-62321.jpeg?auto=compress&cs=tinysrgb&h=350" style>
<button style="position:absolute; top:3px; right:3px; width: 10px; height:10px"></button>
</div>
</div>
Is this what you wanted??
It can be solved in many ways, it depends on what you prefer.
What you did is already correct, just add height: 100%; to the image-wrapper:
<div class="cell" style="width:150px; height:50px;border: 3px solid red">
<div class="image-wrapper" style="position:relative; height: 100%;">
<img src="https://images.pexels.com/photos/62321/kitten-cat-fluffy-cat-cute-62321.jpeg?auto=compress&cs=tinysrgb&h=350" style="max-height:100%; max-width:100%">
<button style="position:absolute; top:3px; right:3px; width: 10px; height:10px"></button>
</div>
</div>
Explanation: The image-wrapper div has width of 100% (by default, since div is a block and blocks have default width of 100%) but no specific height. When you set height for wrapper to 100%, it considers its parent's height (the cell's height), so then, the image will respect its parent height (the image-wrapper's height).

Basic CSS width calculation confuse

All:
I wonder if I set father DIV width:auto and set Child div width:100%, then how browser decides the width of those DIVs
<div class="menuframe" style="position: fixed; width:auto; height:auto;">
<div class="menuitem style="width:100%; height:auto;">item1</div>
<div class="menuitem style="width:100%; height:auto;">item22</div>
<div class="menuitem style="width:100%; height:auto;">item333</div>
<div class="menuitem style="width:100%; height:auto;">item4444</div>
</div>
<div class="menuframe" style="background-color: red; position: fixed; width:auto; height:auto;">
<div class="menuitem" style="background-color: blue; width:100%; height:auto;">item1</div>
<div class="menuitem" style="background-color: black; width:100%; height:auto;">item22</div>
<div class="menuitem" style="background-color: green; width:100%; height:auto;">item333</div>
<div class="menuitem style="width:100%; height:auto;">item4444</div>
</div>
As you can see it will get the largest div width.
The menuitem divs should always be 100% width of the menuframe. How wide menuframe is depends on what CSS you set for it.

Resize an image dynamically with CSS inside a table-cell div

I am trying to create two columns of equal width, the left one containing text and the right one containing an image. I would like the image to resize down to the width of the column if the image width is greater than the column width, but would like the image to keep its original size if the image is less than the column width. I have tried the instructions in this question, but the image, if larger, will consume whatever space it needs, squishing the column on the left containing text. I am wondering if this has to do with how "table-cell" divs behave, or if it is a precedence issue where the browser will accommodate an image before text. My code looks something like this:
<div style="display: table; width:100%;">
<div style="display: table-row;">
<div style="display: table-cell; width: 50%; vertical-align: middle;">
<h1>Header</h1>
<p>Description</p>
</div>
<div style="display: table-cell; width: 50%; vertical-align: middle;">
<img style="max-width: 100%;height:auto;" src="image.jpg">
</div>
</div>
</div>
Also, I am using FireFox as my browser to test. Thanks!
EDIT: I also tried an alternative method to get the same effect using float divs (code below). The problem is I would like the two columns middle-centered, and I cannot get the smaller float div to size to full height in the parent container, so the inner contents are top-aligned. However, the image does resize properly, which backs up my hunch that this problem is related to how "table-cell" divs behave.
<div style="position: relative;">
<div style="float: left; width: 50%;height:100%;background-color:#F00;">
<div style="display: table;height:100%;width:100%;">
<div style="display: table-row;height:100%;width:100%;">
<div style="display: table-cell;height:100%;width:100%;vertical-align:middle;">
<p>content</p>
</div>
</div>
</div>
</div>
<div style="float: left; width: 50%;height:100%;background-color:#F00;">
<div style="display: table;height:100%;width:100%;">
<div style="display: table-row;height:100%;width:100%;">
<div style="display: table-cell;height:100%;width:100%;vertical-align:middle;">
<img style="max-width:100%;height:auto;" src="image.jpg">
</div>
</div>
</div>
</div>
</div>
Code was working fine in Chrome. This seems to be an issue only in Firefox as per this article:
http://www.carsonshold.com/2014/07/css-display-table-cell-child-width-bug-in-firefox-and-ie/
Adding table-layout:fixed; to the div styled display:table; gives the results you're looking for- allowing the image max-width to work and respecting the cell widths.
<div style="display: table; width:100%; table-layout: fixed">
<div style="display: table-row;">
<div style="display: table-cell; width: 50%; vertical-align: middle;">
<h1>Header</h1>
<p>Description</p>
</div>
<div style="display: table-cell; width: 50%; vertical-align: middle;">
<img style="max-width: 100%;height:auto;" src="image.png">
</div>
</div>
</div>

Nested <DIV> does not align to top

<div id="outer" style="position:relative; width:100%; height:100%">
<div id="1" style="position:relative; width:25%; height:auto;"></div>
<div id="2" style="position:relative; width:65%; height:auto;">
<div id="2a" style="position:relative; width:15%; height:auto;"></div>
</div>
</div>
The problem is that the #2 and #2a are not aligned to #1 and I have to use top:-xxpx to align to top.
http://jsfiddle.net/D7HZR/
Fiddle. Is it what you are looking for?
#1, #2{
float:left;
}
Frankly speaking I'm not sure what you want. I think you want that :
<div id="outer" style="position:relative; width:100%; height:400px; border:#000000 solid 1px;">
<div id="1" style="float:left; width:25%; height:400px; background:#fff000;"></div>
<div id="2" style="float:left; width:65%; height:400px;background:#ff0000;">2
<div id="2a" style="float:left; width:15%; height:auto;background:#000fff;">2a</div>
<div style="clear:both"></div>
</div>
</div>
http://jsfiddle.net/D7HZR/2/
Ok, i think that i've understand your problem, just add the display property on your divs of id 1 and 2
<div id="outer" style="width:100%; height:400px; border:#000000 solid 1px;">
<div id="1" style="display: inline-block; width:25%; height:400px; background:#fff000;"></div>
<div id="2" style="display: inline-block; width:65%; height:400px;background:#ff0000;">
<div id="2a" style="width:15%; background:#000fff;"></div>
</div>
</div>
http://jsfiddle.net/p5KQJ/
#outer div {
float: left;
}
You also need to set a height on the inner div if you want it to take up vertical space.
It's unclear if you want the 3rd div nested or not so I made a fiddle that has one version nested and one with them all side by side.
FIDDLE here

Displaying images and text in a column in wordpress

I'm trying to create a 2 column layout with an image to the left and text to the right in wordpress. This needs to be a max of 600px width, the images will all be a fixed size of 250px wide and 142px high, and I will have image/text repeated below, as in this site http://housemusicpodcasts.co.uk/latest-podcasts/
All my attempts have led to the text just appearing below the images. I'd like to just be able to html/css without the need to change core files.
EDIT: Ok I have this code, but it does not align correctly, any ideas what I need to change?
<div class="pod">
<div class="singleblock">
<div class="wp-caption align:left;" style="width: 250px;"><img title="TEST" alt="" src="http://www.djdavestewart.com/site/wp-content/uploads/2013/04/test.jpg" width="250" height="142" hspace="5" vspace="5" />
<p class="wp-caption-text">TEST CAPTION</p>
</div>
<p>TEXT HERE</p>
<p style="text-align:right;">Read More....</p>
</div>
</div>
<div class="pod">
<div class="singleblock">
<div class="wp-caption align:left;" style="width: 250px;"><img title="TEST" alt="TEST" src="http://www.djdavestewart.com/site/wp-content/uploads/2013/04/test.jpg" width="250" height="142" hspace="5" vspace="5" />
<p class="wp-caption-text">TEST CAPTION</p>
</div>
<p style="text-align: left;">TEXT HERE</p>
<p style="text-align: right;">Read More....</p>
</div>
</div>
CSS
.pod p {
margin: 0px;
padding: 0px;
float: left;
clear: right;
}
.singleblock {
float:left;
clear:left;
margin-bottom: 10px;
}
Any guidance will be much appreciated.
Gary
for each couple of image/text, put them in a div. put a class on this div, and on use this CSS
.yourdive img{
float : left;
}

Resources