How do I prevent images structured as a horizontal list from wrapping? - css

I have a row of distinct images that I want to use as a banner. Upon resizing, I don't want the last image(s) to wrap. I've done prerequisite google and search on stackoverflow; the examples I found were regarding text, not images.
Here's the jsfiddle: http://jsfiddle.net/pHytx/
The most relevant part of the code is probably the CSS:
#slidebanners {
width:100%;
}
#slidebanner ul{
padding: 0;
margin: 0;
overflow: hidden;
}
#slidebanner li{
float: left;
}
#slidebanner img{
height: 200px;
}
.page-header {
font-size: 2em;
padding: .5em;
margin-top: 15px;
}
The problem with this solution is that it doesn't get rid of the gap between the pictures; float gets rid of that gap. I could add a negative left margin, but that has weird effects because the negative margin gets applied unevenly (i.e. the rightmost image needs the most negative margin, but this affects the size of the leftmost image)

For this you can use the exotic display:table-cell;
See Demo:
http://jsbin.com/OxEPuJi/1/edit
Basically display:table-cell; prevents elements to drop on the next line. EVER!
And it also forces them to be next to each other without float.
Codes:
.img {
display:table-cell;
}
I apply this to the wrapper element which holds each image in my demo.

It's best to go for white-space: nowrap combined with display: inline-block, as shown in the accepted answer to the question you linked to.
The problem then becomes how to remove the gaps between the lis.
Here's a demo: http://jsfiddle.net/thirtydot/pHytx/4/
I went with removing the whitespace between the elements in the HTML.
Some people don't like editing their HTML to remove the gaps. To those people I say: deal with it. This is the easiest way to remove the gaps.
<ul>
<li>
<img src="media/images/slides/olympic-1.jpg" />
</li><li>
<img src="media/images/slides/olympic-2.jpg" />
</li><li>
<img src="media/images/slides/olympic-3.jpg" />
</li><li>
<img src="media/images/slides/olympic-4.jpg" />
</li><li>
<img src="media/images/slides/olympic-5.jpg" />
</li>
</ul>
#slidebanner ul {
white-space: nowrap;
padding: 0;
margin: 0;
list-style-type: none;
}
#slidebanner li {
display: inline-block;
vertical-align: top;
}
#slidebanner img {
height: 200px;
vertical-align: top;
}
To hopefully stop these pesky downvotes, here is a working implementation of the display: table-cell approach, thanks to user1721135 for the idea.
http://jsfiddle.net/thirtydot/pHytx/5/
#slidebanner {
float: left;
border: 1px solid red;
}
#slidebanner ul {
display: table;
width: 100%;
padding: 0;
margin: 0;
list-style-type: none;
}
#slidebanner li {
display: table-cell;
vertical-align: top;
}
#slidebanner img {
height: 200px;
vertical-align: top;
}
.page-header {
clear: both;
font-size: 2em;
padding: .5em;
margin-top: 15px;
}

Related

Aligning list items with title

I'm trying to center and unordered list perfectly with the title of my website the title on top with the UL elements centered underneath.
The problem is that it does center, but not perfectly aligned with the overhead title. It is slightly to the right.
Here is my code:
.title{
text-align: center;
}
nav{
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
background: blue;
}
li {
text-align: center;
display: inline-block;
list-style-type: none;
margin: 0;
padding: 0;
background: red;
}
ul {
padding: 0;
}
The slight right-side bias is because of the default padding the entire list gets, not the individual list items. Setting it to zero eliminates the unnecessary offset.
I have to guess the HTML, but most likely you have a <ul> inside your <nav> element. Use this CSS:
nav > ul { margin-left: 0; padding-left: 0; }

Display: table-cell image and text on single line (responsive design)

I want to have list item that has a profile picture in the left hand side and Name in the right hand side. The name should be on a single line with overflow:hidden.
How can this be done with responsive widths and heights? I managed to do this with pixels but not with %.
Example:
JSFIDDLE: http://jsfiddle.net/NjqpC/
html, body {
height: 100%;
width: 100%;
margin: 0;
font-family: Verdana, Arial, Helvetica, sans-serif;
}
.first {
width: 8%;
height: 3%;
}
.second {
list-style: none;
margin: 0;
padding: 0;
}
.third {
border: .1em solid;
font-size: .8em;
max-height: 100%;
display: table;
}
.third img {
max-width: 100%;
display: table-cell;
}
.third span {
display: table-cell;
vertical-align: middle;
overflow:hidden;
}
<div class='first'>
<ul class='second'>
<li class='third'>
<img src='http://static.wikiartis.com/img/profile-small.gif'>
<span>First-Surname Family-name</span>
</li>
</ul>
</div>
In the class third, add "white-space: nowrap"
.third {
white-space: nowrap;
}
The class containing the width and height needs to get an overflow: hidden
.first {
overflow: hidden;
}
In this case, that is the div.first.
To get the image working as well, I gave all parents a height. 100% for every parent other than first.
I gave the img the following styling:
.third img {
display: table-cell;
height: 100%;
width: auto;
}
See fiddle
By adding white-space: nowrap; you tell the text it may not wrap over multiple lines. So that means the text will always stay on the same line. source

Make div size as per contents and horizontally center inside it's parent

I have a div message which basically has a text and a link. I want its size to be changing based on the string inside it. Also I want this message div to be centered inside its container.
I have been playing with it for a while without much luck. Here is the link to JSfiddle: http://jsfiddle.net/pDYJ8/
Also I don't know how make that text and link appear one after other ( not on the new line )
Here is the code:
<div class="container">
<div class="message">
<p>do you want to </p>
<a href="somewhere" target="_blank">
buy this product
</a>
</div>
</div>
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
}
.message {
margin-right: auto;
margin-left: auto;
background: #ddd;
width:100px;
}
Tried display inline block to fit its content but then it wouldn't center it inside its parent.
Keeping width 100px for now just to mock my requirements
Just Tweak Some CSS
See the demo fiddle.
.container {
position: absolute;
background: #666;
top:25%;
width:100%;
font-size: 15px;
color: #e47911;
text-align: center; /*added*/
}
.container .message {
display: inline-block; /*added*/
text-align: left; /*added*/
background: #ddd;
}
.message p { /*added*/
display: inline-block;
}
Explanation
The text-align center causes the now inline-block display of .message to center, which is then reset to have its own text-align back at left (this is not necessary). To get the a on the same line, the p also needs to be some type of inline display, here I chose inline-block as well..
I think you are over complicating things. All you need is a text-align: centeron the container and a display: inline-block on the message:
.container {
background: #666;
font-size: 15px;
color: #e47911;
text-align: center;
}
.container .message {
background: #ddd;
display: inline-block;
}
http://jsfiddle.net/Pevara/pDYJ8/9/
The inline block makes the div act as a word inside text, and the text-align center makes the 'word' align to the center...
Here is a simplified approach to a couple of the answers given. It reduces the amount of HTML and CSS needed.
CSS
.container {
color: #e47911;
text-align: center;
}
.message {
display: inline;
background: #DDDDDD;
}
HTML
<div class="container">
<p class="message">
Do you want to buy this product?
</p>
</div>
I would definitely put your anchor tag, <a> inside the paragraph tag, <p>.
You could even remove display: inline; from .message if you made it a <span> rather than a <p>.
Check this out:
http://jsfiddle.net/pDYJ8/10/
Changes made to above link
.container .message {
margin: 0 auto;
width:auto;
}
span{
background: #ddd;
display:inline;
}
You can simplify it with display: table; and margin: 0 auto;
.container {
display: table;
margin: 0 auto;
background-color: #DDD;
}
<div class="container">
do you want to buy this product
</div>

Fluid layout with even width navigation items

I'm building a site for mobile devices and therefore has a fluid layout.
My navigation list looks like this:
<ul>
<li>home</li>
<li>about</li>
<li>work</li>
<li>contact</li>
</ul>
Problem is, the first list item needs to be 100px only (left aligned always), and the other 3 split evenly, therefore is it possible to have even width for all list items except for the first one (without using javascript).
This is the simplest way I could think of:
ul { overflow: hidden; padding-left: 100px; position: relative; }
li { width: 33.33%; float: left; }
li:first-child { position: absolute; top: 0; left: 0; width: 100px; }
The main idea is taking the first li out of the flow (position: absolute) and adding a padding-left to the ul (space for the first li). Now if we set the percentage width for the other lis, they will take up the remaining space.
And here is a jsFiddle Demo. I added a red border on the ul which shows that because of the percentages lis will not accurately fill it.
I am unsure what mobile browsers you want to support, but except :first-child (which can be worked around by adding a class on the first list item) I assume they must support everything I used.
hmm a bit cludgy - but this seems to work, it does require nesting the list (second 3 links in separate list) and a span for the "home" link, theory is that you need the first link to float, width: 100px, then you need the second group not to float and have their overflow hidden so the group take up the remaining space.. then you float the 3 links # 33% inside the non-floated container
Example : HERE
CSS:
div {
width: 90%;
margin: 0 auto;
}
ul {
margin: 0; padding: 0; list-style: none; /* reset */
float: left;
width: 100%;
}
li {
float: left;
width: 100%;
text-align: center;
}
li span {
float: left;
width: 99px;
background: #eee;
border-right: 1px solid #000;
}
ul ul {
float: none;
overflow: hidden;
width: auto;
}
li li {
width: 33%;
background: #ffe;
border-right: 1px solid #000;
}
HTML:
<div>
<ul>
<li><span>home</span>
<ul>
<li>about</li>
<li>work</li>
<li>contact</li>
</ul>
</li>
</ul>
</div>
For what it's worth, this was what I was thinking of when I made my comment on your question:
http://jsfiddle.net/4t9fV/
ul {
display: table;
width: 100%;
background: #ccc;
table-layout: fixed
}
li {
display: table-cell;
text-align: center;
outline: 1px dashed red
}
li:first-child {
width: 100px
}

CSS List Question

I'm looking for a quick cross browser solution to my need for auto margins.
I have a simple list:
<ul>
<li>text</li>
<li>text</li>
<li class="possibly_last">text</li>
</ul>
With a width of 600px.
What I need is CSS code to make sure there is an even margin between each <li>.
So that they stretch across the full 600px evenly.
I may need to as a "last" class, but that's fine.
I just want a browser friendly way to do this.
Any help would be great, Thanks!
Try this:
<style>
li {
display: block;
float: left;
width: 32%;
}
</style>
If that does not work, try this:
<style>
li {
display: block;
float: left;
width: 200px; // or less
}
</style>
I take it you mean you don't want a margin after the last li? In that case, use the CSS :last-child selector:
ul li
{
margin-right: 10px;
width: 190px; // 190px = 200px - margin width
display: inline-block;
zoom: 1;
*display: inline;
}
ul li:last-child
{
margin-right: 0px;
}
Please note that this will NOT work in any internet explorer except IE9. Sorry :-(
As a fix, you could use JavaScript (notably jQuery) to edit the CSS of the last child.
An example here: http://jsfiddle.net/WtLAm/2/
Are you intending to float the list items so they stretch horizontally to fill the ul that way? if so, something like
<style type="text/css" media="screen">
ul {width: 600px;}
li {display: inline; float: left; width: 33%;}
</style>
would work.
I think this can't be done with margins, I suggest you this solution:
http://jsfiddle.net/wY5t6/
css:
ul li {
margin: 0;
display: block;
float: left;
width: 200px;
text-align: center;
}
ul {
width: 600px;
overflow:hidden;
}
If you need to set padding, background etc on list item than you can do it this way:
http://jsfiddle.net/wY5t6/1/
HTML:
<ul>
<li><span>text</span></li>
<li><span>text</span></li>
<li class="possibly_last"><span>text</span></li>
</ul>
CSS:
ul li {
margin: 0;
display: block;
float: left;
width: 200px;
text-align: center;
}
ul {
border: 1px green dotted;
width: 600px;
overflow:hidden;
}
li span {
background: yellow;
padding: 5px;
}

Resources