简体   繁体   English

在 CSS 中居中 div 时遇到问题

[英]Having trouble centering div in CSS

I'm working with one of my first pages.我正在处理我的第一页。 It contains a pop-up that needs to be centered in the exact middle of the page.它包含一个需要在页面正中间居中的弹出窗口。 I'm aware that there are many ways to do this, but I'm having trouble with each one of them;我知道有很多方法可以做到这一点,但我对每一种都有问题;

The first one I tried is this:我尝试的第一个是这样的:

        <div class="mod" id="modal">
            <div class="mod-header">
                <div class="mod-title">15% off for new customers</div>
                <button data-close-button class="close-button">&times;</button>
            </div>
            <div class="mod-body">
                <h2>subscribe to our newsletter and get 15% off your first purchase</h2>
                <form method="POST">
                    {{ form|crispy }}
                    {% csrf_token %}
                    <button class="submit-btn" type="submit">go</button>
                </form>
            </div>
        </div>

Styles: Styles:

 .mod{
        position: fixed;
        transition: 200ms ease-in-out;
        transform: translate(-50%, -50%) scale(0);
        border: 1px solid black;
        z-index: 999;
        padding: 1.2em;
        background-color: #f6f1eb;
    }
    .mod.active{
        top:50%;
        left:50%;
        transform: translate(-50%, -50%) scale(1);
        -webkit-font-smoothing: antialiased;
    }

The problem with this is that I'm getting a blurry effect in the div.问题是我在 div 中得到了模糊的效果。 I tried everything (webkit antialiasing, setting translate to 51%, etc) and it won't change.我尝试了一切(webkit 抗锯齿、设置转换为 51% 等),但它不会改变。

Some other ways of centering implicate selecting the parent container, but that's impossible in my case since this is a large website and I need this to be in the center of the entire page, not the container.其他一些居中方式涉及选择父容器,但在我的情况下这是不可能的,因为这是一个大型网站,我需要它位于整个页面的中心,而不是容器的中心。

I also considered using calc() instead of translate() and subtract the width and height of the container, but then I'd need to know the dimentions all the time, and it would lose the responsiveness.我也考虑过使用calc()而不是translate()并减去容器的宽度和高度,但是我需要一直知道尺寸,它会失去响应能力。

Is there any better way to do this?有没有更好的方法来做到这一点? I'm sure it's very simple, but I'm a beginner and don't know much.我敢肯定这很简单,但我是初学者,知道的不多。 Let me know if I'm lacking code or I'm not being clear enough.让我知道我是否缺少代码或不够清楚。 Thanks in advance!提前致谢!

Just give your modal another container and it's good to go.只需给您的模态另一个容器,它对 go 很好。

 .mod { /* Remove background if backdrop is not needed */ background: rgba(0, 0, 0, 0.1); position: fixed; /* 0, 0, 0, 0 gives 100% full height and width on absolute/fixed elements */ top: 0; left: 0; right: 0; bottom: 0; }.mod.wrapper { width: 300px; height: 300px; /* since there's no feature to vertically center you need margin top, while the auto will center horizontally automatically whatever the width is. Do note that left, top 50% is quite unreliable since it doesn't take into account the size of the container */ margin: 10% auto 0; background: white; display: block; border-radius: 4px; text-align: center; }
 <,-- Will be the one that is position fixed --> <div class="mod" id="modal"> <:-- While this one will make the modal look; with this you can use margin left/right: auto to automatically center horizontally regardless of the width --> <div class="wrapper"> <div class="mod-header"> <div class="mod-title">15% off for new customers</div> <button data-close-button class="close-button">&times;</button> </div> <div class="mod-body"> <h2>subscribe to our newsletter and get 15% off your first purchase</h2> <form method="POST"> </form> </div> </div> </div>

Change the position property of the modal to absolute and add top and left property to it.模态position属性更改为absolute并为其添加topleft属性。 Remove other properties of the active class and add only display property to it.删除active class 的其他属性并仅添加display属性。

 let btn = document.querySelector("#btn"); let closeBtn = document.querySelector(".close-button"); let modal = document.querySelector("#modal"); btn.addEventListener("click", () => { modal.classList.add("active"); }); closeBtn.addEventListener("click", () => { modal.classList.remove("active"); });
 .mod { position: absolute; top: 50%; left: 50%; display: none; width: 400px; transition: 200ms ease-in-out; transform: translate(-50%, -50%); border: 1px solid black; z-index: 999; padding: 1.2em; background-color: #f6f1eb; }.active { display: block; }
 <button id="btn">Show Modal</button> <div id="modal-container"> <div class="mod" id="modal"> <div class="mod-header"> <div class="mod-title">15% off for new customers</div> <button data-close-button class="close-button">&times;</button> </div> <div class="mod-body"> <h2>subscribe to our newsletter and get 15% off your first purchase</h2> <form method="POST"> Your content here! <button class="submit-btn" type="submit">go</button> </form> </div> </div></div>

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

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