What it looks like:
When I decrease the browser's window size I get:
BUT I WANT IT TO LOOK LIKE THIS WITHOUT USING FLEXBOX:
Here's codepen
CODE:
#one {
background-color: grey;
text-align: center;
}
#two {
display: inline-block;
text-align: left;
padding: 10px;
background-color: lightgrey;
}
.square {
display: inline-block;
width: 300px;
padding-bottom: 300px;
background-color: black;
}
<div id="one">
<div id="two">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>
Without using Flexbox, you need #media queries for that.
changed your values to make it responsive
*,
*::before,
*::after {
box-sizing: border-box
}
body {
margin: 0
}
#one {
background-color: grey;
max-width: 900px;
margin: 0 auto;
font-size: 0 /* inline-block gap fix */
}
#two {
padding: 10px;
background-color: lightgrey;
}
.square {
display: inline-block;
width: calc((100% / 3) - 10px);
padding-bottom: 300px;
background-color: black;
margin:5px
}
#media (max-width: 768px) {
.square {
width:calc((100% / 2) - 10px)
}
}
<div id="one">
<div id="two">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>
Try this,
#two {
display: inline-block;
text-align: left;
padding: 10px;
background-color: lightgrey;
width:100%;
}
.square {
display: inline-block;
width: 32.5%;
padding-bottom: 300px;
background-color: black;
}
Here's the solution using Media queries codepen
* {
margin: 0px;
}
#one {
background-color: grey;
text-align: center;
font-size: 0;
}
#two {
display: inline-block;
text-align: left;
padding: 10px;
background-color: lightgrey;
}
.square {
display: inline-block;
width: 300px;
padding-bottom: 300px;
background-color: black;
}
#media screen and (max-width: 920px) {
#two {
width: 600px;
}
}
#media screen and (max-width: 600px) {
#two {
width: 300px;
}
}
<div id="one">
<div id="two">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
</div>
Related
I have a section inside width: 1180px; i want to extend this green color div I want to make width: 100% I have tried using vw but not getting but some extra space is coming. can anyone suggest me? Is there any other way to do using CSS.
.wrapper {
width: 100%;
position: relative;
background: #ccc;
}
.inner {
width: 1180px;
margin: 0 auto;
background: pink;
}
.box1 {
height: 50px;
background: red;
}
.box2 {
height: 50px;
background: green;
margin-left: calc(-100vw/2 + 100%/2);
margin-right: calc(-100vw/2 + 100%/2);
width: 100vw;
}
<div class="wrapper">
<div class="inner">
<div class="box1"></div>
<div class="box2"></div>
</div>
</div>
You need to reset the margin using media query. Initially you have a negative margin but after 1180px it will be a positive one creating the unwanted space. You also don't need to set width using vw unit. Keeping the default width is enough:
.wrapper {
width: 100%;
position: relative;
background: #ccc;
}
.inner {
width: 1180px;
margin: 0 auto;
background: pink;
}
.box1 {
height: 50px;
background: red;
}
.box2 {
height: 50px;
background: green;
margin-left: calc(-100vw/2 + 100%/2);
margin-right: calc(-100vw/2 + 100%/2);
}
#media all and (max-width:1180px) {
.box2 {
margin:0;
}
}
<div class="wrapper">
<div class="inner">
<div class="box1"></div>
<div class="box2"></div>
</div>
</div>
You could use negative margin - the only problem with this approach is that if the page gets a vertical scroll, this will add a horizontal scroll as 100vw doesn't take into account the 20px caused by the vertical scroll:
body {
margin: 0;
}
.wrapper {
width: 100%;
position: relative;
background: #ccc;
}
.inner {
width: 1180px;
margin: 0 auto;
background: pink;
}
.box1 {
height: 50px;
background: red;
}
.box2 {
height: 50px;
background: green;
width: 100%;
}
#media screen and (min-width:1180px) {
.box2 {
margin: 0 calc(((100vw - 1180px) / 2) * -1);
width: auto;
}
}
<div class="wrapper">
<div class="inner">
<div class="box1"></div>
<div class="box2"></div>
</div>
</div>
As I say in my comments, it would be better to just move the green div outside your wrapper
.wrapper {
width: 100%;
position: relative;
background: #ccc;
}
.inner {
width: 1180px;
margin: 0 auto;
}
.box1 {
height: 50px;
background: red;
}
.box2 {
height: 50px;
background: green;
}
<div class="wrapper">
<div class="inner">
<div class="box1"></div>
<div class="box2"></div>
</div>
</div>
Try this:
.wrapper {
width: 100%;
position: relative;
background: #ccc;
}
.inner {
width: 1180px;
margin: 0 auto;
background: pink;
}
.box1 {
height: 50px;
background: red;
}
.box2 {
height: 50px;
background: green;
width: 100%;
}
<div class="wrapper">
<div class="inner">
<div class="box1"></div>
<div class="box2"></div>
</div>
</div>
So my problem is that the parent has some width and if the screen cant fit everything the children (floated left) go one under the other (looks awesome as well) so I want them when they go one under the other to be centered in the parent.
Here is my code. I tried inline-block and it didn't help and so on
.mainpage-articles {
float:left;
width: 60%;
}
.mainpage-article {
width: calc(800px + 8%);
margin-left: auto;
margin-right: auto;
}
.mainpage-article .thumbnail {
position: relative;
height: 200px;
width: 400px;
margin-left: auto;
margin-right: auto;
padding: 2%;
float: left;
}
.mainpage-article .thumbnail img {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.mainpage-article .article {
height: 200px;
width: 400px;
margin-left: auto;
margin-right: auto;
padding: 2%;
float: left;
}
.mainpage-article .article h1 {
height: 60px;
}
.mainpage-article .article p {
height: 120px;
}
You're probably wanting to learn media queries, to respond to the user's screen. Am I right? If it's that, take a look here: https://codepen.io/giovannipds/pen/BwqyLW
This is what you want to learn:
#media (max-width: 1500px) {
.mainpage-articles {
text-align: center;
}
}
This code align text things to the center when the user window's is at max 1500px, above that it doesn't. There are many ways to apply media queries. I recommend you to watch this to learn it a little better.
Full code of the example above:
<style>
.mainpage-articles {
background-color: #eee;
float: left;
width: 60%;
}
.mainpage-article {
background-color: cyan;
width: calc(800px + 8%);
margin-left: auto;
margin-right: auto;
}
.mainpage-article .thumbnail {
background-color: yellow;
position: relative;
height: 200px;
width: 400px;
margin-left: auto;
margin-right: auto;
padding: 2%;
float: left;
}
.mainpage-article .thumbnail img {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.mainpage-article .article {
background-color: #ccc;
height: 200px;
width: 400px;
margin-left: auto;
margin-right: auto;
padding: 2%;
float: left;
}
.mainpage-article .article h1 {
height: 60px;
}
.mainpage-article .article p {
height: 120px;
}
#media (max-width: 1500px) {
.mainpage-articles {
text-align: center;
}
}
</style>
<div class="mainpage-articles">
<div class="mainpage-article">
<div class="thumbnail">
<img src="//lorempixel.com/400/200" alt="">
</div>
<div class="article">
<h1>Lorem ipsum</h1>
<p>Dolor sit amet.</p>
</div>
</div>
</div>
There's an example here with Bootstrap 4 too:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<style>
.parent {
background-color: yellow;
}
.smth-else {
background-color: cyan;
}
.children {
margin: 75px !important;
}
.children [class^=col-] {
border: 1px solid red;
}
</style>
<div class="container-fluid">
<div class="row">
<div class="parent col">
<div class="children row">
<div class="col-lg-6">
Children col 1
</div>
<div class="col-lg-6">
Children col 2
</div>
</div>
</div>
<div class="smth-else col">
Smth else
</div>
</div>
</div>
Your code is not so pretty, you should adapt them to use media queries better.
I have the following simple html code.
.body-content {
width: 100%;
padding-left: 15px;
padding-right: 15px;
}
.left-menu {
background-color: red;
float: left;
width: 50px;
}
.right-container {
background-color: blue;
width: 100%;
}
.middle-view {
width: 70%;
float: left;
background-color: blueviolet;
}
.right-view {
width: 30%;
float: left;
background-color: burlywood;
}
<div class="body-content">
<div class="left-menu">
abc
</div>
<div class="right-container">
<div class="middle-view">
def
</div>
<div class="right-view">
ghi
</div>
</div>
</div>
I am getting the following result:
But I would like to 'def' and 'ghi' side by side.
I don't have much experience using HTML and CSS but I thought middle-view and right-view together will fill right-container (70% + 30%). But as I see the width given to left-menu (50px) has impact on it.
Here is the solution..
.body-content {
width: 100%;
padding-left: 15px;
padding-right: 15px;
float:left;
}
.left-menu {
background-color: red;
float: left;
width: 50px;
}
.right-container {
background-color: blue;
width:calc(100% - 50px);
float:left;
}
.middle-view {
width: 70%;
float: left;
background-color: blueviolet;
}
.right-view {
width: 30%;
float: left;
background-color: burlywood;
}
<div class="body-content">
<div class="left-menu">
abc
</div>
<div class="right-container">
<div class="middle-view">
def
</div>
<div class="right-view">
ghi
</div>
</div>
</div>
.body-content {
display: flex; /* forces children to same row */
padding-left: 15px;
padding-right: 15px;
}
.left-menu {
width: 50px;
background-color: red;
}
.right-container {
display: flex; /* forces children to same row */
flex: 1; /* consume remaining space on the row */
background-color: blue;
}
.middle-view {
width: 70%;
background-color: blueviolet;
}
.right-view {
width: 30%;
background-color: burlywood;
}
<div class="body-content">
<div class="left-menu">abc</div>
<div class="right-container">
<div class="middle-view">def</div>
<div class="right-view">ghi</div>
</div>
</div>
I'm trying to put 3 divs(with different widths respectively : 10%,70% & 20%) in the same row but the middle one always go full width of the page.
Here is my code:
#left-bar {
width: 10%;
background-color: #FF0000;
}
#middle-bar {
width: 70%;
background-color: #6600FF;
}
#right-bar {
width: 20%;
background-color: #99FF99;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
By default div is a block level element that's why they aren't in the same row.
You have a few options to fix this:
option with CSS flexbox:
.row {
display: flex;
width: 100%
}
.row>div {
/*demo purposes */
height: 30px;
}
#left-bar {
flex: 0 10%;
background-color: #F00;
}
#middle-bar {
flex: 1;
background-color: #60F;
}
#right-bar {
flex: 0 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
(old options)
option with display:inline-block
.row {
/*fix inline-block gap*/
font-size: 0;
}
.row>div {
display: inline-block;
/*demo purposes */
height: 30px;
}
#left-bar {
width: 10%;
background-color: #F00;
}
#middle-bar {
width: 70%;
background-color: #60F;
}
#right-bar {
width: 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
option with display:table-[cell]
.row {
display: table;
width: 100%
}
.row>div {
display: table-cell;
/*demo purposes */
height: 30px;
}
#left-bar {
width: 10%;
background-color: #F00;
}
#middle-bar {
width: 70%;
background-color: #60F;
}
#right-bar {
width: 20%;
background-color: #9F9;
}
<div class="row">
<div id="left-bar"></div>
<div id="middle-bar"></div>
<div id="right-bar"></div>
</div>
The table-cell option actually doesn't work in some internet explorer versions. But the same result can be achieved with the property float:
#left-bar{
width:10%;
background-color: #FF0000;
}
#middle-bar{
width:70%;
background-color: #6600FF;
}
#right-bar{
width:20%;
background-color: #99FF99;
}
.row > div {float:left;}
<div class="row">
<div id="left-bar">a</div>
<div id="middle-bar">b</div>
<div id="right-bar">c</div>
</div>
#left-bar{
width:10%;
background-color: #FF0000;
float:left;
}
#middle-bar{
width:70%;
background-color: #6600FF;
float:left;
}
#right-bar{
width:20%;
background-color: #99FF99;
float:left;
}
If that doesn't work, please provide more html and css because the problem will be somewhere else. Also, verify that you have heights set for your divs.
I've tried to add a display:table to a parent element (rowcontainer) and display:table-cell; to the child element (div1, div2) for over 500px width on screen. This worked, but the padding on the child elements now have to left or right padding, and the bottom row has something off with it, any ideas on how to fix this? :
Here is also a codepen of the problem:
http://codepen.io/anon/pen/MYxegN
#media screen and (max-width: 499px) {
.div1,
.div2 {
color: blue;
}
}
#pagewrap {
padding: 10px;
background-color: rgba(21, 21, 21, 0.75);
border: 1px red solid;
max-width: 1024px;
min-height: 87.5% height: inherit;
margin: 0 auto;
}
h1,
h2 {
color: white;
}
p {
color: silver;
}
#tablecontainer {
padding: 5px 5px 5px 0px;
width: 100%;
min-height: 100%;
color: white;
font-size: 20px;
}
.div1,
.div2 {
padding: 5px;
margin: 5px;
}
.div1 {
background-color: darkgreen;
padding: 5px;
}
.div2 {
background-color: green;
}
#media screen and (min-width: 500px) {
.rowcontainer {
padding: 2.5px;
display: table;
width: 100%;
}
.rowcontainer div {
display: table-cell;
}
.div1 {
padding: 1.25px;
}
}
<div id="pagewrap">
<div id="tablecontainer">
<div class="rowcontainer">
<div class="div1">1</div>
<div class="div2">2</div>
</div>
<div class="rowcontainer">
<div class="div1">3</div>
<div class="div2">4</div>
</div>
<div class="rowcontainer">
<div class="div1">5</div>
<div class="div2">6</div>
</div>
<div class="rowcontainer">
<div class="div1">7</div>
<div class="div2">9</div>
</div>
<div class="rowcontainer">
<div class="div1">9</div>
<div class="div2">10</div>
</div>
</div>
I think the correct structure is like this for CSS table.
#tablecontainer {
display:table; /*behaves like <table>*/
}
.rowcontainer {
display:table-row; /*behaves like <tr>*/
}
.rowcontainer div {
display:table-cell; /*behaves like <td>*/
}
As far as I can tell, all that is needed is to add
table-layout:fixed
in the media query
#media screen and (min-width: 500px){
.rowcontainer {
padding:2.5px;
display:table;
table-layout: fixed;
border-spacing: 5px 0;
border-collapse: separate;
width:100%;
}
}
Codepen Updated.
#media screen and (max-width: 499px) {
.div1,
.div2 {
color: blue;
}
}
html {
background: url(img/nature.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
#pagewrap {
padding: 10px;
background-color: rgba(21, 21, 21, 0.75);
border: 1px red solid;
max-width: 1024px;
min-height: 87.5% height: inherit;
margin: 0 auto;
}
h1,
h2 {
color: white;
}
p {
color: silver;
}
#tablecontainer {
padding: 5px 5px 5px 0px;
width: 100%;
min-height: 100%;
color: white;
font-size: 20px;
}
.div1,
.div2 {
padding: 5px;
margin: 5px;
}
.div1 {
background-color: darkgreen;
padding: 5px;
}
.div2 {
background-color: green;
}
#media screen and (min-width: 500px) {
.rowcontainer {
padding: 2.5px;
display: table;
table-layout: fixed;
width: 100%;
}
.rowcontainer div {
display: table-cell;
}
.div1 {
padding: 1.25px;
}
}
<div id="pagewrap">
<div id="tablecontainer">
<div class="rowcontainer">
<div class="div1">1</div>
<div class="div2">2</div>
</div>
<div class="rowcontainer">
<div class="div1">3</div>
<div class="div2">4</div>
</div>
<div class="rowcontainer">
<div class="div1">5</div>
<div class="div2">6</div>
</div>
<div class="rowcontainer">
<div class="div1">7</div>
<div class="div2">9</div>
</div>
<div class="rowcontainer">
<div class="div1">9</div>
<div class="div2">10</div>
</div>
</div>
Are you looking for something like this?
#media screen and (max-width: 499px) {
.div1,
.div2 {
color: blue;
}
}
#pagewrap {
padding: 10px;
background-color: rgba(21, 21, 21, 0.75);
border: 1px red solid;
max-width: 1024px;
min-height: 87.5% height: inherit;
margin: 0 auto;
}
h1,
h2 {
color: white;
}
p {
color: silver;
}
#tablecontainer {
padding: 5px 5px 5px 0px;
width: 100%;
min-height: 100%;
color: white;
font-size: 20px;
display: table;
border-spacing: 5px;
}
.div1,
.div2 {
padding: 5px;
margin: 5px;
}
.div1 {
background-color: darkgreen;
padding: 5px;
}
.div2 {
background-color: green;
}
#media screen and (min-width: 500px) {
.rowcontainer {
padding: 2.5px;
display: table-row;
}
.rowcontainer div {
display: table-cell;
}
.div1 {
padding: 1.25px;
}
}
<div id="pagewrap">
<div id="tablecontainer">
<div class="rowcontainer">
<div class="div1">1</div>
<div class="div2">2</div>
</div>
<div class="rowcontainer">
<div class="div1">3</div>
<div class="div2">4</div>
</div>
<div class="rowcontainer">
<div class="div1">5</div>
<div class="div2">6</div>
</div>
<div class="rowcontainer">
<div class="div1">7</div>
<div class="div2">9</div>
</div>
<div class="rowcontainer">
<div class="div1">9</div>
<div class="div2">10</div>
</div>
</div>
You are almost correct in making a table with div. The above problem is because of the text. (Last cell has 2 digit text, if you make it one digit it works fine ). To fix this issue use below code. In the rowcontainer just add width.
.rowcontainer div{
display:table-cell;
width:50%
}
Your complete code can be seen on http://codepen.io/gauravshankar/pen/OPqXga