How to align unordered list elements with CSS? - css

I would like to build a sample "webshop" project where I would like to list books as on the picture. How can I align text and pictures given this HTML code with CSS similarly as:
HTML code of one book (note this code is replicated the same way with different texts:
<li class="book">
<ul>
<li class="bookcover">
<a href="https://rads.stackoverflow.com/amzn/click/com/1118008189" rel="nofollow noreferrer">
<img src="https://books.google.com/books/content?id=aGjaBTbT0o0C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
alt="Book Cover for HTML and CSS: Design and Build Websites"/>
</a>
</li>
<li class="author">Jon Duckett</li>
<li class="title">
HTML and CSS: Design and Build Websites
</li>
<li class="publisher">John Wiley & Sons</li>
<li class="date">November 2011</li>
<li class="isbn">ISBN: 1118008189</li>
</ul>
</li>
Is there a universal way with CSS to format this?

There are many ways to achieve this. I do want to mention before proceeding that #Tom Faltesek raises a good point. If you have any control over the HTML code, it would be best to approach this with more semantically accurate markup. If, for example, you had the freedom to arrange it this way...
<article class="book">
<div class="book__info">(...)</div>
<div class="book__thumbnail">(...)</div>
</article>
...then your CSS could be done in this way:
.book {
font-size: 0;
}
.book__info,
.book__thumbnail {
display: inline-block;
font-size: 1rem;
}
.book__info {
width: 75%;
}
.book__thumbnail {
width: 25%;
}
Explanation: Inline-block elements naturally follow each other because they are like block elements that display inline with the text. That in mind, setting font-size: 0; on the parent avoids the natural text space that would normally appear between the elements, so that 25% + 75% only spans 100% of the width without extra text spaces between. Remember to reset your font-size on the inner elements with this trick, though.
However, in the way that you are asking this question, I assume you may not have control over this HTML structure. #Gildas.Tambo 's answer will work, visually, but there are challenges that come with floats. The main issue is that float removes an element from the document's flow, so it does not contribute to the height of it's parent. That means the parent has the potential to be 0 pixels high even if its contents are 500+ pixels high. This can cause all sorts of weird buggy layout issues when you get into more involved css.
This seems like a good use case for CSS Grid. At this point, pretty much all the major browsers support it, and it's a hell of a good weapon to have in your arsenal.
Have a look at the code below. Use grid-template-areas to quite literally mock up your layout in the form of words to correspond with elements (in this case, each of your <li>s) and you can span across multiple rows with one element by assigning it to the entire grid area. In the example below, I provided 5 rows and marked the left column as "info" and the right column as "thumbnail", and then to take up all the available space for thumbnail, I just assign .bookcover to it with grid-area: thumbnail;. Don't worry too much about the fancy stuff I did with grid-template-rows, but know that you can use that property to distribute the height of each row.
ul, li {
list-style: none;
padding: 0;
margin: 0;
}
.book ul {
display: grid;
grid-template-areas:
"info thumbnail"
"info thumbnail"
"info thumbnail"
"info thumbnail"
"info thumbnail";
grid-template-rows: repeat(4, auto) 1fr;
}
.bookcover {
grid-area: thumbnail;
}
<li class="book">
<ul>
<li class="bookcover">
<a href="https://rads.stackoverflow.com/amzn/click/com/1118008189" rel="nofollow noreferrer">
<img src="https://books.google.com/books/content?id=aGjaBTbT0o0C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
alt="Book Cover for HTML and CSS: Design and Build Websites"/>
</a>
</li>
<li class="author">Jon Duckett</li>
<li class="title">
HTML and CSS: Design and Build Websites
</li>
<li class="publisher">John Wiley & Sons</li>
<li class="date">November 2011</li>
<li class="isbn">ISBN: 1118008189</li>
</ul>
</li>

You can use float - CSS:left and display : block
.book li{
display: block
}
.bookcover{float:right}
<li class="book">
<ul>
<li class="bookcover">
<a href="https://rads.stackoverflow.com/amzn/click/com/1118008189" rel="nofollow noreferrer">
<img src="https://books.google.com/books/content?id=aGjaBTbT0o0C&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
alt="Book Cover for HTML and CSS: Design and Build Websites"/>
</a>
</li>
<li class="author">Jon Duckett</li>
<li class="title">
HTML and CSS: Design and Build Websites
</li>
<li class="publisher">John Wiley & Sons</li>
<li class="date">November 2011</li>
<li class="isbn">ISBN: 1118008189</li>
</ul>
</li>

Related

Possible to make a list item appear first via css?

I have a jquery mobile listview, and I'd like to make one of the li items appear first in the list via css without moving the li item in the code. I don't want the li item to be in a fixed position (in other words, I do want it to scroll with the list). I just want it to appear as though it were the first li item.
Here is the code for the list (just 2 li items). So I'm wondering how I could style the second li to appear first.
<div data-role="page" data-theme="c">
<div data-role="content">
<ul data-role="listview" data-inset="false"><li>
<a href="http://domain.com/1XyK?id=1117448578">
<img src="https://domain.com/moreicon/EN/bundle-loseweight.png" />
<h2>Healthy Weight Loss</h2>
<p>Now includes Mindful Eating!</p>
<span class="ui-li-count">NEW</span>
</a>
</li>
<li>
<a href="http://domain.com?id=977040364">
<img src="https://domain.com/moreicon/EN/bundle-top10.png" />
<h2>Our Top 10 Apps!</h2>
<p>Save BIG on our chart toppers!</p>
<span class="ui-li-count">SAVE</span>
</a>
</li>
Is that possible?
One possibility, if your browser support allows for it, is Flexbox. This allows you to control the order of elements by using the order property on the child.
For example, if your HTML is:
<ul>
<li>First</li>
<li>Second</li>
</ul>
Then, you can style the UL as a columnar flexbox in CSS:
ul {
display: flex;
flex-direction: column;
}
And you can assign a negative order to the child you want to bring to the top:
ul li:nth-of-type(2) {
order: -1;
}
Working example: http://codepen.io/honzie/pen/oLxENz

CSS - Navigation Links Side Menu

I have a links navigation that drops down on hover (which works fine) then slides right on hover of the li ul li element.
HTML:
<div id="main-links">
<div id="main-links-content">
<ul class="topnav">
<li><a class="link active" href="index.php">Aberdeen Taxis</a></li>
<li><a class="link" href="#!">About Us</a>
<ul class="dropdown">
<li>Who are we?
<ul>
<li>Meet the team</li>
</ul>
</li>
<li>Why use us?
<ul>
<li>Health & Safety Policy</li>
</ul>
</li>
<li>Our commitment to the environment
<ul>
<li>Environmental Policy</li>
</ul>
</li>
</ul>
</li>
<li><a class="link" href="#!">Our Services</a></li>
<li><a class="link" href="#!">Our Tours</a></li>
<li><a class="link" href="#!">Our Fares</a></li>
<li>Online Booking
<ul class="dropdown">
<li>Cash Booking</li>
<li>Account Booking</li>
<li>Credit Card Booking
<ul>
<li>HTML</li>
<li>CSS</li>
</ul>
</li>
</ul>
</li>
<li><a class="link" href="#!">Contact Details</a></li>
</ul>
</div>
</div>
The JSFiddle link: http://jsfiddle.net/mrnaysilva/3Ucd4/
I believe the problem is here (but I may be wrong):-
#main-links-content ul ul ul {
left: 100%;
position: absolute;
top: 80px;
width: 180px;
max-width: 200px;
}
Basically I have set a width and top position. What I want to happen is that the width is set to auto, but when I set this to auto it doesn't set a width according to the paragraph text that's inside it. Also, because I have set a top position, it is always displaying the side menu on the bottom li, where as I want this to display to the right of the li element that is hovered.
i.e. If I hover over About Us and then hover over Who are we?.. Meet the team should display to the right of Who are we?.
I'm just unsure how I can achieve this.
The problem
The main problem that isn't working is the position: relative; of the sub <li> elements.
This is because you use display: table-row;
As stated in the specs:
The effect of 'position:relative' on table-row-group,
table-header-group, table-footer-group, table-row, table-column-group,
table-column, table-cell, and table-caption elements is undefined.
source: http://www.w3.org/TR/CSS2/visuren.html#propdef-position
How to solve this?
Well you can just use an element inside the table-row that will have the position: relative; property:
<li>
<div class="dropdownWrap">
Our commitment to the environment
<ul>
<li>
Environmental Policy
</li>
</ul>
</div>
</li>
Where the css of the .dopdownWrap is:
.dropdownWrap
{
width: 100%;
height: 100%;
position: relative;
}
jsFiddle
Note that i only did this with the "About us" tab.
Some notes about the javascript
You can just find the direct child element with the > selector. This way you don't have to exclude elements with the .not() function. More info here
Instead of defining .slideUp() and .slideDown() seperately, you can define them in one line ( this is because your speed of the animation is the same) with slideToggle.
Some notes about the css + html
If you're assigning classes and IDs to your elements you might as well use them in your css. For example: you never call the class .dropdown in your css. There are more of these IDs and classes that are never used.
Hope this helped you!
Edit
The jQuery UI function slide will function as this, because it will only play the mouseout function when the mousein function has completed. Normally you could cancel the previous animation with .stop(). But seems that jQuery UI slide doesn't support this. So i suggest you to just use plain jQuery for this:
$("#main-links-content li ul li").hover(function () {
$(this).find(' > div > ul').stop(true).animate({left: "100%"}, 300)
}, function () {
$(this).find(' > div > ul').stop(true).animate({left: ""}, 300);
});
Here a great article for this: http://www.learningjquery.com/2009/02/slide-elements-in-different-directions/
I also added the .stop() function to the dropdown menu to fix any delay bugs.
jsFiddle
You may want to look at css transition, as it basically acts the same as you did now.
Here an css transition example: jsFiddle
The first slideToggle is not done in css as it easier and 'better' in jQuery(With css there wouldn't be a dynamic height, which would result in delay in animation).

Block grid shows horizontal scroll bar in Foundation

I am just starting to learn Foundation (from previous messy css experience). I am trying to do a full screen block grid of 4 col images per row. I have this to make the row full width:
.row
max-width: 100%
Here is the code:
<nav class='top-bar'>
<ul class='title-area'>
<li class='name'>
<h1>
<a href='#'>
My Website
</a>
</h1>
</li>
<li class='toggle-topbar menu-icon'>
<a href='#'>
<span>menu</span>
</a>
</li>
</ul>
<section class='top-bar-section'></section>
</nav>
<div class='row'>
<ul class='small-block-grid-2 large-block-grid-4'>
<li>
<img src='http://placehold.it/500x500&text=Thumbnail' />
</li>
<li>
<img src='http://placehold.it/500x500&text=Thumbnail' />
</li>
<li>
<img src='http://placehold.it/500x500&text=Thumbnail' />
</li>
<li>
<img src='http://placehold.it/500x500&text=Thumbnail' />
</li>
</ul>
</div>
I am getting annoying horizontal scroll bar. See below screenshot
I know it is the margin below:
#media only screen
[class*="block-grid-"]
margin: 0 -0.625em;
But do I suppose to override it? It doesn't feel right (seem like a hack). How do I use Foundation properly to display block grid with full screen? It's a simple layout requirement.
If you look at the docs explaining the Foundation grid they already use the box-sizing: border-box star hack
Since the .row containing your block-gridhas a set max-width of 100% it's overflowing the screen width. Typically, elements in the grid would be nested in .rows with defined max-widths and also contained within defined column sizes.
You can simply just do the hacky thing as you deeply fear and adjust the margin:
#media only screen
[class*="block-grid-"]
margin: 0 2em;
Or you can just contain your .block-grid with a container <div class="large-12 columns">.
Six of one half-a-dozen of the other I'd say. If you're afraid of screwing up the grid layout, you can use a conditional class on the body tag so that your customized block-grid only effects the pages that you want.
take a look of this demo by Paul Irish
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
this might be helpful else can you provide a fiddle?
For those who do not want to wrap their block grid within the standard row/column grid, simply hide the overflow on your block grid's parent element. Here's a superfluous code demonstration.
(html)
<div class="block-grid-parent">
<ul class="block-grid">
...
(css)
.block-grid-parent {
overflow: hidden;
}

datalist items need to float

I have the following code which is created with a datalist control:
http://jsfiddle.net/vmE2E/1/
But i am unable to float the to ULs side by side with padding in between.
Please can you help?
edit: added code
<span style="display:inline-block;background-color:Transparent;border-color:#404040;border-style:None;" id="DataList1"><span style="color:Transparent;background-color:Transparent;">
<ul class="latest-posts">
<li>
<a href="http://www.site.co.uk/blog/post/using-jquery.aspx">
using jquery<br>
</a>
</li>
</ul>
</span><br><span style="color:Transparent;background-color:Transparent;">
<ul class="latest-posts">
<li>
<a href="http://www.test.co.uk/First-Blog-Post.aspx">
First Blog Post<br>
</a>
</li>
</ul>
</span></span>
css
ul.latest-posts
{
width:500px;
border:1px solid black;
}
ul li.latest-posts
{
float:left;
}
That is some unusual/unnecessary use of markup (inline block span, span used as containers for block elements, etc). Simply removing that <br> will put your sections next to one another (but not because they float but because they are span, meaning they stay inline)
see fiddle for code and demo
Fiddle: http://jsfiddle.net/vmE2E/2/
Demo: http://jsfiddle.net/vmE2E/2/embedded/result/

CSS Background Image link

Linking a background image with CSS is giving me so me issues. They seem pretty simple but I can't quite get around them:
I have list items for my main menu:
<div id="menuContainer">
<ul id="menu">
<li id="home" title="Home" alt="Home">Home</li>
<li id="current" title="Current Students" alt="Current Students">Current Students</li>
<li id="prospective" title="Prospective Students" alt="Prospective Students">Prospective Students</li>
<li id="facultyStaff" title="Faculty & Staff" alt="Faculty & Staff">Faculty & Staff</li>
<li id="visitors" title="Visitors" alt="Visitors">Visitors</li>
</ul>
my css sets the li to inline-block and gives defines the id's with a size and background image accordingly. I had to use zoom: 1; and *display: inline; for IE to work and everything shows up fine in IE for that now.
When I use text-indent: -9999px; to remove the text and leave the image, Chrome and Firefox works fine with this. However, in IE the whole li shifts the number of pixels listed.
Finally, In Chrome the entire image is the link, in IE and Firefox only the text is the link so with no text the menu has no function.
Any ideas?
You are using syntactically incorrect HTML. You can't wrap an <a> around a <li>. While fixing this may not necessarily make your problem go away, it will probably ensure that every browser behaves the same way.
You're not very clear about what you want to achieve, and what your menu looks like. If you want the whole area of the <li> to become clickable, you're probably best off giving the <a> a display: inline-block and fixed dimensions.
If you need more detailed answers, you may want to give us an online example.
First well form the html, then try your css again.
<ul id="menu">
<li id="home" title="Home" alt="Home">Home</li>
<li id="current" title="Current Students" alt="Current Students"> Current Students</li>
<li id="prospective" title="Prospective Students" alt="Prospective Students">Prospective Students</li>
<li id="facultyStaff" title="Faculty & Staff" alt="Faculty & Staff">Faculty & Staff</li>
<li id="visitors" title="Visitors" alt="Visitors"> Visitors</li>
</ul>
it's better to use line-height instead of text-indent. you need to use image replacement technique. like this
<ul id="menu">
<li><span>Home</span></li>
</ul>
and CSS
ul#menu li a { width: 100px; height: 20px; background: url(../images/myimage.gif) no-repeat 0 0; }
ul#menu li span { line-height: 200px; display: block; }

Resources