简体   繁体   English

CSS 柔性部分背景颜色

[英]CSS Flex Section Background Color

I have built a product landing page using Flex.我已经使用 Flex 构建了一个产品登录页面。 I have a section which I have given an id="hero".我有一个部分,我给了一个 id="hero"。 Right now, the background color (#e0bdfc) extends to the full width of the browser window.现在,背景颜色 (#e0bdfc) 扩展到浏览器 window 的整个宽度。 However, I would like to have it cut off at around 1000px (which is the same as my container max-width and my footer).但是,我想让它在大约 1000 像素处被切断(这与我的容器最大宽度和页脚相同)。 When I tried using max-width: 1000px;, nothing happened.当我尝试使用 max-width: 1000px; 时,什么也没发生。 I'm not sure why.我不确定为什么。 I wonder if something else in my entire CSS is affecting it.我想知道我整个 CSS 中的其他东西是否在影响它。 Maybe not.也许不吧。 Below is my CSS snippet for #hero.下面是我的#hero 的 CSS 片段。

#hero {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    height: 200px;
    margin-top: 50px;
    background-color: #e0bdfc;
    
}

If you need to view my full html and css files, you can view them on Github here .如果您需要查看我的完整 html 和 css 文件,您可以在Github上查看它们。

You can view the live site here to see my code in context.您可以在此处查看实时站点以在上下文中查看我的代码。

Thanks for any advice.感谢您的任何建议。

I'm not sure why max-width didn't work for you, but it did for me in the inspector tool.我不确定为什么max-width对你不起作用,但它在检查器工具中对我有用。 I also centered the element by setting margin left and right to auto while keeping your top margin.我还通过将左右margin设置为auto来使元素居中,同时保持上边距。 Hopefully this helps!希望这会有所帮助! 在此处输入图像描述

#hero {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    height: 200px;
    max-width: 1000px;
    margin: 50px auto 0;
    background-color: #e0bdfc;

You didn't wrap this part of the hero part in container, so the width of that part doesn't follow the container,try to wrap the hero part first.I hope this helps你没有将英雄部分的这部分包裹在容器中,所以该部分的宽度不跟随容器,请先尝试包裹英雄部分。我希望这有帮助

 <div class="container"></div> <section id="hero"> <h2>Handcrafted, home-made masterpieces</h2> <form id="form" action="https://www.freecodecamp.com/email-submit" data-dashlane-rid="8ad77776c40e5c69" data-form-type="register,step"> <input name="email" id="email" type="email" placeholder="Enter your email address" required="" data-form-type="email"> <input id="submit" type="submit" value="Get Started" class="btn" data-form-type="action,next"> </form> </section>

here this your code这是你的代码

I also try to add max-width: 1000px and margin: 0 auto to center the block and then it worked.我还尝试添加 max-width: 1000px 和 margin: 0 auto 以使块居中,然后它就起作用了。

You can try to add this code:您可以尝试添加此代码:

#hero {
 display: flex;
 flex-direction: column;
 align-items: center;
 justify-content: center;
 text-align: center;
 height: 200px;
 background-color: #e0bdfc;
 margin: 0 auto;
 max-width: 1000px;

} }

This is the result:这是结果: 在此处输入图像描述

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

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