CSS layout for box with two text labels? - html

I have an unordered list of items, I want to display each item as a box with two lines of text inside, each line with different properties (font, size, text-align...). The markup I am using for each item looks like this:
<li id="1">
<p class="first"><!-- First line of text --></p>
<p class="last"><!-- Second line of text --></p>
</li>
Using CSS I want it to display sort of like this:
My questions are:
I know this can be achieved in various ways with CSS, but what is the right way or best practice to do it for best cross-browser support? can you point me to a CSS example for something similar? the first text label should be 2 px from the top border and the second one 2 px from the bottom.
Is this the right markup for the text labels? or should I be using other tags instead of <p>? maybe <div>?
I am using display: block for the <li> tags, with fixed width and height.
Thanks in advance.

If I understand correctly, this should be good:
Live Demo
Tested in IE7/IE8 and recent versions of: Firefox, Chrome, Safari, Opera.
I also tested in IE6, works there to.
I'm only doing all this testing because you specifically asked for the "best cross-browser support" :)
I might be tempted to change the <p> tags for <span> tags, because these don't really seem to be paragraphs of text.
CSS:
ul {
list-style: none;
font-size: 200%;
line-height: 0.8
}
li {
display: block;
width: 140px;
height: 100px;
background: #ccc;
padding: 2px;
border: 1px solid #000;
position: relative;
margin: 8px 0
}
li .last {
position: absolute;
bottom: 2px;
right: 2px
}
HTML:
<ul>
<li id="l1">
<p class="first">Text 1</p>
<p class="last">Text 2</p>
</li>
<li id="l2">
<p class="first">Text 1</p>
<p class="last">Text 2</p>
</li>
</ul>

You might be able to use a selector like this:
#1 p + p { padding-right: ..px }
Which if I know anything at all about CSS should get the adjacent sibling of the p element in #1

Here is how I would do it:
li
{
display: block;
padding: 2px;
height: xxpx; /* The height that you gave the li */
width: xxpx; /* The width that you gave the li */
}
li p
{
height: xxpx;
}
li p.first
{
float: left;
}
li p.second
{
float: right;
/*
To place the 2nd text at the bottom right corner you need to give it a
margin top
The value of this margin should be the total height of the li tag minus the
height of text (p tag) and minus the total vertical padding that is 4px.
*/
margin-top: xxpx;
}
I guess that will do it. Hope it helps.

Related

HTML and CSS: fix misbehaving list

I need help in fixing a 2-column list.
The problem is that the right column intrudes into the left one if more than single line used.
Another trouble is that if left column has more than one line, the content inside of the right column will appear at the bottom.
The separating line between columns also acts strange with more than 1 line (see the examples below).
Please note, I'd like to keep "Title" and "Description" columns in separate HTML-tags (at least one of them inside a tag), because I need this for Responsive CSS layout.
ul {
list-style: none!important;
}
ul.my-list>li {
width: 550px!important;
position: relative;
padding-left: 30px;
padding-right: 15px;
background: 0 0;
border-radius: 0!important;
margin: 0;
box-sizing: border-box;
background: #EEE;
}
ul.my-list>li span {
width: 140px;
border-right: 1px solid #E0E0E0;
display: inline-block;
padding: 3px 0;
line-height: 38px;
margin-right: 15px;
}
<ul class="my-list">
<li><span>Title</span>Description. Not too many words – displays well.</li>
</ul>
<br>
<br>
<ul class="my-list">
<li><span>Title</span>Description. More words – this goes wrong. Really wrong. Seriously...At least the "Description" column should not intrude into the "Title" column.</li>
</ul>
<br>
<br>
<ul class="my-list">
<li><span>Title with many words acts weird too</span>Description. How to fix the "Description" column, so it would start from the top side, not from the bottom?</li>
</ul>
<br>
<br>
try this
ul.my-list>li span {
float:left;
}
ul.my-list>li {
min-height: 80px;
}
I've solved the problem, mostly by creating two separated <span> tags for each of the columns and using display: inline-flex; for the whole <li> tag.
The solution is CSS Responsive Layout friendly and works with all window sizes.
JSFiddle: http://jsfiddle.net/tay06de4/4/

Side-by-side links with increased size on hover without upsetting other links

I have a goal that I'm trying to accomplish with HTML and CSS only: With an inline list of links that grow in size upon hover and change fonts (this has caused odd issues before),
A) Hovering one link shouldn't upset other links.
B) Keep it dynamic to avoid tailoring the CSS to each new link.
C) If margin: 0 20px 0 20px, that should be from the ends of the text -- This looks much cleaner, as it makes the spacing constant between links (see example for what should not be done). Fixed width containers usually violate this.
D) Upon hover, the text should stay vertically and horizontally centered in place.
E) Please try to keep it HTML and CSS only. If it includes JS or JQ, it would be more difficult to implement to to my lack of knowledge about the languages and the fact that the JSFiddle is just a stripped down example instead of my actual page.
My best right now fits the first two and last criteria without meeting the third, and it uses tables (I know):
JSFiddle
Or code:
<table><tr>
<td><div>ONE</div></td>
<td><div>TWO</div></td>
<td><div>THREE</div></td>
<td><div>FOUR</div></td>
<td><div>FIVE</div></td>
</tr></table>
And...
div:hover {
font-size: 130%;
font-family: comic sans ms;
}
div {
width: 10px;
margin: 0 30px 0 30px;
height: 20px;
}
The issue is that the margin is being measured from the div, not the text, making it so I'd have to tailor the margin to each link. Additionally, hovering will make it seem like the font / link moves down and to the right, violating guideline D.
This Works dynamically as long as the text isn't too long, but hovering upsets other links. It also violates D.
Any help would be appreciated.
Vertical alignment for inline text is handled easily with the line-height property. Make sure that for both the normal and large font-sizes the line height is the same. Eg. for normal text I used line-height: 1.5em, for the large text I used font-size: 130%; line-height: 1.15385em;. 1.30 × 1.15385 = 1.50
The main issue I see is that when hovering the browser needs the text in the original size for the layout, but it also needs the text in bigger size for display. One solution I see is duplicating the link text and show only one version depending on hover state:
HTML:
<ul>
<li><span>link 1</span><span>link 1</span>
<li><span>link 2, with some long text</span><span>link 2, with some long text</span>
<li><span>link 3</span><span>link 3</span>
</ul>
CSS:
ul, li {margin: 0; padding: 0;}
li { list-style-type: none; display: inline-block; line-height: 1.5em; }
li { border: 1px dotted red; position: relative; }
li a span:first-child { padding: 0 30px; }
li a span:last-child { position: absolute; width: 100%; left: 0; font-size: 130%; line-height: 1.15385em; text-align: center; visibility: hidden; }
li:hover a span:first-child { visibility: hidden; }
li:hover a span:last-child { visibility: visible; }
http://jsfiddle.net/g16Ldusx/2/
Instead of duplicating the link text in HTML, I would probably duplicate it using some javacript.
If you don't want the duplication and really don't want javascript, you can use the :before and :after pseudo-elements instead, and put the link text in a HTML5 data attribute. Not sure how good the browser support for this one is though.
HTML:
<ul>
<li>
<li>
<li>
</ul>
CSS:
ul, li {margin: 0; padding: 0;}
li { list-style-type: none; display: inline-block; line-height: 1.5em; }
li { border: 1px dotted red; position: relative; }
li a:after { content: attr(data-text); padding: 0 30px; }
li:hover a:after { visibility: hidden; }
li:hover a:before { content: attr(data-text); position: absolute; width: 100%; font-size: 130%; line-height: 1.15385em; text-align: center; }
http://jsfiddle.net/kyad4tfh/
Also, note that requirements A and C may conflict with each other. The margin between elements needs to be big enough to accomodate for the increased width of the text.

Links within a list element are interfering with the hover style

I have an unordered list of elements, each list element has a frame, background, and an image within them. When you hover over the list element, the entire background changes to orange, and the text changes to white. But when I link the images within the li to other pages, everything works, except the text stopped changing to white when I hover. The background still changes to orange either way though, and the text color works perfectly as soon as I take out the <a href ""> parts.
HTML
<ol id="selectable">
<li class="ui-state-list">
<b>Title</b>
Text here should change to white when hovering over entire li element.
</br>
<h5 class="discProd">More text</h5>
<a href="www.youtube.com">
<img class="iconAlign" src="images/icons/videoIcon.png">
</a>
</li>
</ol>
CSS
h6 {
font-size: 12px;
color: #2D2D2D;
font-weight: 100;
line-height: 1.5em;
}
.discProd {
font-size: 10px;
color: red;
}
#selectable {
list-style-type: none;
width: 900px;
}
#selectable li:hover {
background: orange;
color: white;
}
.ui-state-list {
border: 1px solid #d3d3d3;
background: #e6e6e6 url(../images/Products/productBG5.png) 50% 50% repeat-x;
/*searched bg gloss wave */
font-weight: normal;
color: #555555;
}
.iconAlign {
float: right;
margin-left: 34px;
margin-right: 8px;
margin-top: -.2em;
}
With this in your CSS
#selectable li:hover span {
background: orange;
color: white;
}
and an added span for the changed text it should all work fine ;)
<ol id="selectable">
<li class="ui-state-list"><b>Title</b>
<span>Text here should change to white when hovering over entire li element. </span>
</br>
<h5 class="discProd">More text</h5>
<a href="www.youtube.com">
<img class="iconAlign" src="images/icons/videoIcon.png">
</a>
</li>
</ol>
First, the code you posted actually does exactly what you want it to do.
But there is a problem.
You're turning the <img> element into a block by floating it (default is inline for images). Putting a block element inside of an inline element (the <a>), is a recipe for disaster (aka rendering outside the bounds of the parent elements as you can see in this fiddle).
To avoid this, remove the float: right on .iconAlign and instead, you can make the <a> element a block and float it. You'll need to change the order of your HTML to have your <a><img></a> setup come at the beginning of your <li> (assuming you're planning to have the element show up on the top right corner as I'm presuming you are).
You also are using an ORDERED list with <ol>. Un-ordered lists are <ul> as the letters suggest. It's also <br /> (XHTML proper) for a line-break, not </br> (although either will result in the same thing in new browsers). You don't actually need that </br> at all since all heading elements (<h1>, <h2>...<h6>) default to block elements that have a line-break before and after to start with.
Here's a little demo
...of what I've covered. I use an icon from my own server to demonstrate the icon placement.
PS - same protocol goes for <li> - not a block element until display: block or float: left/right is applied. More information on your goal with this would be very helpful and I could point you in a more obvious direction (such as using a position: relative/absolute setup with offsets instead of float all together).
PPS - in my demo, Firefox shows my icon smaller than it does in Chrome, just so you are aware that they have different ways of rendering a .ico file.
HTML:
<ul id="selectable">
<li class="ui-state-list">
<a href="www.youtube.com">
<img class="iconAlign" src="http://1054.fleeceitout.com/sites/all/themes/jack/favicon.ico" />
</a>
<b>Title</b>
Text here should change to white when hovering over entire li element.
<h5 class="discProd">More text</h5>
</li>
</ul>
CSS:
h5 {
font-size: 12px;
color: #2D2D2D;
font-weight: 100;
line-height: 1.5em;
}
.discProd {
font-size: 10px;
color: red;
}
#selectable {
list-style-type: none;
width: 900px;
}
#selectable li:hover {
background: orange;
color: white;
}
.ui-state-list {
border: 1px solid #d3d3d3;
background: #e6e6e6 url(../images/Products/productBG5.png) 50% 50% repeat-x;
/*searched bg gloss wave */
font-weight: normal;
color: #555555;
}
a {
float: right;
margin-left: 34px;
margin-right: 8px;
margin-top: -.2em;
}
.iconAlign {
/* no positioning done here since it's an inline element. position using the block element it's inside of - the <a> */
}

How to prevent a span from breaking into the next line - responsive webdesign?

Following simple list, where in every h4, there is a span at the end.
<ul class="items">
<li>
<h4>Prevent LineBreakOfPlus <span class="goto">o</span>
</h4>
</li>
<li>
<h4>Digital Signage <span class="goto">o</span></h4>
… 
</ul>
Screenshot of the page's source:
The CSS for the span looks like this …
.items .goto {
font-family: 'QuaySans-Icons';
font-size: 1.6em;
position: relative;
float: right;
}
The final thing looks like this:
The problem I have with this is that when decreasing the width of the browser window (I'm working on a responsive webdesign) the span-icon is breaking into the next line.
Do you have any creative solution or idea on how to prevent this from happening?
Kind regards and thank you in advance,
Matt
If you want the icon to keep inline with the last word in your text line, you can simply do:
<ul class="items">
<li>
<h4>Prevent LineBreakOfPlus<span class="goto">o</span></h4>
</li>
<li>
<h4>Digital Signage<span class="goto">o</span></h4>
</li>
</ul>
and the CSS might be:
.items {
list-style: none;
margin: 0;
padding: 0;
}
.items li {
border-bottom: 1px solid gray;
margin: 0;
padding: 0;
}
.items h4 {
margin: 0;
}
.items .goto {
background-color: gray;
font-size: 1.6em;
margin-left: 10px; /* optional */
}
If there is no white space between your work and the span, the motif will simply follow the word if the li element is forced to flow into a second line.
You can use margin-left to create visual spacing or insert a &nbsp entity before the span, quite a few ways to do. The details depend a bit on what effect you want.
Fiddle: http://jsfiddle.net/audetwebdesign/VsBet/ (two examples of how to do it)
Keeping Icon Right Justified
Here is one approach to pinning the icon to the right of the h4 element:
.ex2.items h4 {
position: relative;
line-height: 1.5;
outline: 1px dotted blue;
padding-right: 2.00em;
}
.ex2.items .goto {
background-color: wheat;
line-height: 1.00;
font-size: 1.6em;
position: absolute;
right: 0;
bottom: 0.0em;
height: 1.00em;
width: 1.00em;
outline: 1px dotted red;
}
Use absolute positioning of the span to keep it to the right and bottom of h4. If h4 forms to line, the icon will follow the second line. You may need to adjust the positioning depending on the icon size. If you allow the icon to grow in size, you may get other issue in extreme cases. I might fix the icon to a px height or width (or a max value). Finally, set some padding-right in h4 to prevent the icon from overlapping the text as the window gets smaller.
Note I explicitly specified line-height values to accentuate the issue around not knowing the height of the icon. You may need to adjust these to vertically position the icon.
Decrease your font-size when you have less space. I guess you have the problem in media with max-width:480px. I found decreasing the font-size a good alternative to keep the design consistent in responsive sites
I've mocked it up on the demo, however it is a bit raw.
.items {
padding:0;
margin:0;
/*width:180px;*/
}
.items li {
border: 1px solid red;
list-style-type: none;
position: relative;
}
.items h4 {
margin:0; padding:0; font-size:16px; padding-right:10px;
}
.items .goto {
margin-top: -10px;
position: absolute;
right: 0;
top: 50%;
}
DEMO
Check the following link and decrease the width of browser.
RESULT

Sprites and text size

I've created a sprite at a website I'm using to learn CSS at http://flexibletheme.tumblr.com/, however since the width is set at 24px the text tries to make a small vertical column.
Anyway to get it to render normally with 24px of margin on the right?
You should put your sprite inside of a nested <span> instead of wrapping it around your link text. Like this:
<span class="sprite"></span>Sample Link
Be sure to either float your sprite to the left or make it display:inline-block; so that it can retain a width and height but still be inline with your link text.
ditch the width:24px; add padding-left:24px
You should wrap the element around your sidebar unordered list and children instead of closing it before it does anything:
<aside>
<ul>
<li>stuff</li>
</ul>
</aside>
Then give it a width, or let content and sidebar float and clear them after. (I'd also recommend looking into stuff like grids for ease.. http://978.gs/)
write this white-space: nowrap; in your a tag
#links li a{white-space: nowrap;}
If IE7 support (and below) is not an issue, you could use :before pseudo element without adding any extra mark-up:
#links a {
display: block;
position: relative;
padding: 2px 2px 2px 30px;
min-height: 20px;
line-height: 24px;
}
#links a:before {
content: '';
position: absolute;
left: 0;
top: 2px;
width: 24px;
height: 24px;
background: url(http://static.tumblr.com/wgijwsy/itFlt1l8x/links.png);
}
a#rss:before { background-position: -24px 0 }
a#facebook:before { background-position: 0 -24px }
a#twitter:before { background-position: 0 -48px }
Otherwise, add span inside the anchors and replace a:before with a span.icon.
And move the h2 outside of your ul. That's invalid HTML.