responsive grid: different column widths - css

was wondering if the following is possible using grid or flexbox.
I got a 3-column layout that I want to make responsive. Now, on the one hand I can use a media query to lets say make all 3 columns full width on <640px screens, but what if I want the first 2 to be 50/50 and the third one full width?
A possible solution could be to use a combination of a media query and :last-child, but I wonder if this can be done without the use of media queries?

Desktop
Mobile <641
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel="stylesheet" href="">
<style type="text/css" media="screen">
body,
html {
padding: 0;
margin: 0;
}
.bg-color {
background-color: #ccc;
}
.bg-color1 {
background: #817e7e;
}
/* This is for three box Wrapper */
.my-row {
display: flex;
flex-flow: row wrap;
}
/* This is for three box */
.my-col {
flex: 0 0 33.333%;
width: 33.333%;
}
#media (max-width: 641px) {
.my-col {
flex: 0 0 100%;
width: 100%;
}
}
</style>
</head>
<body>
<div class="my-row">
<div class="my-col bg-color">
<h1>Col 1</h1>
</div>
<div class="my-col bg-color1">
<h1>Col 2</h1>
</div>
<div class="my-col bg-color">
<h1>Col 3</h1>
</div>
</div>
</body>
</html>

Related

How to directly align elements under each other using flex direction?

As you can see in the code snippets below when the size of the web page reaches 776px it becomes column like aligned but it isn't directly aligned under each other. I have added the align-items property, How do I directly align the elements under each other?
.banner-content-container {
display: flex;
justify-content: space-evenly;
padding-top: 200px;
}
#media screen and (max-width:776px) {
.banner-content-container {
align-items:center;
flex-direction: column;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="banner-content-container">
<div class="content contexts1">
<h3>01. Mobile conductive</h3>
<p>Smart optimization of RWD design.</p>
</div>
<div class="content contexts2">
<h3>02. User Interface</h3>
<p>Brilliant UI/UX creative designs.</p>
</div>
<div class="content contexts3">
<h3>03. Affordable</h3>
<p>Our offers dont break the bank.</p>
</div>
</div>
</body>
</html>
You could use the align-text attribute in this case:
.banner-content-container {
display: flex;
justify-content: space-evenly;
padding-top: 200px;
}
#media screen and (max-width:776px) {
.banner-content-container {
text-align: center; /* changed this line */
flex-direction: column;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="banner-content-container">
<div class="content contexts1">
<h3>01. Mobile conductive</h3>
<p>Smart optimization of RWD design.</p>
</div>
<div class="content contexts2">
<h3>02. User Interface</h3>
<p>Brilliant UI/UX creative designs.</p>
</div>
<div class="content contexts3">
<h3>03. Affordable</h3>
<p>Our offers dont break the bank.</p>
</div>
</div>
</body>
</html>
Or if you want them centered but justified on the left, you can set the width of the container to fit-content, then center the whole container by setting margin: 0 auto;:
.banner-content-container {
display: flex;
justify-content: space-evenly;
padding-top: 200px;
}
#media screen and (max-width:776px) {
.banner-content-container {
width: fit-content;
margin: 0 auto;
flex-direction: column;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="banner-content-container">
<div class="content contexts1">
<h3>01. Mobile conductive</h3>
<p>Smart optimization of RWD design.</p>
</div>
<div class="content contexts2">
<h3>02. User Interface</h3>
<p>Brilliant UI/UX creative designs.</p>
</div>
<div class="content contexts3">
<h3>03. Affordable</h3>
<p>Our offers dont break the bank.</p>
</div>
</div>
</body>
</html>
Setting a fixed width would solve this, but it can be problematic if your content size is variable.
As an alternative, you can add another container (.demo in this example) to avoid having to specific any width. Here we centre the content with the outer container, and left align the content in the new inner flex container:
.banner-content-container {
display: flex;
padding-top: 200px;
justify-content: center;
}
.demo {
display: flex;
flex-direction: column;
justify-content: space-evenly;
}
#media screen and (min-width:776px) {
.demo {
flex-direction: row;
width: 100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="banner-content-container">
<div class="demo">
<div class="content contexts1">
<h3>01. Mobile conductive</h3>
<p>Smart optimization of RWD design.</p>
</div>
<div class="content contexts2">
<h3>02. User Interface</h3>
<p>Brilliant UI/UX creative designs.</p>
</div>
<div class="content contexts3">
<h3>03. Affordable</h3>
<p>Our offers dont break the bank.</p>
</div>
</div>
</div>
</body>
</html>
The reason is that they are centered but have different width.
The quick fix for this is to set the same width when they are in flex column mode.
#media screen and (max-width:776px) {
.banner-content-container {
align-items: center;
flex-direction: column;
}
.content {
width: 250px;
}
}
display:flex;
flex-direction: column;
Should be together in your media query. Because by default, display:flex is a row.

border-collapse issue on elements (not table)

I have been trying to get the borders on elements that are touching to merge... I thought that border-collapse would be a straightforward solution to this... but apparently not, or I am misusing it.
Here's my code...
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Inter:400,800,900&display=swap" rel=stylesheet">
<link href="style.css" rel="stylesheet">
<title>Responsive Grid</title>
</head>
<body>
<div class="container">
<nav>Navbar</nav>
<main>Main</main>
<div id="sidebar">Sidebar</div>
<div id="content1">Content1</div>
<div id="content2">Content2</div>
<div id="content3">Content3</div>
<footer>Footer</footer>
</div>
</body>
</html>
style.css:
body {
box-sizing: border-box;
}
.container > * {
border:green 1px solid;
border-collapse:collapse;
}
.container {
display:grid;
height: 100vh;
grid-template-columns: 1fr 1fr 1fr 2fr;
}
I am still getting the appearance of 2px borders where elements meet.
Thanks for any help... I've looked around at a few threads but I'm not finding answer. Again Thanks!
This snippet works for the particular case (7 elements in a 4 column grid) given in the question by using the (simplistic) method of making various borders transparent so there is no doubling up:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Inter:400,800,900&display=swap" rel=stylesheet">
<link href="style.css" rel="stylesheet">
<style>
body {
box-sizing: border-box;
}
.container > * {
border:green 1px solid;
position: relative;
overflow: visible;
}
.container > *:nth-child(2), .container > *:nth-child(3), .container > *:nth-child(4), .container > *:nth-child(6), .container > *:nth-child(7) {
border-left-color: transparent;
}
.container > *:nth-child(5), .container > *:nth-child(6), .container > *:nth-child(7) {
border-top-color: transparent;
}
.container {
display:grid;
height: 100vh;
grid-template-columns: 1fr 1fr 1fr 2fr;
}
</style>
<title>Responsive Grid</title>
</head>
<body>
<div class="container">
<nav>Navbar</nav>
<main>Main</main>
<div id="sidebar">Sidebar</div>
<div id="content1">Content1</div>
<div id="content2">Content2</div>
<div id="content3">Content3</div>
<footer>Footer</footer>
</div>
</body>
</html>
More sophisticated attempts that would work in the general case- pseudo elements overwriting border of previous sibling element for example - only work in theory as the placing of single pixels can vary on different displays so results could be half a (CSS) pixel out for example.

Position items in a grid area

I'm making an editor app with a layout like google sheet or any simple web app with a thin, screenwide toolbar in the top and other stuff below. But... I'm having problem to position items in the toolbar. I plan to place:
a logo in the left corner
something in the middle
two buttons on the right side with specific order (like button A and B where B is closer to the right edge of the screen)
And most importantly i want to position these items inside the toolbar to be vertically in the center.
I tried so many things, but nothing really worked, except the very basic grid system below in which i want to place the items:
.layout {
height: 100vh;
display: grid;
grid-template-columns: 34% 33% auto;
grid-template-rows: 50px auto;
grid-template-areas:
"toolbar toolbar toolbar"
"searchbox resultbox editorbox";
}
.toolbar {
grid-area: toolbar;
}
.searchbox {
grid-area: searchbox;
}
.resultbox {
grid-area: resultbox;
}
.manju-editor {
grid-area: editorbox;
}
Can you pls tell me how could i position items the above described way?
Edit: per request the HTML also added, the app is created with create-react-app, so the html is its standard index.html and app is "assigned" to the root:
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
render method from app.js:
<div className={userSession.layout.name}>
{userSession.layout.toolbar.active && <div className="toolbar">
{this.generateComponents("toolbar")}
</div>}
{userSession.layout.search.active && <div className="searchbox">
search
</div>}
{userSession.layout.results.active && <div className="resultbox">
result
</div>}
{userSession.layout.editor.active && <div className="editor">
editor
</div>}
</div>
You can try something like this:
/*
* containing div, position is relative, set the div height here
*/
.toolbar {
position: relative;
height: 30px;
}
/*
* all 3 children have absolute positioning
*/
.left {
position: absolute;
left: 0px;
top: 0px;
}
.right {
position: absolute;
right:0px;
top:0px;
}
.middle {
position: absolute;
top: 0px;
/* center the element */
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%)
}
<div class="toolbar">
<div class="left">left</div>
<div class="middle">middle</div>
<div class="right">right</div>
</div>
Ok, found the answer. It's here and working :)

Can't modify text font size on mobile

I found a post about modifying text size specifically for mobile in a specific div, like this:
#media only screen and (max-width: 480px) {
.news {
font-size: 5px;
}
}
I can't get that to work however for the site I'm messing with. I'd like the font size to be smaller when opened on mobile. While I wonder if I have to set a viewport meta in the html, every time I do this A, it doesn't fix it, B, it totally destroys the balance of my site, clipping tons of information, which I really don't like. So, the only way I'm going to be able to use a viewport right now, is if it doesn't mess with the zoom factor at all. Not sure if that's the issue though.
Really appreciate any help on this. Full code:
css:
*{
margin:0;
padding:0;
}
body{
text-align:center; /*For IE6 Shenanigans*/
font-size: 100%;
font-weight: 1;
font-family: 'rationale';
background:black;
}
#newsusa {
position: absolute; /*makes it relative to the html element, (the first
positioned element).*/
width: 100%; /*makes the element 100%, to center it. */
left: 0px;
top: 200px;
}
.news {
color: white;
font-size: 18px;
}
li {
list-style-type: none;
}
#media only screen and (max-width: 480px) {
.news {
font-size: 0.8em;!important;
}
}
#media only screen and (max-width: 280px) {
.news {
font-size: 0.8em;!important;
}
}
xhtml:
<?php include 'global.php';?>
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang="en">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="mobiletest.css">
<link href='//fonts.googleapis.com
/css?family=Iceland|Rationale|Quantico|VT323|Dorsa' rel='stylesheet'
type='text/css'>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3
/jquery.min.js'></script>
<div id="newsusa">
<h1 class="appear-later">News</h1>
<div class="spacer1">
</div>
<div class="news">
<ul><?php outputValueFromCache('usnews', 'ignore'); ?></ul>
</div>
<div class="spacer2">
</div>
<h1 class="appear-later">Tech</h1>
<div class="spacer1">
</div>
<div class="news">
<ul><?php outputValueFromCache('usatechnews', 'ignore'); ?></ul>
</div>
<div class="spacer2">
</div>
<h1>Business</h1>
<div class="spacer1">
</div>
<div class="news">
<ul><?php outputValueFromCache('usamoneynews', 'ignore'); ?></ul>
</div>
</div>
</html>
You're going to need to add the
<meta name="viewport" content="width=device-width, initial-scale=1">
to your header. If you consider the following code:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.page-header{
font-size: 16px;
color: red;
}
#media (max-width: 430px) {
.page-header{
font-size:100px;
color: green;
}
}
</style>
</head>
<body>
<h1 class="page-header">Hello</h1>
</body>
</html>
The media query only works once you have the viewport meta tag defined. I know that's probably not what you want to hear, but I think that's your solution.

Responsive Website - general enquiries

I'm working with my first responsive website and I'm completely at a halt, I cannot for the life of me figure out how to use these media queries and what not...
Here's what I have...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Responsive Layout Test</title>
<!-- Media Query 1 -->
<link href="mobile.css" rel="stylesheet" type="text/css" media="screen (max-width:480px)" />
</head>
<body>
<!-- Container -->
<div id="container">
<!-- End Container -->
<!-- Header -->
<div id="header">
<!-- Logo -->
<div id="logo">
<img src="images/TalonLogo.png" alt="TalonLogo" />
</div>
<!-- Nav bar -->
<div id="nav">
<ul>
<li>
Test
</li>
<li>
Test
</li>
<li>
test
</li>
</ul>
</div>
</div>
<!-- End header -->
<div id="info">w
</div>
</div>
And the CSS:
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
body {
width: 100%
margin: 0;
padding: 0;
}
#container {
width: 480px;
margin-left: auto;
margin-right: auto;
}
#logo {
max-width: 100%;
text-align: center;
}
#nav {
margin-top: 10.6%;
margin-left: 4.6%;
}
Now, I do have a main style sheet obviously, and here's a problem, the whole document (no matter what size) changes the background color... Example:
Main style sheet
body {
background: #fff;
}
but, if i go into my mobile.css
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
/* Styles */
body {
background: #000;
}
It all turns black...isn't it meant to be if the screen size is at 480px it will change over?
I'm pretty sure you're missing the viewport:
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

Resources