Forum Moderators: not2easy

Message Too Old, No Replies

Styling address information

         

MarkMathers

8:16 pm on Feb 9, 2011 (gmt 0)

10+ Year Member



6 months later and I'm still swearing at CSS when I spend two hours trying to do something I could do with tables in 1 minute.

Sorry...just venting.

So I have a div box. Inside the box I'm trying to do simple formatting of text.

First line I want H2 text with my business name on the left. On the same line I'm trying to get my phone number to the right side of the box using a separate class of text.

On the next line I'm trying to get 3 custom types of text. One to the left, one centered and one to the right.

Span, inline, float, align, header tags...I'm confused. I'm trying to make this compatible with all browsers too. Can anyone help? This should be very easy, I'm just not sure how to do it.

Sub_Seven

9:44 pm on Feb 9, 2011 (gmt 0)

10+ Year Member



Hey Mark,

Good call trying to move away from tables :)

Try this and see if you like it, I just played with colors but you should get the idea

<html>
<head>
<style type="text/css">
#page {width: 960px; height: 100%; border: 1px solid black;}
span {float: right; color: brown;}
#leftp, #centerp, #rightp {width: 33%; float: left; text-align: center;}
#leftp {color: red;}
#centerp {color: blue;}
#rightp {color: green;}
</style>
</head>
<body>
<div id="page">
<h2>Business name</h2><span>1-866-callme</span>
<p id="leftp">I'm on the left</p><p id="centerp">I'm on the center</p><p id="rightp">I'm on the right</p>
</div>
</body>
</html>

MarkMathers

4:22 am on Feb 11, 2011 (gmt 0)

10+ Year Member



Thanks for the effort, but when I saved and looked at that everything was all over the place, not on the same line. I've programmed C, C++, Basic, VB.NET, HTML and probably a couple others I've forgot and CSS is by far the most anti-intuitive thing I've ever seen. Is there anyone that's been using CSS for a decade or so that has figured out how to do this extremely basic thing? Seriously, look at my made up code below...how easy is that? Why couldn't the geniuses who came up with css just have done something like that? Intuitive and simple, but no, let's turn some basic word processor functionality into a two week nightmare. Sigh. Well if anyone has the answer on how to do this the right way, please let me know. As you can tell I'm extremely frustrated right now.

<HTML>
<text.h2>This is a header</text>
<text.phone>800-555-1212</text><br>

<text.address>123 Jaybird Ln.</text>
<text.info>info</text>
<link.custom>www.whatever.com</link>

CSS

text.phone {halign:right}
text.address {halign:left}
text.info {halign:center}
link.custom {halign:right}

alt131

2:43 am on Feb 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mark, and welcome to WebmasterWorld [webmasterworld.com]

as subseven says, great move away from tables. if I correctly understand what you want, there are many different ways to do this, depending on personal taste, and what browsers you need to support. This example uses a range of options to get you going.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>My Title</title>
<style type="text/css">
* {margin:0; padding:0}

div {
width:402px;
border:1px solid black;
margin: 0 auto;
}

h2 {color:red; }

h2 span {
color:blue;
font-size:0.6em;
display:inline-block;
width:30%;
text-align:right;
position:relative;
right:-30%;
}

ul {
list-style-type:none;
width:100%;
border:1px solid red;
display:table-row;
}

li {
display:table-cell;
width:134px;
outline:1px solid tan;
text-align:center;
}

li:first-child {color:green; font-style:italic}
li a {color:fuchsia; font-weight:bold;}
</style>
<!--[if lt IE 8 ]>
<style type="text/css">
li {float:left;}
</style>
<![endif]-->
</head>

<body>
<div>
<h2>This is a header<span>800-555-1212</span></h2>
<ul>
<li>123 Jaybird Ln.</li>
<li>info</li>
<li><a href="#">www.whatever.com</a></li>
</ul>
</div>

</body>
</html>
Why couldn't the geniuses who came up with css just have done something like that?

In part because the writers of the css recommendations [w3.org] did not have the luxury of writing css from scratch. It was an add-on in response to demands to be able to style html elements, and many problems with css implementation derive from historical arguments about how html elements should behave.

Also, css is about style, which has artistic and creative component, while the codes you mention are technical programming languages. They have different perspectives that will collide badly unless their relative merits are enjoyed.

You might find the posts that veered off into history in this thread validating css [webmasterworld.com] interesting.

MarkMathers

4:08 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



Thanks for the help. Let me ask this...while I was waiting for a reply I was messing around and created my main div box, then created 5 divs and positioned them absolutely in the parent div and text manipulation is very easy that way. Are there any really good reasons not to do it that way?

Sub_Seven

5:23 pm on Feb 14, 2011 (gmt 0)

10+ Year Member



Hey Mark,

My humble opinion

I have one good reason (among many others) not to do that: divitis. Explanation here: [csscreator.com...]

Hope that helps.

alt131

6:21 pm on Feb 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Some coders would say there is no good reason to avoid absolute positioning ;)

There's also the disadvantage caused by the way absolute works. Elements are removed from the flow, so cease to interact with each other. If a user has (for example) large sized-text, or a narrower window, the elements may overlap so the content becomes unreadable because one element has been stacked "over the top", or unusable because links or menu's cannot be reached. The difference in dimensions (text-size or viewport size) could also be caused by platform (default font-sizes vary), or device (think cellphone screen versus monitor viewport size.)

Another issue is there is no requirement to respect the document tree as the content can be in any order in the html, and rely on positioning to make sense for visual users - but that leaves users of assistive devices with gibberish. For those who don't care about usability and accessibility, but do care about a design being "perfect" - obviously absolute breaks ;)

Might seem OTT for a single div with a small amount of information, but it is the philosophy behind the question - which is a common when coders make the move away from tables. Tables-for-layout often focussed coders on controlling their layout, rather than the outcome of delivering content to 100% of users. So very tempting to use divs in place of table cells rather than code semantically (lots of examples in subsevens link), even more tempting to regain the "absolute" control of table cells by using absolute positioning. So another disadvantage is ptting yourself to the trouble of moving away from tables, but not taking advantage of the new approach ;)

TheMadScientist

10:42 am on Feb 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I usually keep it simple and do something like this:

<html>
<head>
<body>
<div style="width:450px;padding:0;margin:0 auto;">
<h2 style="width:225px;padding:0;margin:0;text-align:left;float:left;">Text Here</h2>
<p style="width:225px;padding:0;margin:0;text-align:right;float:right;">Other Text Here</p>
<div style="clear:both;"></div>
<p style="padding:0;margin:0;">
<span style="width:150px;float:left;color:#000000;">Text 1 (Left)</span>
<span style="width:150px;float:left;text-align:center;color:#000000;">Text 2 (Mid)</span>
<span style="width:150px;float:right;text-align:right;color:#000000;">Text 3 (Right)</span>
</p>
<div style="clear:both;"></div>
</div>

You can do all the cutting and pasting you like to condense and make it 'not inline' which is usually what I do once I've got it working... Mostly I try to avoid 'the most elegant css ever' and go with the 'down and dirty; simple and effective' solution.

I don't have time to dedicate to css elegance... ;)

Sub_Seven

5:42 pm on Feb 15, 2011 (gmt 0)

10+ Year Member



@TheMadScientist

I have evolved into so many different methods that I no longer know what is better and worse, lately I've been grouping selectors into specific actions like this:

#test, .othertest, #example1, #example1 li {float: left;}
#testarea, .othertestdiv, #example2, #example2 {background: white;}

But I have never had the time to do some research and find out which option(s) is (are) better when it comes to the size of my css files or performance of the final product, I wonder if there are any established methods that prove to be better over the rest...?

TheMadScientist

11:36 pm on Feb 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I usually do the same thing with mine, to an extent...
Meaning if I have something like the above I would probably do:

.TextLeft,TextMid,TextRight {width:150px;}
.TextLeft {float:left;color:#111111;}
.TextMid {float:left;text-align:center;color:#222222;}
.TextRight {float:right;text-align:center;color:#333333;}

But what I wouldn't do is separate them from that grouping just to save a few character or 'inject' other elements into the grouping just to save characters, even if I had other elements using the same width.

It's really not worth it to me to get things 'all spread around' and 'mixed up' in the file when I allow it to be cached by the browser for an extended period of time ... If it was something that is reloaded every time the page is opened or a huge file like some of my js? Sure, I'd probably work to save some more chars, but since it's a '1 time load' and generally a relatively small file I don't worry about it too much.

Sub_Seven

12:36 am on Feb 16, 2011 (gmt 0)

10+ Year Member



Yeah, then again, I'm just experimenting, I guess all options are good as long as the HTML is well formated and displayed the way you want it :)