I'm working on css layout where the first div should contain text, the div's width and height should be flexible according to content. On the right there should be 4 (in future maybe more) images with the same dimensions, at the best adaptable to the first div, but they have to be squares. For now I have just something like this. Any ideas? Thank you!
What about using floats? You would have two container divs one float right. The images could easily be held in a table since they are all the same size. If you know the width of the images ahead of time you could have the first div predefined to that width and height so that if the layout gets too small it won't break apart the images.
<div style="float:right;">
<table>
<tr>
<td><img src="" /></td>
<td><img src="" /></td>
</tr>
<tr>
<td><img src="" /></td>
<td><img src="" /></td>
</tr>
</table>
</div>
<div>
<p>some text here</p>
</div>
You can use the following HTML:
<div id="wrapper">
<div id="display">Some text</div>
<div id="rightBar">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
</div>
Along with the following CSS:
#wrapper {
overflow: auto;
background-color: yellow;
display: inline-block
}
#display {
width: 100px;
height: 100%;
float: left;
border: 1px solid black;
}
#rightBar {
width: 100px;
background-color:red;
float: left;
}
#rightBar div {
border: 1px solid black;
width: 40px;
height: 40px;
display: inline-block;
}
See a live example here
Related
Using Twitter Bootstrap, I'm trying to create a horizontally scrolling series of thumbnails which allows for a scrollbar within the row that the thumbnails are displayed in, like so:
This gets me most of the way there, using this HTML:
<div class="row">Hello there</div>
<div class="row" style="overflow-x:scroll">
<table>
<tr>
<td>
<div class="thumbnail" style="width: 400px; height: 400px">
<img src="http://i.minus.com/iucsUZfSM9v45.gif"/>
</div>
</td>
<td>
<div class="thumbnail" style="width: 400px; height: 400px">
<img src="http://i.minus.com/iucsUZfSM9v45.gif"/>
</div>
</td>
<td>
<div class="thumbnail" style="width: 400px; height: 400px">
<img src="http://i.minus.com/iucsUZfSM9v45.gif"/>
</div>
</td>
</tr>
</table>
</div>
JSFiddle: http://jsfiddle.net/54fgv/2/
The overflow CSS property works great, giving me the scrollbar for the container div.
The thumbnail div elements are going to be a fixed size, which is more than likely going to be smaller than the image. In this case, the image is constrained to fit accordingly. As you can see though, when the image is wider than the thumbnail, the width is set to the thumbnail and the height is scaled accordingly. This is the behavior that I want, but I'd like to have the image vertically centered in the thumbnail.
I've tried adding vertical-align: middle to the thumbnail div elements, but to no avail.
How can I get the image to be centered vertically within the thumbnail?
Approach 1 - (example):
Wrap the img elements:
<div class="thumbnail" style="width: 400px; height: 400px">
<div class="thumbnail_wrapper">
<img src="http://i.minus.com/iucsUZfSM9v45.gif"/>
</div>
</div>
Change the display of the .thumbnail element to table. Use border-collapse: separate to fix padding/spacing issues. Change the display of the wrapper to table-cell and then add vertical-align: middle. Finally, give the img elements a width of 100%.
Example Here
.thumbnail {
display:table;
border-spacing: 2px;
border-collapse: separate;
border-radius:10px; /* Demonstrational.. */
}
.thumbnail_wrapper {
display:table-cell;
vertical-align:middle;
}
.thumbnail_wrapper > img {
width:100%;
}
Approach 2 - (example):
The flexbox approach doesn't require the wrapper element, however it has slightly less support than the table/table-cell approach.
<div class="thumbnail" style="width: 400px; height: 400px">
<img src="http://i.minus.com/iucsUZfSM9v45.gif" />
</div>
Basically, just change the display of the .thumbnail element to flex and then add align-items: center. All the other vendor prefixes are added for cross browser support. Read more about flexbox layouts and properties here - (mdn).
Example Here
.thumbnail {
display: -webkit-flexbox;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
}
As a side note, you can avoid having to use HTML tables - example here.
HTML:
<div class="thumbnail v_align_all" style="width: 400px; height: 400px">
<img src="http://i.minus.com/iucsUZfSM9v45.gif"/>
<span class="v_align_fix"></span>
</div>
CSS:
.v_align_all { white-space: nowrap }
.v_align_all > * {
vertical-align: middle;
display: inline-block !important;
}
.v_align_fix {
height: 100%;
vertical-align: middle;
display: inline-block;
width: 0px;
white-space: nowrap;
}
CSS question: I'm wanting a container with 3 inline images with a border around them (not each image). Under the image row and inside the container border I want a sentence or two of text. Without the text the container border is about the same width and height as the image row using display:inline-block, once I add the text the container width is 100%. I want the text to wrap under the image row and not expand beyond the left/right sides of the image row. I would like to know how this can be done and if it can be done using float:left and/or display:inline, display:inline-block. If it can be done both ways what are the pros and cons.
HTML:
<div class="container">
<img src="image1">
<img src="image2">
<img src="image3">
some text
</div>
CSS:
.container {
display: inline-block;
border: 1px solid black;
}
The following will create a div, with inner blocks for images and a block for text. They should both stay 500px. If the images are > 500px they will be clipped. The text won't cause it to overflow unless its a very long uninterrupted string.
If this doesn't help, use jsFiddle to put up an example.
CSS
.container {
width: 500px;
overflow: hidden;
background:red;
display: inline-block;
border: 1px solid black;
}
HTML
<div class="container">
<div class="images">
<img src="image1">
<img src="image2">
<img src="image3">
</div>
<div class="caption">
some text
</div>
</div>
This is a good example of what I'm wanting but with the text below the images. I would also like it to be HTML 5 compatible.
<div class="container">
<table>
<tr>
<td><img src="image1.jpg" height="200"></td>
<td><img src="image2.jpg" height=200"></td>
</tr>
<caption>a paragraph of text here...</caption>
</table>
</div>
.container {
display: inline-block;
border: 1px solid;
}
table {
margin: 0 auto;
}
Any way to get the red area to fill the rest of the viewable area but not extend into the footer like it is now? I also need the infoContent part to be scrollable. The height of the header portion is variable.
I found a bunch of couple year old answers which said to use JavaScript, but are there any techniques that can be used in modern browsers to avoid that?
HTML:
<div id="page">
<aside id="infoBar">
<header>Variable Sized Header</header>
<div id="infoContent">
<div>First Content Item</div>
<div>Second Content Item</div>
<div>Third Content Item</div>
</div>
</aside>
<footer id="footer">Footer</footer>
</div>
CSS:
#footer { position:fixed; bottom: 0; height: 50px; background-color: rgba(0, 0, 255, 0.5); width: 100%;}
#infoBar { position: fixed; right:0; top: 0; bottom: 50px; padding: 5px; border: 1px solid black; width: 200px; }
#infoBar > header { height: 50px; }
#infoContent { height: 100%; background-color: red; overflow-y: auto; }
#infoContent > div { margin: 5px; height: 50px; background-color: yellow; }
Here's a fiddle to play around with: http://jsfiddle.net/gWmtD/
Using a table was the first thing that came to mind: http://jsfiddle.net/gWmtD/9/
I used inline CSS because it was easier for me to prototype with and you can easily see the changes I've made.
<div id="page">
<aside id="infoBar" style="overflow-y: auto;">
<table style="height:100%; width:100%;">
<tr>
<td>
<header>
Variable Sized Header
</header>
</td>
</tr>
<tr>
<td style="height:100%; width:100%;">
<div id="infoContent">
<div>First Content Item</div>
<div>Second Content Item</div>
<div>Third Content Item</div>
</div>
</td>
</tr>
</table>
</aside>
<footer id="footer">Footer</footer>
</div>
Edit: To enable the scrollbar for the aside when you scrunch the page down in FireFox, add the following property:
overflow-y: auto;
Which will make the y scrollbar appear only when it's needed. This happens by default in Chrome, and can be turned off in Chrome by setting:
overflow-y: none;
A quick google search reveals the overflow method for the scroll bar: http://www.w3schools.com/cssref/pr_pos_overflow.asp
You should look into css wrapper classes for your other problem as a start.
I'm wondering how to make a div to grow bottom-up and not like it does normally.
I've here a example at jsFiddle. As u can see the right div will grow with more content but I want both div to stay fixed at the same level. (The left-div will never grow)
How can i make the right-div to grow bottom-up?
<div id="wrapper">
<div id="div_1">
læskdfsædlfksæ
</div>
<div id="div_2">
<tr>
<td>AUTHENTIQUE</td>
<td class="field_2">1.6 16V</td>
<td class="field_3">239 </td>
</tr>
<tr>
<td>AUTHENTIQUE</td>
<td class="field_2">1.6 16V</td>
<td class="field_3">239 </td>
</tr>
</div>
<div class="clearing"></div>
<div id="base"></div>
</div>
The css:
#wrapper{ overflow: hidden;}
#div_1{
border: 1px solid red;
width: 100px;
height: 50px;
float: left;
}
#div_2{
border: 1px solid black;
width: 200px;
float: left;
}
.clearing{ clear: both;}
#base{ border-top: 2px solid #666;}
No luck with float:left, because they are always aligned top. Change to display:inline-block;
http://jsfiddle.net/HerrSerker/ZEYvk/15/
Note: No whitespace between </div> and <div ...>
Changing float to inline is probably what you are after - but you could also absolutely position your div relative to another div it's inside and use bottom: 0 to keep the div stuck down.
This could be fixed by Javascript.. I used jQuery as shown in the example: jsfiddle
I just added the code:
$(document).ready(function (){
$("#div_1").height($("#wrapper").height());
});
I have a container DIV with a fixed height and width (275x1000px). In this DIV I want to put multiple floating DIVs each with a width of 300px, and have a horizontal (x-axis) scrollbar appear to allow the user to scroll left and right to view everything.
This is my CSS so far:
div#container {
height: 275px;
width: 1000px;
overflow-x: auto;
overflow-y: hidden;
max-height: 275px;
}
div#container div.block {
float: left;
margin: 3px 90px 0 3px;
}
The problem is that the floating DIVs will not continue past the width of the container. After putting three of the floating DIV's they will continue on beneath. If I change overflow-y to auto, then the vertical scrollbar appears and I can scroll down.
How can I change this to make the floating DIVs continue on without going beneath each other?
div#container {
height: 275px;
width: 1000px;
overflow: auto;
white-space: nowrap;
}
div#container span.block {
width: 300px;
display: inline-block;
}
The trick here is only elements that behave as inline by default will behave properly when set to inline-block in Internet Explorer, so the inner containers need to be <span> instead of <div>.
#row {
white-space: nowrap; /* important */
overflow: auto;
}
.items {
display: inline-block;
}
<div id="row">
<div class="items">
<img src="//placehold.it/200/100" alt="item 1" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 2" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 3" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 4" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 5" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 6" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 7" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 8" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 9" />
</div>
<div class="items">
<img src="//placehold.it/200/100" alt="item 10" />
</div>
</div>
The trick here is the "white-space: nowrap" property of the parent which simply tells all it's child elements to continue horizontally and the "display: inline-block" property of it's children. You don't need to add any other property to make this work.
JS Fiddle: http://jsfiddle.net/2c4jfetf/
You need an extra div with a large width to contain the blocks, then they will extend wider than the container div and not drop down to a new line.
The HTML:
<div id="container">
<div id="width">
<div class="block">
<!-- contents of block -->
</div>
<div class="block">
<!-- contents of block -->
</div>
<div class="block">
<!-- contents of block -->
</div>
<!-- more blocks here -->
</div>
</div>
The CSS:
#container {
height: 275px;
width: 1000px;
overflow-x: auto;
overflow-y: hidden;
max-height: 275px;
}
#container #width {
width:2000px; /* make this the width you need for x number of blocks */
}
#container div.block {
float: left;
margin: 3px 90px 0 3px;
}
Wrap your floated divs in another div with the wider width.
<div style="width:230px;overflow-x:auto;background-color:#ccc;">
<div style="width:400px">
<div style="height:100px;width:100px;float:left;border:1px solid #000;"></div>
<div style="height:100px;width:100px;float:left;border:1px solid #000;"></div>
<div style="height:100px;width:100px;float:left;border:1px solid #000;"></div>
</div>
</div>
The table solution should work very well.
If you don't want to use tables, you can also put all .block divs in another div inside the #container and give that "in-between-div" a fixed - calculated - width using javascript after loading the page.
Of course if you already know how many .blocks you have / if the number is fixed, you can give the "in-between-div" a fixed width using css.
It sounds like you are doing gallery with div's?
What exactly are you using the divs for?
It may be easier to use a ul/li with spans inside of the li to get the same effect without all the headaches of floating divs.
Use:
div#container {
overflow: auto;
}
Or add a clearing div below the three divs with the style:
{
clear: both
}
Put the divs you want to scroll in a table like so:
<div style='width:1000;border:2 solid red;overflow-x:auto'>
<table><tr>
<td><div style='width:300;height:200;border:1 solid black'>Cell 1 </div></td>
<td><div style='width:300;height:200;border:1 solid black'>Cell 2 </div></td>
<td><div style='width:300;height:200;border:1 solid black'>Cell 3 </div></td>
<td><div style='width:300;height:200;border:1 solid black'>Cell 4 </div></td>
<td><div style='width:300;height:200;border:1 solid black'>Cell 5 </div></td>
</tr></table>
</div>
Edit:
I tried 3 of these suggested solutions - they all work fine in Google Chrome - but the first one (container1) doesn't work in IE (go figure) - so the SPAN solution gets my vote :-) :
<html>
<body>
<style>
div#container1
{
height: 275px;
width: 100%;
overflow: auto;
white-space: nowrap;
border:2 solid red;
}
div#container1 div.block
{
width: 300px;
height: 200px;
display: inline-block;
border: 1 solid black;
}
div#container2
{
height: 275px;
width: 100%;
overflow: auto;
white-space: nowrap;
border:2 solid red;
}
div#container2 span.block
{
width: 300px;
height: 200px;
display: inline-block;
border: 1 solid black;
}
div#container3
{
height: 275px;
width: 100%;
overflow: auto;
white-space: nowrap;
border:2 solid red;
}
div#container3 div.block
{
width: 300px;
height: 200px;
display: inline-block;
border: 1 solid black;
}
</style>
<p>
<div id='container1'>
<div class='block'>Cell 1 </div>
<div class='block'>Cell 2 </div>
<div class='block'>Cell 3 </div>
<div class='block'>Cell 4 </div>
<div class='block'>Cell 5 </div>
</div>
<p>
<div id='container2'>
<span class='block'>Cell 1 </span>
<span class='block'>Cell 2 </span>
<span class='block'>Cell 3 </span>
<span class='block'>Cell 4 </span>
<span class='block'>Cell 5 </span>
</div>
<p>
<div id='container3'>
<table><tr>
<td><div class='block'>Cell 1 </div></td>
<td><div class='block'>Cell 2 </div></td>
<td><div class='block'>Cell 3 </div></td>
<td><div class='block'>Cell 4 </div></td>
<td><div class='block'>Cell 5 </div></td>
</tr></table>
</div>
</body>
</html>
Edit 2:
I ran this test page through browsershots.org, to see how different browsers handle it.
Conclusion: Browser compatibility sucks. :-)
http://browsershots.org/http://dot-dash-dot.com/files/test_div2.htm
The table solution worked more often - but the span option (which is cleaner) only broke on browsers I've never heard of. :-)
My Ex:
div width: 850px
gridview
templatedcolumn
ItemTemplate
<span class="buttonspanlt"></span><asp:Button ID="imgEditSave" runat="server" Text="Edit SubStatus" CssClass="buttoncenter" OnClick="imgEditSave_OnClick"/><span class="buttonspanrt"></span>
<span style="display:none;float:left;clear:left;" id="spangrdCancel" runat="server"><span class="buttonspanlt"></span><asp:Button ID="imgCancel" runat="server" Text="Cancel" class="buttoncenter"/><span class="buttonspanrt"></span></span>
end ItemTemplate
end templatedcolumn
end gridview
end div
the button has left middle(actual button) right spans which where not floating as there was outer div with fixed width.
I had to use additional div with width 140px outside the button , inside the itemtemplate then it worked.
Hope this helps!!!
Thank You
Harish