CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: CSS and @media problem...  (Read 2318 times)

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
CSS and @media problem...
« on: 24 Nov 2020, 02:50:20 pm »
Hello

I have a slight problem and I cant seem to figure it out...

Code: [Select]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Mobile Burger Test</title>

    <style>

        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        html, body {
            background: #000;
            color: #fff;
            font: 15px Arial;
        }

        header {
            background: #333;
            border-bottom: 1px solid greenyellow;
            padding: 1em;
        }

        #headerNavigation {
            text-align: right;

        }

        #headerNavigation div {
            display: flex;
        }

        #headerNavigation div div {
            flex: auto;
        }

        .row {
            display: flex;
        }

         .column {
            flex: auto;
        }

        #headerNavigation span {
            padding: 0.5em 0.75em;
            display: block;
        }

        #headerNavigation li {
            font-size: 1.7em;
            display: inline-block;
        }

        #headerNavigation a {
            color: inherit;
            text-decoration: none;
            font-weight: bold;
            display: inline-block;
            padding: 0.5em 0.75em;
        }

        #headerNavigation a:hover {
            color: red;
            background: orange;
        }

        #menu {
            display: none;
            text-align: right;
        }

        #menu a {
            color: inherit;
            text-decoration: none;
        }

        #menu a:hover {
            color: red;
        }

        @media only screen and (max-width: 600px) {

            #headerNavigation {
                display: none;
            }

            #headerNavigation li {
                display: block;
            }

            /*#headerNavigation a {
                padding: 0.75em 0.5em;
            }*/

            #menu {
                display: block;
            }

        }

    </style>

</head>
<body>

    <header>

        <div class="row">

            <div class="column">Your Garage Name</div>

            <div class="column" id="menu">

                <a href="javascript:void(0);" class="icon" onclick="myFunction()">Burger</a>

            </div>

            <div class="column">

                <ul id="headerNavigation">

                    <li><span>Home</span></li>
                    <li><a href="">Services</a></li>
                    <li><a href="">About</a></li>
                    <li><a href="">Contact</a></li>

                </ul>

            </div>

        </div>

    </header>

    <script>

        function myFunction() {

            var x = document.getElementById("headerNavigation");

            if (x.style.display === "block") {
                x.style.display = "none";
            } else {
                x.style.display = "block";
            }

        }

    </script>

</body>
</html>

The media query isnt resetting between the full screen and smaller screen if the menu is set to hidden via the javascript also the padding doesn't properly between line and vertical list...
« Last Edit: 24 Nov 2020, 05:09:26 pm by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
CSS and @media problem...
« Reply #1 on: 25 Nov 2020, 02:07:34 am »
Having slepted on the issue, I have fixed some of my issues myself now! :)

Code: [Select]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Your Name Here</title>
    <style>

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        html, body {
            background: black;
            font:  1.2em/1.5em Helvetica, Arial sans-serif;
        }

        header {
            display: flex;
            padding: 1em;
            background: #333;
            border-bottom: 2px solid green;
            color: #fff;
        }

        header div {
            flex: 50%;
        }

        header div:first-child {
            font-weight: bold;
            font-size: 2em;
        }

        #navigation {
            text-align: right;
        }

        #navigation li {
            display: inline-block;
        }

        #navigation a,
        #navigation span,
        #mobileMenu a {
            color: inherit;
            text-decoration: none;
            padding: 0.25em 0.5em;
            font-weight: bold;
        }

        #navigation span {
            color: #555;
        }

        #navigation li a:hover,
        #mobileMenu a:hover {
            color: darkgreen;
            background: #ccc;
        }

        #mobileMenu {
            text-align: right;
            display: none;
        }

        @media only screen and (max-width: 900px) {

            #navigation {
                display: none;
            }

            #mobileMenu {
                display: block;
            }

            #navigation li {
                padding: 0.25em;
                display: block;
            }

        }

    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>

<body>

<header>

    <div>

        Your Name Here

    </div>

    <div>

        <div id="mobileMenu">

            <a href="javascript:void(0);" class="icon" onclick="showNavigation()" ><div class="fa fa-bars"></div></a>

        </div>

        <ul id="navigation">

            <li><span>Home</span></li>

            <li><a href="">Services</a></li>

            <li><a href="">About</a></li>

            <li><a href="">Contact</a></li>

        </ul>

    </div>

</header>

<script>
    function showNavigation() {
        var x = document.getElementById("navigation");
        if (x.style.display === "block") {
            x.style.display = "none";
        } else {
            x.style.display = "block";
        }
    }
</script>

</body>
</html>

The only issues remaing is the padding in the mobile size and off course assigning the correct burger icon and the fact the menu doesn't automagically reappear on maximise if you close the menu...!
« Last Edit: 25 Nov 2020, 04:00:38 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
Re: CSS and @media problem...
« Reply #2 on: 25 Nov 2020, 02:52:40 am »
So I guess my only real question now is how do I update the inline CSS style that the x.style.display has updated when the screen/browser window is resized...?
« Last Edit: 25 Nov 2020, 04:01:16 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

coothead

  • Sr. Member
  • ****
  • Posts: 391
  • Karma: +89/-0
  • I smile benignly
    • coothead's stuff ~ an eclectic collection
Re: CSS and @media problem...
« Reply #3 on: 25 Nov 2020, 11:13:00 am »
Hi there GrumpyYoungMan,

you are a very naughty boy using JavaScript for your little task.  :o

Try it like this...

index.html file:-

Code: [Select]
<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>Your Name Here</title>

<link rel="stylesheet" href="screen.css" media="screen">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

</head>
<body>

<input id="menu-displayer" type="checkbox">

<header>

 <h1>Your Name Here</h1>

 <div id="navigation">

  <label for="menu-displayer" title="show or hide menu">
   <span class="fa fa-bars"></span>
  </label>

  <ul>
   <li><a href="index.html">Home</a></li>
   <li><a href="services.html">Services</a></li>
   <li><a href="about.html">About</a></li>
   <li><a href="contact.html">Contact</a></li>
  </ul>

 <!--#navigation--></div>

</header>

</body>
</html>

screen.css file:-

Code: [Select]
body {
    margin: 0;
    background-color: #000;
    font: normal 1em / 1.6em sans-serif;
 }
#menu-displayer {
    position: absolute;
    left: -999em;
 }
header {
    display: flex;
    padding: 1em;
    background: #333;
    border-bottom: 2px solid #008000;
    color: #fff;
 }
header h1,header div {
    flex: 50%;
 }
label .fa {
    display: none;
    padding: 0.2em 0.3em;
    margin-right: 0.2em;
    font-size: 2em;
    background: #bbb;
 }
label:hover .fa,
label:active .fa {
    color:#006400;
 }
#navigation {
    text-align: right;
 }
#navigation li {
    display: inline-block;
 }
#navigation a {
    color: inherit;
    text-decoration: none;
    padding: 0.25em 0.5em;
    font-weight: bold;
 }
@media ( max-width: 56.25em ) {
label .fa {
    display: inline-block;
  }
#navigation ul {
    display: none;
  }
#menu-displayer:checked ~ header #navigation ul,
#menu-displayer:checked ~ header #navigation ul li{
    display: block;
  }
 }

You can see a working example here...

https://codepen.io/coothead/full/WNGeLxy

coothead
« Last Edit: 25 Nov 2020, 11:20:37 am by coothead »
~ the original bald headed old fart ~

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
Re: CSS and @media problem...
« Reply #4 on: 25 Nov 2020, 01:39:42 pm »
Thank you! Coothead!

I am sorry for using JavaScript, but I knew there had to be a CSS way, but I couldn’t find it, but I knew there must be!

I am on my mobile at the moment but will check it out properly on the laptop soon - it looks like you changed quite a bit of my code? Was it that bad?

Thank you for taking the time to help this novice, who should in all honestly should stop!
« Last Edit: 25 Nov 2020, 01:41:41 pm by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

coothead

  • Sr. Member
  • ****
  • Posts: 391
  • Karma: +89/-0
  • I smile benignly
    • coothead's stuff ~ an eclectic collection
Re: CSS and @media problem...
« Reply #5 on: 25 Nov 2020, 01:56:38 pm »
...it looks like you changed quite a bit of my code?
Was it that bad?

I just reduced your div element count from
4 to 1, added the the essential h1 element
and changed the naughty JavaScript for an
input  element and a label element.

It was only the JavaScript usage that  I would
call bad and was probably the cause of your
rather over complicated HTML.  :)

Also note that coding wise, I am only slightly
more than a novice with minor skills in HTML,
CSS and JavaScript.

coothead
« Last Edit: 25 Nov 2020, 02:00:56 pm by coothead »
~ the original bald headed old fart ~

coothead

  • Sr. Member
  • ****
  • Posts: 391
  • Karma: +89/-0
  • I smile benignly
    • coothead's stuff ~ an eclectic collection
Re: CSS and @media problem...
« Reply #6 on: 25 Nov 2020, 04:12:25 pm »
Hi there GrumpyYoungMan,

there is no real need to use font-awesome for this project.
There is a Character Entity that will do the job...

index.html file:-

Code: [Select]
<!DOCTYPE HTML>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>Your Name Here</title>

<link rel="stylesheet" href="screen.css" media="screen">

</head>
<body>

<input id="menu-displayer" type="checkbox">

<header>

 <h1>Your Name Here</h1>

 <div id="navigation">

  <label for="menu-displayer" title="show or hide menu">&#38;#8801;</label>

  <ul>
   <li><a href="index.html">Home</a></li>
   <li><a href="services.html">Services</a></li>
   <li><a href="about.html">About</a></li>
   <li><a href="contact.html">Contact</a></li>
  </ul>

 <!--#navigation--></div>

</header>

</body>
</html>

screen.css:-

Code: [Select]
body {
    margin: 0;
    background-color: #000;
    font: normal 1em / 1.6em sans-serif;
 }
#menu-displayer {
    position: absolute;
    left: -999em;
 }
header {
    display: flex;
    padding: 1em;
    background: #333;
    border-bottom: 2px solid #008000;
    color: #fff;
 }
header h1,header div {
    flex: 50%;
 }
label {
    display: none;
    padding: 0.15em 0.15em 0.2em;
    margin-right: 0.3em;
    font-size: 3em;
    background: #ccc;
    cursor: pointer;
 }
label:hover,
label:active {
    color:#006400;
 }
#navigation {
    text-align: right;
 }
#navigation li {
    display: inline-block;
 }
#navigation a {
    color: inherit;
    text-decoration: none;
    padding: 0.25em 0.5em;
    font-weight: bold;
 }
@media ( max-width: 56.25em ) {
label {
    display: inline-block;
  }
#navigation ul {
    display: none;
  }
#menu-displayer:checked ~ header #navigation ul,
#menu-displayer:checked ~ header #navigation ul li{
    display: block;
  }
 }

https://codepen.io/coothead/pen/WNGeLxy

coothead
~ the original bald headed old fart ~

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
Re: CSS and @media problem...
« Reply #7 on: 25 Nov 2020, 06:11:41 pm »
Thanks CootHead, I wasn't happy with relying on a third party!

I appericate your help! Thanks again!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

coothead

  • Sr. Member
  • ****
  • Posts: 391
  • Karma: +89/-0
  • I smile benignly
    • coothead's stuff ~ an eclectic collection
Re: CSS and @media problem...
« Reply #8 on: 25 Nov 2020, 06:26:09 pm »
No problem, you're very welcome.  8)

Here is a  link that you may find to be useful...

W3.0rg - Character Entity Reference Chart

...if you want to avoid third parties.  :)

coothead
« Last Edit: 25 Nov 2020, 06:30:41 pm by coothead »
~ the original bald headed old fart ~

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
Re: CSS and @media problem...
« Reply #9 on: 26 Nov 2020, 04:00:01 am »
Thank you! :)
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
Re: CSS and @media problem...
« Reply #10 on: 29 Nov 2020, 07:58:43 am »
What have I missed or dont understand fully:

Code: [Select]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>

        html, body {
            font: normal 1.2em Verdana;
            background: #555;
            color: #FFF;
            box-sizing: border-box;
        }

        header div:first-child {
            display: flex;
            border-bottom: 1px solid;
        }

        footer {
            border-top: 1px solid;
            text-align: center;
            color: greenyellow;
            font-size: 0.75em;
            padding: 0.5em;
        }

        header div:first-child, footer {
            background: #222;
            border-width: 0.25em;
        }

        header div:first-child, footer {
            border-color: blue;
        }

        header h1, header ul {
            flex: 50%;
        }

        header ul {
            text-align: right;
        }

        header li {
            display: inline-block;
            padding: 0.5em;
        }

        header li span {
            color: #A5D6A7;
        }

        header li a {
            color: inherit;
            text-decoration: none;
        }

        header li a:hover {
            color: red;
        }

        header label {
            display: none;
        }

        #showMenu {
            display: none;
        }

        @media only screen and (max-width: 1000px) {

            header ul {
                display: none;
            }

            header label {
                display: inline-block;
            }

            header label:hover {
                color: red;
            }

            #showMenu:checked ~ header ul,
            #showMenu:checked ~ header ul li {
                display: block;
            }

            #showMenu:checked ~ div div:last-child {
                display: block;
            }

        }

        header div div:last-child{
            display:none;
        }

    </style>

</head>
<body>

<header>

    <div>

        <h1>MyCompanyName</h1>

            <label for="showMenu" title="Show/Hide Menu">&equiv;</label>
                <input id="showMenu" type="checkbox">

            <ul>
                <li>Test</li>
                <li><a href="">Link</a></li>
                <li><span>Active</span></li>
            </ul>

        <div>Can I be hid and shown?</div>

    </div>

    <div>
        Your slogan here...
    </div>

</header>

    <footer>

        Footer

    </footer>

</body>
</html>
I can't get the menu to reappear... I must be mis-undstanding something, is it the document flow or a CSS problem...

any help/advice would be greatly appreciated.
« Last Edit: 29 Nov 2020, 09:12:24 am by GrumpyYoungMan »
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
Re: CSS and @media problem...
« Reply #11 on: 29 Nov 2020, 09:22:24 am »
I know CootHead gave me a working sample but I am trying to figure it out so can maybe use it in other projects... :)
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

coothead

  • Sr. Member
  • ****
  • Posts: 391
  • Karma: +89/-0
  • I smile benignly
    • coothead's stuff ~ an eclectic collection
Re: CSS and @media problem...
« Reply #12 on: 29 Nov 2020, 09:42:14 am »
Hi there GrumpyYoungMan,

you have placed the input element within
the header element instead of without.  ::)

coothead
~ the original bald headed old fart ~

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 788
  • Karma: +8/-0
    • Grumpy Young Man
Re: CSS and @media problem...
« Reply #13 on: 29 Nov 2020, 10:39:56 am »
Hi there GrumpyYoungMan,

you have placed the input element within
the header element instead of without.  ::)

coothead
Thanks - so the input needs to be outside the div loop and before the label?

Also how do I reduced the clickabilty of the label?

Sorry for the constant question and stupidness!
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

coothead

  • Sr. Member
  • ****
  • Posts: 391
  • Karma: +89/-0
  • I smile benignly
    • coothead's stuff ~ an eclectic collection
Re: CSS and @media problem...
« Reply #14 on: 29 Nov 2020, 10:47:25 am »
Also how do I reduced the clickabilty of the label?

Please excuse my stupidity, but I am
not entirely sure what that means.  :o

coothead
~ the original bald headed old fart ~

 

SMF spam blocked by CleanTalk

Advertisement