在任何浏览器中(跨浏览器),将div居中和靠左靠右定位

11
Three div panels:-
1) (div-B) one div with max-width = 500px and min-width=400px should always on 
    the center of all browsers.
2) (div-A) one div should show and hide on the left of (div-B) having min-width 
   = 200px and max-width= 300px;
3) (div-C) one div should show and hide on the right of (div-B) having min-width 
   = 200px and max-width= 300px;
div-A及div-C的显示和隐藏应该以这样的方式进行,不会使div-B从其位置移动,但可以使其达到可用的最大宽度和最小宽度。div-B应始终位于所有浏览器的中心。

页面布局:

Header
<--Left button here                                       right button here-->
to open div-A panel                                       to open div-c panel
(basically a dropdown)                                  (basically a dropdown)


         Left Panel <---20px---> Center Panel <---20px---> Right Panel   
         (div-A)                  (div-B)                     (div-C)    

1) (div-A && div-B), (div-B && div-C) having margin 20px in between always.
   div-A should grow towards left and div-c should grow towards right relative to div-B(min-width and 
   max-width availability).
2) div-B should not move to right or left when div-A and div-C should hide. 
 But div-B should grow towards its left or right keeping div-B fixed to 
  center of browser window and tries to achieve its max-width(keeping center 
 axis fixed) if space available towards right or left.

<header>
//show hide button for div-A
<div class="div-A-button">Dropdown button A</div>
//show hide button for div-C
<div class="div-C-button">Dropdown button C</div>
</header>

<main>
<div class="panel div-A">Panel A </div>  //show and hide using div-A-button
<div class="panel div-B">Panel B </div>  //fixed always show
<div class="panel div-C">Panel C </div>  //show and hide using div-C-button
</main>

这里是 StackBlitz 链接:https://stackblitz.com/edit/angular-tpj35u?file=src%2Fapp%2Fapp.component.html


1
你所说的“所有浏览器”是什么意思?解决你的问题有许多不同的可能方法,但如果没有知道你想要支持哪个浏览器的信息,就无法给出一个好的答案。 :) - Gerrit Halfmann
它的意思是跨浏览器/任何浏览器。 - Mahi
10个回答

5
你可以使用CSS网格布局。我认为我已经理解了你的布局。如果下面的示例正确,请告诉我。

main {
  border: 1px solid;
  display: grid;
  grid-template-columns: minmax(200px, 300px) minmax(400px, 500px) minmax(200px, 300px);
  grid-gap: 20px;
  justify-content: center;
}

main>div {
  background: yellow;
  text-align: center;
  padding: 1em;
}

.div-B {
  grid-column: 2;
}
<main>
  <div class="panel div-A">Panel A </div>
  <div class="panel div-B">Panel B </div>
  <div class="panel div-C">Panel C </div>
</main>


3
这可以通过使用flex来实现:
<!DOCTYPE html>
<html>
<head>
  <style>
      .container {
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          flex: 1;
          overflow: hidden;
      }

      .classA {
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          min-width: 200px;
          max-width: 300px;
          height: 200px;
          width: 300px;
          background-color: red;
      }
      .classB {
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          flex: 1;
          min-width: 400px;
          max-width: 500px;
          width: 500px;
          height: 200px;
          margin-left: 20px;
          margin-right: 20px;
          background-color: yellow;
      } 
      .classC {
          display: flex;
          flex-direction: row;
          justify-content: center;
          align-items: center;
          min-width: 200px;
          max-width: 300px;
          height: 200px;
          width: 300px;
          background-color: blue;
      } 
      </style>
</head>
<body>
  <div class="container">
    <div class="classA">A</div>
    <div class="classB">B</div>
    <div class="classC">C</div>
  </div>
</body>
</html>

1
你可以使用 minmax() CSS 函数来固定宽度,如果要隐藏一个 div 以使中心 div 保持在页面中心,则可以使用 css 的 visibility: hidden 属性。这将仅从页面中隐藏它,但会将其保留在 DOM 中,因此如果两侧的任一 div 在单击时被隐藏,则中心 div 不会受到影响。

工作代码

<div id="container">
    <div (click)="visibility = !visibility" [ngClass]="{'hide' : visibility}">
     <select style="width :-webkit-fill-available;"></select>
      at most 300 pixels
    </div>
    <div>20px</div> //separator
    <div>
    <div style="position:absolute">
      Item with 400 to max 500px width
    </div>
  </div>
    <div>20px</div>    //separator
    <div  (click)="visibility1 = !visibility1" [ngClass]="{'hide' : visibility}">
        at most 300 pixels.
    </div>
  </div>

css

#container {
  display: grid;
  grid-template-columns: minmax(200px, 300px) 20px minmax(400px, 500px) 20px minmax(200px, 300px);
  box-sizing: border-box;
  height: 200px;
  background-color: #8cffa0;
  width: 100%;
}

#container > div {
  background-color: #8ca0ff;
  border: 1px dotted red;
 }
 .hide {
   visibility: hidden;
 }

1

好的,就我理解的问题而言,容器div-B应始终居中,不受div-Adiv-B的影响。我做了两件重要的事情:

  1. 我更新了样式为flexbox,它在目前拥有非常好的浏览器支持。
  2. 将隐藏类从display:block更改为visibility:hidden,因此仍会将自由空间阻止到div-B
  3. 为了更好地理解视觉效果,我向.panel容器添加了红色边框

全屏查看示例,以查看.div-B的灵活性

$(".pull-left").click(function() {
  $(".div-A").toggleClass("hide")
});

$(".pull-right").click(function() {
  $(".div-C").toggleClass("hide")
});
p {
  font-family: Lato;
}

.show {
  display: block;
}

.hide {
  visibility: hidden;
}

header {
  height: 40px;
}

div.pull-left {
  float: left;
  width: 100px;
  display: block;
  color: red;
  text-align: center;
  border: 1px solid #ccc;
}

div.pull-right {
  float: right;
  width: 100px;
  text-align: center;
  border: 1px solid #ccc;
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.row .panel {
  border: 1px solid red;
  flex: 0 0 200px;
}

.row .panel.div-B {
  flex: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- <hello name="{{ name }}"></hello> -->
<header>
  <div class="pull-left" (click)='showHide("A")'>button-div-A</div>
  <div class="pull-right" (click)='showHide("C")'>button-div-C</div>
</header>

<main>
  <div class="row">
    <div class="panel div-A">Panel A </div>
    <div class="panel div-B">Panel B</div>
    <div class="panel div-C">Panel C </div>
  </div>
</main>


1
使用CSS Grid并定义grid-column属性到所有可能的情况,在父元素上使用实用类。居中main,而不是div。 下面的代码片段应该可以工作,代码语法与Node.js版本略有不同。查看完整页面。

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.showA = false;
    $scope.showB = false;
    $scope.showHide = function(val) {
      if(val === 'A'){
        this.showA = !this.showA;
      }
      else if(val === 'C'){
        this.showC = !this.showC;
      }
    };
});
p {
  font-family: Lato;
}
div.panel.show{
  display: block;
}

div.panel.hide{
    display: none;
}
header{
  height:40px;
}
div.pull-left{
  float:left;
  width:100px;
    display: block;
    color: red;
  text-align: center;
  border:1px solid #ccc;
}

div.pull-right{
  float:right;
  width:100px;
  text-align: center;
  border:1px solid #ccc;
}
main{
  display: table;
  margin: auto;
}
div.row{
  width:100%;
  float: left;
  display: grid;
  grid-template-columns: minmax(200px, 300px) minmax(400px, 500px) minmax(200px, 300px);
  grid-gap: 20px;
}
.div-A {
  display: none;
  grid-column: 1 / span 1;
  background-color: blue;
}
.div-B {
  display: block;
  grid-column: 1 / span 3;
  background-color: red;
}
.div-C {
  display: none;
  grid-column: 3 / span 1;
  background-color: yellow;
}
.showA .div-A {
  display: block;
}
.showA .div-B {
  grid-column: 2 / span 2;
}
.showC .div-B {
  grid-column: 1 / span 2;
}
.showA.showC .div-B {
  grid-column: 2 / span 1;
}
.div-C {
  display: none;
}
.showC .div-C {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!-- <hello name="{{ name }}"></hello> -->
<div ng-app="myApp" ng-controller="myCtrl">
<header>
<div class="pull-left" ng-click="showHide('A')">button-div-A</div>
<div class="pull-right" ng-click="showHide('C')">button-div-C</div>
</header>

<main>
<div class="row" ng-class="{'showA': showA, 'showC': showC }">
  <div class="panel div-A">Panel A </div>  
  <div class="panel div-B">Panel B </div>  
  <div class="panel div-C">Panel C </div>
</div>
</main>
</div>

可能的解决方案

https://stackblitz.com/edit/angular-4jdpca - 您可以根据自己的需求更改颜色。

参考资料

https://css-tricks.com/snippets/css/complete-guide-grid/


0

使用相对定位无法将元素居中于 <div> 中,因为这样会导致 UI 排列每个元素排在前一个元素的旁边。因此,我将使用 absolute 定位(如果您不希望这些元素滚动,请改用fixed)。

  1. 将元素左对齐:

    #element {
        position: absolute;
        left: 0;
    }
    
  2. 将元素居中对齐:

    #element {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
    }
    
  3. 将元素右对齐:

    #element {
        position: absolute;
        right: 0;
    }
    
为了使元素居中,我想出了一个奇怪但有效的解决方案:我将元素的 left 设置为父元素的中心(通过设置 left: 50%),然后水平移动元素的一半宽度(transform: translateX(-50%))。请注意,这实际上是有效的,因为百分比在任何属性中都是相对于父元素的,但在 transform 属性中是相对于元素本身的。

0
这里有很多关于如何自己编写CSS的好答案,但是复杂的框架样式表很难维护。考虑使用一个框架。
对于你的用例,Skeleton 将是我的首选。
<link rel="stylesheet" href="http://getskeleton.com/dist/css/normalize.css">
<link rel="stylesheet" href="http://getskeleton.com/dist/css/skeleton.css">
<!-- .u-pull-* will float content left/right -->
<button class="u-pull-left">Button A</button>
<button class="u-pull-right">Button C</button>
<!-- .container is a centered responsive wrapper -->
<div class="container">
  <div class="row">
    <div class="four columns">Panel A</div>
    <div class="four columns">Panel B</div>
    <div class="four columns">Panel C</div>
  </div>
</div>

0

用flex很容易。

.parentDiv{
    display: flex;
}
.childDiv{
    margin: auto;
}

0

我已经为您的请求制作了一个简单的方法,请查看下面的代码片段。希望它符合您的要求。

var divchecker = 0;

$('.pull-left').click(function() {
    //you are hiding the Div A now
    divchecker = divchecker+1;
    $('.div-A').hide();
    $('.pull-left2').show();
    $('.pull-left').hide();
    $('.gridsection').css('grid-template-columns', '70% auto');
    if (divchecker == 2){
      $('.gridsection').css('grid-template-columns', '100%');
    }
});
$('.pull-right').click(function() {
    //you are hiding the Div C now
    divchecker = divchecker+1;
    $('.div-C').hide();
    $('.pull-right2').show();
    $('.pull-right').hide();
    $('.gridsection').css('grid-template-columns', 'auto 70%');
    if (divchecker == 2){
      $('.gridsection').css('grid-template-columns', '100%');
    }
});

$('.pull-left2').click(function() {
    //you are showing the Div A now
    divchecker = divchecker-1;
    $('.div-A').show();
    $('.pull-left2').hide();
    $('.pull-left').show();
    $('.gridsection').css('grid-template-columns', 'auto 60% auto');
    if (divchecker == 2){
      $('.gridsection').css('grid-template-columns', '100%');
    }
});
$('.pull-right2').click(function() {
    //you are showing the Div C now
    divchecker = divchecker-1;
    $('.div-C').show();
    $('.pull-right2').hide();
    $('.pull-right').show();
    $('.gridsection').css('grid-template-columns', 'auto 60% auto');
    if (divchecker == 2){
      $('.gridsection').css('grid-template-columns', '100%');
    }
});
main {
  border: 1px solid;
  display: grid;
  grid-template-columns: auto 60% auto;
  grid-gap: 20px;
}

main>div {
  background: #eee;
  text-align: center;
  padding: 1em;
}

header{
  height:40px;
}
div.pull-left,div.pull-left2{
  float:left;
  width:100px;
    display: block;
    color: red;
  text-align: center;
  border:1px solid #ccc;
  background-color:yellow;
  cursor: pointer;
  font-weight:bold;
  border-radius: 5px;
}

div.pull-right,div.pull-right2{
  float:right;
  width:100px;
  text-align: center;
  border:1px solid #ccc;
  background-color:yellow;
  font-weight:bold;
  border-radius: 5px;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<header>
  <div class="pull-left">Hide-div-A</div>
  <div class="pull-left2" style="display:none;">Show-div-A</div>
  <div class="pull-right">Hide-div-C</div>
  <div class="pull-right2" style="display:none;">Show-div-C</div>
</header>

<main class="gridsection">
  <div class="panel div-A">Panel A</div>
  <div class="panel div-B">Panel B</div>
  <div class="panel div-C">Panel C</div>
</main>


谢谢。这不是我要找的。 - Mahi

0

你听说过Bootstrap Framework吗?我给你举个例子,用这个框架来实现一个响应式(跨浏览器)布局,以满足你的需求。唯一无法严格匹配的要求是:

三个div面板:1) (div-B) 一个div,最大宽度为500px,最小宽度为400px,始终位于所有浏览器的中心位置。2) (div-A) 一个div应该在(div-B)的左侧显示和隐藏,最小宽度为200px,最大宽度为300px;3) (div-C) 一个div应该在(div-B)的右侧显示和隐藏,最小宽度为200px,最大宽度为300px。

Bootstrap的列网格系统(将屏幕宽度分成12列)自动管理列的宽度,使网站能够响应各种类型的屏幕,因此我不知道是否可能匹配您的最大/最小像素宽度值,但您可以为您拥有的三个元素分配总列数的百分比,以实现类似的方法。其余都很好。你可以查看下面的例子。希望这能帮到你。

$(document).ready(function()
{
    $("#btnToggleA").click(function()
    {
        $(".div-A").slideToggle();
    });
    
    $("#btnToggleC").click(function()
    {
        $(".div-C").slideToggle();
    });
});
.margin-20px
{
    margin: 0px 20px;
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<!-- Wrapper for the Boostrap layout -->
<div class="container-fluid">

<header class="row">
  <div class="col-12 p-0">
    <button id="btnToggleA" class="float-left btn btn-sm btn-primary">Toogle Div-A</button>
    <button id="btnToggleC" class="float-right btn btn-sm btn-primary">Toogle Div-C</button>
  </div>
</header>

<!-- Class "mt-3" just adds some margin-top for good looking but can be removed -->
<!-- Classes "bg-*" adds background colors, not needed but helps to visualize elements -->

<main class="row mt-3">

  <div class="col-3 text-center p-0">
    <div class="div-A bg-info">Panel A</div>
  </div>

  <div class="col-6 text-center p-0">
    <div class="div-B bg-warning margin-20px">
      Panel B
    </div>
  </div>

  <div class="col-3 text-center p-0">
    <div class="div-C bg-info">
      Panel C
    </div>
  </div>

</main>

</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接