I have HTML structure like:
<div class="container">
<div class="btn-1"></div>
<div class="btn-2"></div>
<div class="btn-3"></div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. A veritatis harum illum assumenda odio ut, quos, ipsam molestias et sint nemo, saepe! Soluta a, quasi sequi, ut corrupti eius molestias.
</div>
</div>
btn-1 should be aligned to the top left, all other buttons (btn-2, btn-3...) should be aligned to the top right.
The text after all these buttons should be 100% width.
Quick mockup:
I figured out the first part (buttons) with:
.container {
display: flex;
justify-content: flex-end;
}
.btn-1 {
/* align first button to the left */
margin-right: auto;
}
Bu not matter what I do, the text doesn't flow to the next line...
Here's my JSFiddle https://jsfiddle.net/an0o7taq/
Thanks for any help!
You need to add flex-wrap: wrap to the container.
By default, flex containers are set to flex-wrap: nowrap, forcing items to remain on a single line.
revised jsfiddle
Spec reference:
5.2. Flex Line Wrapping: the flex-wrap property
You need more container with different flex flows and styles. Tip: learn most important flex props: align-items, flex-flow, justify-content. They all apply to the direct children of the container. So when you want your layout you need more container with different flex flows.
This guide helped me a lot. They also have great examples:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
.container {
display: flex;
flex-flow: column nowrap;
}
.header {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.header-left, .header-right {
display: flex;
flex-flow: row nowrap;
}
.btn {
width: 40px;
height: 40px;
border: 1px solid #ddd;
background-color: #eee;
}
.text {
width: 100%;
}
<div class="container">
<div class="header">
<div class="header-left">
<div class="btn">btn1</div>
</div>
<div class="header-right">
<div class="btn">btn2</div>
<div class="btn">btn3</div>
</div>
</div>
<div class="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. A veritatis harum illum assumenda odio ut, quos, ipsam molestias et sint nemo, saepe! Soluta a, quasi sequi, ut corrupti eius molestias.
</div>
</div>
Related
I was watching a video that used align items and justify center to put a div on top of the page
why is it only at the top not at in the middle of the page?
.wrapper {
display: flex;
width: 100%;
justify-content: center;
align-items: center;
padding: 0 10%;
overflow: hidden;
height: 100%;
}
<div class="wrapper">
<div class="cols cols0">
<span class="topline">Hello</span>
<h1> I'm <span class="multiText">Coder</span></h1>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Possimus, sed nam quia autem voluptatum quae omnis maiores dolorem hic dolores sint quisquam a. Eaque expedita laborum debitis, dolores fugit assumenda!</p>
<div class="btns"> <button> download CV</button>
<button> hire me</button>
</div>
</div>
<div class="cols cols0"></div>
</div>
The issue is that height: 100% means 100% of the parent's height. The parent's height however is by default set to fit-content means the height is undefined. 100% of undefined is also undefined!
In order to vertically center content within an element, the height of the element must be higher than the content itself!
The simple solution is to give it a min-height of 100vh (the height of your content frame within the browser.
To remove the default vertical scrollbar you have to reset the default body margin and set the box-sizing of the element to border-box as otherwise, the padding will add height on top.
/* need to add this body reset + min-height */
body {
margin: 0;
}
.wrapper {
min-height: 100vh;
box-sizing: border-box;
}
/* original css */
.wrapper {
display: flex;
width: 100%;
justify-content: center;
align-items: center;
padding: 0 10%;
overflow: hidden;
height: 100%;
}
<div class="wrapper">
<div class="cols cols0">
<span class="topline">Hello</span>
<h1> I'm <span class="multiText">Coder</span></h1>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Possimus, sed nam quia autem voluptatum quae omnis maiores dolorem hic dolores sint quisquam a. Eaque expedita laborum debitis, dolores fugit assumenda!</p>
<div class="btns"> <button> download CV</button>
<button> hire me</button>
</div>
</div>
<div class="cols cols0"></div>
</div>
The .wrapper div is not spread over the full height.
You can set its height to 100vh
try to remove second block
<div class="cols cols0"></div>
I'm trying to replicate a website that claimed it was only made with the basic Html, CSS and JavaScript. Annoyingly I can't seem to make the images responsive and relatively the same
Can someone explain to me what I’m doing wrong? I'm using flexbox to make the site responsive but the alignments are off and I cant get them to sit next to each other with all the correct padding as well
This is my work:
https://codepen.io/Hitmonchan98/pen/PoQLRPy
This is what I want my site to look like:
https://i.stack.imgur.com/jsdxo.jpg
//html
<body>
<div class="main">
<div class="main-img"><img class="firstImage"src="https://www.amazingonly.com/wp-content/uploads/2013/04/images4-1728x1080.jpg" alt=""></div>
<div class="text">
<h2>Discover innovative ways to decorate</h2>
<p class="para">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Quia repudiandae dolore iure laudantium fugiat fuga sunt unde voluptates et, quasi exercitationem eum consectetur. Doloremque, ab?</p>
<div class="shop">Shop Now</div>
</div>
</div>
<div class="lower">
<div class="dark image">
<img class="dark-img" src="https://www.pixelstalk.net/wp-content/uploads/2016/07/Download-Free-Pictures-4k.jpg" alt="">
</div>
<div class="text2">
<h3>About furniture</h3><p class="para">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Natus ipsa, adipisci perspiciatis debitis magni sed possimus pariatur qui exercitationem fugiat iusto error ducimus, quos quis, eius earum tempore quibusdam laboriosam!</p></p></div>
<div class="light image">
<img class="light-img"src="https://www.pixelstalk.net/wp-content/uploads/2016/07/Download-Free-Pictures-4k.jpg" alt="">
</div>
</div>
</body>
//css
body{
margin: 0;
padding: 0;
line-height: 2rem;
}
/*Body*/
.firstImage,.light-img,.dark-img
{
width: 100%;
}
.text2{
padding: 2rem;
}
.text h2
{
font-size:3rem;
}
#media screen and (min-width: 768px) {
.main{
display: flex;
}
.text{
padding: 1rem 3rem 3.5rem 3rem;
}
.lower{
display: flex;
align-content:center;
}
.text2{
padding:0;
width: 40%;
}
.dark, .light
{
width: 30%;
}
.dark-img, .light-img {
width: 100%;
}
}
#media screen and (min-width: 786px) {
}
Add display: block; to your img elements and this should get rid of the white spacing underneath them. Images are weird in HTML since they have properties of both display inline and block, and I usually set them to block to have maximum styling control over them.
i need a solution guys, i want the two boxes in pink and white to have same height as its parent container in blue box (that is to stop where that blue box stopped) without depending on the content of the div in pink and white boxes.
here is my fiddle code
https://jsfiddle.net/dcq4bufa/3/
my HTML CODE
<head>
<title>eco</title>
</head>
<body>
<header class="header">
<ul class="nav">
<li class="nav-items">HOME</li>
<li class="nav-items">APP</li>
<li class="nav-items">STORE</li>
</ul>
<div class="home">
<div class="home__right">
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente nemo
aliquid saepe tempora doloribus dicta quas aperiam, eius odio ipsa culpa ipsam rerum quam vero
itaque, recusandae sint perferendis ipsum.</p>
<h1 class="home__right--sub"> tenetur quidem ducimus quod odit totam, dolor</h1>
</div>
<home class="home__left">
<h1 class="paragraph">Lorem ipsum dolor sit amet, consectetur ad</h1>
</home>
</div>
</header>
</body>
First you must outline the height of the parent container(blue),then set the two children elements(pink and white)height of 100% since they are in a container that is 100 of your device height.Try this:
.home {
display: flex;
height:100vh;
&__right {
background-color: pink;
height:100%;
flex-basis: 50;
}
&__left {
background-color: white;
height:100%;
flex: 1;
}
}
You can adjust the height of .home if you are not comfortable with the overflow
If you can use flexbox, you may do the following:
.header {
height: 80vh;
background-color: blue;
/* Add flexbox */
display: flex;
flex-direction: column;
}
.home {
display: flex;
flex: 1; /* Set flex-grow: 1 */
...
I know a solution to create equal height columns using display:table like:
.row {
display: table;
width: 100%;
}
.col{
display: table-cell;
}
but my case is a bit different, since I am using flexbox and row class has display:flex:
.row {
display: flex;
display: ms-flex;
flex-wrap: wrap;
}
and all cols have .large-4 class:
.large-4 {
width: 25%;
max-width: 25%;
flex: 0 0 30%;
}
I can't use flex:1 for .large-4 as well because it varies in different viewport.
here is a snippet of html:
<div class="row">
<div class="large-4">
<div class="card">
<img src="https://picsum.photos/200/200" alt="author">
<div class="card-content">
<h1 class="card-title">Title</h1>
<p class="grey-text mgb-05">2012-09-05, by Basir Payenda</p>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repudiandae quas eligendi id est iste
distinctio
optio vel aliquam provident, ipsa reprehenderit in corrupti quia ratione quisquam amet veniam totam
veritatis.
</p>
</div>
</div>
</div>
<div class="large-4">
<div class="card">
<img src="https://picsum.photos/200/200" alt="author">
<div class="card-content">
<h1 class="card-title">Title</h1>
<p class="grey-text mgb-0">2012-09-05, by Basir Payenda</p>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit.
</p>
</div>
</div>
</div>
<div class="large-4">
<div class="card">
<img src="https://picsum.photos/200/200" alt="author">
<div class="card-content">
<h1 class="card-title">Title</h1>
<p class="grey-text mgb-05">2012-09-05, by Basir Payenda</p>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repudiandae quas eligendi id est iste
distinctio.
</p>
</div>
</div>
</div>
</div>
code-pen link can be found here! how to I acheive equal height columns using flexbox? or any other better solution. thanks
You need to add display: flex to the .large-4 element:
.large-4 {
width: 25%;
max-width: 25%;
flex: 0 0 30%;
display: flex;
}
You'll notice when you inspect your elements using the inspector tool, large-4 elements are actually all the same height. It's the content inside that is not. So by making the parent element flex, it will make the children elements fill the space.
You already have equal-height columns, but inside them is a container that is contracting to its content. Expand that container.
.card {
height: 100%;
}
I have this problem that I can't seem to figure out. After researching I can't seem to find anyone that has solved it. I'm not sure that it can be solved in pure CSS, but I have to ask:
Consider a situation where there are two divs next to each other. The divs have a known width.
The size of the content, however, is not known. When the content of one div becomes very large, that div should take space from the other, as such:
This width should only grow if the known width can't accommodate the content.
Likewise, the blue div should grow if its content is very large:
Is this possible to solve with only CSS, or would I need JS?
flexbox and min-width can do that:
.wrap {
width: 50%;
margin: 1em auto;
border: 1px solid grey;
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.wrap div {
flex: 1 0 auto;
max-width: 70%;
}
.left {
background: rgba(255, 0, 0, 0.5);
min-width: 30%;
}
.right {
background: rgba(0, 0, 255, 0.5);
min-width: 30%;
}
<div class="wrap">
<div class="left">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur quas incidunt dolorem doloribus asperiores, iure esse voluptatibus dolore cum sint exercitationem minus aspernatur explicabo perspiciatis distinctio expedita.</div>
<div class="right"></div>
</div>
<div class="wrap">
<div class="left"></div>
<div class="right">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fuga, accusantium libero omnis, quod ea pariatur porro asperiores enim officia minima!</div>
</div>
if you set a fixed size to div, content will flow in multiple lines instead of div expanding to fit the content.
Instead you need to set minimum width to the div then it can expand, but you will have to set max-width that it can occupy as well so that it wont take full space pushing the second div away.