Forum Moderators: not2easy

Message Too Old, No Replies

CSS for old people who weren't paying attention

A heads up - seems to be possible to separate form and function now

         

vordmeister

6:16 pm on Mar 1, 2019 (gmt 0)

10+ Year Member Top Contributors Of The Month



Most of my sites were designed using tables before CSS was widely used. After the @media thing was established I did a find and replace and ended up with CSS tables that could rearrange themselves for mobiles.

The original purpose of CSS was to separate form and function. It seems possible now to write using only <h> <p> and <img> tags and let CSS do the rest which makes it a lot easier for those of us who like to write using a text editor. Browsers seem to have fewer differences these days.

The trick is to put a div with some id around the layout you want, then you can have the basic html tags do what you want when associated with that single layout div id. I want the images on the right and text on the left so use 'float:right' and 'clear:right' on the img css. Don't need to do anything to <p>, but I like to have bits of text beside relevant images so use 'clear:both' for the h2. If I want another heading in the text relevant to the image I'll use an h3.

Only posting because I find CSS brilliant now, and it seems possible to start to use it as intended. I have 80% less code and can write without worrying about layout.

tangor

6:43 pm on Mar 1, 2019 (gmt 0)

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



CSS does make styling so much easier ... so easy that I've encountered sites with 36 different versions of <p>!

lucy24

7:00 pm on Mar 1, 2019 (gmt 0)

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



Conversely, when a table really is appropriate (in my case most often when I have parallel text in 2 or 3 languages), css allows you to change the display of rows and cells so they look like inline paragraphs on small devices. No need to change the underlying structure when it was correct all along.

NickMNS

8:38 pm on Mar 1, 2019 (gmt 0)

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



so use 'float:right' and 'clear:right' on the img css.

Don't use float, it should have been deprecated years ago. Use flexbox instead (display:flex;)
[w3schools.com...]

Float left, float right, clear this, clear that, was is this whitewater kayaking!

Nearly every thread that pops up here on the topic of someone having problems getting elements to play nice with CSS, is caused by a stray float having some unwanted effect.

Yes, I know flexbox doesn't work on IE10, but common if you are still using IE10 you get what you deserve.

tangor

8:59 pm on Mar 1, 2019 (gmt 0)

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



I never use "float". I know where I want it and put it there. :)

We can sometimes get too smart for our britches.

lucy24

7:44 pm on Mar 2, 2019 (gmt 0)

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



Float and flexbox--and {display: inline-block;}--are different things serving different purposes. Learn which one is appropriate, and use it.

Now, personally, the few sites I've seen using flexbox extensively ... yuk. For me it's distracting and annoying.

NickMNS

4:44 pm on Mar 3, 2019 (gmt 0)

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



the few sites I've seen using flexbox extensively ... yuk. For me it's distracting and annoying.

How does that work? Do you first check the CSS of every website to see whether the what is used for the site's is layout? Or is maybe that when a site annoys you check. In which case yes one can use flexbox to make things annoying. But typically, the use of flexbox should go unnoticed. Bootstrap4 uses flexblox extensively.

Float and flexbox--and {display: inline-block;}--are different things serving different purposes. Learn which one is appropriate, and use it.

Teach me? Seriously, show me a case for each where it is specifically appropriate.

vordmeister

5:13 pm on Mar 3, 2019 (gmt 0)

10+ Year Member Top Contributors Of The Month



You can style a site poorly using just about any technology but I had no intention to start a CSS war here.

I tried to completely separate form and function before and it didn't work. Odd as that seemed to be the idea behind CSS in the first place. It was probably more than 5 (maybe 10) years ago when I last tried and it didn't work across browsers.

This time I tried it out of frustration and it works now. Probably this isn't news to anyone who has being paying more attention. Thanks for the pointers about table to mobile and flexbox.

I'm still using H2 tags for both form and function - it would screw my layout if I changed them to H3. But I want to keep my text related to the images beside the text so I suppose it it is kind of acceptable. There are times where I want a couple of images to describe the text and that needs additional CSS so my model is far from perfect.

NickMNS

5:41 pm on Mar 3, 2019 (gmt 0)

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



Is it possible to completely separate form and function?

Yes.

The trick is to put a div with some id around the layout you want, then you can have the basic html tags do what you want when associated with that single layout div id.

One does not specifically need to use "some id". It is in fact preferable to use a class. An ID is a unique identifier and can only be used to identify a single instance of a tag. A class can be used to identify any number of instances of a tag and also the same class can be used for different tags.

More importantly in you specific case you can style a tag directly without any class or ID. As such the style will apply to every instance of the that tag.


<style>
/* use css to style a tag */
h2 {
font-size:1.5em;
color: green;
}
h3{
font-size:1.25em;
color: blue;
}
/* style using class */
.my-class{
font-size:1.1em;
color: black;
}
/* style using id */
#my-id{
font-size:1em;
color: peachpuff;
}
/* Pro tip - you can combined them */
h2.my-class{
color: dodgerblue;
}
<style>

<body>
<h2>
This is simply an H2
</h2>
<h2>
This is another H2
</h2>
<h3>
This is simply an H3
</h3>
<div class="my-class">
This is a div styled with my class
</div>
<p class="my-class">
This is a p styled with my class
</p>
<p class="my-class">
This is a p another styled with my class
</p>
<div id="my-id">
This is a div styled with my class
</div>
<h2 class="my-class">
This is a third h2 but it is identified by class="my-class"
</h2>
<p class="my-class">
note the color of the h2 above is neither the same as h2 or my-class but it is specific to h2.my-class
</p>
</body>


Working demo here:https://jsfiddle.net/m1d0csn4/

lucy24

7:04 pm on Mar 3, 2019 (gmt 0)

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



The difference between float and other display options is that you're explicitly saying “A is subordinate to B”. If, instead, A and B are conceptually parallel--or when there is no B, as with a banner--then float is the wrong thing to use. The same, mutatis mutandis, can be said for tables: just because they have often been used incorrectly doesn’t mean you should never ever use a table for any purpose.

In which case yes one can use flexbox to make things annoying.
I've taken it from the other side: if someone says that suchandsuch site is a good example of flexbox, I go have a look, and that's when I say Eeuw, yuk. In practice, it’s just inline-block with more options.

“Doesn’t work in MSIE 6” can happily be ignored as a criterion. “Doesn’t work in MSIE 10” (or even 11 due to long list of bugs) cannot, unless your target audience is 100% composed of people using the latest cutting-edge browser at all times.

not2easy

7:50 pm on Mar 3, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I thought flexbox would be the solution for a couple of problematic layouts until I tried to use it. I can see that there might be some use for it, but it turned out not to be what I wanted to work with.

NickMNS

10:47 pm on Mar 3, 2019 (gmt 0)

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



“A is subordinate to B”

What does this mean?

Here is a very simple example: [jsfiddle.net...]
1,2,3 are floated right. A,B,C use flex to align them from the right. 4 & D are left as default.
Which of two is more intuitive?
1,2,3 is listed in the html in that order but then appear on screen due to styling as 3,2,1. Whereas ABC appear on the right as ABC. And D appears on the next line as expected.

Do you want to add to float confusion, resize the window, four kind of eventually goes to the next line, but not really because, there is gray still appearing on the first line. What is that? Squeeze some more and then three goes to the next line but four moves back to the first line. Whahh? Why? But four was down now it's up again? Let's squeeze some more.... Wait now four is back down next to three, but still not fully? The order of the elements is 2,1,4,3 huhh?

All the while ABC squeeze down to AB and then C on the next line, and eventually A, B, and C in the same order each on their own line.

I go have a look, and that's when I say Eeuw, yuk.

I must be really missing something here, because I think my ultra simple demo shows that the yuk factor applies far more to float than flex.

lucy24

11:13 pm on Mar 3, 2019 (gmt 0)

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



What does this mean?
a {float: right; clear: right;}
b {display: block}

Did you mean, what does it mean conceptually?

If, for example, you've got an image associated with some particular text, and the image isn't big enough to warrant interrupting the text, then the image is most appropriately floated, so it takes up as much space as it requires, without putting any constraints on the primary text. Sidebars (in the physical sense) are another place where floats are often the most sensible option. I consider the "clear" essential, because that's the whole point of a float: one thing at a time, rather than all jumbled together at random. Otherwise we are once again in “eeuw, yuk” territory.

All of this is based on the assumption that you have not forced a font size, window width or any other aspect of text display that should be left up to the user's preference. Therefore, some things on the page have a fixed size by their nature, while others don't. The ones with a predetermined size are the ones that float.

NickMNS

11:59 pm on Mar 3, 2019 (gmt 0)

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



What you say all makes sense. And clearing the float at the same time as setting it changes things. But then why does it even need to be cleared explicitly, would it no have made more sense that float is simply cleared by default? That's rhetorical.

All of this is based on the assumption that you have not forced ... window width

Window width are subject to change due to a multitude of mobile screen sizes and screen orientations. The problem remains with float, that unexpected results can occur when windows resize. It is impossible to think that one can control or adjust for this with a multitude of break points.

Flex just works for me, I find it simple and intuitive. Let just agree to disagree.

lucy24

1:24 am on Mar 4, 2019 (gmt 0)

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



why does it even need to be cleared explicitly, would it no have made more sense that float is simply cleared by default?
I think I can count on my fingers the times I’ve had occasion to use a float without any clearing--maybe with two very small images belonging to the same text block--but generally it just looks messy. In practice the choice is between clear: one-side (whichever side the float is on) and clear: both in the rare case where there are right and left floats in the same general area.

Let just agree to disagree.
Well, this whole digression did start with me saying “Personally, {blahblah}” :) With that opening, everything that follows has to be more or less a matter of opinion. I do think the real choice is not float vs. flexbox, but inline-block vs. flexbox. I’ve seen a lot of posted CSS that uses float in ways that I would consider inappropriate.

jane_eyre

4:33 pm on Apr 6, 2019 (gmt 0)

5+ Year Member



One thing i recently learned is that sites built with tables are inaccessible, since the screen-reader will read the rows and headings one after the other rather in the visual order.

tangor

5:42 pm on Apr 6, 2019 (gmt 0)

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



The arcane practice of tables for layout (I was guilty of same in the 1990s) keeps popping up whenever css positioning comes up. Thanks for the memories! (not!)

mack

9:07 pm on Apr 6, 2019 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I don't think <Table> was every really a problem because it was originally designed to display tabular data within a table layout. It's people like me that were the problem by using it for what it really wasn't intended for (entire site layouts).

CSS was at first a fairly huge learning curve, but now it just feels like the norm. The days of tables and nested tables and other tables nested within nested tables are long gone... Well apart from my occasional misuse of divs :-)

Mack.