首页 > 其他分享 >css33 CSS Layout - Horizontal & Vertical Align

css33 CSS Layout - Horizontal & Vertical Align

时间:2024-06-01 13:56:39浏览次数:11  
标签:3px Layout center Vertical solid align padding border Align

https://www.w3schools.com/css/css_align.asp

 

 

Center Align Elements

To horizontally center a block element (like <div>), use margin: auto;

Setting the width of the element will prevent it from stretching out to the edges of its container.

The element will then take up the specified width, and the remaining space will be split equally between the two margins:

 

Example

.center {
  margin: auto;
  width: 50%;
  border: 3px solid green;
  padding: 10px;
}
<!DOCTYPE html>
<html>
<head>
<style>
.center {
  margin: auto;
  width: 60%;
  border: 3px solid #73AD21;
  padding: 10px;
}
</style>
</head>
<body>

<h2>Center Align Elements</h2>
<p>To horizontally center a block element (like div), use margin: auto;</p>

<div class="center">
  <p>Hello World!</p>
</div>

</body>
</html>

 

Note: Center aligning has no effect if the width property is not set (or set to 100%).


Center Align Text

To just center the text inside an element, use text-align: center;

 

Example

.center {
  text-align: center;
  border: 3px solid green;
}
<!DOCTYPE html>
<html>
<head>
<style>
.center {
  text-align: center;
  border: 3px solid green;
}
</style>
</head>
<body>

<h2>Center Text</h2>

<div class="center">
  <p>This text is centered.</p>
</div>

</body>
</html>

 

Tip: For more examples on how to align text, see the CSS Text chapter.


   

Center an Image

To center an image, set left and right margin to auto and make it into a block element:

 

 

Example

img {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 40%;
}
<!DOCTYPE html>
<html>
<head>
<style>
img {
  display: block;
  margin-left: auto;
  margin-right: auto;
}
</style>
</head>
<body>

<h2>Center an Image</h2>
<p>To center an image, set left and right margin to auto, and make it into a block element.</p>

<img src="paris.jpg" alt="Paris" style="width:40%">

</body>
</html>

 


Left and Right Align - Using position

One method for aligning elements is to use position: absolute;:

 

Example

.right {
  position: absolute;
  right: 0px;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}
<!DOCTYPE html>
<html>
<head>
<style>
.right {
  position: absolute;
  right: 0px;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}
</style>
</head>
<body>

<h2>Right align with the position property</h2>

<p>An example of how to right align elements with the position property:</p>

<div class="right">
  <p>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.</p>
</div>

</body>
</html>

 

Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.


Left and Right Align - Using float

Another method for aligning elements is to use the float property:

Example

.right {
  float: right;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}
<!DOCTYPE html>
<html>
<head>
<style>
.right {
  float: right;
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}
</style>
</head>
<body>

<h2>Right align with the float property</h2>

<p>An example of how to right align elements with the float property:</p>

<div class="right">
  <p>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.</p>
</div>

</body>
</html>

 


The clearfix Hack

Note: If an element is taller than the element containing it, and it is floated, it will overflow outside of its container. You can use the "clearfix hack" to fix this (see example below).

 

<!DOCTYPE html>
<html>
<head>
<style>
div {
  border: 3px solid #4CAF50;
  padding: 5px;
}

.img1 {
  float: right;
}

.img2 {
  float: right;
}

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}
</style>
</head>
<body>

<h2>Without Clearfix</h2>

<p>This image is floated to the right. It is also taller than the element containing it, so it overflows outside of its container:</p>

<div>
  <img class="img1" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet...
</div>

<h2 style="clear:right">With New Modern Clearfix</h2>
<p>Add the clearfix hack to the containing element, to fix this problem:</p>

<div class="clearfix">
  <img class="img2" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet...
</div>

</body>
</html>

 

 

Then we can add the clearfix hack to the containing element to fix this problem:

Example

.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

Center Vertically - Using padding

There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding:

 

Example

.center {
  padding: 70px 0;
  border: 3px solid green;
}
<!DOCTYPE html>
<html>
<head>
<style>
.center {
  padding: 70px 0;
  border: 3px solid green;
}
</style>
</head>
<body>

<h2>Center vertically with padding</h2>

<p>In this example, we use the padding property to center the div element vertically:</p>

<div class="center">
  <p>I am vertically centered.</p>
</div>

</body>
</html>

 

To center both vertically and horizontally, use padding and text-align: center:

 

Example

.center {
  padding: 70px 0;
  border: 3px solid green;
  text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<style>
.center {
  padding: 70px 0;
  border: 3px solid green;
  text-align: center;
}
</style>
</head>
<body>

<h2>Center with padding and text-align</h2>

<p>In this example, we use padding and text-align to center the div element both vertically and horizontally:</p>

<div class="center">
  <p>I am vertically and horizontally centered.</p>
</div>

</body>
</html>

 


Center Vertically - Using line-height

Another trick is to use the line-height property with a value that is equal to the height property:

 

Example

.center {
  line-height: 200px;
  height: 200px;
  border: 3px solid green;
  text-align: center;
}

/* If the text has multiple lines, add the following: */
.center p {
  line-height: 1.5;
  display: inline-block;
  vertical-align: middle;
}
<!DOCTYPE html>
<html>
<head>
<style>
.center {
  line-height: 200px;
  height: 200px;
  border: 3px solid green;
  text-align: center;
}

.center p {
  line-height: 1.5;
  display: inline-block;
  vertical-align: middle;
}
</style>
</head>
<body>

<h2>Center with line-height</h2>

<p>In this example, we use the line-height property with a value that is equal to the height property to center the div element:</p>

<div class="center">
  <p>I am vertically and horizontally centered.</p>
</div>

</body>
</html>

 


Center Vertically - Using position & transform

If padding and line-height are not options, another solution is to use positioning and the transform property:

 

Example

.center {
  height: 200px;
  position: relative;
  border: 3px solid green;
}

.center p {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<!DOCTYPE html>
<html>
<head>
<style>
.center { 
  height: 200px;
  position: relative;
  border: 3px solid green; 
}

.center p {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
</style>
</head>
<body>

<h2>Center with position and transform</h2>

<p>In this example, we use positioning and the transform property to vertically and horizontally center the div element:</p>

<div class="center">
  <p>I am vertically and horizontally centered.</p>
</div>

</body>
</html>

 

Tip: You will learn more about the transform property in our 2D Transforms Chapter.


Center Vertically - Using Flexbox

You can also use flexbox to center things. Just note that flexbox is not supported in IE10 and earlier versions:

 

Example

.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  border: 3px solid green;
}
<!DOCTYPE html>
<html>
<head>
<style>
.center {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  border: 3px solid green; 
}
</style>
</head>
<body>

<h2>Flexbox Centering</h2>

<p>A container with both the justify-content and the align-items properties set to <em>center</em> will align the item(s) in the center (in both axis).</p>

<div class="center">
  <p>I am vertically and horizontally centered.</p>
</div>

</body>
</html>

 

Tip: You will learn more about Flexbox in our CSS Flexbox Chapter.

 

标签:3px,Layout,center,Vertical,solid,align,padding,border,Align
From: https://www.cnblogs.com/emanlee/p/18225924

相关文章

  • css32 CSS Layout - display: inline-block
    https://www.w3schools.com/css/css_inline-block.aspThedisplay:inline-blockValueComparedtodisplay:inline,themajordifferenceisthatdisplay:inline-blockallowstosetawidthandheightontheelement.Also,withdisplay:inline-block,thetop......
  • css31 CSS Layout - float and clear
    https://www.w3schools.com/css/css_float.asp CSSLayout-floatandclear  TheCSSfloatpropertyspecifieshowanelementshouldfloat.TheCSSclearpropertyspecifieswhatelementscanfloatbesidetheclearedelementandonwhichside. Theflo......
  • css29 CSS Layout - The z-index Property
    https://www.w3schools.com/css/css_z-index.asp CSSLayout-Thez-indexProperty  Thez-indexpropertyspecifiesthestackorderofanelement.Thez-indexPropertyWhenelementsarepositioned,theycanoverlapotherelements.Thez-indexproperty......
  • css30 CSS Layout - Overflow
    https://www.w3schools.com/css/css_overflow.aspCSSLayout-Overflow  TheCSSoverflowpropertycontrolswhathappenstocontentthatistoobigtofitintoanarea. <!DOCTYPEhtml><html><head><style>#overflowTest{b......
  • css28 CSS Layout - The position Property
    https://www.w3schools.com/css/css_positioning.aspCSSLayout-ThepositionPropertyThepositionpropertyspecifiesthetypeofpositioningmethodusedforanelement(static,relative,fixed,absoluteorsticky).ThepositionPropertyThepositionpr......
  • css26 CSS Layout - The display Property
    https://www.w3schools.com/css/css_display_visibility.asp  CSSLayout-ThedisplayPropertyThedisplaypropertyisthemostimportantCSSpropertyforcontrollinglayout.ThedisplayPropertyThedisplaypropertyisusedtospecifyhowanelementi......
  • css27 CSS Layout - width and max-width
    https://www.w3schools.com/css/css_max-width.asp CSSLayout-widthandmax-width  Usingwidth,max-widthandmargin:auto;Asmentionedinthepreviouschapter;ablock-levelelementalwaystakesupthefullwidthavailable(stretchesouttothelef......
  • [论文阅读] Aligner@ Achieving Efficient Alignment through Weak-to-Strong Correct
    Pretitle:Aligner:AchievingEfficientAlignmentthroughWeak-to-StrongCorrectionsource:Arxiv2024paper:https://arxiv.org/abs/2402.02416code:https://aligner2024.github.io/ref:https://mp.weixin.qq.com/s/O9PP4Oc_Ee3R_HxKyd31Qg关键词:LLM,align,fin......
  • Golang初学:项目目录结构,project-layout 项目
    goversiongo1.22.1windows/amd64Windows11+amd64x86_64x86_64GNU/Linux--- 序章golang项目的代码要怎么组织?怎么放比较简洁易读?看下面这个项目就晓得了。 project-layouthttps://github.com/golang-standards/project-layout注,有时访问失败。特写文记录。......
  • GridLayout 等控件来完成多行按钮操作
     第一步,在布局文件中添加一个GridLayout控件,设置它的行列数和间距等属性,例如:<GridLayoutandroid:id="@+id/grid_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:columnCount="4"andr......