Seeking assistance with the manipulation of divs based on the width of the screen for hiding

Recently, I encountered an issue with changing a 3x3 image gallery layout to an image slider when the screen size was less than 768px (mobile). Thanks to the help of everyone and particularly Danilo, who provided solutions in the comments, I was able to identify that my CSS wasn't properly targeting the screen. I realized I needed @media only screen and had also overlooked a DIV, which was a rookie mistake on my part.

If you're facing a similar challenge of switching content display from a desktop layout to a mobile layout in Bootstrap 4, you can utilize CSS media queries targeting the wrapped DIV for displaying or hiding elements accordingly.

Check out the updated working snippet below:

@media only screen and (min-width: 768px) {
    .slider {
        display: none;
    }
    .block {
        display: block;
    }
}

@media only screen and (max-width: 768px) {
    .slider {
        display: block;
    }
    .block {
        display: none;
    }
}

.hold-tests {
    display: flex;
    width: 100%;
}

.col {
    flex: 1;
} 
<!--- Start of Gallery Section -->
<div id="hold-tests">
<div class="col slider">
<div id="sliderIndicators" class="carousel slide carousel-fade z-depth-1-half" data-ride="carousel" data-interval="9000">
<div class="carousel-inner col-" role="listbox">
<!--- First Slide -->
<div class="carousel-item active">
<img src="Img/PHportrait.png" alt="background image">
</div>
<div class="carousel-item">
<img src="Img/PHportrait2.png" alt="background image">
</div>
<div class="carousel-item">
<img src="Img/PHportrait.png" alt="background image">
</div>
</div>
<div class="narrow">
<div class="col-12">
<p class="lead text-center">Want to see more of my work?</p>
<!-- add in social media icons and add target="_blank" to open in new tab -->
</div>
</div>
</div>
</div>

<div class="col block">
<div id="gallery" class="offset">
<div class="col-md">
<div class="row no-padding">
<div class="col-md-4">
<div class="gallery-card">
<a href="Img/PHportrait.png" target="_blank">
<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
</a>
</div>
</div>
<div class="col-md-4">
<div class="gallery-card">
<a href="Img/PHportrait.png" target="_blank">
<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
</a>
</div>
</div>
<div class="col-md-4">
<div class="gallery-card">
<a href="Img/PHportrait.png" target="_blank">
<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
</a>
</div>
</div>
<div class="col-md-4">
<div class="gallery-card">
<a href="Img/PHportrait.png" target="_blank">
<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
</a>
</div>
</div>
<div class="col-md-4">
<div class="gallery-card">
<a href="Img/PHportrait.png" target="_blank">
<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
</a>
</div>
</div>
<div class="col-md-4">
<div class="gallery-card">
<a href="Img/PHportrait.png" target="_blank">
<img src="Img/PHportrait.png" class="img-fluid" alt="Gallery Picture">
</a>
</div>
</div>
</div>
</div>
<div class="narrow">
<div class="col-12">
<p class="lead text-center">Want to see more of my work?</p>
<!-- add in social media icons and add target="_blank" to open in new tab -->
</div>
</div>
</div>
</div>
</div>
<!--- End of Gallery Section -->

Answer №1

If you're utilizing bootstrap, make the most of it. No need for jQuery in this case, as everything is already taken care of.

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1N" crossorigin="anonymous">
<div class="d-none d-md-block">
  Visible only from medium devices onwards
</div>
<div class="d-md-none">
  Visible only on small screens
</div>

Explore more about Bootstrap Display Utilities here

Learn more about Bootstrap Responsive Utilities here


It's important to remember that IDs typically do not begin with a #. While it might be technically valid, if you were to select them using jQuery, you would need to use $('##option1').

Answer №2

You have the ability to achieve this using CSS

html

<div class="hold-tests">
    <div class="col test1">Test1</div>
    <div class="col test2">Test2</div>
    <div class="col test3">Test3</div>
</div>

css

@media only screen and (min-width: 768px) {
    .test1 {
        display: none;
    }
    .test3 {
        display: block;
    }
}
@media only screen and (max-width: 768px) {
    .test3 {
        display: none;
    }
}

.hold-tests {
    display: flex;
    width: 100%;
}

.col {
    flex: 1;
}

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

Disabling a text area or mat-form-field within the same HTML file in Angular 9

Currently, I am working with Angular 9 and angular Material Reactive Forms. My goal is to disable certain fields without relying on the disabled status within formControl in the TypeScript file. Instead, I want to achieve this solely through HTML. Here is ...

Customize Text Area's Font Based on Selected Option in Dropdown List

I'm currently working on an HTML file that includes a dropdown list and a text area. Here's the code snippet: <select id='ddlViewBy' name='ddlViewBy' onchange='applyfonttotextarea()'> <option value = ' ...

Utilize Python to generate a neo4j graph database by incorporating HTML forms

Greetings! I am brand new to the world of neo4j and I am eager to learn how to create nodes and properties of a graph using HTML forms with the help of py2neo and Neo4j. Additionally, I am interested in understanding how to automatically assign IDs to th ...

Can anyone explain the meaning of (0, _jquery["default"]) in relation to jQuery selectors or functions?

Trying to implement jQuery on an offline page can be challenging when dealing with EmberJS, RequireJS, and other technologies. My goal is to replace complex code with simple jQuery. The HTML below should respond to user interaction: Loading i ...

Issue with custom page not displaying summary image in Moodle

Whenever I try to fetch a summary image on my customized page, it does not show the image's name and instead displays a small icon image. echo format_text($course->summary); Click here for an example image ...

Retrieving a PHP string and storing it in a jQuery variable

How can I store the following PHP string in a jQuery variable? { '05-10-2018' : '<a href='http:www.google.com/' target='_blank'>Seeing dads differently</a>', '05-10-2018' : '<a href=&ap ...

Update the DIV element's class to reflect whether the quiz answer provided is correct or incorrect

I am facing a challenge while attempting to assign a CSS class to a dynamically created element in JavaScript. The error I'm encountering pertains to the reference issue with the trackerMarker element within the event listener functionality. Although ...

What is a more efficient way to write nested subscribe in Angular?

I am a beginner with RxJS and I'm interested in learning how to write clean code using it. I currently have a nested subscription that I've been trying to refactor without success. firstMethod() { this.testMethod(name) console.log(this.curren ...

TS2345: The argument provided, which is of type 'Event', cannot be assigned to the parameter expected, which is of type 'HtmlInputEvent'

I am facing an issue while trying to upload a file, and I could use some assistance in resolving it. Angular-----Error: src/app/admin/producto/create-producto-dialog.html:38:47 - error TS2345: Argument of type 'Event' is not assignable to parame ...

Tips for incorporating HTML from a Controller into an AngularJS directive template - encountering interpolation issue "Can't interpolate"

Within my AngularJS controller, there is a function: //some stuff from controller var variable1; var variable2; $scope.setTitle = function () { if ( //sth) { variable1 = "string" return variable1; ...

The unique GopikaTwo Gujarati font is unfortunately not compatible with Android mobile browsers and hybrid applications

I am currently trying to incorporate the custom font GopikaTwo into my hybrid application. Below is the code snippet I have added to my CSS file: @font-face { font-family: 'GopikaTwo' !important; src: url('GopikaTwo.eot') !impo ...

Incorporating Captions with Jquery Cycle for Enhanced Slideshows

Looking for assistance in getting Jquery cycle to display captions with the current script. While typical caption demos on the Jquery Cycle website recommend using the after: callback to show captions, I'm facing challenges because the after: callback ...

What is the best way to generate a variable amount of div elements with constantly changing content using Angular2?

I'm not entirely sure on the process, but I believe ngFor would be used in this scenario. Essentially, my goal is to generate the following div multiple times, with each iteration updating the value inside the brackets from 0 to 1, 2, and so forth... ...

Horizontal Screen Sharing on iPad

Currently, I have a two-column website set up using Wordpress. However, I am facing an issue with the right side column scrolling over on iPads. While the page behaves properly on desktops, on iOS devices, the div is able to scroll over the navigation ba ...

Having difficulty positioning menu items beside the logo

I am facing an issue with aligning my menu items next to the logo in my navbar. Currently, the menu items are positioned slightly below and to the right of the logo. Although the horizontal alignment is correct, I need to adjust the vertical positioning to ...

Update the index of each list object and then add them to the Angular controller

Currently, I am working on integrating Spring with AngularJS. In my setup, I have a Spring controller returning a List object to the Angular controller. When I print the List in the Spring controller console, it appears as follows: [org.com.pro.dto.MyDTO ...

Preserve the HTML file with all its source code intact

Looking to access a specific parameter on this webpage: . For instance, if I need to retrieve the "Topological Polar Surface Area" value. If I manually save the page using Internet Explorer, I can locate the value with these commands: cat file.html | gr ...

Building paths through a jQuery loop

Transform String into Delimited Array, Generate Loop of Check Boxes, and Build New String Given String = "Folder Tier1\Folder Tier2\Folder Tier3\Folder Tier4\Folder Tier5\Folder Tier6\" (Missing) Use "\" as a delimi ...

I'm having trouble getting the activeStyle to work properly with a <NavLink>

I'm facing an issue with setting activeStyle for the NavLink component when the URL is on the map route. Currently, when I click on a NavLink, I can navigate to the correct route, but only <NavLink to='/' is being highlighted as active. ...

The responsiveness of my flexbox containers is compromised when using flex-direction:column

I recently started learning flexbox and I'm having trouble understanding it. My header is somewhat responsive, and when I resize my browser window, it adjusts very well. However, the container below with flex-direction: column behaves strangely (try ...