3

Everything except the actual function of hiding the div is working. The reason I need this without JavaScript is because it is a no JavaScript warning, that is able to be hidden. I am using CSS to try and set the div's display to none but I don't seem to be doing it right.

HTML

<noscript>
    <div class="jswarning">
        JavaScript is disabled. For the best experience, enable it.
        <label class="jswarningbutton" for="hidejswarning">Hide</label>
        <input id="hidejswarning" type="checkbox">
    </div>
</noscript>

CSS

.jswarning {
    box-sizing: border-box;
    padding: 5px;
    background:red;
    width:100%;
    color:white;
    text-align:center;
}

.jswarningbutton {
    background: white;
    color: red;
    cursor: pointer;
}

.jswarningbutton + input {
    display: none;
}

.jswarningbutton + input[type=checkbox]:checked ~ .jswarning {
    display: none;
}
2
  • Man, just use JS for this, really. Display the no JS message in one of the any other ways, this is hack and no good, look at the reply, the "hide" text remains there no matter whether the box is checked or not, this is just ugly, sorry Commented Aug 19, 2017 at 5:11
  • You can use :has(); which is part of the Web Baseline since December 2023: stackoverflow.com/a/78163570/633961 Commented Mar 15, 2024 at 8:27

4 Answers 4

3

The ~ CSS selector is called the General Sibling Selector. It selects siblings of the preceding selector.

.jswarning {
box-sizing: border-box;
padding: 5px;
background:red;
width:100%;
color:white;
text-align:center;
}

.jswarningbutton {
background: white;
color: red;
cursor: pointer;
}

.jswarningbutton + input {
display: none;
}

.jswarningbutton + input[type=checkbox]:checked ~ .jswarning{
display: none;
}
<label class="jswarningbutton" for="hidejswarning">Hide</label>
<input id="hidejswarning" type="checkbox">
<div class="jswarning">
    JavaScript is disabled. For the best experience, enable it.
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Would it be possible to have the label+checkbox "button" inside the div that needs to be hidden?
there is no parent selector in css. you can put in inside the div but then there is no way to select the div and hide it.
1

You are using the ~ CSS selector incorrectly.

The ~ selector should be used as p ~ ul. This means apply this CSS for every ul that is preceded by p tag. You can find the selector reference in here.

Comments

0

Since this is reportedly impossible with CSS (as of August 2017), I've managed to do it with PHP cookies. If anyone is interested, this is the code I've used (there's probably a better way):

Main page (index.php)

<?php
    if(!isset($_COOKIE['hidejswarning'])){
        setcookie('hidejswarning', '0', time()+3600, '/');
        header('Location: index.php');
    }
?>

<!DOCTYPE html>
<html>
    <body>
        <?php
            if ($_COOKIE["hidejswarning"] != '1') {
                echo "
        <noscript>
            <div class='jswarning'>
                JavaScript is disabled. For the best experience, enable it. <a href='hidejswarning.php'>Hide</a>
            </div>
        </noscript>
                ";
            }
        ?>
    </body>
</html>

Page which sets the cookie (hidejswarning.php)

<?php
    setcookie('hidejswarning', '1', time()+3600, '/');
    header('Location: index.php');
?>
<html><body>Please wait...</body></html>

Comments

0

1) The thing you're hiding needs to be a sibling of the input, not a parent. There is no parent selector. Easy fix.

2) It needs to be after the input tag in the html. There is no preceding sibling selector. Difficult fix for your given appearance. You need some messy positioning to get a message that's after the checkbox in the html to appear before it.

3) But this interaction doesn't need to visually be a checkbox. Hide the checkbox input tag (with a display:none), clicking on the label still toggles checkbox state. Put the message in a div/span inside the label, after the input tag. Now you can use the ~ selector to hide the message. Maybe style the word "hide" so it looks buttonish.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.