CSS如何让中间的元素在其兄弟跳跃之前缩小

时间:2015-08-12 11:10:31

标签: html css css3

我在父<divs>中有3 <div>他们都是inline-block

enter image description here

JSFiddle

当我缩小窗口宽度时,“继续按钮”会跳下

enter image description here

虽然我想要的是'请点击继续购买'换行,直到它在'继续'跳下来之前缩小为止。

enter image description here

“继续”按钮已修复width : 127px,背景为精灵。

我尝试将%em宽度添加到“请点击继续购买”,但是当“继续”按钮仍然跳转时,它会包含最后一个字。在“请点击继续购买”中添加固定宽度不是一个选项,因为要求它必须尽可能长一行。

<!DOCTYPE html>
<html lang="en" style="-ms-overflow-y: scroll;">
<head>
<title></title>
<style>
* {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;
}
article, aside, details, figcaption, figure, footer, header, hgroup, main, nav, section, summary {
	display: block;
}
body {
	margin: 0;
}
html {
	font-family: sans-serif;
	-ms-text-size-adjust: 100%;
	-webkit-text-size-adjust: 100%;
}
a {
	background: transparent;
}
*::before, *::after {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;
}
.price-and-button {
	display: block;
}
.premium-wrapper {
	border: 3px solid #2280c4;
	-webkit-border-radius: 6px;
	-moz-border-radius: 6px;
	border-radius: 6px;
	padding: 15px;
}
.legal.premium-wrapper {
	border-radius: 30px;
	border-width: 2px;
	padding-left: 3em;
	padding-right: 3em;
}
.lpa-quote-wrapper .premium-wrapper, .conveyancing-quote-wrapper .premium-wrapper, .summary-fee-breakdown .premium-wrapper {
	border: 1px solid #2280c4;
	*zoom: 1: ;
}
.premium-wrapper.legal::before, .premium-wrapper.legal::after {
	content: none;
}
.lpa-quote-wrapper .premium-wrapper::before, .conveyancing-quote-wrapper .premium-wrapper::before, .summary-fee-breakdown .premium-wrapper::before, .lpa-quote-wrapper .premium-wrapper::after, .conveyancing-quote-wrapper .premium-wrapper::after, .summary-fee-breakdown .premium-wrapper::after {
	display: table;
	content: "";
	line-height: 0;
}
.lpa-quote-wrapper .premium-wrapper::after, .conveyancing-quote-wrapper .premium-wrapper::after, .summary-fee-breakdown .premium-wrapper::after {
	clear: both;
}
.sections-wrapper {
	margin-bottom: 27px;
}
.left-column {
	margin: 38px 0 0 76px;
}
.col-xs-1, .col-sm-1, .col-md-1, .col-lg-1, .col-xs-2, .col-sm-2, .col-md-2, .col-lg-2, .col-xs-3, .col-sm-3, .col-md-3, .col-lg-3, .col-xs-4, .col-sm-4, .col-md-4, .col-lg-4, .col-xs-5, .col-sm-5, .col-md-5, .col-lg-5, .col-xs-6, .col-sm-6, .col-md-6, .col-lg-6, .col-xs-7, .col-sm-7, .col-md-7, .col-lg-7, .col-xs-8, .col-sm-8, .col-md-8, .col-lg-8, .col-xs-9, .col-sm-9, .col-md-9, .col-lg-9, .col-xs-10, .col-sm-10, .col-md-10, .col-lg-10, .col-xs-11, .col-sm-11, .col-md-11, .col-lg-11, .col-xs-12, .col-sm-12, .col-md-12, .col-lg-12 {
	position: relative;
	min-height: 1px;
	padding-left: 15px;
	padding-right: 15px;
}
/* @media all and (min-width:992px) */
.col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12 {
	float: left;
}
/* @media all and (min-width:992px) */
.col-md-8 {
	width: 66.66%;
}
.row {
	margin-left: -15px;
	margin-right: -15px;
}
/* @media all and (min-width:1200px) */
.container {
	width: 1010px;
}
.clearfix::before, .clearfix::after, .container::before, .container::after, .container-fluid::before, .container-fluid::after, .row::before, .row::after, .nav::before, .nav::after, .navbar::before, .navbar::after, .navbar-header::before, .navbar-header::after, .navbar-collapse::before, .navbar-collapse::after, .form-horizontal .form-group::before, .form-horizontal .form-group::after {
	content: " ";
	display: table;
}
.clearfix::after, .container::after, .container-fluid::after, .row::after, .nav::after, .navbar::after, .navbar-header::after, .navbar-collapse::after, .form-horizontal .form-group::after {
	clear: both;
}
.content-container {
	background-color: #ffffff;
	min-height: 400px;
	box-shadow: 25px 0px 25px -25px #333333, -25px 0px 25px -25px #333333;
	-moz-border-radius: 10px 10px 0px 0px;
	-webkit-border-radius: 10px 10px 0px 0px;
	border-radius: 10px 10px 0px 0px;
	-moz-border-radius: 9px 9px 0px 0px;
	-webkit-border-radius: 9px 9px 0px 0px;
	border-radius: 9px 9px 0px 0px;
}
.container {
	margin-right: auto;
	margin-left: auto;
	padding-left: 15px;
	padding-right: 15px;
}
/* @media all and (min-width:768px) */
.container {
	width: 750px;
}
/* @media all and (min-width:992px) */
.container {
	width: 970px;
}
/* @media all and (min-width:1200px) */
.container {
	width: 1170px;
}
.body-wrap {
	background: url(../images/saga-home/background-gradient.png) repeat-x;
	background-attachment: fixed;
	min-height: 500px;
}
body {
	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
	font-size: 14px;
	line-height: 1.4285;
	color: #333333;
	background-color: #ffffff;
}
body {
	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
	font-size: 13px;
	color: #333333;
	background-color: #ffffff;
}
html {
	font-size: 62.5%;
	-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
.price-and-button-cell {
	display: inline-block;
}
.please-click-to-continue {
	padding-right: 0.7em;
	position: relative;
	top: -15px;
}
.bottom-buttons, .summary-group-bottom-buttons, .premium-bottom-buttons {
	margin-top: 30px;
	min-height: 38px;
	position: relative;
	width: 100%;
}
.legal.bottom-buttons {
	width: 130px;
	top: -2px;
}
.button-and-shadow-container {
	float: right;
}
.bottom-buttons .button-and-shadow-container, .summary-group-bottom-buttons .button-and-shadow-container, .premium-bottom-buttons .button-and-shadow-container {
	float: right;
}
a {
	color: #428bca;
	text-decoration: none;
}
a {
	color: #0088cc;
	text-decoration: none;
}
.lpa-quote-wrapper a, .conveyancing-quote-wrapper a, .summary-fee-breakdown a {
	cursor: pointer;
	text-decoration: underline;
}
.bottom-buttons .button-green-small, .summary-group-bottom-buttons .button-green-small, .premium-bottom-buttons .button-green-small, .bottom-buttons .button-blue-small, .summary-group-bottom-buttons .button-blue-small, .premium-bottom-buttons .button-blue-small, .bottom-buttons .button-green-medium, .summary-group-bottom-buttons .button-green-medium, .premium-bottom-buttons .button-green-medium, .bottom-buttons .button-blue-medium, .summary-group-bottom-buttons .button-blue-medium, .premium-bottom-buttons .button-blue-medium, .bottom-buttons .button-green-large, .summary-group-bottom-buttons .button-green-large, .premium-bottom-buttons .button-green-large, .bottom-buttons .button-blue-large, .summary-group-bottom-buttons .button-blue-large, .premium-bottom-buttons .button-blue-large, .bottom-buttons .button-green-extra-large, .summary-group-bottom-buttons .button-green-extra-large, .premium-bottom-buttons .button-green-extra-large, .bottom-buttons .button-green-extra-large-lm, .summary-group-bottom-buttons .button-green-extra-large-lm, .premium-bottom-buttons .button-green-extra-large-lm, .bottom-buttons .button-blue-extra-large, .summary-group-bottom-buttons .button-blue-extra-large, .premium-bottom-buttons .button-blue-extra-large {
	border: none;
	color: #f2f5fd;
	display: block;
	font-weight: 700;
	height: 38px;
	line-height: 38px;
	text-align: center;
	text-decoration: none;
}
.bottom-buttons .button-green-large, .summary-group-bottom-buttons .button-green-large, .premium-bottom-buttons .button-green-large, .bottom-buttons .button-blue-large, .summary-group-bottom-buttons .button-blue-large, .premium-bottom-buttons .button-blue-large {
	width: 135px;
}
.bottom-buttons .button-green-large, .summary-group-bottom-buttons .button-green-large, .premium-bottom-buttons .button-green-large {
	background: #16C52F 0 -304px;
}
h1, h2, h3, h4, h5, h6 {
	margin: 9px 0;
	font-family: "Trebuchet MS", "Helvetica Neue", Helvetica, Arial, sans-serif;
	font-weight: normal;
	line-height: 18px;
	color: #2280c4;
	text-rendering: optimizelegibility;
}
h1, h2, h3 {
	line-height: 36px;
}
h2 {
	font-size: 24px;
}
.premium-wrapper h2 {
	font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.premium-wrapper h2:first-child {
	margin: 0;
}
.lpa-quote-wrapper .premium-wrapper h2, .conveyancing-quote-wrapper .premium-wrapper h2, .summary-fee-breakdown .premium-wrapper h2 {
	*zoom: 1: ;
}
.lpa-quote-wrapper .premium-wrapper h2::before, .conveyancing-quote-wrapper .premium-wrapper h2::before, .summary-fee-breakdown .premium-wrapper h2::before, .lpa-quote-wrapper .premium-wrapper h2::after, .conveyancing-quote-wrapper .premium-wrapper h2::after, .summary-fee-breakdown .premium-wrapper h2::after {
	display: table;
	content: "";
	line-height: 0;
}
.lpa-quote-wrapper .premium-wrapper h2::after, .conveyancing-quote-wrapper .premium-wrapper h2::after, .summary-fee-breakdown .premium-wrapper h2::after {
	clear: both;
}
.legal.premium-wrapper h2.your-quote {
	padding: 0;
	width: 6em;
}
.premium-wrapper h2 span {
	display: block;
}
.premium-wrapper h2 span + span {
	color: #333333;
	font-size: 18.2px;
	line-height: 20px;
}
.lpa-quote-wrapper .premium-wrapper h2 span, .conveyancing-quote-wrapper .premium-wrapper h2 span, .summary-fee-breakdown .premium-wrapper h2 span {
	float: left;
}
.legal.premium-wrapper h2 span + span {
	color: #2280c4;
	font-size: 24px;
	line-height: 36px;
}
</style>
</head>
<div class="sections-wrapper"><section><div class="premium-wrapper legal "><div class="price-and-button">
                        <div style="display: block;">
                            <div class="price-and-button-cell">
                                <h2 class="your-quote">
                                    <span>Your Quote</span>
                                    <span>
                                        £847.20
                                    </span>
                                </h2>
                            </div>
                            <div class="price-and-button-cell magic-magic-disappear please-click-to-continue">Please click continue to purchase</div>
                            <div class="bottom-buttons legal price-and-button-cell">
                                <div class="button-and-shadow-container">
                                    <a class="button-green-large" href="/legal-services/Lpa/customerDetails?product=Property%20and%20finance%20AND%20health%20and%20welfare">Continue</a>
                                </div>
                            </div>
                        </div>
                    </div></div></section></div></div></div></div></form></div></div></div></body>
</html>

如何让中间的元素在兄弟跳下来之前收缩?

2 个答案:

答案 0 :(得分:2)

如果您只定位现代浏览器,请使用flex-box,否则您可以使用旧版浏览器的css表格布局。

.wrapper {display:table;}
.wrapper div {display:table-cell; padding:10px;}
@media only screen and (max-width:300px) {
    .wrapper div {display:inline-block;}
}

这是一个片段(下方),但在this jsFiddle

中更容易调整大小

.wrapper {display:table;}
.wrapper div {display:table-cell; padding:10px;}

@media only screen and (max-width:300px) {
    .wrapper div {display:inline-block;}
}
<div class='wrapper'> 
    <div class='first'>Your Quote</div>
    <div class='second'>Please click to enable purchase</div>
    <div class='third'>Buy now</div>
</div>

答案 1 :(得分:1)

这可以通过多种方式解决,具体取决于您的需求(浏览器支持)。

媒体查询

.please-click-to-continue div达到阈值时,您可以更改它的宽度。假设阈值为600px ......

@media screen and (max-width:600px){
    .please-click-to-continue {
        width:100px; /* or whatever works for you.*/
    }
}

然而可能不是更简洁的方式,因为您不确定有多少空间可用。这里有 flexbox

Flexbox的

浏览器支持(来自CSS Tricks) -

  • Chrome any
  • Firefox any
  • Safari any
  • Opera 12.1+
  • IE 10+
  • iOS any
  • Android any

Flexbox在元素之间提供原生且干净的空间分配方式。请参考以下示例并根据您的需要进行修改。

* {
  box-sizing: border-box;
}
.wrapper {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  -ms-flex-pack: justify;
  justify-content: space-between;
}
.left,
.center,
.right {
  height: 200px;
  background: #bada55;
}
.left,
.right {
  width: 20%;
  margin: 20px;
}
.center {
  background: #ff0066;
  -webkit-align-self: center;
  -ms-flex-item-align: center;
  align-self: center;
  width: 50%;
}
<div class="wrapper">
  <div class="left"></div>
  <div class="center"></div>
  <div class="right"></div>
</div>

有一种表格方式,我不打算写,因为@Moob在我写这篇文章时已在回答中提到过。 :)