我正在尝试制作一个包含横幅和4列的网格。该示例可以为seen here。
行之间的间距为10像素,但我无法使框适合,因此它们沿网格中的行移动。当我查看代码时,对我来说看起来是正确的。所以我想这可能是图片的大小。但是改变大小并不能解决我的问题。
有人知道如何使网格元素适合图片中的以下线条吗?
我的代码: (我删除了一些与该问题无关的HTML代码)
/* Card and Button */
body {
background-color: #f5f5f5;
}
div {
background-color: #fff;
}
.index-content a:hover {
color: black;
text-decoration: none;
}
.index-content {
/*margin-bottom: 20px;*/
/*padding: 50px 0px;*/
}
.index-content .row {
margin-top: 20px;
}
.index-content a {
color: black;
}
.index-content .card {
background-color: #FFFFFF;
padding: 0;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}
.index-content .card:hover {
box-shadow: 0 16px 24px 2px rgba(0, 0, 0, 0.14), 0 6px 30px 5px rgba(0, 0, 0, 0.12), 0 8px 10px -5px rgba(0, 0, 0, 0.3);
color: black;
}
.index-content .card img {
width: 100%;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
height: 350px;
}
.index-content .card h4 {
margin: 20px;
}
.index-content .card p {
margin: 20px;
opacity: 0.65;
}
.index-content .blue-button {
width: 100px;
-webkit-transition: background-color 1s, color 1s;
/* For Safari 3.1 to 6.0 */
transition: background-color 0.5s, color 0.5s;
min-height: 20px;
background-color: #002E5B;
color: #ffffff;
border-radius: 4px;
text-align: center;
font-weight: lighter;
margin: 0px 20px 15px 20px;
padding: 5px 0px;
display: inline-block;
}
.index-content .blue-button:hover {
background-color: #dadada;
color: #002E5B;
}
/* Set width between block elements */
.small-padding.top {
padding-top:10px;
}
.small-padding.bottom {
padding-bottom:10px;
}
.small-padding.left {
padding-left:5px;
}
.small-padding.right {
padding-right:5px;
}
.row [class*="col-"] {
padding-left: 5px;
padding-right: 5px;
}
/* GRID ELEMENTS MEDIA QUERIES */
@media only screen and (max-width: 768px) {
.index-content .col-lg-4 {
margin-top: 20px;
}
.small-padding.bottom {
padding-bottom:10px;
}
div {
background-color: #fff;
padding-left: 2px;
padding-right: 2px;
}
.row [class*="col-"] {
padding-left: 5px;
padding-right: 5px;
}
}
@media (min-width: 768px) {
.card {
position: relative;
}
.card-content {
position: absolute;
bottom: 0;
width: 100%;
background: rgba(0, 0, 0, 0.5);
}
.card-content h4,
.card-content p {
color: white;
width: 100%;
float: left;
margin: 0 0 5px;
}
.card-content a {
float: right;
}
.index-content .card h4,
.index-content .card p {
padding: 15px 20px;
margin: 0;
}
.index-content .card p {
padding: 0 20px 15px;
margin: 0;
}
}
@media (min-width: 991px) {
.medium-padding.top {
padding-top:10px;
}
.medium-padding.bottom {
padding-bottom:10px;
}
.medium-padding.left {
padding-left:5px;
}
.medium-padding.right {
padding-right:5px;
}
}
<div class="container">
<!-- Col md 3 - 3 - 3 - 3 + Col sm 6 -6 -->
<div class="row">
<div class="col-sm-6 col-md-3 small-padding right bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding bottom left medium-padding right bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding left bottom medium-padding right bottom left">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding left bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg" class="img-responsive"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 small-padding right bottom ">
<img src="http://placehold.it/600x400" alt="1" class="img-responsive center-block"></img>
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-xs-6 small-padding right bottom">
<img src="http://placehold.it/300x205" alt="2" class="img-responsive"></img>
</div>
<div class="col-xs-6 small-padding right left bottom">
<img src="http://placehold.it/300x205" alt="3" class="img-responsive"></img>
</div>
<div class="col-xs-6 small-padding right bottom">
<img src="http://placehold.it/300x205" alt="4" class="img-responsive"></img>
</div>
<div class="col-xs-6 small-padding right left bottom">
<img src="http://placehold.it/300x205" alt="5" class="img-responsive"></img>
</div>
</div>
</div>
</div>
答案 0 :(得分:4)
请不要将“ col-”类与“ row”类用于同一元素。
喜欢这个。
<div class="row col-sm-6">
因为类名“ row”具有margin-right:-15px;和margin-left:-15px;如果您使用类名“ col-”,它将以padding-right:15px;影响行样式。和padding-left:15px;
这是此问题的主要原因,请再添加一个div结构,例如以下方法
<div class="col-sm-6">
<div class="row">
</div>
</div>
输出:
答案 1 :(得分:2)
您的代码有很多问题。
其中之一是您使用BS3而不是4。BS3使用float
,这是一个不好的做法。请使用BS4与flex。一切都变得容易得多。
另一个是在同一元素上使用2个完全不同的类。 row
和col-
绝对不能在同一元素上使用。在您的情况下,您需要在列内添加行。
我在您的html中更改了它,并在文件顶部添加了一些CSS。这应该可以帮助您实现所需的目标。
我再次强调您应该使用BS4。
/* Card and Button */
.row.mx-0 {
margin-left:0;
margin-right:0;
}
.row .col-sm-6.no-padding {
padding-right:0;
padding-left:0;
}
.row .col-sm-6.no-bottom {
padding-bottom:0;
}
body {
background-color: #f5f5f5;
}
div {
background-color: #fff;
}
.index-content a:hover {
color: black;
text-decoration: none;
}
.index-content {
/*margin-bottom: 20px;*/
/*padding: 50px 0px;*/
}
.index-content .row {
margin-top: 20px;
}
.index-content a {
color: black;
}
.index-content .card {
background-color: #FFFFFF;
padding: 0;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}
.index-content .card:hover {
box-shadow: 0 16px 24px 2px rgba(0, 0, 0, 0.14), 0 6px 30px 5px rgba(0, 0, 0, 0.12), 0 8px 10px -5px rgba(0, 0, 0, 0.3);
color: black;
}
.index-content .card img {
width: 100%;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
height: 350px;
}
.index-content .card h4 {
margin: 20px;
}
.index-content .card p {
margin: 20px;
opacity: 0.65;
}
.index-content .blue-button {
width: 100px;
-webkit-transition: background-color 1s, color 1s;
/* For Safari 3.1 to 6.0 */
transition: background-color 0.5s, color 0.5s;
min-height: 20px;
background-color: #002E5B;
color: #ffffff;
border-radius: 4px;
text-align: center;
font-weight: lighter;
margin: 0px 20px 15px 20px;
padding: 5px 0px;
display: inline-block;
}
.index-content .blue-button:hover {
background-color: #dadada;
color: #002E5B;
}
/* Set width between block elements */
.small-padding.top {
padding-top:10px;
}
.small-padding.bottom {
padding-bottom:10px;
}
.small-padding.left {
padding-left:5px;
}
.small-padding.right {
padding-right:5px;
}
.row [class*="col-"] {
padding-left: 5px;
padding-right: 5px;
}
/* GRID ELEMENTS MEDIA QUERIES */
@media only screen and (max-width: 768px) {
.index-content .col-lg-4 {
margin-top: 20px;
}
.small-padding.bottom {
padding-bottom:10px;
}
div {
background-color: #fff;
padding-left: 2px;
padding-right: 2px;
}
.row [class*="col-"] {
padding-left: 5px;
padding-right: 5px;
}
}
@media (min-width: 768px) {
.card {
position: relative;
}
.card-content {
position: absolute;
bottom: 0;
width: 100%;
background: rgba(0, 0, 0, 0.5);
}
.card-content h4,
.card-content p {
color: white;
width: 100%;
float: left;
margin: 0 0 5px;
}
.card-content a {
float: right;
}
.index-content .card h4,
.index-content .card p {
padding: 15px 20px;
margin: 0;
}
.index-content .card p {
padding: 0 20px 15px;
margin: 0;
}
}
@media (min-width: 991px) {
.medium-padding.top {
padding-top:10px;
}
.medium-padding.bottom {
padding-bottom:10px;
}
.medium-padding.left {
padding-left:5px;
}
.medium-padding.right {
padding-right:5px;
}
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<!-- Col md 3 - 3 - 3 - 3 + Col sm 6 -6 -->
<div class="row">
<div class="col-sm-6 col-md-3 small-padding right bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding bottom left medium-padding right bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding left bottom medium-padding right bottom left">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding left bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg" class="img-responsive"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 small-padding right bottom no-bottom">
<img src="http://placehold.it/600x400" alt="1" class="img-responsive center-block"></img>
</div>
<div class="col-sm-6 no-padding">
<div class="row mx-0">
<div class="col-xs-6 small-padding right bottom">
<img src="http://placehold.it/300x205" alt="2" class="img-responsive"></img>
</div>
<div class="col-xs-6 small-padding right left bottom">
<img src="http://placehold.it/300x205" alt="3" class="img-responsive"></img>
</div>
<div class="col-xs-6 small-padding right bottom">
<img src="http://placehold.it/300x205" alt="4" class="img-responsive"></img>
</div>
<div class="col-xs-6 small-padding right left bottom">
<img src="http://placehold.it/300x205" alt="5" class="img-responsive"></img>
</div>
</div>
</div>
</div>
</div>
答案 2 :(得分:2)
请删除您为col- -和行编写的所有样式。为此使用这种风格
.margin_bottom{margin-bottom:10px;}
.row [class*="col-"]{ padding-left: 5px; padding-right: 5px; }
.row { margin-left: -5px; margin-right: -5px; }
并使用此html代码
<div class="row">
<div class="col-sm-6 col-md-3 margin_bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 margin_bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 margin_bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 margin_bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg" class="img-responsive"></img>
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 margin_bottom">
<img src="http://placehold.it/600x400" alt="1" class="img-responsive center-block"></img>
</div>
<div class=" col-sm-6">
<div class="row">
<div class="col-xs-6 margin_bottom">
<img src="http://placehold.it/300x205" alt="2" class="img-responsive"></img>
</div>
<div class="col-xs-6 margin_bottom">
<img src="http://placehold.it/300x205" alt="3" class="img-responsive"></img>
</div>
<div class="col-xs-6 margin_bottom">
<img src="http://placehold.it/300x205" alt="4" class="img-responsive"></img>
</div>
<div class="col-xs-6 margin_bottom">
<img src="http://placehold.it/300x205" alt="5" class="img-responsive"></img>
</div>
</div>
</div>
</div>
输出:
答案 3 :(得分:0)
@ MK-DK,切勿在div中一起使用row
和col
。请检查以下示例,以获取对HTML和CSS进行较小更改的有效修复程序。
/* Card and Button */
.row.mx-0 {
margin-left: 0;
margin-right: 0;
}
.row .col-sm-6.no-padding {
padding-right: 0;
padding-left: 0;
}
.row .col-sm-6.no-bottom {
padding-bottom: 0;
}
body {
background-color: #f5f5f5;
}
div {
background-color: #fff;
}
.index-content a:hover {
color: black;
text-decoration: none;
}
.index-content .row {
margin-top: 20px;
}
.index-content a {
color: black;
}
.index-content .card {
background-color: #FFFFFF;
padding: 0;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}
.index-content .card:hover {
box-shadow: 0 16px 24px 2px rgba(0, 0, 0, 0.14), 0 6px 30px 5px rgba(0, 0, 0, 0.12), 0 8px 10px -5px rgba(0, 0, 0, 0.3);
color: black;
}
.index-content .card img {
width: 100%;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
height: 350px;
}
.index-content .card h4 {
margin: 20px;
}
.index-content .card p {
margin: 20px;
opacity: 0.65;
}
.index-content .blue-button {
width: 100px;
-webkit-transition: background-color 1s, color 1s;
/* For Safari 3.1 to 6.0 */
transition: background-color 0.5s, color 0.5s;
min-height: 20px;
background-color: #002E5B;
color: #ffffff;
border-radius: 4px;
text-align: center;
font-weight: lighter;
margin: 0px 20px 15px 20px;
padding: 5px 0px;
display: inline-block;
}
.index-content .blue-button:hover {
background-color: #dadada;
color: #002E5B;
}
/* Set width between block elements */
.small-padding.top {
padding-top: 10px;
}
.small-padding.bottom {
padding-bottom: 10px;
}
.small-padding.left {
padding-left: 5px;
}
.small-padding.right {
padding-right: 5px;
}
.row [class*="col-"] {
padding-left: 5px;
padding-right: 5px;
}
/* GRID ELEMENTS MEDIA QUERIES */
@media only screen and (max-width: 768px) {
.index-content .col-lg-4 {
margin-top: 20px;
}
.small-padding.bottom {
padding-bottom: 10px;
}
div {
background-color: #fff;
padding-left: 2px;
padding-right: 2px;
}
.row [class*="col-"] {
padding-left: 5px;
padding-right: 5px;
}
}
@media (min-width: 768px) {
.card {
position: relative;
}
.card-content {
position: absolute;
bottom: 0;
width: 100%;
background: rgba(0, 0, 0, 0.5);
}
.card-content h4,
.card-content p {
color: white;
width: 100%;
float: left;
margin: 0 0 5px;
}
.card-content a {
float: right;
}
.index-content .card h4,
.index-content .card p {
padding: 15px 20px;
margin: 0;
}
.index-content .card p {
padding: 0 20px 15px;
margin: 0;
}
}
@media (min-width: 991px) {
.medium-padding.top {
padding-top: 10px;
}
.medium-padding.bottom {
padding-bottom: 10px;
}
.medium-padding.left {
padding-left: 5px;
}
.medium-padding.right {
padding-right: 5px;
}
}
/* Custom css added to fix the margin */
@media (min-width: 768px) {
.row-small-gutters {
margin: 0 -5px !important
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<!-- Col md 3 - 3 - 3 - 3 + Col sm 6 -6 -->
<div class="row">
<div class="col-sm-6 col-md-3 small-padding right bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg">
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding bottom left medium-padding right bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg">
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding left bottom medium-padding right bottom left">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg">
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
<div class="col-sm-6 col-md-3 small-padding left bottom">
<div class="index-content">
<div class="card">
<img src="https://image.ibb.co/iiWA2T/bmw_1.jpg" class="img-responsive">
<div class="card-content">
<h4>BMW NEW MODELS</h4>
<a href="#" class="blue-button">Read More</a>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6 small-padding right bottom ">
<img src="http://placehold.it/600x400" alt="1" class="img-responsive center-block">
</div>
<div class="col-sm-6">
<div class="row row-small-gutters">
<div class="col-xs-6 small-padding right bottom">
<img src="http://placehold.it/300x205" alt="2" class="img-responsive">
</div>
<div class="col-xs-6 small-padding right left bottom">
<img src="http://placehold.it/300x205" alt="3" class="img-responsive">
</div>
<div class="col-xs-6 small-padding right bottom">
<img src="http://placehold.it/300x205" alt="4" class="img-responsive">
</div>
<div class="col-xs-6 small-padding right left bottom">
<img src="http://placehold.it/300x205" alt="5" class="img-responsive">
</div>
</div>
</div>
</div>
</div>
答案 4 :(得分:0)
您的HTML结构应类似于
const initialObject = [
{
'ADF': ['test']
},
{
'BDF': ['test']
}
,
{
'CDF': ['test']
}
];
const finalObject = initialObject.reduce((acc, curr) => {
const [currentKey] = Object.keys(curr)
acc[currentKey] = currentKey;
return acc;
}, {});
console.log(finalObject);