简体   繁体   English

网站:如何将所有屏幕尺寸的div居中

[英]Web: How to center right div for all screen sizes

I want to fix my header div which has two components. 我想修复我的标头div,它包含两个部分。 An Image and the title of the page as shown in the figure: 图片和页面标题,如图所示:

The image shown below is on MDPI screen. 下图显示在MDPI屏幕上。

在此处输入图片说明

Here is my Code 这是我的代码

 #headerdiv { font-size: 4em; font-weight: bold; line-height: 60px; padding: 1% 0; margin: auto auto; } .headerdiv>#leftheaderdiv { min-height: 750px; text-align: center; } 
 <div class="headerdiv"> <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12" id="leftheaderdiv"> <img src="http://lorempixel.com/200/200/sports" id="mainlogo" /> </div> <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12" id="leftheaderdiv"> <p id="headerdivp">TXYZ RD MEDIA</p> </div> </div> 

It looks like this on a mobile screen, which is fine as per the bootstrap arrangement: 在移动屏幕上看起来像这样,按照引导程序的设置可以:

在此处输入图片说明

The problem I am facing is on HDPI screen where the header looks like this. 我面临的问题是在HDPI屏幕上,标题看起来像这样。 The text goes on the top. 文本在顶部。 :

在此处输入图片说明

How Can I make sure that the right div also occupies 100% height / fills the right part and the text is always in the middle no matter what screen size? 如何确定右div也占据100%的高度/填充右部分,并且无论屏幕大小如何,文本始终在中间?

Thank you! 谢谢!

Solution 1: 解决方案1:

You can use flex to align the text in vertically center. 您可以使用flex将文本垂直居中对齐。 Add the following CSS in your paragraph parent element like below. 在您的段落父元素中添加以下CSS,如下所示。

.headerdiv > #leftheaderdiv{
  min-height: 750px; 
  display: flex;
  align-items: center;
  text-align: center;
 }

Solution 2: 解决方案2:

If you don't want to use flex you can use the old method like make your parent as table and child element as table-cell . 如果您不想使用flex,则可以使用旧方法,例如将父级设为table ,将子级元素设为table-cell

 #headerdivp{
    font-size: 4em; 
    font-weight: bold;
    line-height: 60px; 
    padding: 1% 0; 
    display:table-cell;
    vertical-align:middle;
    margin: auto auto;
  }

 .headerdiv > #leftheaderdiv{
    min-height: 750px; 
    display:table;    
    text-align: center;
 }

Try to use flex .. 尝试使用flex ..

For vertical align center use align-items:center . 对于垂直对齐中心,请使用align-items:center

And for mobile stacked view use flex-direction:column using media query. 对于移动堆叠视图,请使用flex-direction:column使用媒体查询)。

Stack Snippet 堆栈片段

 #headerdivp { font-size: 4em; font-weight: bold; line-height: 60px; } .headerdiv>#leftheaderdiv { text-align: center; } .headerdiv { display: flex; align-items: center; } @media (max-width:768px) { .headerdiv { flex-direction: column; } } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <div class="container"> <div class="headerdiv row"> <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12" id="leftheaderdiv"> <img src="http://lorempixel.com/200/200/sports" id="mainlogo" /> </div> <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12" id="leftheaderdiv"> <p id="headerdivp">TXYZ RD MEDIA</p> </div> </div> </div> 

Reference Link 参考链接

You are missing row class. 您缺少row类。 Also add class align-items-center . 还添加align-items-center Also also have id leftheaderdiv twice. 也有两次ID leftheaderdiv Make it unique. 使其独特。

<div class = "row headerdiv align-items-center">               
            <div class = "col-lg-6 col-md-12 col-sm-12 col-xs-12" id = "leftheaderdiv">
                <img src="https://s3-us-west-1.amazonaws.com/xesdcf/xdclogo3d2.svg" id = "mainlogo"/>
            </div>
            <div class = "col-lg-6 col-md-12 col-sm-12 col-xs-12" id = "leftheaderdiv">
                <p id = "headerdivp">TXYZ RD MEDIA</p>              
            </div>
</div>

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

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