Utilize a component's CSS styles by inheriting them and applying them to another component

I have created custom styles for an object as shown below:

form#sign_in{
    position:relative;
    margin:85px auto 50px auto;
    width:455px;
    background:#fff;
    border:#dfdfdf 1px solid;
    border-radius:5px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
}

Now, I would like to apply all the styles from sign_in to another component, except for the width property.

form#another_sign_in{
    width: 520px;
}

Is there a way to inherit the styles from form#sign_in without including the width property?

Answer №1

To enhance the design of your login forms, consider creating a default class to style them uniformly (or using the form tag to apply styles to all forms). Additionally, use specific ids or classes to define the width of each individual form.

.default_sign_in{
    position:relative;
    margin:85px auto 50px auto;
    background:#fff;
    border:#dfdfdf 1px solid;
    border-radius:5px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
}

.big_form{
     width:700px;
}
.small_form{
     width:350px;
}

html

<form class="default_sign_in big_form">...</form>

Answer №2

To keep things organized, consider creating a class that contains these common styles. Alternatively, you can avoid creating an additional class:

form#sign_in, form#another_sign_in{
    position:relative;
    margin:85px auto 50px auto;
    background:#fff;
    border:#dfdfdf 1px solid;
    border-radius:5px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
}

form#sign_in{
    width:455px;
}

form#another_sign_in{
    width: 520px;
}

If you're looking for a more efficient approach, I recommend exploring CSS preprocessors like SASS, where you can define a mixin:

@mixin formStyles($width){
    width:$width;
    position:relative;
    margin:85px auto 50px auto;
    background:#fff;
    border:#dfdfdf 1px solid;
    border-radius:5px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;   
}

You can then utilize the mixin like so:

form#sign_in{
    @include formStyles(455px);
}

form#another_sign_in{
    @include formStyles(520px);
}

Answer №3

It is not feasible to assign two different ids to the same element in this case.

Instead, you can assign the same class attribute to both form elements and then add an additional class to the second form.

HTML:

<form class="sign_in">.....</form>
<form class="sign_in another_sign_in">....</form>

CSS:

form.sign_in{
    position:relative;
    margin:85px auto 50px auto;
    width:455px;
    background:#fff;
    border:#dfdfdf 1px solid;
    border-radius:5px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
}

form.another_sign_in{
width: 520px;
}

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 are some ways to create a div section within a Google Map interface?

Is there a way to create a div area within the Google map iframe? Some of my code is already prepared here (). The image in this link () illustrates exactly what I'm trying to achieve. ...

Hide the scroll bar in html/css while keeping the functionality of the arrows intact

Is there a way to remove the scroll bar but still be able to access overflown data using arrows? Any assistance would be greatly appreciated. Thank you in advance. ...

The localization feature in jQuery mobile is causing the button style to disappear

For my current project, I am utilizing the jquerymobile framework along with the i18Next localization library. Here is the HTML code snippet: <div id="messageboxPage" data-role="page" data-theme="a"> &l ...

Creating a CSS dropdown menu with absolute positioning and setting the left position to auto

Just laying out my usual HTML menu structure: <div id="page"> <h1>Home</h1> <ul id="nav"> <li><a href="http://example.com/">Home</a> <ul> <li><a href="http://examp ...

Customizing the appearance of all Angular components with styles.scss

Is there a way to create a universal style in styles.scss that can be applied to all Component selectors (e.g. app-componentA, app-componentB ...)? I understand that I could manually add the style to each selector, but I am concerned that it may be forgot ...

Adjust color in real-time with JavaScript

I am using a json file to store data for generating a diagram, and I want to change the color of the diagram conditionally based on an attribute in the json. If the attribute is false, the color should be red, and if true, it should be green. Here is a sni ...

Change the text in a CSS checkbox label when it is selected

There is an innovative Pure-CSS accordion that caught my eye, but I have a unique feature in mind that I would like to incorporate. What I am looking for is the ability for the labels to dynamically change based on the state of the checkboxes without relyi ...

Tips for exchanging divs in a mobile view using CSS

Illustrated below are three separate images depicting the status of my divs in desktop view, mobile view, and what I am aiming for in mobile view. 1. Current Status of Divs in Desktop View: HTML <div id="wrapper"> <div id="left-nav">rece ...

DataTables.js, the powerful JavaScript library for creating interactive tables, and its compatible counterpart

I'm currently making some changes to a dynamic table that require enabling a vertical scroll bar. As a result, I need to implement this vertical scroll bar mechanism on an existing table. The code in our project utilizes dataTables.js version 1.5.2 ...

css The issue of ul and ol elements not inheriting the padding property

https://codepen.io/unique-user123/pen/abcDeFG Why is it necessary to utilize the ul, ol, and li selectors in order to remove the default 40px left padding and 16px top and bottom margin? Can't we simply use the body selector to achieve this? <hea ...

What are the best methods for ensuring a website maintains consistent visibility across all different screen sizes?

Recently, I created a website tailored for a viewport size of 1366px by 768px. My goal is to maintain the same design regardless of the browser window size without implementing responsive design techniques. For instance: When viewing the website on a 360 ...

What is the best way to trigger an event function again once a specific condition has been satisfied?

I'm currently working on a project where I need a sidebar to scroll at a slower rate until it reaches a specific point, and then stop scrolling once that point is reached. Below is the jQuery code I've been using: $(window).on("scroll", function ...

What is the best way to switch the visibility of a div on click from another div?

My goal is to make a div toggle visible or hidden when another div is clicked. The only connection between the two divs is that they are both nested within the same parent div. There's a DIV element with the class of "comment" which contains a DIV ele ...

CSS Grid having trouble with wrapping elements

Greetings to all, As a newcomer to the world of web development, I am currently exploring the wonders of the CSS grid tool. However, I have encountered a roadblock: My intention is for the cards to automatically flow one by one into the next row while ma ...

Despite having the correct image URLs, the images are failing to display in the custom section of Shopify

Currently, I'm in the process of developing a unique Shopify section that showcases text alongside images. To enable users to upload images via the theme customizer, I have implemented the image_picker settings within the schema. The issue I am facing ...

Combining MySQL tables for main menu and subcategories

Working on a new project and seeking help with a certain situation. I have a SQL database featuring two tables: menu and submenu. The table structure is as follows: Menu: id | title | order Submenu: id | title | parentId Currently, I'm using two si ...

Maintaining the original layout upon refreshing the page

Incorporating two forms on my website, I added a button that transitions between the login and sign-up forms smoothly. The process is as follows: Initially, the login form is displayed; clicking the button switches to the sign-up form. Proceeding to subm ...

I noticed a random gap between the elements with no discernible explanation

<div id="colorscheme"> </div> <div id="content"> <div id="display_saved"> TEXT TEXT TEXT </div> Here is the HTML structure related to a particular document issue. CSS: #colorscheme{ width:25%; display:inline-blo ...

The dropdown function in Bootstrap seems to be malfunctioning

I have implemented a basic dropdown menu using the code below: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" ...

Styling multiple divs using CSS

What is the method for attaching CSS to sp-copyright? <div class="container"> <div class="row"> <div id="sp-footer1" class="col-sm-12 col-md-12"> <div class="sp-column "> <span class="sp-copyright"> ...