简体   繁体   English

如何使顶线短于底线(在div内)

[英]How to make top line shorter than bottom line (within div)

I have an unordered list which contains 3 list items (represented in my example as 3 green boxes). 我有一个无序列表,其中包含3个列表项(在我的示例中表示为3个绿色框)。 Each box has an image and 3 divs (title,location,price). 每个方框都有一个图像和3个div(标题,位置,价格)。 I'm only concerned with each box's title div. 我只关心每个盒子的标题div。

If the title is long enough so that it produces 2 lines, I want the top line to always be shorter than the bottom line. 如果标题足够长以便产生2行,我希望顶行总是短于底线。 My demo site seen here shows boxes 1 & 2 with the wrong setup, and box #3 shows the correct setup. 在这里看到的演示站点显示了错误设置的方框1和2,方框#3显示了正确的设置。

I'm having trouble... this may require js to determine line length and then set the bottom line to be longer. 我遇到了麻烦...这可能需要js来确定行长度,然后将底线设置得更长。 Screen resolution will likely play a role but can't I make the lines consistent across different screen sizes? 屏幕分辨率可能会起作用,但我不能在不同的屏幕尺寸上使线条一致吗?

Here is one of the list items, I'm interested in the div "titlebox": 这是列表项之一,我对div“titlebox”感兴趣:

<li class="list__item">
            <figure class="list__item__inner">

   <p class="vignette" style="background-image:url(http://www.ht-real-estate.com/template/ht2014/images/landscape/05.jpg)"></p>
   <div class="titlebox">This line is longer than this line</div>
   <div class="locationbox">Location</div>
   <div class="pricebox">Price</div>

</li>

Any help is great, thanks!! 任何帮助都很棒,谢谢!!

Screenshot attached too: 附上截图: 截图

Here is a JS solution that might work for you in most instances (I say most instances because text characters can have varying widths). 这是一个可能在大多数情况下适合您的JS解决方案(我说大多数情况是因为文本字符可以有不同的宽度)。 It basically splits your sentences into 2 lines with a dynamically inserted <br> tag. 它基本上将您的句子翻译成2线用动态插入<br>标签。 Comments in the code: 代码中的评论:

$(".titlebox").each(function(){
  var len = $(this).text().length,
      words = $(this).text().split(" "),
      line1 = [],
      line2 = [],
      html = "";

  // iterate through each word in the title
  $.each(words, function(i,word){
    // if line 1's current length plus the length of this word
    // is less than half the total characters, add word to line 1
    // else add to line 2
    // (check index of word to maintain order)
    if((line1.join(" ")+" "+word).length < (len/2) && (i == line1.length)){
      line1.push(word);
    } else {
      line2.push(word);
    }
  });

  // concatenate the results with a '<br>' separating the lines
  html = line1.join(" ")+"<br>"+line2.join(" ");

  // replace the .titlebox content with this new html string
  $(this).html(html);
});

 html { width: 100%; height: 40%; } body { background-color: #FFFFFF; overflow-y: scroll; overflow-x: hidden; } /* JavaScript disabled */ html.no-js .list__item { width: 100%; float: none; } html.no-js .list__item img { max-width: 9.375rem; /* 150 */ float: right; margin-left: 1.25rem; /* 20 */ } @supports ( display: -webkit-flex ) or ( display: -ms-flex ) or ( display: flex ) { html.no-js .list__item { width: 25%; float: left; } html.no-js .list__item img { max-width: none; float: none; margin-left: 0; } } nav { border: 1px solid #ccc; border-right: none; border-left-color: #006600; width: 100%; margin-bottom: 20px; background-color: #006600; font-family: Arial, Helvetica, sans-serif; font-size: 100%; color: #030303; } nav ul { display: flex; flex-direction: row; margin: 0; padding: 0; } nav ul li { list-style: none; text-align: center; border-left: 1px solid #fff; border-right: 1px solid #ccc; } nav ul li:first-child { border-left: none; } nav ul li:hover { background: #9cb369; } nav ul li a { display: block; text-decoration: none; color: #FFFFFF; padding: 10px 0; } nav { display: table; table-layout: fixed; } ul li { flex-grow: 1; } .x { display: none; } .p { text-align: center; font-size: 14px; margin-top: 80px; } .d { color: #ccc; } nav ul li { display: block; border-bottom: 1px solid #ccc; } .whywelove img{ margin-top: 1%; height: auto; width: 50%; } .font3 { margin-top: 2%; color: #030; font-family: "Times New Roman", Times; font-size: 8vh; } .font4 { font-size: 3.5vh; font-weight: bolder; font-family: "Times New Roman", Times; color: #002B00; line-height:25px; margin-top: -0.5%; margin-bottom: 2% } .vignette { width: 90%; margin-top:5%; margin-left:auto; margin-right:auto; box-shadow: 15px 15px 40px #defeec inset,-15px -15px 40px #defeec inset; height: 240px; background-size: cover; background-repeat: no-repeat; } .vignette2 { width: 800px; margin-top:3%; margin-left:auto; margin-right:auto; box-shadow: 75px 75px 50px #defeec inset,-75px -75px 50px #FFFF inset; height: 600px; background-size: cover; background-repeat: no-repeat; } .container { width: 100%; max-width: 76rem; font-size: 0.875rem; /* 14 */ line-height: 1.375rem; /* 22 */ margin: 0 auto; padding: 0.625rem; /* 10 */ } .container, .container a { color: #cfd7db; } .container a:hover { color: #fff; } h1 { font-size: 2.5rem; /* 40 */ font-weight: 300; line-height: 2.875rem; /* 46 */ text-align: center; margin-bottom: 2.5rem; /* 40 */ } a.divLink { text-decoration: none; } .list { width: 100%; overflow: hidden; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-flex-wrap: wrap; -ms-flex-wrap: wrap; flex-wrap: wrap; } .list__item { width: 32%; float: left; /* 10 */ display: -webkit-flex; display: -ms-flexbox; display: flex; padding-top: 0.625em; padding-bottom: 0.825em; margin-left:1%; margin-right:0%; position:relative; line-height:40px; } .list__item .caption { position: absolute; width: 20%; height: 10%; top: 30%; left: 32%; font-size: 3.3em; font-weight:bold; color: red; } .list__item__inner { width: 100%; color: #474d51; background-color: #DEFEEC; border: 1px groove #F8F8F8; overflow: hidden; margin-left:2%; margin-right:2%; -webkit-box-shadow: 0 0.125rem 0.313rem rgba( 0, 0, 0, .2 ); /* 2 5 */ box-shadow: 0 0.125rem 0.313rem rgba( 0, 0, 0, .2 ); /* 2 5 */ } .list__item figcaption { padding: 1.25rem; /* 20 */ } .img-shadow { position: relative; max-width: 100%; float: left; } .img-shadow::before { content: ""; position: absolute; top: 0; bottom: 0; left: 0; right: 0; box-shadow: inset 0 0 80px rgba(0,0,0,.6); -moz-box-shadow: inset 0 0 80px rgba(0,0,0,.6); -webkit-box-shadow: inset 0 0 80px rgba(0,0,0,.6); } .img-shadow img { float: left; } .titlebox{ width: 80%; height: 10%; display: inline-block; font-size: 4.2vh; font-family: Garamond; color: #002000; text-align: center; line-height: 35px; font-weight:bold; margin-top: 5%; margin-right: 10%; margin-bottom: 5%; margin-left: 10%; background-color:grey; } .locationbox{ width: 80%; display: inline-block; font-size: 3.7vh; text-align: center; font-weight:bold; margin-top: 10%; margin-right: 10%; margin-bottom: 5%; margin-left: 10%; font-family: Garamond; color: #002000; font-style: italic; } .pricebox{ width: 80%; font-size: 4.2vh; text-align: center; font-family: Garamond; font-weight:bold; color: #002000; margin-top: 10%; margin-right: 10%; margin-bottom: 5%; margin-left: 10%; } .footer { text-align: center; margin: 2.5rem 0.625rem 0; /* 40 10 */ } .footer a { border-bottom: 1px solid #cfd7db; } @media screen and (max-width: 1950px){ .list__item { width: 32%; } .container { padding: 0; /* 10 */ } .titlebox{font-size:28px;} .locationbox{font-size:28px;} .pricebox{font-size:28px;} } @media screen and (max-width: 1700px){ .list__item { width: 32%; } .vignette { width: 92%; margin-top:5%; margin-left:auto; margin-right:auto; box-shadow: 15px 15px 40px #defeec inset,-15px -15px 40px #defeec inset; height: 240px; background-size: cover; background-repeat: no-repeat; } .container { padding: -10em; /* 10 */ } .titlebox{font-size:27px;} .locationbox{font-size:27px;} .pricebox{font-size:27px;} } @media screen and (max-width: 1440px){ .list__item { width: 32%; } .container { padding: 2.2em /* 10 */ } .titlebox{font-size:27px;} .locationbox{font-size:27px;} .pricebox{font-size:27px;} nav { border: 1px solid #ccc; border-right: none; border-left-color: #006600; width: 100%; margin-bottom: 20px; background-color: #006600; font-family: Arial, Helvetica, sans-serif; font-size: 100%; color: #030303; } nav ul { display: flex; flex-direction: row; margin: 0; padding: 0; } nav ul li { list-style: none; text-align: center; border-left: 1px solid #fff; border-right: 1px solid #ccc; } nav ul li:first-child { border-left: none; } nav ul li:hover { background: #9cb369; } nav ul li a { display: block; text-decoration: none; color: #FFFFFF; padding: 10px 0; } nav { display: table; table-layout: fixed; } ul li { flex-grow: 1; } .x { display: none; } .p { text-align: center; font-size: 14px; margin-top: 80px; } .d { color: #ccc; } nav ul li { display: block; border-bottom: 1px solid #ccc; } } @media screen and (max-width: 64em ) /* 1024 */ { .list__item { margin-right:2%; margin-left:-1%; } .titlebox{font-size:24px;} .locationbox{font-size:24px;} .pricebox{font-size:24px;} .list__item .caption { font-size: 2em; } body { padding: 2.5rem 0; /* 40 */ } .list__item { width: 30%; /* 1 item per row */ float: none; } @supports ( display: -webkit-flex ) or ( display: -ms-flex ) or ( display: flex ) { html.no-js .list__item { width: 30%; } } html.no-js .list__item img { max-width: none; float: none; margin-left: 0; } } @media screen and ( max-width: 50em ) /* 800 */ { .vignette { height: 400px; } .titlebox{font-size:23px;} .locationbox{font-size:23px;} .pricebox{font-size:23px;} .list__item { width: 70%; /* 1 items per row */ } } @media screen and ( max-width: 40em ) /* 640 */ { .vignette { width: 80% height: 300px; } .titlebox{font-size:21px;line-height:25px;} .locationbox{font-size:21px;} .pricebox{font-size:21px;line-height:25px;} .list__item { width: 70%;/* 1 items per row */ } } 
 <div class="container" role="main"> <ul class="list"> <li class="list__item"> <figure class="list__item__inner"> <p class="vignette" style="background-image:url(http://www.ht-real-estate.com/template/ht2014/images/landscape/05.jpg)"></p> <div class="titlebox">This line is <BR/> shorter than this :)</div> <div class="locationbox">Location</div> <div class="pricebox">Price</div> </a> </li> <li class="list__item"> <figure class="list__item__inner"> <p class="vignette" style="background-image:url(https://upload.wikimedia.org/wikipedia/commons/c/ce/James_Wadsworth_Rossetter_House_Front_1.jpg)"></p> <div class="titlebox">Thisssss Lineeeeee is longer too</div> <div class="locationbox">Location</div> <div class="pricebox">Price</div> </a> </li> <li class="list__item"> <figure class="list__item__inner"> <p class="vignette" style="background-image:url(http://www.mytickerz.com/wp-content/uploads/prairie-style-house-plans-2.jpg)"></p> <div class="titlebox">This line is shorter likeeeeeeeee it should be</div> <div class="locationbox">Location</div> <div class="pricebox">Price</div> </a> </li> </ul> </div> 

I'm not really sure if there is any way to limit the top line width. 我不确定是否有任何方法可以限制顶线宽度。 The only way I can think of, is to add a <br/> tag in a place where you would like to break down the title and then just set the text-align: center; 我能想到的唯一办法,就是增加一个<br/>标签在一个地方,你想打破标题,然后只设置text-align: center; for the .titlebox . 对于.titlebox

<div class="titlebox">This line is <br/> longer than this line</div>

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

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