I have a site where I am displaying products. I want the products to fit as many on one line as the set width and margins will allow. I want it to have the items float left (at least have the last items float left), however I also want them centered.
Code (fiddle):
ul {
list-style-type: none;
margin: 0px;
padding: 0px;
overflow: hidden;
}
li {
list-style-type: none;
float: left;
width: 150px;
height: 150px;
margin: 20px;
padding: 3px;
background: #CCCCCC;
text-align: center;
}
li img {
max-width: 144px;
}
<ul>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
</ul>
<ul>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
</ul>
<ul>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
</ul>
As shown on jsfiddle, this is what I essentially have but want the products (represented by the boxes) to also be centered in the page.
You can try display: inline-block instead of float: left:
ul {
text-align: center;
}
li {
display: inline-block;
}
ul {
margin: 0px;
padding: 0px;
text-align: center;
}
li {
display: inline-block;
width: 150px;
height: 150px;
margin: 20px;
padding: 3px;
background: #CCCCCC;
}
li img {
max-width: 144px;
}
<ul>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
</ul>
<ul>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
</ul>
<ul>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
</ul>
Try this, I changed your css:
<style type="text/css">
ul { list-style-type:none; margin:0px; padding:0px; overflow:hidden; text-align: center;}
li { list-style-type:none; display: inline-block; width:150px; height:150px; margin:20px; padding:3px; background:#CCCCCC; text-align:center; }
li img { max-width:144px; }
ul { text-align: center; }
</style>
My solution, trying to preserve your code, is wrapping everything inside a div with a fixed width and margin set to auto:
HTML
<div>
<ul>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
<li><img src="" /></li>
</ul>
</div>
CSS
ul { list-style-type:none; margin:0; padding:0;overflow:hidden;}
li { list-style-type:none;float:left; width:150px; height:150px; margin:20px; padding:3px; background:#CCCCCC; text-align:center; }
img { max-width:144px; }
div{margin: auto;width: 590px;}
FIDDLE
http://jsfiddle.net/33baohc7/1/
Related
I would like 3 images on the first line and 3 images on the second line and want them in the middle of the page
* { box-sizing: border-box; }
ul {
list-style: none;
font-size: 0;
margin: 0;
padding: 0;
text-align: center; }
li { display: inline-block; padding: 10px;}
li:nth-child(1), li:nth-child(4) {
width: 50%;
}
li:nth-child(4) { text-align: right; }
li:nth-child(2) { text-align: left; }
<ul>
<li><img src="http://lorempixel.com/400/200" /></li>
<li><img src="http://lorempixel.com/400/200" /></li>
<li><img src="http://lorempixel.com/400/200" /></li>
<li><img src="http://lorempixel.com/400/200" /></li>
<li><img src="http://lorempixel.com/400/200" /></li>
</ul>
there are many ways to do it, for example:
ul {
list-style: none;
font-size: 0;
margin: 0;
padding: 0;
text-align: center;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
}
https://jsfiddle.net/d6wn79pv/4/
or
ul {
list-style: none;
font-size: 0;
margin: 0;
padding: 0;
display: flex;
flex-wrap: wrap;
text-align: center;
}
ul li {
flex-grow: 1;
width: 33%;
}
https://jsfiddle.net/d6wn79pv/9/
with use of flexbox:
* {
box-sizing: border-box;
}
ul {
list-style: none;
padding: 0;
display: flex;
justify-content:center;
flex-wrap: wrap;
}
li{
width:30%;
margin:3px auto;
}
<ul>
<li><img src="https://via.placeholder.com/150" /></li>
<li><img src="https://via.placeholder.com/150" /></li>
<li><img src="https://via.placeholder.com/150" /></li>
<li><img src="https://via.placeholder.com/150" /></li>
<li><img src="https://via.placeholder.com/150" /></li>
<li><img src="https://via.placeholder.com/150" /></li>
</ul>
check this out.
<!DOCTYPE html>
<html>
<style>
* { box-sizing: border-box; }
ul {
list-style: none;
font-size: 0;
margin: 0;
padding: 0;
text-align: center; }
li {
width: 31%;
display:block;
float:left;
margin:3px;
}
</style>
<body>
<ul>
<li><img src="http://lorempixel.com/100/100/cats/" /></li>
<li><img src="http://lorempixel.com/100/100/cats/" /></li>
<li><img src="http://lorempixel.com/100/100/cats/" /></li>
<li><img src="http://lorempixel.com/100/100/cats/" /></li>
<li><img src="http://lorempixel.com/100/100/cats/" /></li>
<li><img src="http://lorempixel.com/100/100/cats/" /></li>
</ul>
</body>
</html>
You can do it simply by using flex
<ul class="here">
<li><img src="http://lorempixel.com/400/200" /></li>
<li><img src="http://lorempixel.com/400/200" /></li>
<li><img src="http://lorempixel.com/400/200" /></li>
<li><img src="http://lorempixel.com/400/200" /></li>
<li><img src="http://lorempixel.com/400/200" /></li>
</ul>
.here{
list-style: none;
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
width: 100%;
}
.here li {
width: 32%;
}
.here li img{
width: 100%;
}
Change the .here width to auto resize the content
I have a nested list structure in my dom. I want the child list elements to be listed horizontally and take full width. How can I achieve this?
My code
.workicons-list {
padding: 0;
margin: 0;
list-style:none;
display: inline-block;
background: #fff;
}
.workicons-list li {
float: left;
max-width:224px;
border:1px solid #000;
}
.workicons-sub-list {
padding: 0;
margin: 0;
list-style:none;
display: inline-block;
}
.workicons-sub-list li {
float:left;
max-width:224px;
border: none;
}
<div class="options-row">
<ul class="workicons-list">
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive">
<ul class="workicons-sub-list">
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
</ul>
</li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
</ul>
</div>
This is How it looks in full screen
And this is how I want it to be
.workicons-list {
padding: 0;
margin: 0;
list-style:none;
display: inline-block;
background: #fff;
position: relative;
}
.workicons-list li {
float: left;
max-width:224px;
border:1px solid #000;
}
.workicons-list li.expanded {
max-width: 100%;
}
.workicons-sub-list {
padding: 0;
margin: 0;
list-style:none;
display: block;
position:absolute;
top:235px;
left:0;
width: 100%
}
.workicons-sub-list li {
float:left;
max-width:224px;
border: none;
}
<div class="options-row">
<ul class="workicons-list">
<li class="expanded"><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive">
<ul class="workicons-sub-list">
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
<li><img src="https://s31.postimg.org/a6irdmg5n/cleaner.png" class="img-responsive"></li>
</ul>
</li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
<li><img src="https://s32.postimg.org/8t9ekfovp/brush.png" class="img-responsive"></li>
</ul>
</div>
Your .workicons-list li is set to max-width of 224, so anything inside it will by default be only that wide. And, with float on, it'll just tile the inner menu items down. You may need to add a class to the parent li of the opened nested menu so you can override its width to 100%, or whatever width.
Also, not sure if the desired behavior is for the submenu to be ontop of the parent menu when expanded?
How do I put the text beside these images? I've tried everything but I can't seem to figure out how? I'd like it to appear BESIDE the image.
jsFiddle using random image: https://jsfiddle.net/0z1pfbs7/
HTML
<section class="pdc">
<div class="moreproduct">
<div class="bigimg">
<img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" />
</div>
<ul>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
</ul>
<div class="producttext">
<h3>New Balance</h3>
<h1>Men's MX608v4</h1>
<p>SGF</p>
</div>
</div>
</section>
CSS
.pdc{
display: inline;
}
.moreproduct img {
position: relative;
border: 1px solid #93a2ad;
margin-top: 4px;
}
.moreproduct ul{
list-style: none;
}
.moreproduct ul li img{
width: 100px;
height: 100px;
margin-left: 80px;
}
.bigimg{
margin-top: 2em;
position: absolute;
width: 450px;
height: 450px;
margin-left: 240px;
}
.producttext{
position: absolute;
overflow: hidden;
}
Here's the code, you don't need to use position absolute to do this see the code.
.pdc{
display: inline;
}
.moreproduct img {
position: relative;
border: 1px solid #93a2ad;
margin-top: 4px;
}
.moreproduct ul{
list-style: none;
float:left;
}
.moreproduct ul li img{
width: 100px;
height: 100px;
}
.bigimg{
margin-top: 2em;
float:left;
width: 400px;
height: 450px;
margin-left: 20px;
}
.producttext{
float: left;
overflow: hidden;
margin-top: 20px;
}
.clear{
clear:both;
}
<section class="pdc">
<div class="moreproduct">
<ul>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
</ul>
<div class="bigimg">
<img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" />
</div>
<div class="producttext">
<h3>New Balance</h3>
<h1>Men's MX608v4</h1>
<p>SGF</p>
</div>
<div class="clear"></div>
</div>
</section>
As others already mentioned: Don't use absolute positioning to layout your page.
Note: You need to view the example in full-screen to see that everything is aligned "in one line".
Do you mean something like this:
.pdc{
display: inline;
}
.moreproduct img {
position: relative;
border: 1px solid #93a2ad;
margin-top: 4px;
}
.moreproduct ul{
list-style: none;
display: inline-block;
}
.moreproduct ul li img{
width: 100px;
height: 100px;
}
.bigimg{
margin-top: 2em;
display: inline-block;
height: 300px;
}
.producttext{
display: inline-block;
overflow: hidden;
height: 450px;
}
<section class="pdc">
<div class="moreproduct">
<ul>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
</ul>
<div class="bigimg">
<img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" />
</div>
<div class="producttext">
<h3>New Balance</h3>
<h1>Men's MX608v4</h1>
<p>SGF</p>
</div>
</div>
</section>
<style>
.pdc{
display: inline;
}
.moreproduct img {
position: relative;
border: 1px solid #93a2ad;
margin-top: 4px;
}
.moreproduct ul{
list-style: none;
}
.moreproduct ul li img{
width: 100px;
height: 100px;
margin-left: 80px;
}
.bigimg{
margin-top: 2em;
position: absolute;
width: 636px;
height: 450px;
margin-left: 240px;
left: 11px;
}
.producttext{
overflow: hidden;
float:right;
padding-top:25%;
}
</style>
<section class="pdc">
<div class="moreproduct">
<div class="bigimg">
<img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" />
<div class="producttext">
<h3>New Balance</h3>
<h1>Men's MX608v4</h1>
<p>SGF</p>
</div>
</div>
<ul>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
<li><img src="http://i.imgur.com/Xt6vUQD.jpg" alt="" /></li>
</ul>
</div>
</section>
Here's the deal. I need a lot of pictures in one line. Too many to be on whole width of the site. So i need to apply horizontal scrolling. The problem is the header and footer. I need it to be fixed.
<div style="background-color: green; height: 80%">
<div style="background-color: purple; height: 100%; width: 1000px; white-space: nowrap">
<img style="max-height: 100%" src="Barum/01.jpg">
<img style="max-height: 100%" src="Barum/02.jpg">
<img style="max-height: 100%" src="Barum/03.jpg">
<img style="max-height: 100%" src="Barum/04.jpg">
<img style="max-height: 100%" src="Barum/05.jpg">
<img style="max-height: 100%" src="Barum/06.jpg">
<img style="max-height: 100%" src="Barum/07.jpg">
</div>
</div>
<div style="background-color: red; height: 10%"></div>
thanks,
I'm assuming what you want to do is have a sticky/fixed header and footer, with the images appearing 100% vertically in-between.
Nothing scales 100% height unless you have each previous element (each successive parent) stretching 100% height from the html tag. Or you can use the measurement vh. My example below does not use the vh measurement.
Here's a JSFiddle of the following: http://jsfiddle.net/fxxgb85f/6/
For the following structure:
<body>
<div class="header"></div>
<div class="content">
<ul class="imglist">
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
<li><img src="http://sifatit.com/wp-content/uploads/2012/07/dummy-500x337.jpg" /></li>
</ul>
</div>
<div class="footer"></div>
</body>
You will have the following CSS:
html, body { width: 100%; height: 100%; }
body {
margin: 0;
padding: 50px 0;
overflow: hidden;
}
*,
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.header {
position: fixed;
top: 0;
left: 0;
z-index: 5;
width: 100%;
height: 50px;
background-color: #f00;
}
.footer {
position: fixed;
bottom: 0;
left: 0;
z-index: 5;
width: 100%;
height: 50px;
background-color: #f00
}
.content {
position: relative;
margin: 0;
display: block;
width: 100%;
height: 100%;
max-height: 100%;
overflow-x: auto;
}
ul.imglist {
position: absolute;
top: 0;
left: 0;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
list-style-type: none;
overflow-x: auto;
overflow-y: hidden;
}
ul.imglist li {
margin: 0 5px 0 0;
padding: 0;
display: block;
float: left;
width: 100%;
height: 100%;
}
ul.imglist li img {
display: block;
width: auto;
height: 100%;
}
jQuery:
$(document).ready(function(){
horScoll();
// On Load and On Resize
$(window).on('load resize', function(){
horScoll();
});
});
function horScoll(){
var parentSel = $('ul.imglist');
var listSel = $('ul.imglist li');
var liCount = $('ul.imglist li').length;
var imgWidth = $('ul.imglist li img').width();
parentSel.css({ width: liCount * imgWidth });
listSel.css({ maxWidth: imgWidth });
}
I have product page, with 4 bootstrap columns. I am adding products through CMS editor. Below is HTML structure of single product. Now, Images of products have different proportions. So when I added them, they are displayed with different height and width. How to keep them in same size, in different size of screen.
<div class="row">
<div class="col-sm-3">
<p>
<img src="PowerAbWheel.jpg" alt="PowerAbWheel.jpg" style="display:block; margin: auto;">
</p>
<p style="text-align: center;"><strong>Power Ab Wheel<br></strong>
<strong>Price:</strong>
$37.95</p>
</div>
</div>
Here is snapshot of page
Hopefully the images are of same proportion. Limit all the images by max-height and max-width!
.grid {display: block; overflow: hidden; margin: 0; padding: 0; list-style: none;}
.grid li {width: 32%; float: left; margin: 0.5%; height: 80px; display: block; background: #ccf;}
.grid li img {max-width: 100%; max-height: 75px; border: 1px solid #999; display: block; margin: 2px auto;}
<h3>Same Proportions</h3>
<ul class="grid">
<li><img src="http://placehold.it/350x150" alt=""></li>
<li><img src="http://placehold.it/700x300" alt=""></li>
<li><img src="http://placehold.it/175x75" alt=""></li>
<li><img src="http://placehold.it/700x300" alt=""></li>
<li><img src="http://placehold.it/700x300" alt=""></li>
</ul>
<h3>Different Proportions</h3>
<ul class="grid">
<li><img src="http://placehold.it/350x150" alt=""></li>
<li><img src="http://placehold.it/300x300" alt=""></li>
<li><img src="http://placehold.it/175x750" alt=""></li>
<li><img src="http://placehold.it/500x320" alt=""></li>
<li><img src="http://placehold.it/750x100" alt=""></li>
</ul>