简体   繁体   English

CSS: <div> 具有弯曲的边缘和居中的文本(水平和垂直居中)

[英]CSS: <div> with curved edges and centered text (horizontal & vertical centered)

I want to achieve something like shown below. 我想要实现如下所示的功能。

In the code, i am using fixed height and margin. 在代码中,我使用固定的高度和边距。

How do i achieve the same without the fixed height and margin? 没有固定的高度和边距怎么办? How do i center the text vertically? 如何将文字垂直居中?

Note: the white border between the div's is for illustration only. 注意:div之间的白色边框仅用于说明。

在此处输入图片说明

Code: 码:

 <style> .div{ width:160px; height:50px; background-color: gray; margin-top: 10px; margin-bottom: 10px; display:inline-block; text-align: center; font-size: 30px; } .bg-gray-left{ border-top-left-radius: 25px; border-bottom-left-radius: 25px; } .bg-gray-middle{ border-radius:0px; } .bg-gray-right{ border-top-right-radius: 25px; border-bottom-right-radius: 25px; } .container{ border:solid 1px gray; width: 485px; } .div:nth-child(1),.div:nth-child(2){ border-right-color: white; border-right-width: 1px; border-right-style: solid } </style> <!-- <div class="container"> --> <div class="div bg-gray-left">div 1</div> <div class="div bg-gray-middle">div 2</div> <div class="div bg-gray-right">div 3</div> <!-- </div> --> 

You don't need height to fix it centrally. 您不需要高度即可将其固定在中央。 Just add padding and remove the margin and it should create the effect you need. 只需添加填充并删除边距,即可创建所需的效果。

.div{
    width:160px;
    background-color: gray;
    display:inline-block;
    text-align: center;
    font-size: 30px;
    padding: 10px;
  }

https://jsfiddle.net/dbnrvxkq/ https://jsfiddle.net/dbnrvxkq/

Try to use padding in this instance. 尝试在这种情况下使用填充。

 <style> .div{ width:160px; padding: 5px 0; background-color: gray; display:inline-block; text-align: center; font-size: 30px; } .bg-gray-left{ border-top-left-radius: 25px; border-bottom-left-radius: 25px; } .bg-gray-middle{ border-radius:0px; } .bg-gray-right{ border-top-right-radius: 25px; border-bottom-right-radius: 25px; } .container{ border:solid 1px gray; width: 485px; } .div:nth-child(1),.div:nth-child(2){ border-right-color: white; border-right-width: 1px; border-right-style: solid } </style> <!-- <div class="container"> --> <div class="div bg-gray-left">div 1</div> <div class="div bg-gray-middle">div 2</div> <div class="div bg-gray-right">div 3</div> <!-- </div> --> 

Padding is lame. 馅很la脚。 It's only ever a matter of time before the size of something changes, or something takes multiple lines, or anything at all goes wrong, and then padding will no longer do what you want. 更改大小,更改多行内容或根本不做任何事情只是时间问题,然后填充将不再能满足您的要求。 Using flex center is much more reliable: 使用flex center更可靠:

 div { height: 100px; width: 200px; display: flex; justify-content: center; align-items: center; background: aliceblue; } 
 <div> Center anything </div> 

This will require you to either wrap all your items in a container, or work along a single axis (flex-direction: row or column), but it's really the most simple way to keep everything totally centered. 这将需要您将所有项目包装在一个容器中,或者沿单个轴(弹性方向:行或列)工作,但这实际上是使所有内容完全居中的最简单方法。

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

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