Considering a grid that contains fixed size items (not my choice), and is not fixed size.
I would like to centralize the container (.grid-container) to have same side margin no matter how many items it have and how they are placed inside container as long as they are left-aligned.
If it isn't possible to do this only with CSS how can I do using JavaScript?
It's not necessary to use twitter-bootstrap grid system like the demo code.
Here a code to illustrate: https://embed.plnkr.co/F6niiwaosnO8tr0ss4XF/
.container {
background-color: #2B3643;
}
.grid-container {
padding: 30px 30px 0 30px;
}
.grid-item-wrapper {
width: 200px;
margin-bottom: 15px;
}
.grid-item {
margin-bottom: 15px;
}
.grid-item > .item-desc {
padding: 8px;
background-color: #333333;
color: #CFCFCF;
}
<div class="container">
<div class="grid-container row">
<!-- grid items -->
</div>
</div>
<template id="tmpl_grid_item">
<div class="grid-item-wrapper col-xs-3">
<div class="grid-item">
<div class="item-picture">
<img src="http://lorempixel.com/350/200/food/" style="width: 100%;" />
</div>
<div class="item-desc">
Lorem Ipsum
</div>
</div>
</div>
</template>
If you are willing to use absolute positioning and CSS3, there is a way.
HTML:
<div class="grid">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
CSS:
.grid{
position: absolute;
width: auto;
left: 50%;
-ms-transform: translateX(-50%); /* IE 9 */
-webkit-transform: translateX(-50%); /* Safari */
transform: translateX(-50%);
}
.item{
width: 50px;
height: 50px;
background: #fff;
float: left;
margin: 20px;
}
Here is a working fiddle. Hope it helps!
Just so that the code is out there in case it's useful, you can get the same behavior as F. Versehgi's answer without absolute positioning. This uses flexbox (so requires IE11, but any other browser should be fine) and some media queries (to account for the Bootstrap responsive container).
Note: You'll want to view the JSFiddle or CodePen link below, because the Stack Snippet is not working properly, for some reason:
JSFiddle example
CodePen example
$(document).ready(function() {
function fillGrid() {
var $gridContainer = $('.grid-container');
var htmlGridItem = $('#tmpl_grid_item').html();
$gridContainer.empty();
for (var i = 0; i < 20; i++) {
var $gridItem = $(htmlGridItem);
$gridContainer.append($gridItem);
}
};
fillGrid();
});
.container {
background-color: #2B3643;
}
.container * {
box-sizing: content-box;
}
.grid-container {
padding-top: 30px;
padding-bottom: 0px;
display: flex;
flex-wrap: wrap;
margin: 0px auto;
}
.grid-item-wrapper {
width: 200px;
margin-bottom: 15px;
}
.grid-item {
margin-bottom: 15px;
}
.grid-item > .item-desc {
padding: 8px;
background-color: #333333;
color: #CFCFCF;
}
body {
padding-top: 60px;
}
#media (min-width: 762px) and (max-width: 991px) {
.grid-item-wrapper:nth-child(3n+1) {
margin-left: 15px;
}
}
#media (min-width: 992px) {
.grid-item-wrapper:nth-child(4n+1) {
margin-left: 9px;
}
}
#media (min-width: 1200px) {
.grid-item-wrapper:nth-child(4n+1) {
margin-left: 109px;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div class="container">
<div class="grid-container row">
<!-- grid items -->
</div>
</div>
<template id="tmpl_grid_item">
<div class="grid-item-wrapper col-xs-3">
<div class="grid-item">
<div class="item-picture">
<img src="http://lorempixel.com/350/200/food/" style="width: 100%;" />
</div>
<div class="item-desc">
Lorem Ipsum
</div>
</div>
</div>
</template>
Related
Its required to create a mobile responsive card menu like this using HTML & CSS:
**
Requirement
**
required design
But the outcome was like the below. The text are going all over,cards aren't centered,and changes the position from device to device:
actual outcome
**
My coding
**
<section id="edu-coursecategory-section" class="ed-coursecategory layout-2">
<style>
* {
box-sizing: border-box;
}
/* Float four columns side by side */
.columncat {
float: left;
width: 25%;
padding: 0 10px;
}
/* Remove extra left and right margins, due to padding */
.row {margin: 0 -5px;}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Style the counter cards */
.cardcat {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
padding: 16px;
text-align: center;
background-color: #f1f1f1;
width: 80px;
height: 80px;
margin-left: 50%;
font-size: 50%;
}
}
#media screen and (min-width: 780px) {
.ed-coursecategory layout-2 , .isotop-active, .ed-courses layout-2{
display:box;
}
.columncat{
display:none ;
}
}
#media screen and (max-width: 781px) {
.ed-coursecategory layout-2, .isotop-active, .ed-courses layout-2{
display:none;
}
.columncat{
width: auto;
display: box;
margin-bottom: 20px;
}
}
</style>
<div class="row">
<div class="columncat">
<div class="cardcat">
<h3>Technology</h3>
</div>
</div>
<div class="columncat">
<div class="cardcat">
<h3>Science</h3>
</div>
</div>
<div class="columncat">
<div class="cardcat">
<h3>Math</h3>
</div>
</div>
<div class="columncat">
<div class="cardcat">
<h3>Health & Lifestyle</h3>
</div>
</div>
<div class="columncat">
<div class="cardcat">
<h3>Language</h3>
</div>
</div>
</div>
</section>
Appreciate if you could support me to achieve the required design.
I am trying to create 4 responsive divs for homepage; tried to decreae padding and margin of div not working.divs should be responsive on mobile devices they shpuld move below each one ; two divs appear on tabs; 4 divs on desktop and one div on mobile
i have tried following HTML:
<div class="flex-container">
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
</div>
here is css divs are not showing properly on tab and mobile devices and i want to decreae the padding and margin:
.homepage-div-shadow
{
padding: 20px;
box-shadow: 2px 2px 8px 1px #cccccc;
border: solid 1px #cccccc;
border-radius: 2px;
}
.homepage-div-icon{
background-image: url("image.png");
width: 100px;
height: 100px;
}
.homepage-div a
{
text-decoration: none;
color: white;
font-size: 16px;
}
.homepage-div h2{
font-size: 28px;
opacity: 0.90;
font-weight: 600;
margin: 5%;
margin-bottom: 6px;
}
.homepage-div h6{
font-size: 17px;
padding: 0px;
margin: 0px;
margin-bottom: 20px;
opacity: 0.6;
}
#media(min-width: 769px) {
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-container .flex-item {
flex: 1 0 50%;
}
}
#media (min-width: 1024px) {
.flex-container .flex-item {
flex: 1 0 25%;
}
}
.flex-item-inner {
padding: 25px;
margin: 20px;
background-color: white;
}
Here is the demo link: testfellow
Hope the solution below helps. I have just tried to mimic the scenario where there are a certain number of div s. And as per your question and requirement, have:
1. Followed mobile-first approach.
2. For mobile devices, each div should come in separate lines.
3. For tabs (at your breakpoint: 769px and above), two divs should come in a line.
4. For desktops (at your breakpoint: 1024px and above), four divs should come in a line.
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
background-color: #2980b9;
}
.flex {
max-width: 80%;
}
.flex-item {
background-color: #ffffff;
padding: 5em;
border: 1px solid black;
margin: 2% auto;
width: 25%;
}
#media(min-width: 769px) {
.flex {
display: flex;
flex-wrap: wrap;
}
.flex-item {
width: 50%;
}
}
#media(min-width: 1024px) {
.flex {
display: flex;
flex-wrap: wrap;
}
.flex-item {
width: 25%;
}
}
<div class="flex">
<div class="flex-item"></div>
<div class="flex-item "></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item "></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
I am trying to make a grid system with inspiration from this Bootstrap 3 template:
Link to template
I can see in the code that the 4 big grid elements are controlled with setting a height. I can make the first 2 grid items, but I am lost right now how to make the 2 small columns.
Here is the code what I made until now:
[Link to my demo model][3]
<style>
.bg-img {
background-position: center center;
background-size: cover;
background-repeat: no-repeat;
}
.item1 {
height:600px;
}
.item2 {
height:300px;
}
.item3 {
height: 300px;
}
</style>
<section>
<div class="container-fluid">
<div class="row">
<div class="col-sm-6 bg-img item1" style="background-image:url(https://placehold.it/600x600;)"></div>
<div class="col-sm-6 bg-img item2" style="background-image:url(https://placehold.it/300x600;)"></div>
</div>
</div>
</section>
Can anybody help me?
Best regards.
I hope this code will help you. I make this by Grid css.
.grid-container {
display: grid;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px;
font-size: 30px;
}
.item1 {
grid-column: 1 / span 2;
grid-row: 1;
}
.item2 {
grid-column: 3;
grid-row: 1 / span 2;
}
<div class="grid-container">
<div class="grid-item item1">1</div>
<div class="grid-item item2">2</div>
<div class="grid-item item3">3</div>
<div class="grid-item item4">4</div>
</div>
I hope this will solve your issue.
I replaced your images with a simple background-color, but overall it should be fully responsive. (It's not 100% perfect - left container is wider than the right container)
Some helpful links: bootstrap overview, bootstrap grid
Note: view example on full page :)
body {
padding: 15px;
}
.item-1 {
height: 600px;
background: blue;
}
.item-2 {
height: 600px;
padding-right: 0;
/* container right */
background-color: white;
}
.item-3 {
height: 292.5px;
margin-bottom: 7.5px;
background-color: green;
}
.item-4 {
padding: 0 15px;
}
.item-5 {
height: 292.5px;
margin-top: 7.5px;
border-right: 7.5px solid white;
background-color: purple;
}
.item-6 {
height: 292.5px;
margin-top: 7.5px;
border-left: 7.5px solid white;
background-color: yellow;
}
/* Small devices (landscape phones, less than 768px) */
#media (max-width: 767.98px) {
.item-2 {
padding: 0 !important;
margin: 0;
height: auto;
}
.item-3,
.item-5,
.item-6 {
border: 0;
padding: 0;
margin: 0;
height: 300px;
}
/* To prevent responsiveness issue, caused by .row bootstrap class */
.no-margin {
margin: 0;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="no-margin row">
<div class="item-1 col-md"></div>
<div class="item-2 col-md">
<div class="item-3 col-md"></div>
<div class="item-4 row">
<div class="item-5 col-md"></div>
<div class="item-6 col-md"></div>
</div>
</div>
</div>
</div>
I have this html and css layout. I want to make it when reaching tablet with media query and phone to show like in those 2 images. So I have in normal (desktop mode) 4 products all of them in the same line. When the view reaches tablet - 1199 pixels I want those 4 products to be each 2 on a separate line, so 2 lines with 2 products. On the phone mode 768 pixels I want each product to be on its line with the width full like the wrapper.
Here is the code :
body {
margin: 0px;
padding: 0px;
}
/* Header */
.header {
background-color: yellow;
width: 100%;
margin: 0 auto;
}
.wrapper {
max-width: 1200px;
margin: 0 auto;
}
.logo {
background-color: grey;
width: 150px;
height: 50px;
margin-top: 20px;
float: left;
}
.info {
background-color: grey;
width: 285px;
height: 50px;
margin-top: 20px;
float: right;
}
.clear {
clear: both;
}
.menu {
background-color: grey;
width: 1200px;
height: 50px;
margin-top: 20px;
}
/* */
/* Nav-Bar */
.nav-bar {
background-color: lightgray;
width: 1200px;
height: 340px;
margin-top: 20px;
}
/* */
/* Product grid */
.product1,
.product2,
.product3 {
background-color: lightgray;
width: 285px;
height: 320px;
margin-top: 20px;
margin-right: 20px;
float: left;
}
.product4 {
background-color: lightgray;
width: 285px;
height: 320px;
margin-top: 20px;
float: left;
}
/* */
/* Bottom Part */
.content-area {
background-color: lightgray;
width: 100%;
height: 420px;
margin-top: 20px;
}
/* */
/* Footer */
.footer-area {
background-color: gray;
width: 100%;
height: 50px;
margin-top: 20px;
}
/* */
/* Responsive */
#media screen and (max-width: 1199px) {
.product1,
.product2,
.product3,
.product4 {
display: none;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Home View</title>
<link rel="stylesheet" type="text/css" href="../style/homestyle.css">
</head>
<body>
<!-- Header -->
<div class="header">
<div class="wrapper">
<div class="logo"></div>
<div class="info"></div>
<div class="clear"></div>
<div class="menu"></div>
</div>
</div>
<!-- -->
<!-- NavBar -->
<div class="header">
<div class="wrapper">
<div class="nav-bar"></div>
<div class="clear"></div>
</div>
</div>
<!-- -->
<!-- Product Grid -->
<div class="wrapper">
<div class="def1">
<div class="product1"></div>
</div>
<div class="def2">
<div class="product2"></div>
</div>
<div class="def3">
<div class="product3"></div>
</div>
<div class="def4">
<div class="product4"></div>
</div>
<div class="clear"></div>
</div>
<!-- -->
<!-- Bottom Part -->
<div class="wrapper">
<div class="content-area"></div>
</div>
<!-- Footer -->
<div class="header">
<div class="wrapper">
<div class="footer-area"></div>
</div>
</div>
</body>
</html>
The CSSTablet :
I attached the images needed for the layout. Please help!
First you add the view port element to Head of the HTML page.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Ref. link - https://www.w3schools.com/css/css_rwd_viewport.asp
Then Remove fixed width's from .menu and .nav-bar classes and add as max-width.
.menu {
background-color: grey;
max-width: 1200px;
height: 50px;
margin-top: 20px;
}
.nav-bar {
background-color: lightgray;
max-width: 1200px;
height: 340px;
margin-top: 20px;
}
And also you need to wrap your "Product slots" using "div".
(I used class called "wrapper-inner")
<!-- Product Grid -->
<div class="wrapper">
<div class="wrapper-inner">
<div class="def1">
<div class="product1"></div>
</div>
<div class="def2">
<div class="product2"></div>
</div>
<div class="def3">
<div class="product3"></div>
</div>
<div class="def4">
<div class="product4"></div>
</div>
<div class="clear"></div>
</div>
</div>
<!-- -->
Then after you can add required CSS media queries.
/* Responsive */
#media all and (max-width: 1199px) {
.wrapper-inner {
margin: 0 -20px;
background: orange;
padding: 0 20px;
}
.def1, .def2, .def3, .def4 {
width: 50%;
padding: 20px;
float: left;
}
.product1, .product2, .product3, .product4 {
width: 100%;
margin: 0 0;
}
}
#media all and (max-width: 768px) {
.def1, .def2, .def3, .def4 {
width: 100%;
padding: 20px;
float: left;
}
}
You are correct in using media queries. In those you can f.e. set the width of your elements like this:
#media screen and (max-width: 1199) {
.product { // you can write .product1, .product2,... but I would suggest creating this new class, as none of those images really need their own css
max-width: 25%; //this will change for tablet and mobile to 50% and 100%
}
}
Now do that for all modes that you want.
Also note that instead of setting a margin-right you could use flexbox with justify-content: space-between in the wrapper; Otherwise you will have to remove those margins for different screen sizes.
In the code below I have 3 col-md-4 that I want to reduce the space between to about 25px. I've tried reducing the margin between them along with increasing the padding on the left and right side of the first and last column respectively. The problem with this approach is that when the window size is manipulated it makes the image columns offset because of the increased padding.
HTML:
<div class="cards">
<div class="container">
<h2>
Our Expertise.
</h2>
</div>
<div class="row">
<div class="col-md-4">
<!-- <img> -->
<!-- <img> -->
<!-- <img> -->
</div>
<div class="col-md-4">
<!-- <img> -->
<!-- <img> -->
<!-- <img> -->
</div>
<div class="col-md-4">
<!-- <img> -->
<!-- <img> -->
<!-- <img> -->
<!-- <img> -->
</div>
</div>
</div>
CSS:
html, body {
margin: 0;
padding: 0;
font-family: 'Libre Baskerville', sans-serif;
}
.first {
padding-left: 200px !important;
}
.last {
padding-right: 200px !important;
}
.container {
max-width: 980px;
margin: 0 auto;
}
.header {
padding-top: 20px;
}
.header h1 {
font-size: 20px;
}
.nav li {
margin: 0px;
border: 0px;
}
.nav li a {
color: #333;
}
.jumbotron {
background-color: transparent;
padding-top: 100px;
padding-bottom: 100px;
}
.jumbotron h2 {
font-size: 50px;
}
.jumbotron h2 span {
color: #ffc200;
}
.banner {
background-color: #333;
height: 140px;
color: white;
text-align: center;
padding-top: 30px;
}
.cards {
background-color: #FFC200;
text-align: center;
}
.cards img {
margin-bottom: 25px;
padding: auto;
}
.cards h2 {
margin-top: 20px;
margin-bottom: 80px;
}
#media (max-width: 992px) {
.col-md-4 {
margin: 0 auto 0;
text-align: center;
width: 100%;
}
.cards img {
width: 60%;
}
}
#media (max-width: 500px) {
.header h1 {
text-align: center;
}
.nav li {
text-align: center;
width: 100%;
}
.cards img {
width: 100%;
}
}
There are suppose to be images in the columns but because I don't have 10 rep points it wont let me post them.
Just change the default column padding. You need to keep the total space between the columns an even number, so 24 would be good, you would simply change the default column padding from 15px to 12px and override the negative margin on the row as well:
.row {
margin-left: -12px!important;
margin-right: -12px!important;
}
div[class*='col-'] {
padding-left: 12px;
padding-right: 12px;
}
.inner {
border:1px solid black;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="cards">
<div class="container">
<h2>
Our Expertise.
</h2>
</div>
<div class="row">
<div class="col-md-4">
<div class="inner">Column 1</div>
</div>
<div class="col-md-4">
<div class="inner">Column 2</div>
</div>
<div class="col-md-4">
<div class="inner">Column 3</div>
</div>
</div>
</div>