CSS(位置:绝对+左侧:50%=最大宽度:50%)?

编程入门 行业动态 更新时间:2024-10-28 15:22:16
本文介绍了CSS(位置:绝对+左侧:50%=最大宽度:50%)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在一个临时性问题的网站上工作,我有一个 div 和 CSS ,如下所示:

I'm working on a website where I'm having a temporary problem, I've a div with CSS like this:

.box{ position: absolute; width: auto; max-width: 75%; left: 50%; transform: translate(-50%,0); -ms-transform: translate(-50%,0); -webkit-transform: translate(-50%,0); background: #000; color: #fff; }

您可以在此处看到一个简单的 JSFiddle > box1 正常工作,它的文本短且 width:auto; 正常工作...

You can see a simple JSFiddle here, where the box1 is working correctly, it has short text and the width: auto; is working perfectly ...

但是第二个框 box2 具有较长的文本,并且 max-width:75%; ,但是它无法正常工作,您会注意到它的宽度看起来像 50%

But the second box box2 has long text and max-width: 75%;, but it's not working correctly, you can notice that its width looks like 50%

我的问题是 box2 从哪里获得 50%宽度的?我该如何解决该问题?

My question is from where did the box2 get 50% width ?? And how can I fix that issue ?

预先感谢

推荐答案

您可以使用一个额外的标签(例如,一个 span )来实现所需的布局

You may achieve the desired layout using an extra tag (a span for example)

演示

DEMO

HTML:

<div class="box box1"><span>box1 Lara had been back and</span></div> <div class="box box2"> <span>box2 Lara had been back and forth along the river path many times in her short life. Her people had not created the path, it had always been there, like the river, but their deerskin-shod feet and the wooden wheels of their handcarts kept the path well worn. Lara’s people were salt traders, and their livelihood took them on a continual journey</span> </div>

CSS:

.box{ position: absolute; width: 100%; text-align:center; } .box1{ top: 20px; } .box2{ top: 100px; } .box span{ display:inline-block; max-width:75%; background: #000; color: #fff; text-align:left; }

更多推荐

CSS(位置:绝对+左侧:50%=最大宽度:50%)?

本文发布于:2023-11-29 18:06:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   位置   CSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!