I want to make an adaptive page with CSS grid like this for PC:
IMG | Text
Text | IMG
IMG | Text
And like this for mobile:
IMG
Text
IMG
Text
IMG
Text
The problem is I can not wrap each pair Text IMG to a div.
How can I make such layout with that chess order and without it for mobile?
img {
max-width: 100%;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
}
#media screen (max-width: 540px) {
.grid {
grid-template-columns: 1fr;
}
}
<div class="grid">
<div class="info">
<h2>Lorem, ipsum.</h2>
<p>Lorem ipsum dolor sit amet.</p>
<button>Lorem.</button>
</div>
<div class="img">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
<div class="info">
<h2>Lorem, ipsum.</h2>
<p>Lorem ipsum dolor sit amet.</p>
<button>Lorem.</button>
</div>
<div class="img">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
<div class="info">
<h2>Lorem, ipsum.</h2>
<p>Lorem ipsum dolor sit amet.</p>
<button>Lorem.</button>
</div>
<div class="img">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
</div>
You can adjust the placement using grid-column:
img {
max-width: 100%;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-flow:dense; /* Don't forget this to fill all the tracks */
}
/* you pattern repeat each 4 elements */
.img:nth-child(4n + 2) {
grid-column:1;
}
.info:nth-child(4n + 1) {
grid-column:2;
}
/**/
#media screen and (max-width: 540px) {
.grid {
grid-template-columns: 1fr;
}
.info:nth-child(4n + 1) {
grid-column:1;
}
}
<div class="grid">
<div class="info">
<h2>Lorem, ipsum.</h2>
<p>Lorem ipsum dolor sit amet.</p>
<button>Lorem.</button>
</div>
<div class="img">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
<div class="info">
<h2>Lorem, ipsum.</h2>
<p>Lorem ipsum dolor sit amet.</p>
<button>Lorem.</button>
</div>
<div class="img">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
<div class="info">
<h2>Lorem, ipsum.</h2>
<p>Lorem ipsum dolor sit amet.</p>
<button>Lorem.</button>
</div>
<div class="img">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
<div class="info">
<h2>Lorem, ipsum.</h2>
<p>Lorem ipsum dolor sit amet.</p>
<button>Lorem.</button>
</div>
<div class="img">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
<div class="info">
<h2>Lorem, ipsum.</h2>
<p>Lorem ipsum dolor sit amet.</p>
<button>Lorem.</button>
</div>
<div class="img">
<img src="https://via.placeholder.com/350x150" alt="">
</div>
</div>
Related
I am trying to create a layout using Flexbox where I have elements which consist of 3 other internal elements. The parent item element contains 3 divs: image, button, text. The issue is that my items will not always contain images or text that is the same height as the others. The button is the one thing that will have a consistent height. I am trying to figure out if it's possible to have each of my image divs be the same height as the tallest one and same for the text divs. I would also like the images to be vertically aligned to the bottom, so if one element has a shorter image, the white space to make the element the same height will go above the image like this:
And here is what I have so far:
.container {
display:flex;
flex-wrap:wrap;
flex-flow:row wrap;
justify-content:center;
}
.item {
max-width:200px;
margin:0 20px;
}
<div class="container">
<div class="item">
<div class="image">
<img src="http://placehold.it/150x300" />
</div>
<div class="button">
<button>Click Me</button>
</div>
<div class="text">
<p>consectetur adipiscing elit.</p>
</div>
</div>
<div class="item">
<div class="image">
<img src="http://placehold.it/150x200" />
</div>
<div class="button">
<button>Click Me</button>
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<div class="item">
<div class="image">
<img src="http://placehold.it/150x250" />
</div>
<div class="button">
<button>Click Me</button>
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet</p>
</div>
</div>
<div class="item">
<div class="image">
<img src="http://placehold.it/150x270" />
</div>
<div class="button">
<button>Click Me</button>
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
</div>
I know that I could do this using Javascript to loop through each item to get the tallest and change the CSS of all others, but I'd like to use only CSS if possible. I also know that I could just set the height of the image container to the height of the tallest image, but these images are going to be dynamic and there are going to be a lot of them, so I'd rather have a solution that doesn't require hardcoding values.
Flexing the .item class and adding justify-content: flex-end; would provide the majority of the affect, but as far as I know you'd have to set a specific height on at least one of the items if you want two elements to be aligned the same across flexbox. Happy to be proven wrong though.
You could alternatively use margin-top: auto on the first child to push any unused space to the top and everything else down.
.container {
display:flex;
flex-wrap:wrap;
flex-flow:row wrap;
justify-content:center;
}
.item {
max-width:200px;
margin:0 20px;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.text {
height: 36px; /* magic number */
}
<div class="container">
<div class="item">
<div class="image">
<img src="http://placehold.it/150x300" />
</div>
<div class="button">
<button>Click Me</button>
</div>
<div class="text">
<p>consectetur adipiscing elit.</p>
</div>
</div>
<div class="item">
<div class="image">
<img src="http://placehold.it/150x200" />
</div>
<div class="button">
<button>Click Me</button>
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
<div class="item">
<div class="image">
<img src="http://placehold.it/150x250" />
</div>
<div class="button">
<button>Click Me</button>
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet</p>
</div>
</div>
<div class="item">
<div class="image">
<img src="http://placehold.it/150x270" />
</div>
<div class="button">
<button>Click Me</button>
</div>
<div class="text">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
</div>
</div>
I've made a bootstrap carousel with items each containing three columns.
As the slide transition ends, the items snap into place and very briefly change the height of the parent div.
I've spent all day trying to find a solution but can't see anything! I'm assuming it is something to do with padding/margins as the slide happens. I've tried forcing the items to have no padding or margins but no luck.
Does anyone know anything about this issue? Is there something obvious I'm missing?
The link to the page with the issue is here
And the code itself:
.tourenEvents {
background: #2a2a2a;
}
.tourenEvents .tourenEventsInner {
padding-top: 30px;
padding-bottom: 50px;
color: #fff;
}
.tourenEvents .tourenEventsInner .carousel .item .carouselBlock {
margin: 0 !important; /* This was one of the attempts to fix the issue */
}
.tourenEvents .tourenEventsInner .carousel .item .carouselBlock img {
width: 100%;
}
/* == Carousel Control styles - I don't think the issue comes from here but I'm posting it just incase == */
#tourenCarousel .carousel-indicators {
display: none;
}
#tourenCarousel .carousel-control {
background-image: none !important;
color: #fff;
font-size: 50px;
top: 15vh;
opacity: 1;
-webkit-text-stroke: 2px #2a2a2a;
text-shadow: none;
}
#media (max-width: 768px) {
#tourenCarousel .carousel-control {
padding-top: 10vh;
}
}
#tourenCarousel .carousel-control:hover {
color: #fff;
}
#tourenCarousel .carousel-control:focus {
color: #fff;
}
#tourenCarousel .left {
padding-right: 15px;
}
#tourenCarousel .right {
padding-left: 15px;
}
<div class="tourenEvents col-sm-12">
<div class="tourenEventsInner col-lg-12 col-md-offset-1 col-md-10 col-sm-10 col-sm-offset-1 col-xs-10 col-xs-offset-1 text-center">
<h3 class="text-center">Check out our events</h3>
<!-- div:tourenCarousel is the carousel of events -->
<div id="tourenCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#tourenCarousel" data-slide-to="0" class="active"></li>
<li data-target="#tourenCarousel" data-slide-to="1"></li>
<li data-target="#tourenCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<div class="col-xs-10 col-xs-offset-1">
<div class="carouselBlock col-lg-4 col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
<div class="carouselBlock col-lg-4 col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
<div class="carouselBlock col-lg-4 col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
</div>
</div>
<div class="item">
<div class="col-xs-10 col-xs-offset-1">
<div class="carouselBlock col-lg-4col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
<div class="carouselBlock col-lg-4 col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
<div class="carouselBlock col-lg-4 col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
</div>
</div>
<div class="item">
<div class="col-xs-10 col-xs-offset-1">
<div class="carouselBlock col-lg-4 col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
<div class="carouselBlock col-lg-4 col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
<div class="carouselBlock col-lg-4 col-xs-10 col-xs-offset-1 text-center">
<p class="lightPara">Lorem ipsum dolor</p>
<h4>Lorem ipsum dolor</h4>
<p class="lightPara">Lorem ipsum dolor</p>
</div>
</div>
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#tourenCarousel" role="button" data-slide="prev">
<i class="fa fa-angle-left" aria-hidden="true"></i>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#tourenCarousel" role="button" data-slide="next">
<i class="fa fa-angle-right" aria-hidden="true"></i>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
Remove width:100% on .tourenEvents .tourenEventsInner .carousel .item .carouselBlock img
you are resizing width on active class.
In my project I've set background image with the following attributes:
body {
background-image: url("/");
background-size: cover;
background-repeat: no-repeat;
}
But when there are lots of elements on some page, this image stretching and enlarging to the bottom and, therefore, becomes ugly. How to do it non-stretchable? So that I can scroll down and that image would be constant.
Here's the example of the image stretching on CodePen. Below is the snippet:
body {
background-image: url("http://www.planwallpaper.com/static/images/canberra_hero_image.jpg");
background-size: cover;
background-repeat: no-repeat;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
<div class=col-md-4>
<div class="jumbotron">
Some element
</div>
</div>
</div>
</div>
If you do not want the background image to stretch then you should not set background-size: cover on the element. Setting the background size to cover would mean that the background image would be scaled (stretched) to fit the size of the container element.
Here is what MDN says about background-size: cover:
Scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). The image "covers" the entire width or height of the container.
If you want the background image to remain constant even when scrolling down the page, you should add background-attachment: fixed to the element. This would keep the image in its place.
The background-position: 50% 50% in the below snippet is an optional setting which is just used to keep the background image positioned at the center-mid. This can be avoided if such a positioning is not required. By default the image would get positioned at top-left.
body {
background-image: url("http://lorempixel.com/850/420/nature/1");
background-position: 50% 50%;
background-attachment: fixed;
background-size: cover;
background-repeat: no-repeat;
}
div{
height: 100px;
}
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
Note that stretching happens with background-size: cover only when the background-attachment: fixed setting is not applied (meaning, the image is not constant and gets stretched to fit container's full height/width) or when the image is smaller than the container. When it is larger than the container and background-attachment: fixed setting is applied, the image does not get stretched.
Another thing to note with background-size: cover is that when the image and the container have different sizes, the image is clipped at the sides. If it should be shrunk instead of being clipped then background-size: 100% 100% could also be used.
body {
background-image: url("http://lorempixel.com/850/420/nature/1");
background-position: 50% 50%;
background-attachment: fixed;
background-size: 100% 100%;
background-repeat: no-repeat;
}
div{
height: 100px;
}
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
<div>Lorem Ipsum Dolor Sit Amet......</div>
I would like to create this layout with twitter bootstrap.
http://i.stack.imgur.com/f6mn7.png
This my start Fiddle
http://jsfiddle.net/antoc/n8ec9j2h/
My html
<div class="container-fluid">
<div class="container">
<div class="row">
<div class="col-md-4">
<h3 class="bg-blue">title</h3>
<p>Lorem ipsum</p>
</div>
<div class="col-md-8">
<h3 class="bg-red">title</h3>
<p>Lorem ipsum</p>
<h3 class="bg-green">title</h3>
<p>Lorem ipsum</p>
</div>
</div>
</div>
My problems are:
how to create the blu, red and green strip in css?
how to create the yellow background in css?
Add some margin/padding hackery into it... like this updated fiddle
Here's the relevent CSS:
.bg-yellow{
background:yellow;
margin:-10px 0 -9999px -9999px;
padding:10px 0 9999px 9999px;
}
.bg-blue {
margin-left:-1000px;
padding-left:1000px;
}
.bg-green, .bg-red{
margin-right:-1000px;
}
And the HTML
<div class="container">
<div class="row">
<div class="col-md-4">
<h3 class="bg-blue">title</h3>
<div class="bg-yellow">
<p>Lorem ipsum</p>
</div>
</div>
<div class="col-md-8">
<h3 class="bg-red">title</h3>
<p>Lorem ipsum</p>
<h3 class="bg-green">title</h3>
<p>Lorem ipsum</p>
</div>
</div>
</div>
Other than some padding editing for the yellow box and some text content, I think this is what you wanted:
http://jsfiddle.net/AndrewL32/n8ec9j2h/17/
<div class="row">
<div class="col-md-3 bg-yellow">
<h3 class="bg-blue">title</h3>
<p>Lorem ipsumLorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsumLorem ipsumLorem ipsum Lorem ipsumLorem ipsum</p>
<p>Lorem ipsumLorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsumLorem ipsumLorem ipsum Lorem ipsumLorem ipsum</p>
</div>
<div class="col-md-9">
<h3 class="bg-red">title</h3>
<p>Lorem ipsum</p>
<h3 class="bg-green">title</h3>
<p>Lorem ipsum</p>
</div>
</div>
this is my code :
#for (int j = 0; j < 10; j++)
{
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-12">
<div class="thumbnail">
<img src="http://placehold.it/800x500" alt="">
<div class="">
<h3>Feature Label</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p>
Buy Now! More Info
</p>
</div>
</div>
</div>
}
how can i reduce the margin between each item ( check attachment please)
thanks
.col-lg-2.col-md-3.col-sm-4.col-xs-12
{
padding: 0 your desired padding/2;
}
actually, it is padding. not margin.
here is the bootply example:- http://www.bootply.com/4yqAuLl9tM