/    Sign up×
Community /Pin to ProfileBookmark

.clearfix and .group class — neighter working

In the following code, neither the .clearfix or .group class works correctlly
but I am at a loss as to why.

The only thing that seems to correct the problem is the inclusion
of the following line.

[code]
<!– br style=”clear:both” –> <!– works correctly with this line in place –>
[/code]

But this defeats the purpose of either of the class definitions.

Any ideas as to what I’m doing wrong?
?

[code]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> Menu Styles – Button Menu </title>
<!– clearfix NOT WORKING correctly ??? –>

<style type=”text/css”>
#apple,
#samsung,
#sony { border: 5px solid red; height: 100px; width: 100%; }

span { float: right; cursor: pointer; background-color: red; color: white; }
.hide { display: none; }
a { text-decoration: none; cursor: pointer; }
button { width: 80px; }

/* .group { } */
.group-item { width: 50%; float: left; }
.group:after { content: “”; display: table; clear: both; }

/* alternative to group:after from: http://cssmojo.com/the-very-latest-clearfix-reloaded/ */
/* */
.clearfix:after {
content: “”;
display: block;
clear: both;
}
/* */

#displayDivs { width: 90%; }
</style>

<body>

<div class=”clearfix”> <!– alternative to ‘group’ class below –>
<!– div class=”group” –>

<div class=”group-item”>
<h1>Vertical Groups</h1>
<h3>Button Grouping (toggle)</h3>
<div class=”Vbtn-group”>
<button>Apple</button><br>
<button>Samsung</button><br>
<button>Sony</button>
</div>
</div>

<div class=”group-item”>
<h1>Horizontal Groups</h1>
<h3>Button Grouping (toggle)</h3>
<div class=”Hbtn-group”>
<button>Apple</button>
<button>Samsung</button>
<button>Sony</button>
</div>
</div>

<div>

<!– br style=”clear:both” –> <!– works correctly with this line in place –>

<!– –>
<p>
<div id=”displayDivs”>
<div id=”apple” class=”hide”> Apple <span onclick=”closeX(this)”>X</span></div>
<div id=”samsung” class=”hide”> Samsung <span onclick=”closeX(this)”>X</span></div>
<div id=”sony” class=”hide”> Sony <span onclick=”closeX(this)”>X</span></div>
</div>

<script>
function btnAction(IDS) {
document.getElementById(IDS).classList.toggle(‘hide’);
}

function init() {
var sel = document.getElementById(‘displayDivs’).querySelectorAll(‘div’);
var displayDivs = [];
for (let i=0; i<sel.length; i++) { displayDivs[i] = sel[i].id; }

sel = document.querySelectorAll(‘.Vbtn-group button’)
for (let i=0; i<sel.length; i++) { sel[i].addEventListener(‘click’, function() { btnAction(displayDivs[i]); }); }

/* —————————————————————————————————————– */

sel = document.querySelectorAll(‘.Hbtn-group button’)
for (let i=0; i<sel.length; i++) { sel[i].addEventListener(‘click’, function() { btnAction(displayDivs[i]); }); }

}
init();

function closeX(IDS) {
document.getElementById(‘apple’).classList.add(‘hide’);
document.getElementById(‘samsung’).classList.add(‘hide’);
document.getElementById(‘sony’).classList.add(‘hide’);

// document.getElementById(IDS).classList.add(‘hide’);
}
</script>

</body>
</html>
[/code]

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 13.2017 — Such issues are the reason why I never loved floating and was glad to find flex layout as an alternative.

Your layout could be done like this:
&lt;style type="text/css"&gt;
#apple,
#samsung,
#sony {
border: 5px solid red;
height: 100px;
width: 100%;
}

<i> </i> button {
<i> </i> width: 80px;
<i> </i> }

<i> </i> .outer {
<i> </i> display: flex;
<i> </i> flex-direction: row;
<i> </i> justify-content: space-around;
<i> </i> }

<i> </i> .group-item {
<i> </i> display: flex;
<i> </i> flex-direction: column;
<i> </i> }

<i> </i> .Vbtn-group {
<i> </i> display: flex;
<i> </i> flex-direction: column;
<i> </i> }

<i> </i> .Hbtn-group {
<i> </i> display: flex;
<i> </i> flex-direction: row;
<i> </i> }
<i> </i>&lt;/style&gt;
&lt;/head&gt;


&lt;body&gt;

<i> </i>&lt;div class="outer"&gt;
<i> </i> &lt;div class="group-item"&gt;
<i> </i> &lt;h1&gt;Vertical Groups&lt;/h1&gt;
<i> </i> &lt;h3&gt;Button Grouping (toggle)&lt;/h3&gt;
<i> </i> &lt;div class="Vbtn-group"&gt;
<i> </i> &lt;button&gt;Apple&lt;/button&gt;
<i> </i> &lt;button&gt;Samsung&lt;/button&gt;
<i> </i> &lt;button&gt;Sony&lt;/button&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;

<i> </i> &lt;div class="group-item"&gt;
<i> </i> &lt;h1&gt;Horizontal Groups&lt;/h1&gt;
<i> </i> &lt;h3&gt;Button Grouping (toggle)&lt;/h3&gt;
<i> </i> &lt;div class="Hbtn-group"&gt;
<i> </i> &lt;button&gt;Apple&lt;/button&gt;
<i> </i> &lt;button&gt;Samsung&lt;/button&gt;
<i> </i> &lt;button&gt;Sony&lt;/button&gt;
<i> </i> &lt;/div&gt;
<i> </i> &lt;/div&gt;
<i> </i>&lt;/div&gt;
(Didn't get what you intend to do by the javascript and didn't take it into account.)
Copy linkTweet thisAlerts:
@JMRKERauthorOct 14.2017 — Thanks 'Sempervivum', that helped alot.

The reason for the 'float' approach was that I wanted to show the button groups

normally side-by-side except for when applied to smaller screens.

It was my attempt at creation of a responsive screen.

In my original post I could not get the hidden displays to flow underneath

the selection menus. I could not get the CSS 'clearfix' or 'group' commands to perform correctly. Now, with the flexbox, there is no need for the extra classes.

With you CSS suggestions, I was able to accomplish the side-by-side task

as well as create a responsive column display on smaller screens.

The JS was not the question here.


I was trying to apply the addEventListener functions

to the horizontal, vertical and 'X' button displays.

Here is what I came up with when I added the CSS changes.
<i>
</i>
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8" /&gt;
&lt;title&gt; Menu Styles - Button Menu &lt;/title&gt;

&lt;style type="text/css"&gt;
#apple,
#samsung,
#sony { border: 5px solid red; height: 100px; width: 100%; }

button { width: 80px; background-color: lime; }
button:hover { background-color: green; color: white; }
.hide { display: none; }
span { float: right; cursor: pointer; background-color: red; color: white; }

.outer { display: flex; flex-direction: row; justify-content: space-around; /* center groups */ }
.group-item { display: flex wrap; flex-direction: column; }
.Vbtn-group { display: flex; flex-direction: column; }
.Hbtn-group { display: flex; flex-direction: row; }

@media all and (max-width: 800px) {
.outer { display: block; }
}
&lt;/style&gt;

&lt;body&gt;

&lt;div class="outer"&gt;
&lt;div class="group-item"&gt;
&lt;h1&gt;Vertical Groups&lt;/h1&gt;
&lt;h3&gt;Button Grouping (toggle)&lt;/h3&gt;
&lt;div class="Vbtn-group"&gt;
&lt;button&gt;Apple&lt;/button&gt;&lt;br&gt;
&lt;button&gt;Samsung&lt;/button&gt;&lt;br&gt;
&lt;button&gt;Sony&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div class="group-item"&gt;
&lt;h1&gt;Horizontal Groups&lt;/h1&gt;
&lt;h3&gt;Button Grouping (toggle)&lt;/h3&gt;
&lt;div class="Hbtn-group"&gt;
&lt;button&gt;Apple&lt;/button&gt;
&lt;button&gt;Samsung&lt;/button&gt;
&lt;button&gt;Sony&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;
&lt;div id="displayDivs"&gt;
&lt;div id="apple" class="hide"&gt; Apple &lt;span&gt;X&lt;/span&gt;&lt;/div&gt;
&lt;div id="samsung" class="hide"&gt; Samsung &lt;span&gt;X&lt;/span&gt;&lt;/div&gt;
&lt;div id="sony" class="hide"&gt; Sony &lt;span&gt;X&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;script&gt;
function btnAction(IDS) {
document.getElementById(IDS).classList.toggle('hide');
}

function init() {
var sel = document.getElementById('displayDivs').querySelectorAll('div'),
displayDivs = [];
for (let i=0; i&lt;sel.length; i++) { displayDivs[i] = sel[i].id; }

sel = document.querySelectorAll('#displayDivs span')
for (var i=0; i&lt;sel.length; i++) { sel[i].addEventListener('click', function() { closeX(this); }) }

sel = document.querySelectorAll('.Vbtn-group button')
for (let i=0; i&lt;sel.length; i++) { sel[i].addEventListener('click', function() { btnAction(displayDivs[i]); }); }

/* ---------------------------------------------------------------------------------------- */

sel = document.querySelectorAll('.Hbtn-group button')
for (let i=0; i&lt;sel.length; i++) { sel[i].addEventListener('click', function() { btnAction(displayDivs[i]); }); }

}
init();

function closeX(IDS) {
document.getElementById('apple').classList.add('hide');
document.getElementById('samsung').classList.add('hide');
document.getElementById('sony').classList.add('hide');

// document.getElementById(IDS).classList.add('hide');
}
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;


Thanks again for the suggestions.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...