Styled-Elements Text or Passages

Can we apply styling to text or paragraphs with styled-components? And if so, how can we insert text into the component?

For instance, consider this Footer component:

const Footer = () => (
  <footer className="site-footer">
    <p className="text"> HELLO</p>
    <Copyright
      link={'https://hotmail.com'}
      text={'HELOO'}></Copyright>
  </footer>
);

export default Footer;

I decided to transition from using global classes to css in js, which led me to think about utilizing styled-components. I attempted the following approach:

export const StyledText = styled.text`
    text: Hellooo
    color: white;
    text-align: center;
`

However, when I remove the paragraph and use the StyledText component instead, nothing displays on screen. Attempting to pass Styled Component text={'HELLO'} results in a crash. How can I transform my footer to utilize styled-components effectively?

Answer №1

To give your component a fresh look, you can make the following updates:

import styled from 'styled-components';

const StyledText = styled.p`
  color: white;
  text-align: center;
`;

export default function Footer({text}){
  return   <footer className="site-footer">
    <StyledText>{text}</StyledText>
    <Copyright
      link={'https://hotmail.com'}
      text={text}/>
  </footer>;
}

You can now use the Footer component like this:

<Footer text="HELLO"/>

This solution should meet your requirements.

Answer №2

styled-components focuses solely on the styling of a component, leaving the content untouched. It maintains all children elements, allowing you to easily implement styles like this:

// Example JSX
<StyledText>Hello</StyledText>

// In StyledText.js
export default styled.p`
  color: white;
  text-align: center;
`;

Answer №3

Absolutely, each and every html element along with all custom components can be styled as long as they have a valid className.

This particular example demonstrates the fundamental usage of the API:

const StyledText = styled.p`
  color: blue;
  text-align: center;
`;

const CopyrightInfo = ({ className, link, text }) => (
  <div className={className}>
    <StyledText>{link}</StyledText>
    <StyledText>{text}</StyledText>
  </div>
);

const ModifiedCopyright = styled(CopyrightInfo)`
  border: 1px black solid;
  ${StyledText} {
    color: palevioletred;
  }
`;

const PageFooter = () => {
  return (
    <footer>
      <StyledText>HELLO</StyledText>
      <CopyrightInfo link="https://hotmail.com" text="World" />
      <ModifiedCopyright link="https://hotmail.com" text="World" />
    </footer>
  );
};

https://codesandbox.io/s/serene-hugle-qgsfy?fontsize=14&hidenavigation=1&theme=dark

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

Creating a custom fav icon within a button with CSS styling

https://example.com/code-example Hey there, I'm attempting to replicate the button showcased in the code example above. However, I'm facing a challenge when trying to incorporate the stars without altering the existing script. Another issue is w ...

Issues with the menu pop-up functionality arise when utilizing the CSS class .active

When I have this code, the ".active" function doesn't work when clicked. However, if I change the div class from "top-menu-popup" to "top-menu-popup active" when opening the page, the menu is already activated. <div class="top-menu-popup"> ...

Animate your images with a captivating wave effect using SVG animation

I've been working on animations and I have an SVG image that I want to move like a wave continuously. I've tried using GSAP and CSS but still haven't been successful. Can anyone offer any suggestions or help? It would be greatly appreciated. ...

Fill your HTML form effortlessly using data from Google Sheets

I am relatively new to this topic, but I'm seeking a solution to populate an Apps Script Web App HTML dropdown form with names directly from a Google Spreadsheet. At the moment, I've managed to retrieve an array of names from column A in my sprea ...

What could be causing me to consistently receive a 0 value despite my collection having content stored within it?

How can I retrieve the value of dropVolume and use it in another method after executing my getAllDropsVolumePerDate(date) function? Each time I try to access dropVolume, it returns a value of 0. dropVolume = 0; getAllDropsVolumePerDate(date: string) { ...

Unable to transfer content to clipboard from a Popper element nested within a Dialogue component in Material-UI

If you want to see the bug, try opening this link in Firefox. It almost crashed Chrome :P https://codesandbox.io/s/73z5293391 Click on the OPEN SIMPLE DIALOGUE button. A dialogue will appear, then click on the TOGGLE POPPER button. Now a Popper will be d ...

Debugging TypeScript on a Linux environment

Approximately one year ago, there was a discussion regarding this. I am curious to know the current situation in terms of coding and debugging TypeScript on Linux. The Atom TypeScript plugin appears promising, but I have not come across any information ab ...

How shouldjs makes value verification effortless for unordered arrays

In my express.js application, I'm currently using supertest and should.js as my preferred testing framework. However, I've encountered some difficulties when it comes to testing for specific values within an unordered array. After referring to t ...

Tracking page views through ajax requests on Google Analytics

I have implemented a method to log page views through ajax action when the inner page content is loaded. However, I am facing an issue where the bounce rate data is not getting updated and always shows 0%. The default Google Analytics page view is logged ...

Using a JSON object to navigate to a different page in ReactJS

My code fetches data from an API and sets the response of state in set_ProductDetails on success. I need to pass this response state to a different component and page, bind the data, and I am utilizing "react-router-dom": "^5.2.0". Pro ...

The width of PrimeVue InputNumber is too wide for its parent container

I currently have a collection of items in the shopping cart that are displayed using Bootstrap rows and columns. Each item includes a PrimeVue InputNumber component for adjusting the quantity of a specific product within the cart. If you'd like to ex ...

The array is arranged properly, yet React is failing to render it in the correct order

Here's the code snippet I am working with: { this.state.rows.map((qc) => qc.BinsByDayByOrchardsQCs.map((qc2) => qc2.BinsByDayByOrchardsQCsDefects.map((qc3) => !defectsArray.includes(qc3.Defect) &am ...

Mastering Complex Selectors in ExtJS

In jQuery, I have code that fades out links which do not contain the text entered into a textfield (#searchServices) and fades in links that do contain the text. How can I achieve this in ExtJS? $('#searchServices').keyup(function () { if ( ...

The Angular Google Maps Directive zooms in too much after the "place_changed" event has fired

Currently, I am developing a store locator app for DHL accessible at storefinder.hashfff.com/app/index.html For this project, I decided to utilize the angular-google-maps library for its features. However, in hindsight, working directly with the Google Ma ...

What is the best way to position two elements inline?

I was trying to create a container named project-item that looks like the image linked below: https://i.stack.imgur.com/6XzZ9.png My goal was to align the project logo and the title (h3) in one line, but I struggled to find a suitable solution. This is ...

Utilize Protractor to extract the text within a specified span element

Is it possible to check if the text of the span within the button is "Vigente" using expect and Jasmine? If so, how can I achieve this? <button _ngcontent-hke-28="" class="btn btn-success disabled" tabindex="-1"> <span _ngcontent-hke-28="" class= ...

What is the reason behind Bootstrap 5's decision to have the getOrCreateInstance() method automatically toggle the collapsible element upon creation?

Today was my first encounter with the Collapse feature in Bootstrap 5, and I have to admit, I am not a fan! Imagine a scenario where I have a collapsible element that is initially hidden, and a button to toggle its visibility, like so: <button class=&q ...

List of coordinates extracted from PolylineMeasure plugin in the React Leaflet library

I have 3 different scripts: 1. PolylineMeasure.jsx import { MapControl, withLeaflet } from "react-leaflet"; import * as L from "leaflet"; class PolylineMeasure extends MapControl { createLeafletElement() { return L.control.poly ...

Is there a way to retrieve the Marker that is being dragged when the event handler has already been bound?

How can I identify the Marker that is being dragged when the handler is a bound function? Take a look at this snippet from my react component: constructor() { this.handleMarkerMove = this.handleMarkerMove.bind(this); } createMarker() { const marker ...

Aligning images both horizontally and vertically inside a div with changing dimensions

Looking for advice on centering forward and back arrows on either side of an image within a resizable div. The dimensions of the div change based on the image size. Check out my progress here: Play around with the layout on this jsfiddle: http://jsfiddle ...