有没有办法创建一个具有最大宽度限制的自扩展图层,其中包含左对齐文本

发布于 2024-12-22 00:15:25 字数 1916 浏览 1 评论 0原文

JSFIDDLE

HTML:

<div class="SummaryTable">

    <div class="AddressContainer">

        <div class="Address">
          <b>Ship To: </b>
            <ul>
                <li>John Smith</li>
                <li>45 Holland drv.</li>
            </ul>
        </div>

            <div class="Address">
             <b>Bill To: </b>
                <ul>
                    <li>John Smith</li>
                    <li>P.O Box 456</li>
                </ul>
            </div>
    </div>
</div>

CSS:

.SummaryTable
{
width: 650px;   
text-align:center;
}


.SummaryTable .AddressContainer
{
width:650px;
}

.SummaryTable .Address
{
width:323px;
background-color:Yellow;
float:left;
border: 1px solid black;
height:200px;
}

.Address ul
{
width:130px;
min-width:130px;
max-width: 280px;
margin-left:auto;
margin-right:auto;
background-color:Green;
text-align:left;
padding:0px;
}

.Address li
{
list-style-type: none;
}

JSFIDDLE

  • 我想将绿色框\元素(ul)保持在323px宽/200px或自动高(.Address)层的水平和中心位置垂直。
  • 我通过设置 fixed width + margin:0px auto 实现了
      的水平居中,但地址的长度各不相同,我不只想要一些固定大小的框其中一个较长的地址将显示在(收货地址\收单地址)标题下方的中心,而另一个较短的地址将看起来不合适,因为文本左对齐。
  • 我想看到

  • 中的文本拉伸到
      绿色框,直到最大宽度设置(例如 280px),并且绿色框,无论其大小如何,都将在 .Address 层中保持水平和垂直居中,并因此位于 Ship To\Bill To 标题下。

  • 这样做的主要原因是,较短的地址看起来会在固定宽度 ul 元素中向左偏移,如果我使宽度小于换行符,则会出现较长的地址,从而扩展第一个以及姓氏和/或地址,共两行。在 ul 元素中使用居中对齐的文本 -当然解决了这个问题,但我需要左对齐文本。

JSFIDDLE

HTML:

<div class="SummaryTable">

    <div class="AddressContainer">

        <div class="Address">
          <b>Ship To: </b>
            <ul>
                <li>John Smith</li>
                <li>45 Holland drv.</li>
            </ul>
        </div>

            <div class="Address">
             <b>Bill To: </b>
                <ul>
                    <li>John Smith</li>
                    <li>P.O Box 456</li>
                </ul>
            </div>
    </div>
</div>

CSS:

.SummaryTable
{
width: 650px;   
text-align:center;
}


.SummaryTable .AddressContainer
{
width:650px;
}

.SummaryTable .Address
{
width:323px;
background-color:Yellow;
float:left;
border: 1px solid black;
height:200px;
}

.Address ul
{
width:130px;
min-width:130px;
max-width: 280px;
margin-left:auto;
margin-right:auto;
background-color:Green;
text-align:left;
padding:0px;
}

.Address li
{
list-style-type: none;
}

JSFIDDLE

  • I want to keep the green box\element (ul) centred in the 323px wide/200px or Auto high (.Address) layer both horizontally and vertically.
  • I have achieved horizontal centering of <ul> by setting fixed width + margin:0px auto, but addresses vary in length and I don't just want some fixed size box where one longer address would appear centred under (Ship To\Bill To) heading and another shorter one will look out of place since text is aligned to the left.
  • I want to see the text in the <li> stretch to the <ul> green box up to the maximum-width setting (e.g 280px) and the green box, whatever its size will be to remain centered horizontally and vertically in the .Address layer and consequently under the Ship To\Bill To headings.

  • Main reason for this is that shorter addresses would look offset to the left in a fixed width ul element and if I make the width small than a line break would ensue for longer address, spreading the first and last name and or address over two lines. Using centrally aligned text in the ul element
    -Of course solves this problem, but I need left aligned text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2024-12-29 00:15:25

DEMO jsBin

如果我很好地理解你的问题,

这里是修改后的 CSS:

.SummaryTable{
  width: 650px;   
  text-align:center;
}    
.AddressContainer{
  width:650px;
}   
.Address{
  width:323px;
  background-color:white;
  float:left;
  border: 1px solid black;
  height:200px;
}
.address b{
    display:block;           /*should do the trick*/
}
.Address ul{
  display:inline-block;     /*should do the trick*/
  background-color:orange; 
  text-align:left;
  padding:0px; 
}
.Address li{
    list-style-type: none;
}

我删除了一些 margin-reft/right:auto、widths、min-width、max width 并格式化了您的 .summaryTable css“外观”。稍微玩一下 display 属性就可以了。

这是使用 jQuery 的演示: 使用 jQ 演示 jsBin

DEMO jsBin

If I understood well your question

Here is the modified CSS:

.SummaryTable{
  width: 650px;   
  text-align:center;
}    
.AddressContainer{
  width:650px;
}   
.Address{
  width:323px;
  background-color:white;
  float:left;
  border: 1px solid black;
  height:200px;
}
.address b{
    display:block;           /*should do the trick*/
}
.Address ul{
  display:inline-block;     /*should do the trick*/
  background-color:orange; 
  text-align:left;
  padding:0px; 
}
.Address li{
    list-style-type: none;
}

I removed some margin-reft/right:auto, widths, min-width, max width and formatted a bit your .summaryTable css 'appearances'. Than a bit of playing with display property have done the trick.

Here is a demo using jQuery : demo jsBin with jQ

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文