Two responsive/flexible divs side by side - css

I have two responsive divs side by side.
When screen size is smaller than 600px wide, those two divs rearrange one on top of the other, like they should do, BUT... I can't get those two divs to be flexible and 100% wide when screen size gets smaller than 600px.
I've tried flexbox and many other things but just can't get divs flexible. Anyone knows?
body
{
background-color: #fcfcfc;
}
.columns
{
text-align: center;
padding-right: 15px;
padding-left: 15px;
padding-top: 0;
}
.left-div
{
display: inline-block;
max-width: 400px;
text-align: left;
padding: 10px;
background-color: #ddd;
border-radius: 3px;
margin: 5px;
vertical-align: top;
}
.right-div
{
display: inline-block;
max-width: 400px;
text-align: left;
padding: 10px;
background-color: #ddd;
border-radius: 3px;
margin: 5px;
}
.left-text, .right-text
{ text-align:justify;
}
#media screen and (max-width: 600px)
{
.left-div, .right-div
{
max-width: 100%;
}
}
<div class="columns">
<div class="left-div left-text">
<p> Lorem ipsum LEFT.</p>
</div>
<div class="right-div right-text">
<p> Lorem ipsum RIGHT.</p>
</div>
</div>

You need to also make width:100% in addition to specifying the max-width:100%. You also need to add box-sizing:border-box; to avoid some overflow due to the use of padding.
Check the full code, i used 800px instead of 600px so we can see the result here.
body {
background-color: #fcfcfc;
}
.columns {
text-align: center;
padding-right: 15px;
padding-left: 15px;
padding-top: 0;
}
.left-div {
display: inline-block;
max-width: 400px;
text-align: left;
padding: 10px;
background-color: #ddd;
border-radius: 3px;
margin: 5px;
vertical-align: top;
}
.right-div {
display: inline-block;
max-width: 400px;
text-align: left;
padding: 10px;
background-color: #ddd;
border-radius: 3px;
margin: 5px;
}
.left-text,
.right-text {
text-align: justify;
}
#media screen and (max-width: 800px) {
.left-div,
.right-div {
max-width: 100%;
width: 100%;
box-sizing: border-box;
}
}
<div class="columns">
<div class="left-div left-text">
<p> Lorem ipsum LEFT.</p>
</div>
<div class="right-div right-text">
<p> Lorem ipsum RIGHT.</p>
</div>
</div>

Display your elements as block so that they can occupy the full horizontal width available, e.g: display: block;
And for good measure, declare width: 100%
#media screen and (max-width: 600px) {
.left-div,
.right-div {
max-width: 100%;
/* Additional */
width: 100%;
display: block;
}
}
body {
background-color: #fcfcfc;
}
.columns {
text-align: center;
padding-right: 15px;
padding-left: 15px;
padding-top: 0;
}
.left-div {
display: inline-block;
max-width: 400px;
text-align: left;
padding: 10px;
background-color: #ddd;
border-radius: 3px;
margin: 5px;
vertical-align: top;
}
.right-div {
display: inline-block;
max-width: 400px;
text-align: left;
padding: 10px;
background-color: #ddd;
border-radius: 3px;
margin: 5px;
}
.left-text,
.right-text {
text-align: justify;
}
#media screen and (max-width: 600px) {
.left-div,
.right-div {
max-width: 100%;
/* Additional */
width: 100%;
display: block;
}
}
<div class="columns">
<div class="left-div left-text">
<p> Lorem ipsum LEFT.</p>
</div>
<div class="right-div right-text">
<p> Lorem ipsum RIGHT.</p>
</div>
</div>
Or you could "Just use Flex"...
#media screen and (max-width: 600px) {
.columns {
display: flex;
justify-content: center;
flex-direction: column;
}
.left-div,
.right-div {
max-width: 100%;
}
}
body {
background-color: #fcfcfc;
}
.columns {
text-align: center;
padding-right: 15px;
padding-left: 15px;
padding-top: 0;
}
.left-div {
display: inline-block;
max-width: 400px;
text-align: left;
padding: 10px;
background-color: #ddd;
border-radius: 3px;
margin: 5px;
vertical-align: top;
}
.right-div {
display: inline-block;
max-width: 400px;
text-align: left;
padding: 10px;
background-color: #ddd;
border-radius: 3px;
margin: 5px;
}
.left-text,
.right-text {
text-align: justify;
}
#media screen and (max-width: 600px) {
.columns {
display: flex;
justify-content: center;
flex-direction: column;
}
.left-div,
.right-div {
max-width: 100%;
}
}
<div class="columns">
<div class="left-div left-text">
<p> Lorem ipsum LEFT.</p>
</div>
<div class="right-div right-text">
<p> Lorem ipsum RIGHT.</p>
</div>
</div>
Heads up! flex-box has poor or limited support for legacy browsers, so if this is going to be a concern for you, it's probably better not to use it in production.
IE <= 9 - Not Supported
IE 10,11 - Partial Support
See more: https://caniuse.com/#feat=flexbox
Edit: As #TemaniAfif has pointed out in the comments, you should set a border-box property as well, e.g: box-sizing: border-box
.left-text,
.right-text {
text-align: justify;
box-sizing: border-box;
}
Typically, this is set as a global rule, using a global selector, e.g: * { box-sizing: border-box; }
border-box tells the browser to account for any border and padding
in the value you specify for width and height. If you set an element's
width to 100 pixels, that 100 pixels will include any border or
padding you added, and the content box will shrink to absorb that
extra width. This typically makes it much easier to size elements.
Want to learn more? https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

Related

How to align columns to center on smaller screens using inline-block (responsive)

My row with columns works great, but on smaller screens it is aligned to left, which should be centered. I have tried hundreds tutorials without any positive results. It must be simple, thx in advance for any help :)
HTML:
<div class="container">
<div id="offer" class="row">
<div class="column">
<div class="box">
</div>
</div>
<div class="column">
<div class="box">
</div>
</div>
<div class="column">
</div>
</div>
</div>
</div>
CSS:
/* Offer start Section*/
.container {
text-align:center;
margin:0 auto;
}
.container .row {
display:inline-block;
vertical-align: middle;
float: none;
}
.column {
width: 33.33%;
padding-right: 0;
padding-left: 0;
margin: -10px;
}
#media screen and (max-width: 767.98px) {
.column {
width: 100%!important;
}
}
#media screen and (max-width: 991.98px) {
.column {
width: 50%;
}
}
.row {
text-align:center;
margin:0 auto;
}
.row .column {
display:inline-block;
vertical-align: middle;
float: none;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
.box {
width: 300px;
padding: 10px;
margin: 30px;
box-shadow: 0px 0px 10px 1px rgba(136, 136, 136, 0.226);
border-radius: 30px 30px 30px 30px;
border: 0px solid #fff;
text-align: center;
}
I was trying to do it as a grid, inline-grid and other different styles. Inline-block works perfect, but not responsive. Probably the solution is simple. THX!
I'm not sure if you really do need to change your row to get the result you are after try the following: https://jsfiddle.net/s4qkgjeL/1/
The only thing to change was adding position: relative and display: inline-block to .box because you already have a width and a margin set for this class adding the two mentioned properties will ensure they are always centre aligned.
/* Offer start Section*/
.container {
text-align:center;
margin:0 auto;
}
.container .row {
display:inline-block;
vertical-align: middle;
float: none;
}
.column {
width: 33.33%;
padding-right: 0;
padding-left: 0;
margin: -10px;
}
#media screen and (max-width: 767.98px) {
.column {
width: 100%!important;
}
}
#media screen and (max-width: 991.98px) {
.column {
width: 50%;
}
}
.row {
text-align:center;
margin:0 auto;
}
.row .column {
display:inline-block;
vertical-align: middle;
float: none;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
.box {
position: relative;
display: inline-block;
width: 300px;
padding: 10px;
margin: 30px;
box-shadow: 0px 0px 10px 1px rgba(136, 136, 136, 0.226);
border-radius: 30px 30px 30px 30px;
border: 0px solid #fff;
text-align: center;
}
<div class="container">
<div id="offer" class="row">
<div class="column">
<div class="box"></div>
</div>
<div class="column">
<div class="box"></div>
</div>
<div class="column">
<div class="box"></div>
</div>
</div>
</div>

Vertically Centered, 2-Col Responsive Content Overflowing

I'm trying to set up 2-columns that are responsive (e.g., collapse with one on top of the other). The left column has text and the right has some buttons.
I want both to center align with each other vertically within the container. However, when I test for responsiveness, my buttons overflow out of the container instead of wrapping with the 2nd column:
.boxContent {
max-width: 1024px;
margin-left: auto;
margin-right: auto;
padding: 20px;
display: flex;
flex-direction: row;
align-items: center;
}
.boxCol {
flex: 0 0 50%;
box-sizing: border-box;
padding-right: 75px;
}
a.boxBtn {
display: block;
padding: 0.35em 1.2em;
border: 0.1em solid #1e1e1e;
margin: 0 0.3em 0.3em 0;
border-radius: 2%;
box-sizing: border-box;
text-decoration: none;
background-color: #fff;
text-align: left;
transition: all 0.2s;
}
.boxBtn:hover {
color: #000;
background-color: #e5bc73;
cursor: pointer;
}
#media all and (max-width: 30em) {
a.boxBtn {
margin: 0.4em auto;
}
}
#media screen and (max-width: 800px) {
.boxCol {
flex: 0 1 100%;
padding-right: 0;
padding-bottom: 75px;
}
}
<div class="Container">
<div class="boxContent">
<div class="boxCol">
<h1>Heading</h1>
</div>
<div class="boxCol">
<a class="boxBtn">
Button 1
</a>
<a class="boxBtn">
Button 2
</a>
<a class="boxBtn">
Button 3
</a>
</div>
</div>
</div>
from my comment :
you need either to reset flex-direction or allow wrapping on smaller screen. Your media queries do not reset any of them rules
If it is only to pile them without reordering purpose, display reset is good enough ,
possible example:
.boxContent {
max-width: 1024px;
margin-left: auto;
margin-right: auto;
padding: 20px;
display: flex;
flex-direction: row;
align-items: center;
}
.boxCol {
flex: 0 0 50%;
box-sizing: border-box;
padding-right: 75px;
}
a.boxBtn {
display: block;
padding: 0.35em 1.2em;
border: 0.1em solid #1e1e1e;
margin: 0 0.3em 0.3em 0;
border-radius: 2%;
box-sizing: border-box;
text-decoration: none;
background-color: #fff;
text-align: left;
transition: all 0.2s;
}
.boxBtn:hover {
color: #000;
background-color: #e5bc73;
cursor: pointer;
}
#media all and (max-width: 30em) {
a.boxBtn {
margin: 0.4em auto;
}
}
#media screen and (max-width: 800px) {
.boxContent {display:block;}
}
<div class="Container">
<div class="boxContent">
<div class="boxCol">
<h1>Heading</h1>
</div>
<div class="boxCol">
<a class="boxBtn">
Button 1
</a>
<a class="boxBtn">
Button 2
</a>
<a class="boxBtn">
Button 3
</a>
</div>
</div>
</div>
Tip: another approach would be to trigger the flex display at min-width:800px only, before no need for a 1 column layout.

divs positioning for a chat message

I want to create chat component in such a way that if I display all its elements it will look like the following:
and if I remove most of the elements, the message will be shown in this way:
However my code has 4 problems:
the picture and the bubble element are not next to each other
the speech bubble do not get shorter with less text;
the text in the bubble doesn't centre vertically on its parent div, rather on the upper parent container;
the day-time element stay fix on the right, rather than being aligned to the right side of the speech-bubble.
I tried many options, but the more I touch the CSS, the more issues I create.
Any suggestion on how to achieve the desired result?
This is my code:
.container {
position: relative;
margin-left: 5%;
max-width: 90%;
height: auto;
}
.upper-text {
width: 100%;
color: black;
font-size: 12px;
line-height: 120%;
}
.message-container {
width: auto;
}
.character-picture {
width: 30px;
height: 30px;
background-color: aqua;
display: inline-block;
}
.buble-wrapper {
max-width: 75%;
height: auto;
}
.speech-bubble {
background-color: #f0f0f0;
border-radius: 0 20px 20px 20px;
width: auto;
min-height: 40px;
}
.message {
color: black;
font-size: 16px;
padding-left: 4%;
padding-right: 4%;
top: 50%;
transform: translateY(-50%);
}
.lower-tex {
color: black;
font-size: 12px;
line-height: 120%;
text-align: right;
}
<div class="container">
<div class="upper-text">Name</div>
<div class="message-container">
<div class="character-picture"></div>
<div class="buble-wrapper">
<div class="speech-bubble">
<div class="message">text message</div>
</div>
</div>
</div>
<div class="lower-tex">Monday: 20:38</div>
</div>
This should do what you're looking for. There is a lot of room for improvement, but this will get you started.
I've removed some of the html you had that I thought unnecessary and changed how you size and place some elements.
I suggest you take a look at CSS Flexbox and Grid, they allow you to do complex layouts like this in a breeze.
.container {
width: auto;
max-width: 250px;
display: inline-flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-start;
}
.upper-text {
color: black;
font-size: 12px;
align-self: flex-start;
line-height: 1.5;
}
.message-container {
display: flex;
justify-content: flex-start;
align-items: flex-start;
}
.character-picture {
width: 30px;
height: 30px;
background-color: aqua;
margin-right: 10px;
border-radius: 15px;
flex-shrink: 0;
}
.message {
color: black;
font-size: 16px;
word-wrap: break-word;
padding: 5px 10px;
border-radius: 15px;
background-color: #ccc;
display: flex;
align-items: center;
}
.lower-tex {
color: black;
font-size: 12px;
align-self: flex-end;
line-height: 2.5;
}
<div class="container">
<div class="upper-text">Name</div>
<div class="message-container">
<div class="character-picture"></div>
<div class="message">text message example wow this text wraps very well</div>
</div>
<div class="lower-tex">Monday: 20:38</div>
</div>
As mentioned by DoHn, you can achieve this using flex or grid.
Below is the solution using grid.
Read more here complete-guide-grid
For browser support, checkout https://caniuse.com/#search=grid
.container {
display: grid;
grid-template-columns: 30px auto;
grid-gap: 2px 4px;
width: 360px;
}
.upper-text {
color: black;
font-size: 12px;
line-height: 120%;
grid-column: 1 / 3;
grid-row: 1;
}
.message-container {
width: auto;
}
.character-picture {
width: 30px;
height: 30px;
background-color: aqua;
border-radius: 50%;
grid-row: 2;
}
.speech-bubble {
background-color: #f0f0f0;
border-radius: 0 20px 20px 20px;
grid-column: 2 / 3;
grid-row: 2;
justify-self: stretch;
display: grid;
padding: 10px;
}
.message {
margin: auto 0;
}
.lower-text {
color: black;
font-size: 12px;
line-height: 120%;
justify-self: end;
grid-column: 2 / 3;
grid-row: 3;
}
<div class="container">
<div class="upper-text">Name</div>
<div class="character-picture"></div>
<div class="speech-bubble">
<div class="message">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley o</div>
</div>
<div class="lower-text">Monday: 20:38</div>
</div>
.container {
width: 500px;
height: 100px;
padding: 10px;
}
.container .chat-log {
width: 500px;
height: 100px;
margin-top: 20px;
}
.left-side {
width: 20%;
height: auto;
float: left;
}
.right-side {
width: 80%;
height: auto;
position: relative;
left: -15px;
float: right;
}
.text {
font-size: 14px;
text-align: center;
}
.thumbnail {
width: 50px;
height: 50px;
background: #000;
border-radius: 50%;
clear: both;
margin: 0 auto;
position: relative;
}
.thumbnail span.caption {
color: #fff;
text-align: center;
position: relative;
margin: 0;
padding: 0;
top: 30%;
left: 10%;
}
.msg {
position: relative;
width: auto;
height: auto;
text-align: justify;
padding: 20px;
background-color: #f0f0f0;
border-radius: 0px 20px 20px;
}
.msg::before {
content: " ";
position: absolute;
bottom: 79%;
transform: rotate(90deg);
left: -20px;
border-width: 10px;
border-style: solid;
border-color: #f0f0f0 transparent transparent transparent;
}
.time{
float: right;
margin-right:10px;
font-size: 12px;
}
<div class="container">
<div class="chat-log">
<div class="left-side">
<div class="text">
Name
</div>
<div class="thumbnail">
<span class="caption">image</span>
</div>
</div>
<div class="right-side">
<div class="msg">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum tempore inventore architecto a voluptatum ratione, sunt doloribus nobis iure debitis? Veniam.
</div>
<div class="time">Monday 5:13 PM</div>
</div>
</div>
</div>
I hope this will help you. I do not use flexbox or grid I have made it simple.

why does my column go down?

http://codepen.io/anon/pen/OMLLwB
#news {
width: 85%;
margin: 0 auto;
}
#news ul {
list-style: none;
margin: 0;
padding: 0;
}
#worldMap img {
width: 100%;
}
.newspiece {
margin-bottom: 2.5%;
padding: 20px;
background-color: #90C3D4;
height: 130px;
}
.newspiece h3 {
border-bottom: 3px solid black;
margin-bottom: 10px;
}
#media(min-width: 600px) {
.newspiece {
width: 25%;
margin-left: 5%;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
.newspiece:first-child {
margin-left:0;
}
}
Am i missing something here? the width the total container (#news) is 85%, the width of each item is 25%, and two of them have a 5% left margin, total sums to 85%, then why do i resize it, the rightmost column goes down?
i have changed your html/css. this is a cleaner solution and is suported among all browsers
html:
<div class="flex">
<div class="box">
<h3>Title</h3>
<p>Content</p>
</div>
<div class="box">
<h3>Title</h3>
<img src="http://www.placecage.com/400/300" alt="">
</div>
<div class="box">
<h3>Title</h3>
<p>Content</p>
</div>
</div>
css:
.flex {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.box {
width: 300px;
margin: 10px;
padding: 20px;
background: #90C3D4;
}
.box h3 {
padding-bottom: 5px;
border-bottom: 3px solid black;
}
.box img {
max-width: 100%;
}
* { box-sizing: border-box; }
The padding adds to the total width of the element if box-sizing: border-box is not used.

Vertical align (inline, middle) divs with image and text

I just want to align two divs next to each other and aligning the content vertically middle in each. Any help could save my mental health. Here is my code:
.main-kozossegitag-container {
display: block;
width: 100%;
height: 100%;
}
.main-kozossegitag-text1 {
display: inline-block;
width: 60%;
height: 100%;
vertical-align: middle;
text-align: right;
}
.main-kozossegitag-nev {
font-size: 2em;
}
.main-kozossegitag-title {
font-size: 1em;
}
.main-kozossegitag-visszhang {
font-size: 1em;
}
.main-kozossegitag-image1 {
display: inline-block;
width: 39%;
}
.profilkep {
max-width: 100%;
height: auto;
border-radius: 50%;
border: 3px solid rgba(255,255,255,0.5);
box-shadow: 1px 1px 5px rgba(0,0,0,0.3);
}
<div class="main-kozossegitag-container">
<div class="main-kozossegitag-text1">
<h3 class="main-kozossegitag-nev">Rita</h3>
<p class="main-kozossegitag-title">CEO</p>
<p class="main-kozossegitag-visszhang">Lorem ipsum dolor sit amet.</p>
</div>
<div class="main-kozossegitag-image1">
<img src="http://www.kaptarcoworking.hu/wp-content/uploads/2015/07/szabo_rita.jpg" alt="Szabó Rita" class="profilkep">
</div>
</div>
As you see the two divs are next to each other, but I can not align the text vertically in the middle :(
.main-kozossegitag-container {
display: table;
width: 100%;
height: 100%;
}
.main-kozossegitag-text1 {
display: table-cell;
vertical-align : middle;
width: 60%;
height: 100%;
vertical-align: middle;
text-align: right;
}
.main-kozossegitag-nev {
font-size: 2em;
}
.main-kozossegitag-title {
font-size: 1em;
}
.main-kozossegitag-visszhang {
font-size: 1em;
}
.main-kozossegitag-image1 {
display: table-cell;
vertical-align : middle;
width: 39%;
}
.profilkep {
max-width: 100%;
height: auto;
border-radius: 50%;
border: 3px solid rgba(255,255,255,0.5);
box-shadow: 1px 1px 5px rgba(0,0,0,0.3);
}
<div class="main-kozossegitag-container">
<div class="main-kozossegitag-text1">
<h3 class="main-kozossegitag-nev">Rita</h3>
<p class="main-kozossegitag-title">CEO</p>
<p class="main-kozossegitag-visszhang">Lorem ipsum dolor sit amet.</p>
</div>
<div class="main-kozossegitag-image1">
<img src="http://www.kaptarcoworking.hu/wp-content/uploads/2015/07/szabo_rita.jpg" alt="Szabó Rita" class="profilkep">
</div>
</div>
I added display : table; to your main container (main-kozossegitag-text1) and display : table-cell; vertical-align : middle to your both sub-div. Those properties let them have the same behaviour as a table and cells that contains vertically aligned content. I just messed up with some left/right margin but the rest seems to work.
Add Vertical-align to the second Div.
.main-kozossegitag-image1 {
display: inline-block;
width: 39%;
vertical-align: middle;
}
Please see my fiddle here.
I used CSS3 flex-box model. I changed properties for these classes:
.main-kozossegitag-container {
display: flex;
align-items: stretch;
flex-flow: row wrap;
width: 100%;
height: 100%;
border: 1px solid red;
}
.main-kozossegitag-text1 {
display: flex;
flex-flow: column wrap;
justify-content: center;
align-items: flex-end;
align-content: flex-end;
box-sizing: border-box;
width: 60%;
padding-right: 20px;
}
More on CSS3 flex-box here.
Just add a float-left to this div :
.main-kozossegitag-text1{
display: table-cell;
vertical-align : middle;
width: 60%;
height: 100%;
vertical-align: middle;
text-align: right;
float: left;
}

Resources