For a web application I'm creating (in Umbraco, but don't think that really matters in this case) I need a page that can show an overview of different media types; audio, video and images.
No problem there, for images and videos (hosted on YouTube) I will show a thumbnail and for audio I will show a static image.
The rough layout of an item will be that the image is shown on top, and below that is some info like the title and a short description.
Now because of the difference in dimensions of the images (thumbnails can have a variable size, the audio static image will probably always be smaller than the thumbnails, etc.) one item (or column if you will) can be of less width than another.
What I would like to do is show three items per row, and when the row isn't completely filled I would like to fill it up with a colored box. But that box should not always be at the end, it could also be in between, or the beginning. It just is inserted 'randomly' when a space fill is needed.
Because a picture says more than 1000 words (wire-frame of what I'm trying to describe);
Now my question; is this at all possible? If yes, how?
I can't wrap my mind around it, it can't be done in pure HTML and CSS I think. Because you couldn't determine how big an item is and if a 'filler' is needed.
The rough HTML I have right now is something like this:
<table id="portfolio">
<tr>
<td>
<div class="portfolioItem">
<div class="portfolioItemImage">
<a rel="prettyPhoto" href="http://www.youtube.com/watch?v={video}"><img src="http://img.youtube.com/vi/{video}/1.jpg"/></a>
</div>
<br clear="both" />
<div class="portfolioItemDescription">
<h3>Title</h3>
<p>Description lorem ipsum etc.</p>
</div>
</div>
</td>
</tr>
</table>
Of course there is some more dynamic stuff in there to determine whether it is a video, audio or image, determine when to start a new row, etc. but that isn't relevant here.
Here is the CSS associated with it:
#portfolio {
width:100%;
}
#portfolio td {
width:33%;
}
#portfolio .portfolioItem {
border: 1px solid #000;
}
#portfolio .portfolioItem .portfolioItemImage {
margin:0px;
padding:0px;
}
Again; can this be done? And how?
Thank you!
I think that what you want is jQuery Masonry or the Wookmark jQuery Plugin.
I would create the grid using DIVs instead of TABLES, regardless I think this is what you are looking for?:
#portfolio td
{
min-width:33%;
}
EDIT:
Here is a rudimentary example of a grid created with DIV's:
http://jsfiddle.net/rdtnU/
<div class="con">
<div class="row">
<div class="cell">a</div>
<div class="cell">b</div>
<div class="cell is_last">c</div>
</div>
<div class="row">
<div class="cell">d</div>
<div class="cell">e</div>
<div class="cell is_last">f</div>
</div>
</div>
.con {}
.row { width:340px; margin:0 0 20px 0; overflow:hidden; }
.cell { width:100px; margin:0 20px 0 0; float:left; background:orange; }
.is_last { margin:0; }
I would use the div's as suggested but I would not limit myself to the row/columns as stated. I would use a more fluid layout even if it is for a specified width of a certain section.
The following will only work if you know the width of the div with the content, to allow the floating to occur (this could work if there is a min-width or if your code can determine the size of the image)
Here is the HTML
<div class="elements">
<div class="singleElement">
text and graphics here.
</div>
<div class="singleElement">
text and graphics here.
</div>
<div class="singleElement">
text and graphics here.
</div>
<div class="singleElement">
thisonewillpushthewidthoftheboxfartherthanthe150pxwidth
</div>
<div class="singleElement">
small text
</div>
</div>
Here is the CSS (I put some simple background colors so you can see what is going on with the width and how things are tucked in where space is available.
.elements { overflow: hidden; width: 500px; background: #FCC; }
.singleElement { padding: 5px; white-space:nowrap; float: left;
height: 200px; min-width: 100px; background: #CCC;margin: 0 10px 10px 0; }
Please note the details of the styles are just for demonstrating the example. They can be altered to fit your need.
EXAMPLE: Here is the example in jsFiddle.
Related
As some of you guys may know allowing users to upload images can be a hassle and especially if you have to create some sort of list with them.
I have been looking all over the web and have been unable to find concrete answers to what you do in the case where you need to show a list of images of different shapes. Therefor i turn to you.
Say User 1 uploads the following image:
And User 2 uploads this image:
As you can see these two images are very different in both height and width.
Now lets say that you have 10 images of different sizes and wish to display them in a grid 4 by 4 (for this purpose i use ng-repeat to show a loop)
<div class="col-xs-4" ng-repeat="image in images">
<img alt="" ng-src="{{image}}">
</div>
if you do this, this will create a list that is uneven! and will look very "ugly" to say the least.
So my question is what do you do? Are there any tricks using CSS to make it fit any images of any size so that everything is aligned?
I hope my description of the problem was accurate enough for the sake of demonstration here is a fiddle that shows this issue as well.
In short how do i make sure they are all the same size without making one of the images look cramped and / or distorting the individual image?
fiddle
As mentioned in my comment, one option is to crop all the images to a suitable format, a square might be a good compromise. You can do this by wrapping your images in a container first, and positioning the image in relation to the container. Example:
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
.image-container {
width: 150px;
height: 150px;
overflow: hidden;
position: relative;
padding: 20px;
}
.image-container img {
width: 100%;
height: auto;
position: absolute;
margin: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-xs-12">
<div class="col-xs-4">
<div class="image-container"><img src="http://pngimg.com/upload/girls_PNG6448.png" width="100%" height="100%" class="image image-responsive"></div>
</div>
<div class="col-xs-4">
<div class="image-container">
<img class="image image-responsive" width="100%" height="100%" src="http://yeemei.mobile9.com/download/media/442/niceandsim_s8mhs1do.jpeg"></div>
</div>
<div class="col-xs-4">
<div class="image-container">
<img src="http://pngimg.com/upload/girls_PNG6448.png" width="100%" height="100%" class="image image-responsive"></div>
</div>
<div class="col-xs-4">
<div class="image-container">
<img class="image image-responsive" width="100%" height="100%" src="http://yeemei.mobile9.com/download/media/442/niceandsim_s8mhs1do.jpeg"></div>
</div>
</div>
</div>
Fiddle
You can also position the image in the container. For example if you wanted to center it you could add:
top: -100%;
bottom: -100%;
left: -100%;
right: -100%;
One solution is to provide the users with a cropper to your preferred ratio and allow them to select the part of the image to show.
An alternative is to use the images as background on a div with specific ratio and hope that it does not show irrelevant areas.
Here is a solution for the second case (with a - just for laughs - animation to show the whole of the image)
http://jsfiddle.net/mrccf3sv/
.image{
display:block;
background: url('') 50% 0% no-repeat;
background-size:cover;
border:1px solid #ccc;
animation:pan 10s linear infinite alternate;
}
.image:before{
content:'';
display:block;
padding-top:56.25%; /*ratio of 16:9*/
}
And see it responsive by using different bootstrap column count for each breakpoint.
http://jsfiddle.net/mrccf3sv/1
Scaling with CSS is incredibly bad practice. I mean, we all have to do it sometimes, but if you CAN scale server-side, better do that. Try PHP's imagick, if available.
I am developing mobile application using jQuery Mobile and Phonegap. I want to divide a page into 5 parts and responsive depend on the height of the screen.
I tried this:
<div data-role="content" style="width:100%; height:100%">
<img src="www/image/1.png" style="width:100%; height:20%">
<img src="www/image/2.png" style="width:100%; height:20%">
<img src="www/image/3.png" style="width:100%; height:20%">
<img src="www/image/4.png" style="width:100%; height:20%">
<img src="www/image/5.png" style="width:100%; height:20%">
</div>
What I want is:
emaple
Thanks!
html,body{ height:100%; width:100%}
it will set height of page
As you are using jQuery Mobile, the first thing to do is size the content div to fill the screen. Have a look at this: http://jqmtricks.wordpress.com/2014/02/06/content-div-height-fill-page-height/.
The script to keep the content at the height of the device screen is:
function ScaleContentToDevice(){
scroll(0, 0);
var content = $.mobile.getScreenHeight() - $(".ui-header").outerHeight() - $(".ui-footer").outerHeight() - $(".ui-content").outerHeight() + $(".ui-content").height();
$(".ui-content").height(content);
}
$(document).on( "pagecontainershow", function(){
ScaleContentToDevice();
});
$(window).on("resize orientationchange", function(){
ScaleContentToDevice();
});
Next put your 5 images within the content div, e.g.:
<div data-role="page" id="page1">
<div role="main" class="ui-content">
<img src="http://lorempixel.com/800/300/technics/1/" />
<img src="http://lorempixel.com/800/300/technics/2/" />
<img src="http://lorempixel.com/800/300/technics/3/" />
<img src="http://lorempixel.com/800/300/technics/4/" />
<img src="http://lorempixel.com/800/300/technics/5/" />
</div>
</div>
Finally use CSS to size the images:
.ui-content {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
border: 0;
border-image-width: 0;
}
img {
display: block;
width:100%;
height:20%;
padding: 0;
margin: 0;
border: 0;
border-image-width: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
The first rule removes all padding, margin, borders, etc. from the content div. The second one sizes the images so each one is 20% of the content div.
Here is a DEMO
This also works if you decide to include a header and/or footer on the page as the scaling code takes this into account.
DEMO which includes header
The first answer is correct, but you may find this useful as well.
It may be easier to wrap your images in divs, or use divs and set the images as backgrounds using CSS, unless you just want straight images, which may prove difficult to overlay anything else on top unless you plan on using lots of nesting and absolute positioning. I do this all the time and it works great in all browsers (old IE of course sucks but I don't code for it anymore), AND if you have clients that don't want images easily pulled from a page, putting images in backgrounds is an easy way that will deter most users who won't dig into the code.
Using css3 box-sizing will make your life much easier when dealing with percentage-based divs, as you may decide to add padding to your divs which will make things go nuts. Just check caniuse dot com to make sure that the mobile versions you are targeting support box-sizing. Most of them do, and there should be some polyfills at this link if you want fall-backs for old browsers. They are usually JS files you include in the head or footer with minimal coding.
It is also advised not to use inline CSS if it can be helped, and to use external stylesheets. Pretty standard now unless you're doing HTML emails. I understand you may know that and just put up this snippet as an example, but figure I'd mention it just in case.
Here's what I'd do in your situation:
HTML
<div class="wrap" data-role="content">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="box5"></div>
</div>
and the css (albeit simplified for this example)
div {-webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box}
.wrap {width:100%; height:100%}
.wrap div {width:100%; height:20%}
.box1 {background:url('www/image/1.png') top left no-repeat; background-size:cover}
.box2 {background:url('www/image/2.png') center center no-repeat; background-size:100%}
.box3 {background:url('www/image/3.png') top center no-repeat; background-size:contain}
.box4 .......
you get the idea. and there's more than one way to size backgrounds which is why I showed three, and I don't know what your images are so I can't be sure how you should size them. Modern browsers don't need to have '' around the file path, but I added it for posterity.
Using the above code, you could then add this line of CSS without breaking the page:
.box1, .box2, .box3, .box4, .box5 {padding:20px}
then you could add in text, extra divs, headers, or whatever you wanted, as long as you sized them right. such as:
<div class="box1"><h2>some title</h2><p>some meaningless text</p></div>
As long as you use CSS to position those other elements, things will work fine, and you might just need to add a few #media queries to make sure they align and size properly on various screens.
also, you could use CSS3 flex-box, but support for it is still a bit buggy, and it's more complex, and you might need a bunch of fallbacks for it. You can read more about flex-box at css-tricks. I can't add more than two links to this to give you the direct links.
Hope that helps.
Okay, here you go. I added in some titles and such and a media query so you can see how things can easily be restyled depending on screen size / browser window. You would obviously need your html, head, and body tags.
Here's the working fiddle.
If you still need an explanation let me know. I personally use this method because it's fast and is pure html and css2/3. Instead of the background colors, you would put in the path to your images as I showed in my first reply.
I also adding 2px of padding just to show you that it won't break. You can make it higher, but in the small jsfiddle area it would make the text overflow without a font resize.
HTML
<div class="wrap">
<div class="box box1">
<h2>Title 1</h2>
<p>meaningless text</p>
</div>
<div class="box box2">
<h2>Title 2</h2>
<p>meaningless text</p>
</div>
<div class="box box3">
<h2>Title 3</h2>
<p>meaningless text</p>
</div>
<div class="box box4">
<h2>Title 4</h2>
<p>meaningless text</p>
</div>
<div class="box box5">
<h2>Title 5</h2>
<p>meaningless text</p>
</div>
</div>
CSS
html, body {width:100%; height:100%; margin:0; padding:0; font-family:'Helvetica Neue', Helvetica, sans-serif; font-size:16px}
div {-webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box}
.wrap {width:100%; height:100%}
.box {width:100%; height:20%; text-align:center; padding:2px}
.box1 {background:#555}
.box2 {background:#777}
.box3 {background:#999}
.box4 {background:#aaa}
.box5 {background:#ccc}
h2 {color:#fff;font-size:1.3em;margin-bottom:0}
p {font-size:.85em; font-weight:300; color:#ffcc00; margin-top:-4px}
#media only screen and (max-width: 360px) {
h2 {font-size:.9em}
p {font-size:.7em}
}
Edit: And here's the fiddle with hover states added.
How do you set a background on a div? I have a block looking like this:`
<div class="span4">
<font color="000000">
<h4 class="text-center">This is the title</h4>
<p>This is my text</p>
<a href="#" class="btn btn-success">
<i class="icon-star icon-white"></i> This is my button</a>
</font>
</div>
I want to make this whole block have a background image, but when I try (I've tried 6 different methods already), It either doesn't work at all, doesn't fill the whole box, or fills the whole page.
After using JoshC's CSS related answer, I got this problem. In the image you can see my background, but it doesn't stretch across the whole area I want it to. The side bar, as far across as the left edge of the blue highlight on "Home", is meant to be a different image, (not set up yet) and the rest would be the displayed image.
This is the image
Another thing I'd like is a background applied to multiple divs, but not all, however that's optional...
Any help is much appreciated.
Apply a background-image like this:
.span4 {
background-image: url(http://example.com/image.png);
}
jsFiddle demo
or.. style within the HTML like this:
<div class="span4" style="background-image: url(http://example.com/image.png);"></div>
jsFiddle demo
You obviously have to change the URL to an image that exists.
See the specs on the background property here: https://developer.mozilla.org/en-US/docs/Web/CSS/background-image
.span4
{
background-image:url('your image path');/* by this image set*/
background-color:#cccccc;/* and can use color*/
}
You can give somethinglike this: <div class="span4" style="background:url(../Pictures/website.png) no-repeat fixed;">
We are going to warp your head for a second. Divs are like boxes that exist with no border, no per-defined styles other than 100% width.
You are combining table-line HTML3 standard structure to the nature of a HTML5 div. In the newest code does not contain such attributes like table cells did in the past.
We would accomplish this today through CSS bound to ID's or classes to call those elements that exist within the DOM.
css
<style type="text/css">
.span4{ background-image:url('images/myimage.jpg');
background-repeat:no-repeat;
width:100px; height:100px; }
.span h4{ color:#000; text-align:center; }
.span4 p{ font-weight:bold; /*or whatever*/}
.span4 a{ font-style:italic; color:#0f0; }
.span4 a:hover{}
.span4 a:active{}
.span4 a:visited{}
.span4 a.btn{ color:blue; }
.span4 a.btn-success{ color:green; }
.span4 a.btn.btn-success
.icon-star:before{ content: '* '; }
.icon-white:before{ color:#fff; }
</style>
html
<div class="span4">
<h4>This is the title</h4>
<p>This is my text</p>
This is my button
</div>`
My goal is an alignment as shown in the attached image (the fields on the left may have any width, but the ones on the right should begin at the same X coordinate).
Right now I am using a simple table code to achieve this:
<table><tr>
<td>Left1</td><td>Right 1</td></tr>
<tr><td>Left 2</td><td>Right 2</td></tr></table>
However, I've heard that using tables is generally bad. Is there a way I could achieve the same design using CSS? The website is being designed for mobile devices which might not support fancy CSS, so the code must be as simple as possible.
EDIT: since I still occasionally get a notification on this question from people who (presumably) are just starting out with HTML like I was when I made it, please refer to the accepted answer by B T as this is by far the best way to achieve this functionality. The question suggested as a "possible duplicate" (31 May 2016) does not currently offer the table-row/table-column CSS-based approach and requires you to do guess work.
I found a much easier way to do this by accident. Say you have the following:
<div class='top'>
<div>Something else</div>
<div class='a'>
<div>Some text 1</div>
<div>Some text 2</div>
</div>
<div class='a'>
<div>Some text 3</div>
<div>Some text 4</div>
</div>
</div>
You can align Some text 1 and Some text 2 using css table display styling like this:
.a {
display: table-row;
}
.a div {
display: table-cell;
}
The coolest thing is that as long as the 'top' div is NOT styled display: table, then other things like "Something else" can be ignored in terms of alignment. If the 'top' div IS styled display: table, then "Some text 1" will be aligned with "Something else" (ie it treats all its children like table rows, even if they have a different display style).
This works in Chrome, not sure if its supposed to behave this way, but I'm glad it works.
.a {
display: table-row;
}
.a div {
display: table-cell;
}
<div class='top'>
<div>Something else</div>
<div class='a'>
<div>Some text 1</div>
<div>Some text 2</div>
</div>
<div class='a'>
<div>Some text 3</div>
<div>Some text 4</div>
</div>
</div>
While it is possible to achieve the same with tables, it would be considered semantically incorrect to use a table for the purpose of layout. Especially since you can achieve the same using just a line or two of CSS.
Give your labels a fixed width (something larger than your longest label text).
<style>
label {
width: 100px;
display: inline-block;
}
</style>
<label>Name</label>
<input type="text" />
<br/>
<label>Email Address</label>
<input type="text" />
Example
Here, you could use this for getting the output required.
Using tables IMO is not bad practice, in fact they should be used where tabular data is required, or the format of data resembles a table.
However, designing a full page, or anything not to be displayed in a tabular format, using a table is discouraged, and is in fact very very wrong.
Here goes a sample using a non-table structure:
HTML :
<form>
<label for="name">Email: </label><input id="name" type="email" placeholder="#" />
<br/><br />
<label>Password: </label><input type="password" id="password" placeholder="*"/>
</form>
CSS:
label {
width: 80px;
display: block;
vertical-align: middle;
float:left;
clear:left;
}
input {
border-top-left-radius:5px;
border-bottom-right-radius: 10px;
background: #141414;
color: #fdd56c;
outline: none;
}
Here is an example
Yes, such alignment is possible. Using CSS classes, you can markup your HTML in such a way to achieve the same look of a table without the headache of using a table (or making the markup look ugly).
Using this CSS:
.label {
display: inline-block;
width: 100px;
}
.inputBox {
width: 200px;
}
and this HTML:
<span class="label">E-mail:</span><input type="email"></input><br>
<span class="label">Password:</span><input type="text"></input>
you'll get the layout you want.
To do this with IE7 support, change the CSS above to this:
.label {
display: block;
width: 100px;
float: left;
clear: left;
}
Then, add this line below the lines already shown:
<div style="clear: left"></div>
Example using IE7-compatible settings: http://jsfiddle.net/bbXXp/
True. I am learning it the hard way. I used table for alignment, and now, certain alignments are becoming bizzare in smaller screens (e.g. mobile phone, tablets etc). Hence, am switching over to div. Preferable use <div style="display:inline-block">...</div>, which will align automatically if the screen is smaller.
Hence, my advice is that Table should be used only for genuine tables, and not for aligning controls in a body.
I'd like to be able to position an image (blue) so that what ever size it is, it is always centered relative to the baseline of the div behind (red, not the containing div). The div behind will always be the same size/position.
Preferably this would be using css only as I'm using drupal and I don't know much about editing beyond the tpl files.
Thanks!
EDIT: Here's the layout http://pastebin.com/SisQHM4y
Hi you can do this pure css as like this
css
.wraptocenter {
display: table-cell;
text-align: center;
vertical-align: middle;
width: 500px;
height: 400px;
background:green;
}
HTML
<div class="wraptocenter"><img src="//?" alt="images" /></div>
Live demo http://jsfiddle.net/rohitazad/tvrMp/
More information about this http://www.brunildo.org/test/img_center.html
Perhaps something like this:
<style>
#blue { margin-left:auto;margin-right:auto; }
</style>
<div id="red">
<div id="blue">
<img src="?" id="myImg" />
</div>
</div>
EDIT
I see, so you wish to center the x-axis horizontally, not vertically. And that link is a little messy. Perhaps you could try to
<style>
.user-picture { margin-top: auto; margin-bottom: auto; }
</style>
<div class="content">
<div class="profile" typeof="sioc:UserAccount" about="/users/diver1">
<div class="user-picture">
<a href="/users/diver1" title="View user profile." class="active">
<img typeof="foaf:Image" src="http://waterworksworldwide.com/sites/default/files/pictures/picture-126-1333572014.gif" alt="Diver1's picture" title="Diver1's picture" />
</a>
</div>
</div>
I am still having a little bit of a hard time seeing where the overlap between areas is as suggested by the red and blue in the question. If there is no overlap with my suggestion then please let me know and perhaps we can try to use some variations with position: absolute;