div在任何浏览器(跨浏览器)中居中和居中的位置

时间:2018-10-13 02:17:26

标签: html css angular flexbox angular6

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

10 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

可以使用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>

答案 2 :(得分:1)

据我所知,您的问题是容器private static String getXSIType(String fieldType) { String result=new String(); switch (fieldType) { case "_checkBox": result="platformCore:BooleanCustomFieldRef"; break; case "_integerNumber": result="platformCore:LongCustomFieldRef"; break; case "_multipleSelect": result="platformCore:MultiSelectCustomFieldRef"; break; case "_currency": case "_decimalNumber": case "_percent": result="platformCore:DoubleCustomFieldRef"; break; case "_document": case "_listRecord": case "_image": result="platformCore:SelectCustomFieldRef"; break; case "_date": case "_datetime": case "_timeOfDay": result="platformCore:DateCustomFieldRef"; break; case "_freeFormText": case "_eMailAddress": case "_help": case "_hyperlink": case "_inlineText": case "_longText": case "_password": case "_phoneNumber": case "_richText": case "_textArea": result="platformCore:StringCustomFieldRef"; break; default: result="platformCore:StringCustomFieldRef"; break; } return result; } 应该始终居中,而不考虑div-Bdiv-A。我改变了两件事:

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

全屏查看示例,以查看.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;
}

答案 3 :(得分:1)

使用CSS网格,并在父元素上使用实用工具类为所有可能的情况定义ffmpeg属性。使path而不是createWriteStream居中。 下面的代码段应该可以使用,代码语法与Node.js版本略有不同。查看整页。

grid-column
main
div

可能的解决方案

https://stackblitz.com/edit/angular-4jdpca-根据需要更改颜色。

参考

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

答案 4 :(得分:1)

您可以使用minmax() CSS函数来固定宽度,并隐藏div以使中心div保持在页面中心,请使用css的visibility : hidden属性将其从页面中隐藏,但会保持它在DOM中显示,因此如果侧面的两个div中的任何一个在点击时隐藏,则中心div不会受到影响

working code

<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;
 }

答案 5 :(得分: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>

答案 6 :(得分:0)

您是否听说过Bootstrap Framework。我在这里为您提供一个示例,说明我将如何使用此框架针对您的需求进行响应式(跨浏览器)布局。我不能严格匹配的唯一要求是下一个:

  

三个div面板: 1)(div-B),一个max-width = 500px和min-width = 400px的div应该始终位于所有浏览器的中心。 2)(div-A):一个分区应显示并隐藏在(div-B)的左侧,其最小宽度为200px,最大宽度为300px; 3)(div-C),一个分区应显示并隐藏在(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>

答案 7 :(得分:0)

您不能使用<div>定位将元素放置在relative中,因为这样,您正在设置UI来将每个元素排列在前一个元素的旁边。因此,我将最终使用absbolute定位(如果您不希望这些元素滚动,请使用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%))。注意,这实际上是可行的,因为百分比在trasform之外的任何属性中都是相对于父级的,相对于元素本身。

答案 8 :(得分: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>

答案 9 :(得分:-1)

使用flex很容易。

.parentDiv{
    display: flex;
}
.childDiv{
    margin: auto;
}
相关问题