Floating alongside a variable-width image - html

Is there a simple way in CSS to position a number of images—stacked vertically—on the right) of a variable-sized image. The variable-sized image has a max-width defined that should be relative to the size of the browser window (i.e. it should be as big as possible to not fall off the screen, but not bigger than the actual image pixel dimensions). To make this more difficult, the markup is such that all <img>s are listed as equals, i.e., the images to appear on the right are not in a separate container.
Using a markup like the following, the size of each image is about equal.
<ul>
<li><img/></li> <!-- the big image -->
<li><img/></li>
<li><img/></li>
etc...
</ul>
By request, in a jsFiddle: http://jsfiddle.net/2p9gR/
It would be nice to do this in pure CSS(3), I don't need to support any browsers except my own (the latest Chrome).
Oh. And I will accept "no" for an answer, if it is the truth.

given your picture I have come up with the following solution:
HTML
<div class="container">
<div class="main-image"><img src="http://lorempixel.com/800/800/sports/1/" /></div>
<ul class="small-image-list">
<li><img src="http://lorempixel.com/120/120/sports/2/" /></li>
<li><img src="http://lorempixel.com/120/120/sports/3/" /></li>
<li><img src="http://lorempixel.com/120/120/sports/4/" /></li>
<li><img src="http://lorempixel.com/120/120/sports/5/" /></li>
</ul>
</div>
CSS
.container {padding-right:150px;}
.container .main-image {width:100%; float:left;}
.container .main-image img {width:100%; max-width:800px; max-height:800px;}
.small-image-list {list-style:none; margin:0 -150px 0 0; padding:0; width:120px; float:right;}
.small-image-list li {width:100%; overflow:hidden; padding-bottom:10px;}
#media all and (min-width: 950px) {
/*this is optional if you want the images to stick left when the page is over 950px;*/
.container {padding:0;}
.container .main-image {width:800px;}
.small-image-list {margin:0 0 0 30px; float:left;}
}
Example
Delete the media query if you want the large gutter
EDIT
Given the need for it all to be in a list you can try this:
HTML
<ul class="list">
<li><img src="http://lorempixel.com/800/800/sports/2/" /></li>
<li><img src="http://lorempixel.com/120/120/sports/3/" /></li>
<li><img src="http://lorempixel.com/120/120/sports/4/" /></li>
<li><img src="http://lorempixel.com/120/120/sports/5/" /></li>
</ul>
CSS
.list {list-style:none; padding:0 150px 0 0; margin:0;}
.list li {width:120px; float:right; padding:0; display:block; overflow:hidden; margin-right:-150px;clear:right; display:block;}
.list li:first-child {width:100%; float:left; margin:0; padding:0;}
.list li:first-child img {width:100%; max-height:800px; max-width:800px;}
List Example

Here is a partial solution. I needed to consider two cases that depend on the aspect ratio of the large image, portrait and landscape.
Case 1 - Portrait
<ul class="portrait">
<li class="first">
<img src="http://placekitten.com/300/1000" />
</li>
<!-- the next two images should 'float' right of the first one -->
<li>1
<img src="http://placekitten.com/800/600" />
</li>
<li>2
<img src="http://placekitten.com/800/560" />
</li>
</ul>
ul.portrait {
list-style: none;
border: 1px solid blue;
margin: 0;
padding: 0;
float: left;
}
ul.portrait li {
border: 1px dotted red;
width: 120px;
float: right;
clear: right;
margin-left: 30px;
}
ul.portrait li img {
width: 100%;
}
ul.portrait li.first {
float: left;
width: auto;
border: 1px dashed blue;
margin: 0;
}
ul.portrait li.first img {
vertical-align: top;
height: 100%;
max-height: 800px;
}
Case 2 - Landscape
<ul class="landscape">
<li class="first">
<img src="http://placekitten.com/1000/500" />
</li>
<!-- the next two images should 'float' right of the first one -->
<li>1
<img src="http://placekitten.com/800/600" />
</li>
<li>2
<img src="http://placekitten.com/800/560" />
</li>
<li>3
<img src="http://placekitten.com/800/560" />
</li>
<li>4
<img src="http://placekitten.com/800/560" />
</li>
<li>5
<img src="http://placekitten.com/800/560" />
</li>
<li>6
<img src="http://placekitten.com/800/560" />
</li>
</ul>
ul.landscape {
list-style: none;
border: 1px solid blue;
margin: 0;
padding: 0;
float: left;
}
ul.landscape li {
border: 1px dotted red;
width: 120px;
float: right;
clear: right;
margin-left: 30px;
}
ul.landscape li img {
width: 100%;
}
ul.landscape li.first {
float: left;
width: auto;
border: 1px dashed blue;
margin: 0;
}
ul.landscape li.first img {
vertical-align: top;
width: 100%;
max-width: 600px;
}
It is possible to get the images appearing in the correct configuration, bit there are some limitations.
Since floats are being used, as you make the screen more narrow, the right hand thumbnails will eventually stack below the large image. This suggests specifying a min-width for the parent ul containing block.
See Fiddle: http://jsfiddle.net/audetwebdesign/rsqW3/
The gist of the problem is in the rule for the large image. In the portrait case, you need to specify height: 100% and max-height: 800px and for the landscape case, you need to specify width: 100% and max-width: 600px. You can't quite make this distinction using CSS alone. The calc() value may be of some help but it is not yet widely supported.

Related

How to move all text down without moving the borders

How do I move all the text and images slightly down to center them without moving the borders down with it?
I tried relative positioning each item but the border kept moving down and out of the height of parent container.
Here is the code (I am using an old page from the BBC website and trying to copy it to learn):
<head>
<meta charset="utf-8">
<title>Technology - BBC News </title>
<style>
body {
margin:0;
padding:0;
}
#topbar{
width:1000px;
height: 40px;
margin: 0 auto;
background-color: red;
}
#topbar-menu li {
list-style-type: none;
float:left;
border-left: 1px solid grey;
height: 40px;
padding: 0 10px 0 10px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="topbar">
<ul id="topbar-menu">
<li><img src="images/bbc-logo.png"></li>
<li id="signin-text">Sign in</li>
<li><img src="images/bell.png"></li>
<li>Sport</li>
<li>Weather</li>
<li>TV</li>
<li>Radio</li>
<li>CBBC</li>
<li>More</li>
<li> <input type="text" value ="Search"> </li>
</ul>
</div>
</body>
I would suggest using the line-height attribute. For example, see screenshot below.
There is no need for <div id="topbar"> because your ul will serve as the container. From there, you can set that ul to be a flex container using the Flexbox layout.
You should also reconsider using fixed sizes because they will only appear correctly on a fixed viewport size.
body {
margin:0;
padding:0;
}
#topbar-menu {
display:flex;
justify-content:center;
margin:0;
padding:0;
background:red;
text-align:center;
height: 3em; /* 300% of the inherited font size */
min-width:900px; /* never less than 900px wide regardless of viewport width */
margin: 0 auto;
background-color: red;
}
#topbar-menu li {
list-style-type: none;
border-left: 1px solid grey;
padding: 1em; /* This is what makes the content vertically align */
font-weight: bold;
}
<ul id="topbar-menu">
<li><img src="images/bbc-logo.png"></li>
<li id="signin-text">Sign in</li>
<li><img src="images/bell.png"></li>
<li>Sport</li>
<li>Weather</li>
<li>TV</li>
<li>Radio</li>
<li>CBBC</li>
<li>More</li>
<li> <input type="text" value ="Search"> </li>
</ul>
Try this. This will center align the contents vertically.
#topbar-menu li{
display: flex;
align-items: center;
}
Try line-height: 100%;
This should let the text be vertically centered

html entries horizontally, two across - line up evenly

I have a large group of "li" elements (consisting of an image, and several lines of text) that resides in a content div. My "li" elements are going to vary in size (some have long links, some have long titles, etc)
I'd like two columns across when possible; I need to do responsive, so when I get to mobile I'll probably have one column across. I thought having a fixed size "li" would do the trick, but obviously not. Each time one of the titles or links is longer than the other, it throws off all the blocks.
Would using div's be better vice using "li" elements?
<div>
<img src="test.png" />
<h2>One - average title is here</h2>
<h3>Link is here and this might be long also</h3>
</div>
Is there a better way to do this without the fixed height for an "li"? And why does my first element always seem lower? I know this isn't rocket science, but I can't seem to come up with the fix.
jsfiddle
I believe the following layout is better:
(Try resizing your browser window less than 480px to view the mobile layout.)
*{
margin: 0px;
padding: 0px;
}
html, body{
width: 100%;
}
li{
width: 40%;
vertical-align: top;
}
#media screen and (max-width: 480px){
li{
width: 80%;
}
}
ul,li {
padding: 0;
margin: 0;
}
#links {
font-size: 0;
text-align: center;
}
#links li {
font-size: 12px;
display: inline-block;
border: 1px solid #000000;
padding: 2px;
background: #c0c0c0;
margin: 5px;
}
#links li h2 {
font-size: 1em;
}
<ul id="links">
<li>
<img src="test.png" />
<h2>One - average title is here</h2>
<h3>Link is here and this might be long also</h3>
</li>
<li>
<img src="test.png" />
<h2>Two - this title is here and is really, really long</h2>
<h3>Link is here </h3>
</li>
<li>
<img src="test.png" />
<h2>Threee -title is here</h2>
<h3>Link is here </h3>
</li>
<li>
<img src="test.png" />
<h2>Four - this title is here and is really, really long</h2>
<h3>Link is here </h3>
</li>
<li>
<img src="test.png" />
<h2>Five title is here</h2>
<h3>Link is here </h3>
</li>
<li>
<img src="test.png" />
<h2>Six title is here</h2>
<h3>Link is here and this area can be long also </h3>
</li>
</ul>
Updated jsFiddle Demo
Readup: CSS #media queries | MDN
Remove that fixed size and put float: left instead.
#links li {
font-size: 12px;
border: 1px solid #000000;
padding: 0;
background: #c0c0c0;
float: left; /*change here*/
margin: 2%; /*responsive margin*/
width: 45%; /*and width*/
}
Now clear the left side of each 2n + 1 div:
#links li:nth-child(2n + 1){
clear: left;
}
And add a media query:
#media screen and (max-width: 400px){
#links li {
margin: 2% 0;
width: 97%;
}
}
JSFIDDLE.

How to think to get all content inline (HTML & CSS)

I'm a really new at this but I don't manage to ge the menu bar to get in line with the rest of the content on the page. Can someone just explain how to think to get everything in line and so everything scale nicely?
HTML:
<div id="all">
<ul id="menu1">
<li><img src="Pic/Homemini.png"></li>
<li><img src="Pic/MeMINI.png"></li>
<li><img src="Pic/portfolioMINI.png"></li>
<li><img src="Pic/contactMINI.png"></li>
</ul>
<ul id="menu2">
<li><img src="Pic/Home.png"></li>
<li><img src="Pic/Me.png"></li>
<li><img src="Pic/portfolio.png"></li>
<li><img src="Pic/Contact.png"></li>
</ul>
<div id="box1MINI"><img src="Pic/box1MINI.png"></div>
<div id="main"><img src="Pic/main.png"></div>
<footer>
<p>DID YOU KNOW?</p>
<p>alsdjaljsdkasjd askldjalksdj <br> asdlkjaslkd asldkjasldj asldk <br> alsdkjalksdj lakdj</p>
</footer>
</div>
CSS:
/* ALL CONTENT */
* {
max-width: 100%;
}
#all{
margin: auto;
}
/* END ALL CONTENT */
/* HEADER */
ul li{
display: inline-block;
width: 20%;
}
#menu2{
display: none;
}
#box1MINI{
max-width: 80%;
min-width: 60%;
margin: auto;
}
/* END HEADER*/
/* MAIN SPACE */
#main{
margin: auto;
max-width: 80%;
}
/* END MAIN SPACE */
/* FOOTER*/
footer{
background-color: darkgreen;
width: 80%;
padding: 1px;
margin: auto;
text-align: center;
}
/* END FOOTER*/
The issue you were getting because you gave "60%" width to "#box1MINI" and "80%" to "#main and footer". And no width to "Menus"
Whereas you should give width to the "Outermost" container which is "#all" in your case. and set that to center by "margin: 0 auto;" property. And keep everything else inside the "#all" container.
I have updated your code and made some modifications as per good practice.
Please find the Updated code below:
HTML
<div id="all">
<ul id="menu1">
<li>
<img src="Pic/Homemini.png">
</li>
<li>
<img src="Pic/MeMINI.png">
</li>
<li>
<img src="Pic/portfolioMINI.png">
</li>
<li>
<img src="Pic/contactMINI.png">
</li>
</ul>
<ul id="menu2">
<li>
<img src="Pic/Home.png">
</li>
<li>
<img src="Pic/Me.png">
</li>
<li>
<img src="Pic/portfolio.png">
</li>
<li>
<img src="Pic/Contact.png">
</li>
</ul>
<div id="box1MINI">
<img src="Pic/box1MINI.png">
</div>
<div id="main">
<img src="Pic/main.png">
</div>
<footer>
<p>DID YOU KNOW?</p>
<p>
alsdjaljsdkasjd askldjalksdj
<br>
asdlkjaslkd asldkjasldj asldk
<br>
alsdkjalksdj lakdj
</p>
</footer>
</div>
CSS
html,
body {
height: 100%;
}
body {
margin: 0 auto;
}
#all {
margin: 0 auto;
width: 80%;
}
/* END ALL CONTENT */
/* HEADER */
ul {
margin: 0;
padding: 0;
}
ul li {
display: inline-block;
width: 24.6%;
margin: 0;
padding: 0;
}
#menu2 {
display: block;
}
#box1MINI {
min-width: 60%;
margin: auto;
}
/* END HEADER*/
/* MAIN SPACE */
#main {
margin: auto;
}
/* END MAIN SPACE */
/* FOOTER*/
footer {
background-color: darkgreen;
padding: 1px;
margin: auto;
text-align: center;
}
/* END FOOTER*/
Working Fiddle: http://jsfiddle.net/96ft87ev/1/
Hope this will help!
Let me know if you were looking for something else!
Add this selector:
#menu1 {
text-align:center;
}
Since you've set the li elements to be displayed as inline, they conform to text-alignment rules.
Edit: After this, the nav bar was still slightly off-center. This is due to a couple reasons. First of all, parts of your HTML are invalid, resulting in tags not being nested properly. Your link, img, and br tags all need to be self-closing. That just means you need to add a / (slash) before the > symbol on the tag.
After you've fixed that, simply remove the padding that is added to the li elements by adding padding:0 to the #menu1 selector.
Here is an example (I've added borders, but you can remove them).
When using percentages, it uses the parent to calculate. You want each li to be 20% of the page, not 20% of the parent (ul). Set the width for menu1 as 80%, then each li is 25% of that. Give the ul auto margins and it will line up with content.
#menu1{
margin: auto;
width:80%; //same as content
padding: 0px;
}
ul li{
display:inline-block;
width: calc(25% -4px); //each image is 25% of the parent, parent is 80% of page
box-sizing: border-box; //includes border and padding in box size
}
fiddle

Simple html code I'm having such a difficult time with

Okay seriously I feel so stupid for not being able to figure this out, I guess it's just Monday or something. Anyway I'm trying simply to make something like this as well as the logic I'm using.
. So I wrote what's happening into a js fiddle
for you all and hoping you can help.
I know that this is going to be one of those things that ends up making me feel so stupid but for some reason I cannot figure out what's wrong.
HTML5
<div id="logo-wrapper">
<ul>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
</ul>
<ul>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
</ul>
<ul>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
<li><img src="Img" alt="Image"></li>
</ul>
</div>
CSS3
#logo-wrapper {
width: 70%;
margin: 2% auto;
background-color: #2CAD96;
}
#logo-wrapper ul {
margin: 2%;
float: left;
background-color: #FFFFFF;
}
#logo-wrapper ul li {
list-style: none;
display: block;
margin: 2%;
}
#logo-wrapper ul li img{
width: 200px;
height: 100px;
}
ul lists have a default padding so you'll probably want to remove that with padding:0;.
Also you set the width of your #logo-wrapper to a certain value, even though the images inside might not fit. So you can either scale the images using width:100%;height:auto; or not set a width on the wrapper.
Also you'll want to add display:inline-block; to the ul and li elements and give them a width 40% or so.
And finally put a padding not a margin on the #logo-wrapper.
Edit: here's the JSFiddle http://jsfiddle.net/6AvNM/9/ if you tweak it some more you'll get your squares sorted out :)
First of all you need to clear floats. In my demo I used clearfix class. Also you might want to set a width to ul and li. I set them to 46% providing that you use margin 2%. Also make sure you reset default paddings on UL and LI.
#logo-wrapper ul li {
list-style: none;
display: block;
float: left;
margin: 2%;
width: 46%;
}
http://jsfiddle.net/6AvNM/11/
Ok, I fiddled around also a bit, the difference to the other posts is: mine is not resizing, you did not specify that. Also on #dfsq's solution, the images loose side-relation, however, nice one. (y)
I used mainly display: inline-block here to avoid clearing floats and giving the wrapper a height. Since this is static now, you either have to give all elements the right size to work (wrap automatically) or put a <br> where you want it to wrap.
*
{
box-sizing: border-box;
}
#logo-wrapper
{
width: 1000px;
margin: 2% auto;
background-color: black;
display:inline-block;
}
#logo-wrapper ul
{
margin: 2%;
display:inline-block;
background-color: red;
padding: 0;
width : 450px;
}
#logo-wrapper ul li
{
list-style: none;
display: inline-block;
margin: 2%;
}
#logo-wrapper ul li img
{
width: 200px;
height: 100px;
}
jsFiddle: http://jsfiddle.net/6AvNM/12/

Weird <ul> element behavior with HTML5 and CSS3

I am attempting to make a simple navigation bar using 4 images, wrapped inside an unordered list.
I am having issues, because the bar is not lining up, it is acting as if the parent div it is nested within has a padding-left assigned to it and pushing the unordered list to the right. Here's a picture of what is happening:
I have a border on the main navigation div to see what is going on.
Here is my code:
<div id="container">
<div id="header">
<h1 class="hidden">Blue Ridge Fencing</h1>
</div>
<div id="navigation">
<ul>
<li><img src="images/website_build/nav_bar/home.jpg" width="208" height="50" alt="Home" border="0"></li>
<li><img src="images/website_build/nav_bar/about.jpg" width="227" height="50" alt="About" border="0"></li>
<li><img src="images/website_build/nav_bar/contact_us.jpg" width="290" height="50" alt="Contact Us" border="0"></li>
<li><img src="images/website_build/nav_bar/quote.jpg" width="235" height="50" alt="Quote" border="0"></li>
</ul>
</div>
<div id="content">
</div>
</div>
And the CSS:
#navigation {
height: 50px;
width: 1000px;
background-image: url(../images/backgrounds/otis_redding.png);
overflow: hidden;
padding: 0px;
border: 1px solid #000;
}
#container #navigation ul {
margin: 0px;
list-style-type: none;
font-size: 34px;
}
#container #navigation li {
float: left;
}
Thank you in advance!
<ul> elements generally have default padding set by the browser (or one of your stylesheets). Just remove it:
#navigation ul {
padding:0;
}
You might want to look into using a CSS reset if you haven't already:
https://stackoverflow.com/questions/167531/is-it-ok-to-use-a-css-reset-stylesheet
Why is there the need for browser resets?
You need to remove the padding from the ul element. You can do by adding padding: 0; to #container #navigation ul in your css.