Is concealing a button an effective strategy?

Recently, I decided to design a login form for my website. To quickly style the form, I opted to use Bootstrap through CDN. While working on the form, I encountered an issue with the button size as the device width changed. Initially, I applied the classes .btn .btn-danger .btn-lg to the button element. However, as the screen size decreased, I needed a smaller button without the .btn-lg class.

To address this, I created two buttons with identical text and properties. One button had the .btn-lg class while the other had the .btn-sm class. Through some CSS modifications, I toggled the visibility of these buttons based on the device's width - hiding the .btn-lg when the width was less than 767px and vice versa for the .btn-sm.

While this solution seems to work well on the front-end, I am unsure if it is considered a good practice in web development. Could this approach potentially cause issues in future developments?

Below are snippets of the relevant HTML and CSS code:

HTML:
<form class="form-group">
    <input type="text" class="form-control" name="user" placeholder="Username" required>
    <input type="password" class="form-control" name="pwd" placeholder="Password" required>
    <p>Forgot your password?</p>
    <button type="submit" class="btn btn-danger bg-gradient btn-lg">LOG IN</button>
    <button type="submit" class="btn btn-danger bg-gradient btn-sm">LOG IN</button>
</form>
CSS:
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.btn-danger {
    background-color: orangered;
    color: white;
}

.btn-lg {
    border-radius: 30px;
    display: none;
}

.btn-sm {
    border-radius: 30px;
}

@media screen and (min-width: 768px) {
    .btn-lg {
        display: block;
        margin: auto;
    }

    .btn-sm {
        display: none;
    }
}

Answer №1

In my opinion, the best approach depends on the situation, but I personally like to stick with a single element for simplicity. Here's one potential solution that may not suit everyone:

<button type="submit" class="btn m-0 p-0">
    <span class="btn btn-danger btn-sm d-md-none">BTN</span>
    <span class="btn btn-danger btn-lg d-none d-md-inline">BTN</span>
</button>

It seems like the crux of your query revolves around different button styles for various viewport sizes rather than the button's functionality itself. Bootstrap provides "Display property notation" and "Layout breakpoints" which can prove helpful in managing elements based on viewport changes.

The button's children dictate its appearance:

  • Viewport less than 768px This
    <span class="btn btn-danger btn-sm d-md-none">BTN</span>
    will be hidden when the viewport is >=768px thanks to d-md-none.
  • Viewport greater or equal than 768px The other
    <span class="btn btn-danger btn-lg d-none d-md-inline">BTN</span>
    will only display when the viewport size is >=768px using d-none d-md-inline.

Feel free to check out a live demo here. Simply adjust your browser width to view the outcome.

https://i.sstatic.net/557ch.gif

The method I presented doesn't require extra custom styles for managing viewport changes, keeping it straightforward with just one button element. In my view, this approach is simpler to maintain and work with.

Answer №2

Whenever I encounter the same issue and dilemma, particularly when using Bootstrap, my goal is always to minimize the amount of CSS code I need to write. As a result, I tend to lean towards the first approach:

Approach 1 - Using 2 Buttons;

Having two separate buttons and displaying them based on the device width may not be the most efficient or recommended practice for responsive design. It can lead to maintenance issues in the future and add unnecessary complexity to your code.

However, this method provides a quick solution to the problem. Additionally, it gives you flexibility for future design changes, such as adjusting the font size, by simply adding classes like fs-4 and fs-6 to your buttons.

We can achieve the desired responsiveness by leveraging Bootstrap's existing classes without the need for extra CSS code.

<form>
    <!-- Other Form Elements -->
    <button type="submit" class="btn btn-danger btn-lg d-none d-md-inline">Login</button>
    <button type="submit" class="btn btn-danger btn-sm d-inline d-md-none">Login</button>
</form>

Approach 2 - Styling with CSS;

While this solution may work well for many people, it does require some CSS adjustments to change the button widths. By utilizing CSS media queries, you can easily set the width of your login button.

<form>
    <!-- Other Form Elements -->
    <button type="submit" class="btn btn-danger btn-lg login-button">Login</button>
</form>
<style>
@media (min-width: 992px) {
    .login-button {
       width: 120px; /* Adjust the width as needed */
    }
}

@media (max-width: 576px) {
    .login-button {
        width: 60px; 
    }
}
</style>

Both approaches have their merits, and the decision between them involves a trade-off that should be made based on your specific circumstances.

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

Tips for updating the hover background color of a table row with a unique class in bootstrap

Modified: Here is the table along with its corresponding CSS styling: .table-hover tbody tr:hover { background-color: #fff !important; } <html lang="en"> <head> <meta charset="utf-8" /> <meta name="v ...

Css technique for changing color on mouse hover

I currently have a social media bar on my website with icons for Facebook, Twitter, Google+, and RSS. Here is how it looks: When I hover over the icons, I want the circle around the image to change color to blue. However, every attempt I've made end ...

Download personalized HTML design

Situation When exporting to HTML, the resulting code appears as follows: <html> <head> <title></title> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <style type="text/css" ...

choose from the list of options that appear above the control

It's strange, but no matter how simple the example is - whether it's regular HTML or Bootstrap - the option list always appears on top of the select control itself. I have no idea why this is happening. Can someone help me with this issue? Here ...

Tips for adding an offset to a #link located on a different webpage

I have a website that consists mainly of an index.html file with a few additional subpages. The index.html file is divided into sections, but the navigation has a fixed position and a height of 100px, which requires a 100px offset for links like <a hre ...

I'm having trouble finding a solution to remove the white space

I've been troubleshooting a website and noticed some unexpected whitespace on the page. Even though I have set my page wrapper div and other elements to width: 100%, the content in between is not utilizing the full width of the screen. I suspect this ...

Whenever I try to execute watir-webdriver, I notice that the CSS file is

Currently, I am in the process of learning how to utilize watir-webdriver in ruby for my QA tasks. However, a recurring issue arises when running my watir-webdriver scripts on certain sites - some pages have missing CSS Files. This particular problem seems ...

When the state changes, initiate the animation

Currently, I am working with Vue.js and need to animate a navigation menu. My goal is to display two li elements when a user hovers over one of the navigation buttons. At the moment, I have set the data type showActivities to false by default and changed ...

The unique identifier for a specific child element in Firefox

While working on this website: I customized the right sidebar navigation using: .sidenav a:after { display: block; position: relative; z-index: 0; top: -3.6em; left: 0.15em; width: 90%; content: "."; font-family: 'R ...

What is the proper way to assign a class name to an animation state in Angular 2/4?

I am currently working with Angular Animations using version 4.1.3 Check out the code snippet below: @Component({ selector : 'my-fader', animations: [ trigger('visibilityChanged', [ state('true' , style({ opaci ...

Guide on incorporating Bootstrap JS into HTML5 reusable web elements

RESOLVED: the solution is in a comment TL;DR: Issues triggering Bootstrap's JS, likely due to incorrect import of JS scripts I've been working on integrating Bootstrap with my custom reusable web components across all pages. Specifically, I&apo ...

Align text to the left of a button with Bootstrap 4

Currently, I have two buttons stacked on top of each other. I'm attempting to include some text to the left of the bottom button, but I am encountering two obstacles: The first issue is that it creates a white round-ish shape around my top button w ...

Just starting out with JavaScript - updating the appearance of an element

Based on the value of a boolean, I am looking to control the visibility of specific tabs in my sidebar when the page loads. var someVar = true; function show_ifTrue() { if (Boolean(someVar) == true) { document.getElementById('x'). ...

Is there a way I can alter the font style of the word 'Logout'?

Hey, does anyone have a solution for changing the text color of "Logout!" to white? echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout!"; <font color="#ffffff">your answer</font> ...

Enable the image to extend beyond the boundaries of its containing div

How can I prevent a div from extending its width, allowing an image to be visible outside of it in IE8? Specifically, my div may be 200px wide while the image is 250px wide. ...

Tips for locating an element within pre-processed HTML content

Is there a way to modify this code snippet to target the 'price' element within each 'info_block' instead of displaying the entire 'info_block' content? My ultimate goal is to locate the price within every 'info_block&apo ...

How to Achieve an Overlapping Background Image Effect with Divs Using HTML and CSS

Is it possible to achieve the following scenario using just HTML and CSS? I want to display the background image of my HTML body through a clipped div. Keep in mind that the final website needs to be responsive (mobile-first), so any solution should accom ...

the table image does not resize correctly in mobile view

I need to resize an image inside a table's <td> when the screen size is in mobile mode. Although I have already added a viewport, my mobile query works correctly for other purposes except for the image. Here is the HTML code: <table class="t ...

Delete the initial double line break and then switch the remaining double line breaks to single line breaks

I am facing an issue with some HTML content fetched from an external source. My aim is to specifically target instances of double br tags using jQuery. I want to delete the first occurrence of double br and convert all subsequent occurrences into single br ...

Placeholder fails to appear

After implementing some jQuery validation, I wanted to display a text as a placeholder when the user skipped out of an input field without entering anything. This is the code I wrote: $('input[type="text"]').on('blur', function() { ...