简体   繁体   English

在响应式设计中为“顶部”和“底部”留出与“右侧”和“左侧”相同的边距

[英]Give same margin to 'top' and 'bottom' as margin of 'right' and 'left' in responsive design

I've got a container div which has got dynamic width, it changes depending on screen resolution. 我有一个具有动态宽度的容器div,它随屏幕分辨率而变化。 Inside such div I have another element with fixed height and width. 在这样的div内部,我还有另一个具有固定高度和宽度的元素。 I can give such element a margin: 0 auto; 我可以给这样的元素留一个margin: 0 auto; and align it horizontally in the middle, however this trick doesn't work to align it vertically in middle, as height of container div remains same (it's not fixed height, it depends on content inside the div). 并在中间水平对齐,但是此技巧对在中间垂直对齐不起作用,因为容器div的高度保持不变(它不是固定高度,取决于div内的内容)。 Therefore I'd like to somehow apply same margins as go to right and left, to top and bottom, when users change resolution. 因此,当用户更改分辨率时,我想以某种方式应用与左右移动,上下移动相同的边距。 Therefore there should be same dynamic margin on all sides? 因此,各方应有相同的动态裕度吗?

It would be good to have css based solution, but if that is not possible, jQuery is good as well. 拥有基于css的解决方案会很好,但是如果这不可能,那么jQuery也会很好。

Basically what I need is to calculate margins of either right or left side and apply those values to top and bottom margins. 基本上,我需要计算右侧或左侧的边距,并将这些值应用于顶部和底部边距。

I don't think you need a JavaScipt/jQuery solution here. 我认为您在这里不需要JavaScipt / jQuery解决方案。 You can do this with just CSS. 您可以仅使用CSS进行此操作。

Look into the vertical-align property. 查看vertical-align属性。 You will need to review its caveats and requirements, as it requires elements to be inline / inline-block . 您需要查看其注意事项和要求,因为它需要将元素设置为inline / inline-block

What you will want is something like: 您想要的是这样的:

#centered-element {
    display: inline-block;
    height: 300px; //your fixed height
    width: 250px; // your fixed width
    margin-left: auto;
    margin-right: auto;
    vertical-align: middle;
}

Take a look at this article . 看一下这篇文章 It gives a pretty good description of the vertical-align property, its use and its limitations. 它很好地描述了vertical-align属性,其用法和局限性。


UPDATE UPDATE

Based on your comments, if you want to (literally) apply the left or right margin to also be the top margin, you can do this using the following jQuery: 根据您的评论,如果您想(字面上)将左边界或右边界应用为最高边界,则可以使用以下jQuery做到这一点:

$(document).ready(function() {
    var $ele = $("#centered-element");
    var marginL = $ele.css("margin-left");
    $ele.css({
        "margin-top": marginL,
        "margin-bottom": marginL
    });
});

if you have fixed width and height of inside element, you may use this little trick 如果内部元素的宽度和高度固定,则可以使用此小技巧

#outside {
    position: relative;
}
#inside {
    position: absolute;
    top: 50%;
    left: 50%;
    height: 500px;
    width: 500px;
    margin-top: -250px;
    margin-left: -250px;
}

This gives a container div floating with a constant margin. 这使容器div以恒定的边距浮动。

<div>Content</div>

div {
    background: #282;
    width:90%;
    height:90%;
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    margin:auto;
}

http://jsfiddle.net/djwave28/tATZU/1/ http://jsfiddle.net/djwave28/tATZU/1/

In addition with reference to content, you can place this inside the container. 除参考内容外,您还可以将其放置在容器中。 Upon exceeding the height, a scroll function in CSS can be applied. 超过高度时,可以应用CSS中的滚动功能。 If not the scroll function of the window will take over. 否则,窗口的滚动功能将接管。

http://jsfiddle.net/djwave28/tATZU/2/ http://jsfiddle.net/djwave28/tATZU/2/

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

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