Home > other >  changing the brightness of a web page using the slider
changing the brightness of a web page using the slider

Time:02-03

have a task in front of me - when changing the slider parameters to change the brightness of the entire page, I tried the option with a full-screen div, but it blocked access to the rest of the elements, I also tried to set the filter parameter for the body, but in this case the page construction went wrong

CodePudding user response:

All you need to do is either use a filter: brightness(..) on <body>, make clever use of your website colors using hsl(..) colors and change their lightness value or a mix of both methods.

All can easily be done with a range slider modifying a single --brightness custom variable with one line of Javascript. Just en/disable the various options as mentioned in the CSS.

Just as a reminder: without a proper minimal reproducible example no one on SO will be able to help you with your specific issue...

I created a small demo showcasing the above:

/*
   Custom variable to hold overall brightness percentage
   modified with the range slider min [0.25] max [1.75]
   default value [1].

   Modified with slider "oninput" JS...
*/
html { --brightness: 1 }

.webpage {
    margin-top: 3rem;
    
    /* Default color setting */
    background-color: hsl(0,0%, 41.2%); color: hsl(0,0%, 100%);
    
    /* Override by changing (L)ightness parameter of HSL color */
    background-color: hsl(0,0%, calc(var(--brightness) * 41.2%)); color: hsl(0,0%, calc(var(--brightness) * 100%));
    /* disable when using filter */

    /* Modify overall brightness */
/*    filter: brightness(var(--brightness)); /* enable to see difference with HSL */

    /* Modify overall brightness and invert B/W */
/*    filter: brightness(var(--brightness)) invert(var(--brightness)); /* enable to see difference with HSL */

}

/* Extras, just to make the demo look good */

/********************************/
/* some convenient global rules */
/********************************/
*, ::before, ::after { box-sizing: border-box }

html, body  { width: 100%; max-width: 100% }
html        { scroll-behavior: smooth }
body        { min-height: 100vh; margin: 0; line-height: 1.5 }

/************************************************/
/* element responsive behavior         [STABLE] */
/************************************************/
/* https://codepen.io/renevanderlende/pen/YzEaKvO?editors=1100 */
/* responsive base font size using y = mx   b 'y-intercept form' => y = 0.00625x   12 */
html { font-size: calc(0.625vmin   0.75rem) } /* (320,14)(1280,20) */
body { font-size: 1rem; line-height: 1.5 }

/* Generic page spacers, root font size independent, also y = mx   bs
    T/B: (360,0)(2160, 90) => y = 0.05x -  18 <= base  90, 72 at 1920x1080
    L/R: (640,0)(1920,448) => y = 0.35x - 224 <= base 160, 360x640 full screen
*/
:root        { --min-pad: 1rem }

[padded]     { padding: max(var(--min-pad),calc(5vmin - 18px))
                        max(var(--min-pad),calc(35vw - 224px)) }

[padded="1"] { padding: var(--min-pad) } /* minimal padding */
[padded="0"] { padding: 0 }              /* no padding */

label { 
    position: fixed; top: 0; width: 100%;
    display: inline-flex; flex-direction: column;
    background-color: white;
    cursor: pointer;
}
<label>Brightness&nbsp;<input type="range" min="0.25" max="1.75" value="1" step="0.01"
                              oninput="javascript:document.documentElement.style.setProperty('--brightness', this.value)">&nbsp;</label>

<div padded >
    <h2>Lorem Ipsum dolor...</h2>
    <p>Lorem ipsum dolor sit amet, mel omnium vulputate percipitur ex. Ex vel quas inani appellantur. Iusto dolore cetero duo ad. Per facer mediocrem eu. An legimus voluptatibus eam, cetero aperiri consectetuer sit id, hinc sale evertitur cum ei.
      </p>
      <p>
    Erant maiorum iracundia ius ne. Veri summo clita in usu, possim apeirian interesset usu no. Malorum repudiandae cu eum, ad vel putant torquatos. Idque feugait aliquando his eu, sed te sanctus omittam placerat. Ex eum ridens euismod facilisis, sea id lorem reformidans complectitur.
      </p>
      <p>
    Qui purto elitr at, at corrumpit signiferumque mea. Vix quidam consulatu ei, no esse vocibus convenire usu, est te erroribus imperdiet. Soleat molestiae deseruisse ei sea, ea sale mollis nam. Ut eam eius vidit consulatu.
      </p>
      <p>
    Amet justo ex mel. Utroque accusata cu nam. Et vel mucius audiam rationibus, mea regione alterum ne. Perpetua persequeris te eos, no sale mnesarchum quo. Atomorum reformidans ea per, ne timeam tractatos philosophia usu.
      </p>
      <p>
    Id vix atomorum scribentur. Sit habeo ancillae facilisi in. Purto tacimates no mei, in duo nemore senserit, inani debet ut mea. Eu homero timeam adversarium vis, vix molestie luptatum atomorum ei. Ut eos sumo atqui, sumo ponderum iudicabit vix ne, ea errem maiorum postulant sea.
      </p>
      <p>
    Et nam putant ancillae, et vel luptatum vivendum efficiendi, eos diam nulla legendos an. Putent fastidii mediocritatem sit in. Id per odio delenit complectitur. Altera mentitum efficiendi ex usu, an veri tation partiendo quo. In nominavi recteque adversarium mel, duo suas populo eu.
      </p>
      <p>
    Et integre facilisi sea, et agam utinam nam. Facilis laboramus democritum sit at, eos sint probatus iracundia an, duo ei eirmod malorum. Pro quem nihil aliquip at. Eum dico debet graece ut, has tamquam intellegat an.
      </p>
      <p>
    Sea no magna dolores corrumpit. Nam nulla hendrerit te, vivendum dissentiet eu vix, at quas zril putent vim. Eu pro maiestatis incorrupte, has magna posse ei, pri cetero quaerendum ut. Autem justo atqui usu id, sumo ipsum fierent pro ea. Oporteat tacimates suavitate per ei, molestie delicata at pro, erant prompta quo no.
      </p>
      <p>
    Id qui elit partiendo iracundia, ei eos nostrud graecis. No postulant ullamcorper signiferumque qui, eu pri augue vidisse eligendi, corpora iudicabit et eos. Ea purto impetus per, porro partiendo efficiendi ex mel. Cum vero eius epicuri eu. An virtute albucius nec, hinc ceteros referrentur cu mei, audiam admodum ad nam.
      </p>
      <p>
    Mel albucius qualisque ut, mutat simul omnesque at vim, te eam minim bonorum. Iisque insolens atomorum nam et. No nihil epicuri efficiantur pro. No mel vide argumentum, nonumes interesset nec ex. Duis commodo placerat pro cu, mei laudem aliquip numquam ut, case latine salutatus eum ne. Ad tollit convenire mei, nam te amet eruditi conclusionemque.
    </p>
</div>

  • Related