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

Having trouble implementing font css file in Reactjs

When working with Reactjs (Nextjs), every time I try to incorporate "css" into my project, I encounter the following error on my screen: Module not found: Can't resolve '../fonts/fontawesome-webfont.eot?v=4.7.0' How can I solve this issue? ...

Is there a way to achieve the same zooming and panning effects on an element using CSS transform instead of jQuery's

If you click on the following link, you will see a jsFiddle that I have created to demonstrate my question: http://jsfiddle.net/VbCcA/3/ In my project, I have developed a function that allows for panning and zooming to a specific element. This function i ...

The font color fails to change when hovering over

I'm experiencing an issue with the styling of my input box. When the user starts typing, a div containing a list of possible clubs is displayed. Currently, I am having trouble with the .clubLink:hover class and its background color. CSS: <style& ...

The reason the section's height is set to 0 is due to the absolute positioning of the div

Currently, I have a section tag with three divs inside that are positioned absolute (used for the isotope plugin). <section id="section1" class="cd-section"> // pos. static/relative has height 0px <div class="itemIso"> // pos. absolute, he ...

Hide Scrollbar in CSS

Looking for a way to conceal the vertical scrollbar on my website, especially on mobile devices with touch scrolling capabilities. I attempted using CSS but didn't get satisfactory results, especially on older browser versions. ...

The process of retrieving CSS attributes from a control found by XPath using Selenium IDE

During my Selenium IDE script execution, I am looking to identify and handle an error state. This specific error state is visually highlighted on the page with a select control changing its background color to a light shade of red. The xpath for the selec ...

What is the best method to position a modal in the exact center of the screen?

Is there a way to position the modal at the center of the screen? I have tried implementing this HTML and JavaScript code. Interestingly, it works fine in Chrome console but fails when I try to refresh the page. $('.modal').css('top', ...

Is it possible for flex-grow to affect images? What steps can I take to identify any issues within my code?

<section> <div class="section-header"> <h5>Discover Our Team</h5> <h1>Meet the Experts</h1> <h5>Lorem ipsum dolor sit amet consectetur adipisicing elit. Nesciunt, eaque.</h5> ...

React Semantic UI Grid and Div components can be toggled by using a button to decrease

My goal is to create a menu that initially displays with a certain width, but when a button is clicked, it will decrease the width to 50px and only show the icon. I'm struggling to figure out how to adjust the width of my div and integrate this funct ...

The bottom portion of my page vanishes without a

My footer has the following CSS class: .footer{ border-top:solid 2px #BBB; background : url('../images/footer.png'); font-family : helvetica; font-style : italic; height:9em; clear: both; color: black; wid ...

Lists within lists and the use of percentages

There is a common belief that separating design and functionality is beneficial. Let's focus on the separation between HTML and CSS, specifically when organizing a menu hierarchy. The issue with nested <ol>, <ul>, and <li> elements ...

Using CSS, eliminate the drop-down menu from the main navigation on Squarespace

While working on my Squarespace website, I've been impressed with how the FAQ page/questions are structured. However, a drawback is that it creates a clunky drop-down menu when hovering over the FAQ tab in the main navigation. You can see an example ...

The alignment of images loop and videos loop is off

https://i.stack.imgur.com/6o38F.jpg I am facing an issue with a td container in which I am using PHP to loop through a collection of videos and images. Strangely, the videos are displaying higher than the images even though they are both contained within ...

Using CSS animations to animate a div while creating a subtle pause between the two

I am working on creating a notification bar that will be displayed and hidden using CSS animation. My challenge is finding the right delay between the two animations. HTML <div id="notification" class="alert" role="alert"></div> JS $(' ...

Text that spans across multiple lines

I have a multi-line text that I need to adapt to a div. For example: https://i.stack.imgur.com/mtZmf.png Is there a way to adjust the text within the div? Can we prevent the blue spacing after the word "text" using CSS? If I have various texts of differ ...

PrimeNG - Sticky header feature malfunctioning in the p-table

Hello there, I am currently using PrimeNG p-table which features both horizontal and vertical scrolling. My goal is to implement a sticky header for the table, so far I have attempted two methods: [scrollable]="true" scrollHeight="350px" ...

Creating a bootstrap form field that spans 100% in width:

I am encountering an issue with two column divs in a row, each containing two text fields. When resizing on mobile, the width of the textbox is not expanding to 100% and looks unattractive. Below is my Bootstrap code, but strangely, the width looks fine f ...

The <img> element is able to fill an entire div while maintaining its original aspect ratio

I am currently facing a challenge with implementing a responsive gallery slider in Bootstrap. In order to achieve responsiveness, I need to use properties like max-height. The issue arises from the fact that the images in the gallery can vary in size and ...

Display the GIF when the mouse hovers over it, but ensure it plays just a single time

Is it possible to make a GIF only play once when you hover over it on a website? I want to avoid the animation repeating if the user hovers over it again without refreshing the page. Currently, I have a static image that changes to a gif on mouseover, but ...

Tips for passing a timestamp to a Postgresql DB using a Button in a Form instead of a traditional rails time_select box

I am currently developing a CAD App in Rails 4 with Ruby 2. The goal is to provide users with a button on the screen to log an on-scene time and clear scene time, especially since most users will be using touch screen computers. Instead of using the defaul ...