To implement a class based on a media query, only pure CSS or HTML is required

Is there a way to apply a specific class to an ID based on screen height using only CSS, HTML, or LESS without pre-compilation? The goal is to set classes from Add2Any, not CSS properties.

Here is a link to the jsfiddle.

The desired outcome is to have the #add2any div with specific classes for small screens:

<div id="add2any" class="a2a_kit a2a_default_style">

For larger screens, the classes should be:

<div id="add2any" class="a2a_kit a2a_kit_size_32 a2a_default_style">

Any suggestions on how to achieve this?

We are looking for a solution that does not involve JavaScript or jQuery to avoid any lag and unnecessary <div> elements. Only the relevant style should be displayed.

Background

The objective is to modify the layout and size of the AddToAny bar for smaller screens, showcasing a more compact design with fewer buttons. By using AddToAny's classes, future updates won't rely on fixed CSS within stylesheets. Ensuring browser compatibility is crucial.

Current CSS Approach

@media screen and (max-height: 430px) {
  .a2a_button_google_plus, .a2a_button_pinterest, .a2a_button_print { display:none;}
  #add2any a, hr#add2any, hr#add2any a, .a2a_divider { font-size: 15px; padding-top:2px; padding-bottom:-2px; }
  .a2a_divider { top:5px ; position: relative}
}

Edit

Despite investigating frameworks like Foundation, no suitable solution has been found yet.

Conditional CSS based upon div, not screen

Toggle mobile view in Foundation using CSS class or JS

How to toggle class using pure JavaScript in HTML

**Edit 2 **

Using Less or Sass as suggested in this question seems excessive since the solution needs to be applied across all pages.

Considering self-hosting the script and adding custom JavaScript might be a more practical choice, as AddToAny's class names are likely to remain consistent even if the underlying script changes. All customization instructions endorse direct utilization of AddToAny's class names.

Answer №1

Updated

Suppose you have the following HTML code:

 <div  class="a2a_kit a2a_default_style">
 <div  class="a2a_kit a2a_kit_size_32 a2a_default_style">

You can implement a media query like this:

 /* initial state */
 .a2a_kit { display: block; }
 .a2a_kit.a2a_kit_size_32 { display: none; }

 @media screen and (max-height: 430px) {
     /* switch behavior at max height of 430 px */
     .a2a_kit { display: none; }
     .a2a_kit.a2a_kit_size_32 { display: block; }
 }

Answer №2

To customize your styles in different screen sizes, utilize media queries like so:

#add2any {
   /* common styles */
   background-color: blue;
   width: 100px;
   color: white;
}


@media (min-width: 420px) and (max-width: 760px) {
    /* styles for screens between 420px and 760px wide */
    #div1 {
        background-color: red;
        width: 300px;
        color: yellow;
    }
}

@media (min-width: 760px) {
    /* styles for screens wider than 760px */
    #div1 {
        background-color: green;
        width: 600px;
    }
}

View Demo on JSFiddle

*If you prefer to use classes instead of IDs for styling, simply add a unique class and apply styles accordingly

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What's the best way to add line numbers to source code on an HTML webpage after parsing?

I am currently working with AngularJS and MongoDB. In my MongoDB data, there are some text elements that contain a \n, causing each line to be displayed on a new line based on the occurrence of \n. However, I also want to add line numbers to each ...

What is causing lxml to not remove section tags?

Having some trouble parsing HTML using lxml and Python. My goal is to eliminate section tags, but seems like lxml can remove other specified tags except for sections. For instance: test_html = '<section> <header> Test header </header& ...

show items in UL LI elements as "inline" within a bootstrap column

Struggling with organizing UL LI items in a bootstrap setup? Trying to make them align horizontally instead of vertically but can't figure it out. Here's my code: <div class="container-fluid" style="margin-bottom:45px;"> <div cl ...

What is the method for obtaining the width of a g element?

I am encountering an issue with retrieving the width of the <g></g> element using jQuery. Both .innerWidth() and width() methods are returning null for me. Is there a way to obtain the width of the <g></g> element using jQuery? I a ...

Obtaining search engine results from a webpage without using an API

In my attempts, I tried the following: $url = “http://www.howtogeek.com”; $str = file_get_contents($url); However, this code displays the entire website instead of the content from the URL specified in $url. The website I want to retrieve data from ...

Dynamic Cell Class Assignment in Ag Grid

My Div's dimensions can change based on user interaction, with the div containing an ag-grid component. Initially, the div/grid loads in a compressed size, so I've applied specific classes (like small font-size, height, padding, etc.) to eliminat ...

Currently experimenting with jQuery in conjunction with the reddit API to generate individual divs for every post

Issue with displaying post titles, URLs, and permalinks separately. Need them to display together for each post. See the code below: $(document).ready(function() { $.getJSON( "http://www.reddit.com/r/pics.json?jsonp=?", function foo(data) { ...

Styles for Tab Alignment

.tabs { position: relative; margin: 20px auto; width: 1500px; } .tabs input { position: absolute; z-index: 1000; width: 120px; height: 40px; left: 0px; top: 0px; opacity: 0; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; filt ...

Incorporating a scrolling text box within an <aside> element set in a flex layout

Just trying to make sure the title is clear, as this happens to be my initial post in this space. Lately, I've been venturing back into the creation of websites and currently looking to incorporate a "concert log" below a set of GIFs on my website&apo ...

Tips for creating spacing between an image and a button when utilizing the routerLink feature in CSS

To enhance the user interface, I've utilized a routerLink with an image to navigate back to the home page, and a button to direct users to add a new customer. Now, I am aiming to create some space between these elements. Previously, I used "& ...

The row and col classes will only expand to the full width if they are used within form or p elements

I am facing some unusual behaviors while using Bootstrap in my current project. It seems to be the first time I have encountered such issues. Below is the HTML code snippet: <div class="row" style="background-color: blue; padding: 2rem; ...

Updating user credits through the Model::factory() method is a common practice in my workflow

I need to update the UserCredits after a purchase. For example, if a user currently has 10 credits and purchases 10 more, I want to add the purchased credits to their existing total. The goal is to increment the current credit score with the new credits ...

locate an inner div element

Here are two div elements: <body> <div id="divParent"> <div id="divChild"></div> </div> </body> What is the best way to select the divChild element using JavaScript? ...

Discovering the page load times of web elements on a website with the help of Selenium WebDriver

On my HTML5 webpage, there are 4 charts that each take a different amount of time to load after the static content has already loaded. The loading process is indicated by a 'rendering' circle image for all 4 charts. I am looking for a way to dete ...

Tips for choosing the ancestor's ancestor in CSS

<div id="maincontent"> <div id="content"> <div id="admin"></div> </div> </div> Looking for a CSS rule that will target #maincontent only when #admin is present? I have a background color set in the admi ...

python selenium style attribute for element

There is a snippet of HTML that resembles this: <a class="" data-style-name="Black" data-style-id="16360" "true" data-description="null"<img width="32" height="32" I am trying to extract the text "Black" from it and then click on it. However, there ...

Looking to adjust the width of a Bootstrap dropdown menu?

I am trying to adjust the width of my dropdown menu Here is a link to jsfiddle with my code: [jsfiddler][1]. On the right side of the main menu, I have a languages dropdown menu. When it expands, its width is very large and I want it to match the main m ...

Scrolling to a specific position on a page when a Vue 3 component appears is a must-do for

When I click a button, my basic form component appears without using the router. I would like the scroll position of the form to automatically move down slightly (for example, on the y-axis at 40) once it is displayed. However, I am unsure of how to achi ...

CSS Horizontal Menu positioned left losing its background color due to float property

Can you explain why the background color of the ul element disappears when its child elements are floated? I have a vague memory of reading something about floats causing this issue, but I can't recall the details. (JSFiddle) ul { padding-left: ...

Incorporating HTML/PHP elements into JQuery

I am currently working on a website project for a manufacturing company that involves PHP, HTML, CSS, and JQuery. My goal is to create a seamless user experience where they can easily contact sales representatives for quotes and other information by clicki ...