简体   繁体   English

为什么我的包装器会破坏它正在包装的内容的 CSS?

[英]Why is my wrapper disrupting the CSS of the content it is wrapping?

When I add a wrapper to this jsx, it breaks the existing CSS and I can't determine why.当我向这个 jsx 添加包装器时,它会破坏现有的 CSS,我无法确定原因。 The page CSS is proper at first, but this wrapper causes the menu-item s to shrink.页面 CSS 一开始是正确的,但是这个包装器会导致菜单项缩小。 What am I missing here?我在这里想念什么?

Before:前:

import React from "react";
import { withRouter } from "react-router-dom";
import "./menu-item.styles.scss";

const MenuItem = ({ title, imageUrl, size, history, match }) => (
    <div className={`${size} menu-item`}>
        <div
            className="background-image"
            style={{
                backgroundImage: `url(${imageUrl})`,
            }}
        />
        <div className="content">
            <h1 className="title">{title.toUpperCase()} </h1>
            <span className="subtitle">SHOP NOW</span>
        </div>
    </div>
);

export default withRouter(MenuItem);

After:后:

import React from "react";
import { withRouter } from "react-router-dom";
import "./menu-item.styles.scss";

const MenuItem = ({ title, imageUrl, size, history, match }) => (
    <a href="https://www.google.com">
        <div className={`${size} menu-item`}>
            <div
                className="background-image"
                style={{
                    backgroundImage: `url(${imageUrl})`,
                }}
            />
            <div className="content">
                <h1 className="title">{title.toUpperCase()} </h1>
                <span className="subtitle">SHOP NOW</span>
            </div>
        </div>
    </a>
);

export default withRouter(MenuItem);

The browsers generally add their own style to the anchor tag, and the tag also inherits some style from the body and whatnot.浏览器一般会在anchor标签中添加自己的样式,标签也继承了body之类的样式。 What you need to do is make apply some CSS of your own to the anchor to make sure it's always extended like a wrapper.您需要做的是将一些您自己的 CSS 应用到锚点,以确保它始终像包装器一样扩展。

Maybe something like this should help.也许这样的事情应该有所帮助。

#mainMenu a {
 display: inline-block;
 width: 100%; //depending if your menu direction is in rows or columns
}

If your menu item also has some padding, I suggest moving that padding to the anchor as well.如果您的菜单项也有一些填充,我建议将该填充也移动到锚点。

Anyhow, you can always toy around with the CSS applied to your anchor tag if you inspect the element (Right Click -> Inspect).无论如何,如果您检查元素(右键单击 - > 检查),您始终可以使用应用于锚标记的 CSS 来玩弄。 Let me know if you have more questions about this.如果您对此有更多疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM