So I'm making a pinterest-style page where there are four divs per row and there can be an unlimited number of rows. The problem that I've ran into is that if the height of the divs are not all the same, the divs will go all over the place.
This is what I mean:
What Should happen:
What Actually happens:
My code (I need to keep it simple and can't do four larger divs for each column because of some php stuff that I've added):
<div id="newsList">
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
</div>
CSS:
#newsList {
width: 1330px;
margin: 0 auto 0 auto;
display: table;
height: auto;
overflow: auto;
}
#newsList > div {
width: 313px;
height: auto;
float: left;
cursor: pointer;
margin-left: 15px;
position: relative;
overflow: auto;
border-radius: 2px;
background-color: white;
margin-bottom: 16px;
display: inline-block;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.22);
transition: all 0.3s ease-in-out;
}
Place the an outside container div that contains the divs with the content div that you would like to place inside them that way they can all be different lengths that way that the website pinterest does it.
.container {
width:150px;
float:left;
}
.Blue {
width:140px;
padding:2px;
margin:5px;
background-color:blue;
}
.Green {
width:140px;
padding:2px;
margin:5px;
background-color:green;
}
Here is a fiddle link: http://jsfiddle.net/ztdgz24n/1/
Related
I am trying to put a few images in a row and display text that is directly underneath them and centered this is for my college work. I have tried some code but could not get it to work.
<div class="test">
<p>
<a href="https://www.facebook.com">
<img height="200px" style="display: block; margin-left: 100px; float: left;border: 2px solid white; border-radius: 100%; margin-top: 30px;" src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
</p>
<p>
<a href="https://www.facebook.com">
<img height="200px" style="display: block; margin-left: 500px; float: left;border: 2px solid white; border-radius: 100%; margin-top: 30px;" src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
</p>
<p>
<a href="https://www.facebook.com">
<img height="200px" style="display: block; margin-left: 500px; float: left;border: 2px solid white; border-radius: 100%; margin-top: 30px;" src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
</p>
</div>
Personally I would do so:
.test a {
margin-left: 10px;
float: left;
text-align: center;
overflow: auto;
}
.test a img {
width: 100px;
border:2px solid white;
border-radius: 50%;
}
<div class="test">
<a href="https://www.facebook.com">
<img src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
<a href="https://www.facebook.com">
<img src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
<a href="https://www.facebook.com">
<img src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
</div>
Key points:
Use HTML for markup. Use CSS for style. Separate them.
The p tag is not necessary.
To float the three block on one line you have to set 'float:left' to the <a> element not in the img.
If we use a float based layout we have to Clearing floats (because the floats' container doesn't want to stretch up to accomodate the floats). This is why I added 'overflow: auto' to the container named '.test'
Last but not least, there are tons of method for lay-out things with CSS. I recommend watching Flexbox.
First of all, close your <p> tags.
Then personally I would an inline-block to the p and remove the margin-left on the img.
.test {
text-align: center;
}
img {
display: block;
float: left;
border: 2px solid white;
border-radius: 100%;
margin-top: 30px;
}
p {
display: inline-block;
}
You needed to two things:
HTML wrapping DIVs to be able to control your blocks.
Using CSS
Please, try this fiddle which may meet your requirement:
<style>
div.test {
width:auto;
margin: auto;
}
div.block {
float: left;
}
div.block a{
display:inline-block;
margin-left: 50px;
}
div.block img {
border: 2px solid white;
border-radius: 100%;
width:150px;
}
h3 {
text-align: center;
}
</style>
<div class="test">
<div class='block'>
<a href="https://www.facebook.com">
<img src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
</div>
<div class='block'>
<a href="https://www.facebook.com">
<img src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
</div>
<div class='block'>
<a href="https://www.facebook.com">
<img src="https://scontent-lhr3-1.xx.fbcdn.net/v/t1.0-9/12512240_1005566046198664_2775546349199755757_n.jpg?oh=a37edadd99a4631e165accf6bd193a9f&oe=58C8AD58">
<h3>Test</h3>
</a>
</div>
</div>
I have a problem with image descriptions. I have HTML structure like this:
<div class="scrollable-content" data-mcs-theme="dark-thick" style="padding: 0px; overflow-x: auto;">
<ul style="list-style: none; white-space:nowrap; padding: 0px;">
#foreach($projects as $project)
<li style="display: inline; margin: 0px;">
<a href="{!! url($locale.'/projects/project/'.$project->id) !!}">
<img class="project-cover-image" src="/images/{!! $project->cover_image_name !!}" height="250px" width="auto">
</a>
</li>
#endforeach
</ul>
</div>
It creates a nice looking gallery with horizontal scrollbar. But I need to add descriptions to images that will be placed at the bottom of the pictures covering whole their widths and they should have to be transparent to some degree.
The problem is, whatever I do, I either get description that takes 100% width of the page, or it has width of the text inside it.
I have tried doing it with div, span, different combinations of position absolute/relative, everything and I couldn't manage to make it work.
It should look something like this:
How can I do that?
You have two options (wich produce the same result):
1- A div with a image as background, and a subtitle inside this div;
#image {
width:550px;
height:150px;
position:relative;
background: url('http://i.imgur.com/HNj6tRD.jpg');
background-repeat: no-repeat;
background-size:100% 100%;
}
.coverdown {
color: white;
width: 100%;
height: 30%;
position:absolute;
bottom:0%;
background: rgba(0,0,0,0.5);
text-align: center;
}
<div id="image">
<div class="coverdown">Subtitle here with a description.</div>
</div>
2- The image and a subtitle with position:absolute inside a position:relative container;
#container {
width:550px;
height:150px;
position:relative;
}
img {
width:550px;
height:150px;
position:absolute;
top:0px;
left:0px;
}
.subtitle {
color: white;
width: 100%;
height: 30%;
position:absolute;
bottom:0%;
background: rgba(0,0,0,0.5);
text-align: center;
}
<div id="container">
<img src="http://i.imgur.com/HNj6tRD.jpg" alt=img>
<div class="subtitle">Subtitle here with a description.</div>
</div>
use position:relative/absolute
body {
margin: 0
}
.scrollable-content {
padding: 0;
overflow-x: auto
}
ul {
list-style: none;
white-space: nowrap;
padding: 0;
margin:0
}
li {
position: relative;
display:inline-block
}
span {
background: rgba(0, 0, 0, .5);
display: inline-block;
height: 50px;
position: absolute;
bottom: 0;
left: 0;
width: 100%
}
img {
display: block
}
a {
color: #fff
}
<div class="scrollable-content" data-mcs-theme="dark-thick">
<ul>
<li>
<a href="">
<img class="project-cover-image" src="//lorempixel.com/250/250">
<span>description</span>
</a>
</li>
<li>
<a href="">
<img class="project-cover-image" src="//lorempixel.com/500/250">
<span>description</span>
</a>
</li>
<li>
<a href="">
<img class="project-cover-image" src="//lorempixel.com/400/250">
<span>description</span>
</a>
</li>
</ul>
</div>
basicly, you need relative/absolute as #dippas's answer.
as I advised, use inline-block, so it gets sized by content and will allow easily to position your description.
example below with figure and figcaption.
figure can be wrap in a <a>link displayed inline-block and figure as a block then to avoid a gap underneath.
figure {
display: inline-block;
position: relative;
margin: 0;
}
img {
display: block;
max-height:80vh;height:250px/*snippet demo purpose */
}
figcaption {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
/* makeup*/
background: rgba(0, 0, 0, 0.5);
color: white;
}
/* demo purpose */
div {
white-space: nowrap;
overflow:auto;
}
figure {
white-space: normal;
text-align: center;
}
<div>
<figure>
<img src="http://www.hyperkreeytiv.com/wp-content/uploads/2014/08/IMG_4973.jpg" alt="wolves" />
<figcaption>
<p>these are wolves</p>
</figcaption>
</figure>
<figure>
<img src="http://4.bp.blogspot.com/-oSEWgZNEopE/TtL8kfGuBzI/AAAAAAAAB6U/b8VSzZaoK3g/s400/action_wolf_1111_photo1.jpg" alt="wolves" />
<figcaption>
<p>these are wolves</p>
</figcaption>
</figure>
<figure>
<img src="http://1.bp.blogspot.com/-GfOyrk3kZ0w/TewM0BMvbNI/AAAAAAAABM0/KPm3li5Xwko/s1600/alpha+male+Wallpaper__yvt2.jpg" alt="wolves" />
<figcaption>
<p>these are wolves</p>
</figcaption>
</figure>
<figure>
<img src="http://www.ooyuz.com/images/2015/10/13/1447449028465.jpg" alt="wolves" />
<figcaption>
<p>these are wolves</p>
</figcaption>
</figure>
</div>
Sorry if I posted something similar to other questions, but actually I needed solution. Well what I am trying to do is am creating gallery of images. So I have images of 200px, each image is block. So in order to center those image according to screen-sizes I am trying to wrap those images inside other div(i.e. inline-block). But its not ready to move in center.
I also want to mention that I am using this within a container.
.gallery{
display:inline-block; /* I am not sure what to do here */
}
.thumbnail{
width:200px;
height:200px;
border:1px solid #aaa;
border-radius:5px;
display:block;
}
.thumbnail img{
width:100%;
height:100%;
border:5px solid white;
border-radius:5px;
}
.gallery-item{
float:left;
margin:10px;
}
.clearfix:after{
clear:both;
}
.clearfix:after, .clearfix:before{
display:table;
content: " ";
}
.clearfix:after{
content:" ";
height:10px;
}
<div class="gallery">
<div class="thumbnail gallery-item">
<img src="images/slide1.jpg" />
</div>
<div class="thumbnail gallery-item">
<img src="images/slide1.jpg" />
</div>
<div class="thumbnail gallary-item">
<img src="images/slide1.jpg" />
</div>
<div class="thumbnail gallery-item">
<img src="images/slide1.jpg" />
</div>
<span class="clearfix"></span>
</div>
Don't use float property if you want elements to be centered.
.gallery {
font-size: 0; /*fix for white space */
text-align: center;
}
.thumbnail {
display: inline-block;
margin: 10px;
width: 200px;
height: 200px;
border: 1px solid #aaa;
border-radius: 5px;
box-sizing: border-box;
}
.thumbnail img {
max-width: 100%;
height: auto;
border: 5px solid white;
border-radius: 5px;
box-sizing: border-box;
}
<div class="gallery">
<div class="thumbnail">
<img src="http://placehold.it/200x200" />
</div>
<div class="thumbnail">
<img src="http://placehold.it/200x200" />
</div>
<div class="thumbnail">
<img src="http://placehold.it/200x200" />
</div>
<div class="thumbnail">
<img src="http://placehold.it/200x200" />
</div>
</div>
JSFiddle: http://jsfiddle.net/ntbh1rmk/
well since i need 50 reputation to comment (very stupid restriction in my opinion).. i was going to ask why you have 100% for width and height, then add 5px padding to it? isn't that like making it 100% + 10PX?
.thumbnail img{
width:100%;
height:100%;
border:5px solid white;
border-radius:5px;
maybe it's just how i'm being taught... (i'm in CSS class now).. but thats always been a bad thing.
This is what I'm trying to achieve. My problems are of course at generating the thumbnails right. What I tried is using a shadow rather than a border for the grey outline and I intended to put a margin for the thumbnails so they get to have the common outline you can probably see. I'm pretty much out of ideas and approaches to getting this to work so I would like to hear your suggestions. Thing is I want this to be responsive so I gave percent dimensions to the thumbnails and all. Here is the code:
<div class="gallery preview">
<div class="main frame">
<img src="images/pic-1.png" class="img-responsive" alt="a">
</div>
<div class="navigation">
<div class="control left">
<i class="fa fa-fw fa-chevron-left"></i>
</div>
<div class="thumb clearfix">
<div class="frame">
<img src="images/pic-1-thumb.png" class="img-responsive" alt="a">
</div>
<div class="frame">
<img src="images/pic-2-thumb.png" class="img-responsive" alt="a">
</div>
<div class="frame">
<img src="images/pic-3-thumb.png" class="img-responsive" alt="a">
</div>
<div class="frame">
<img src="images/pic-1-thumb.png" class="img-responsive" alt="a">
</div>
<div class="frame">
<img src="images/pic-2-thumb.png" class="img-responsive" alt="a">
</div>
<div class="frame">
<img src="images/pic-3-thumb.png" class="img-responsive" alt="a">
</div>
<div class="frame">
<img src="images/pic-1-thumb.png" class="img-responsive" alt="a">
</div>
</div>
<div class="control right">
<i class="fa fa-fw fa-chevron-right"></i>
</div>
</div>
</div>
and the css:
.preview.gallery {
margin: 10px 0;
}
.preview.gallery .frame {
border-radius: 3px;
}
.preview.gallery .thumb {
max-width: 508px;
float: left;
margin: 1px;
}
.preview.gallery .navigation .control {
width: 22px;
display: block;
}
.preview.gallery .navigation .control.left {
float: left;
}
.preview.gallery .navigation .control.right {
float: right;
}
.preview.gallery .thumb .frame {
width: 14.28571428571429%;
display: block;
float: left;
}
.preview.gallery .thumb .frame img {
width: 100%;
}
.preview.gallery .frame {
box-shadow: 0 0 1px 0 rgba(0,0,0,0.5);
}
.preview.gallery .main.frame {
border: 8px solid #fff;
}
.preview.gallery .navigation .control {
background: #fff;
line-height: 74px;
box-shadow: 0 0 1px 0 rgba(0,0,0,0.5);
margin-top: 1px;
border-radius: 3px;
}
1. Set the position of .navigation to relative (so that we can place the two controls on the side of this fluid div):
.navigation {
position:relative;
}
2. Add to .preview.gallery .navigation .control this css code:
.preview.gallery .navigation .control {
position:absolute;
top:0;
}
3. and to the left navigation div:
.preview.gallery .navigation .control.left {
left:0;
}
4. and to the right navigation div:
.preview.gallery .navigation .control.right {
right:0;
}
5. Finally change the margin on .preview.gallery .thumb to:
.preview.gallery .thumb {
margin:0 22px; /* or whatever width you have on the controls */
}
DEMO.
Note
You mentioned wanting some space between the thumbnail and the border. In order for that to work with the percentege values you assigned them, you will want to add this code too:
.preview.gallery .frame {
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
So i have an image, I have a text box with a transparent background that is overlaid on the image (FYI it contains the price of the item and if it is onsale).
I would like the text box to "fit" the width of the image it is over.
Currently the text is wider than the image. I've tried adjusting the width but that only seems to shrink it and move the box out from being over the image.
http://jsfiddle.net/Ggqy4/
Here is what I'm aiming to create. Notice the text is only as wide as the image.
http://imgur.com/zKzjIyF
The red box inside the box is on the first item on the left, the girl in the vest.
HTML:
<div class="date-container">
<div class="date-heading" style="display: block;">Friday, Oct 11, 2013</div>
<div class="items-purchased-container">
<DIV style="position: absolute; top:10px; left:355px; width:200px; height:25px">3</span> items purchased</p>
</div>
<div class="total-spend-container">
<div class="product">
<img src="https://theory.alluringlogic.com/api/v1/productvariation/3/1058.jpg?preset=XS" alt="" />
<div class="description">
<p>Sex shirt in sparkly black <span class="price">Price $500</span>
</p>
</div>
</div>
<div class="product">
<img src="https://theory.alluringlogic.com/api/v1/productvariation/3/1058.jpg?preset=XS" alt="" />
<div class="description">
<p>Sex shirt in sparkly black <span class="price sale">Sale Price $500</span>
</p>
</div>
</div>
CSS:
body {
background:#00000;
text-align:center;
}
.product {
display:inline-block;
position:relative;
margin-right:10px;
vertical-align:top;
}
.product img {
display:block;
max-width:100%;
}
.description {
position:absolute;
background:rgba(255, 255, 255, 0.75);
top:60%;
;
text-align:center;
width:100%;
}
.description span {
display:block;
margin-top:10px;
padding:5px;
}
.sale {
background:red;
}
Here is a JSFiddle file for your review: CLICK HERE
As mentioned above by Giovanni Silveira, your images contain a large left/right border. You can either crop in your favorite image editor or you will have to manipulate your code to accommodate the added area to your liking.
I did add some changes to force the appearance you were looking for, however if you want a less intrusive set up then it would be prudent to change you images to fit your needs.
To view how the page was set up, just add
border: 1px solid #ccc;
to your product and description classes to see the flow and gain a better understanding of the images impact on flow of document.
Hope this helps.
Here is the code:
HTML:
<div class="orderinfo">
<div class="date">Friday, Oct 11, 2013</div>
<div class="purchaseitems">3 items purchased</div>
</div>
<div class="total-spend-container">
<div class="product">
<img src="https://theory.alluringlogic.com/api/v1/productvariation/3/1058.jpg?preset=XS" style="width:300px;height:250px" alt="" />
<div class="description">
<p>Sex shirt in
<br>sparkly black</br> <span class="price">Price $500</span>
</p>
</div>
</div>
<div class="product">
<img src="https://theory.alluringlogic.com/api/v1/productvariation/3/1058.jpg?preset=XS" style="width:300px;height:250px" alt="" />
<div class="description">
<p>Sex shirt in
<br>sparkly black</br> <span class="price sale">Sale Price $500</span>
</p>
</div>
</div>
CSS:
body {
background: #00000;
text-align: center;
}
.orderinfo {
position: relative;
width: 100%;
min-width: 650px;
font-family: "Courier", sans-serif;
font-size: 20px;
}
.date, .purchaseitems {
float: left;
width: 49%;
padding-bottom: 10px;
}
.total-spend-container {
min-width: 650px;
}
.product {
position: relative;
display: inline-block;
width: 300px;
height: 250px;
}
.product img {
background-position: center;
background-size: cover;
}
.description {
position: absolute;
background: rgba(255, 255, 255, 0.75);
font-family: "Courier", sans-serif;
font-style: italic;
top: 50%;
text-align: center;
left: 25%;
width: 50%;
}
.description span {
display: inline-block;
margin-top: 10px;
padding: 3px;
}
.sale {
background: red;
color: white;
}