This code looks good on a desktop PC. I want to make the boxes appear one below the other on a mobile phone. Box2 and box4 (these will contain text) should be full width on mobile. Unfortunately, no matter how I try, it fails, it collapses on mobile. Can you help me to adjust the code?
.box {
width: 10%;
padding: 13px;
height: 200px;
background: blue;
margin-right: 5px;
float: left;
}
.box2 {
width: 30%;
padding: 13px;
height: 200px;
background: green;
margin-right: 5px;
float: left;
}
.box3 {
width: 10%;
padding: 13px;
height: 200px;
background: yellow;
margin-right: 5px;
float: left;
}
.box4 {
width: 30%;
padding: 13px;
height: 200px;
background: red;
margin-right: 0;
float: left;
}
<div class="box">
<img alt="" src="https://erteksziget.hu/custom/erteksziget/image/cache/w0h0q80np1/spots/h2a.png" style="width: 90px; border: 0!important;" />
</div>
<div class="box2">
kdféajkdéfdéf
</div>
<div class="box3">
<img alt="" src="https://erteksziget.hu/custom/erteksziget/image/cache/w0h0q80np1/spots/h2a.png" style="width: 90px; border: 0!important;" />
</div>
<div class="box4">
hdhdhgdghd
</div>
The easiest way to do this is to use a media query to apply rules at mobile widths. Then wrap your .box divs in a containing div then use flexbox to change the flex-direction from row to column. In the same query set your .box div widths to the size you want. Kevin Powell has a good introduction to flexbox here
Example below
/* Added this */
.container {
display:flex;
}
.box {
width: 10%;
padding: 13px;
height: 200px;
background: blue;
margin-right: 5px;
float: left;
}
.box2 {
width: 30%;
padding: 13px;
height: 200px;
background: green;
margin-right: 5px;
float: left;
}
.box3 {
width: 10%;
padding: 13px;
height: 200px;
background: yellow;
margin-right: 5px;
float: left;
}
.box4 {
width: 30%;
padding: 13px;
height: 200px;
background: red;
margin-right: 0;
float: left;
}
/* added this */
#media only screen and (max-width: 600px) {
.container {
flex-direction:column;
}
.box2, .box4 {
width:auto;
}
}
<div class='container'>
<div class="box">
<img alt="" src="https://erteksziget.hu/custom/erteksziget/image/cache/w0h0q80np1/spots/h2a.png" style="width: 90px; border: 0!important;" />
</div>
<div class="box2">
kdféajkdéfdéf
</div>
<div class="box3">
<img alt="" src="https://erteksziget.hu/custom/erteksziget/image/cache/w0h0q80np1/spots/h2a.png" style="width: 90px; border: 0!important;" />
</div>
<div class="box4">
hdhdhgdghd
</div>
</div>
Take all the four boxes and store them in a container div.
Then use display:flex on the container. By default all the children elements in this case the boxes will remain in row.
But in media query set them to line in column.
The code is below :
/* Add this to style the child elements of the container */
.container {
display:flex;
}
/* This will fix your issue */
#media only screen and (max-width: 600px) {
.container {
flex-direction:column;
}
}
.box {
min-width: 10%;
padding: 13px;
height: 200px;
background: blue;
margin-right: 5px;
float: left;
}
.box2 {
min-width: 30%;
padding: 13px;
height: 200px;
background: green;
margin-right: 5px;
float: left;
}
.box3 {
min-width: 10%;
padding: 13px;
height: 200px;
background: yellow;
margin-right: 5px;
float: left;
}
.box4 {
min-width: 30%;
padding: 13px;
height: 200px;
background: red;
margin-right: 0;
float: left;
}
<div class="container">
<div class="box">
<img alt="" src="https://erteksziget.hu/custom/erteksziget/image/cache/w0h0q80np1/spots/h2a.png" style="width: 90px; border: 0!important;" />
</div>
<div class="box2">
kdféajkdéfdéf
</div>
<div class="box3">
<img alt="" src="https://erteksziget.hu/custom/erteksziget/image/cache/w0h0q80np1/spots/h2a.png" style="width: 90px; border: 0!important;" />
</div>
<div class="box4">
hdhdhgdghd
</div>
</div>
This will work the way you wanted.
Try and let me know in the comments
The best way would be to use CSS Grid
You enclose your box in a container grid
And put a media query on columns od this grid
.container {
display: grid;
grid-template-columns: 100%;
grid-template-rows: 1fr;
gap: 10px
}
#media (min-width: 35em) {
/* 560px */
}
#media (min-width: 48em) {
/* 768px */
.container {
grid-template-columns: 20% 80%;
}
}
#media (min-width: 62em) {
/* 992px */
.container {
grid-template-columns: 10% 40% 10% 40%;
}
}
#media (min-width: 92em) {
/* 1312px */
}
.container img {
width: 90px;
max-width: 90%;
}
.box,
.box3 {
text-align: center;
margin: 0;
padding: 0;
}
.box {
padding: 13px;
background: blue;
}
.box2 {
padding: 13px;
background: green;
}
.box3 {
padding: 13px;
background: yellow;
}
.box4 {
padding: 13px;
background: red;
}
<div class="container">
<div class="box">
<img alt="" src="https://erteksziget.hu/custom/erteksziget/image/cache/w0h0q80np1/spots/h2a.png" />
</div>
<div class="box2">
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. At exercitationem labore pariatur perferendis quas rem voluptas? Beatae fuga hic ipsam laboriosam mollitia perspiciatis porro quam sapiente vel. Incidunt, iusto, sapiente.</div>
<div>Alias et incidunt magni mollitia nam, nesciunt nulla odit officiis pariatur porro, quas quibusdam ratione, saepe sint suscipit tempora voluptatem? Deserunt fuga hic itaque reiciendis repellat. Consectetur fugit qui voluptas?</div>
<div>Architecto assumenda beatae deserunt dicta distinctio enim est et eveniet expedita fugit id incidunt iusto minima molestiae neque non praesentium repudiandae sit tempora, ut veniam vero voluptas voluptate, voluptates voluptatibus?</div>
<div>Eaque eveniet expedita itaque molestias quisquam reprehenderit tempore voluptatibus. Accusantium at consequatur culpa cumque dignissimos dolore, ducimus eaque ex libero magnam, modi nam natus odit quis quisquam, quo rem voluptates!</div>
<div>Ad alias delectus dicta dolorem nesciunt non odit recusandae sapiente. Accusantium consequuntur corporis debitis eaque earum, esse ex, exercitationem expedita explicabo maiores molestiae nesciunt odit porro quidem rem temporibus tenetur!</div>
<div>Adipisci eveniet exercitationem explicabo labore nam numquam perferendis possimus quasi quidem recusandae, reprehenderit sapiente sunt tempore unde veritatis. Blanditiis maxime, natus quo sit voluptatem voluptatibus? Facere molestias nesciunt obcaecati
totam.</div>
<div>Ab, accusamus alias doloribus non odit suscipit veritatis! Iure magnam numquam quo sequi? Asperiores consectetur dolorem doloremque exercitationem, impedit iure laborum nesciunt nulla officiis porro, praesentium quas vitae. Cupiditate, sequi!</div>
</div>
<div class="box3">
<img alt="" src="https://erteksziget.hu/custom/erteksziget/image/cache/w0h0q80np1/spots/h2a.png" />
</div>
<div class="box4">
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. A aliquam asperiores aspernatur consequatur consequuntur et explicabo fugiat hic iste magnam magni, optio placeat praesentium quae quod recusandae reiciendis, tempora temporibus?</div>
<div>Labore nulla possimus quas quis reiciendis sapiente soluta suscipit tenetur veniam voluptatum. At beatae consequatur earum natus neque placeat sapiente. Consectetur cum, debitis enim harum maiores non pariatur possimus quos?</div>
<div>Aliquam aliquid animi aperiam aut consequatur consequuntur culpa dicta ducimus eaque eligendi error fuga harum, ipsam iste laboriosam maxime mollitia necessitatibus nobis odio odit ratione repellat suscipit tempora vero voluptates?</div>
<div>Accusamus ad, architecto at, beatae consectetur consequatur consequuntur culpa cum cumque delectus dicta doloremque earum eos excepturi facilis impedit in ipsum labore minima quae quas qui quibusdam quidem repellat, vero.</div>
<div>Autem beatae consequuntur delectus, doloribus excepturi incidunt ipsum neque non omnis pariatur recusandae sapiente veritatis? Assumenda commodi corporis debitis illo in modi, molestias neque officia officiis quaerat repellat vitae voluptatum.</div>
<div>Deleniti impedit in laborum sunt. Alias, architecto beatae consectetur deleniti deserunt dicta eius excepturi facilis necessitatibus nesciunt obcaecati omnis porro, provident, quisquam repellat similique sit soluta suscipit tempore vel voluptatibus.</div>
<div>Consequatur ea ex hic, ipsam itaque natus nesciunt non porro quas, quod reprehenderit rerum temporibus ut veritatis voluptas voluptate voluptatibus. Doloribus et quisquam suscipit ullam veniam! Ab aut fuga natus.</div>
<div>Commodi consequatur expedita quae quia voluptatibus! Aliquam animi architecto debitis delectus doloremque error esse est, et hic labore, odio quae quasi, quibusdam quidem repellat reprehenderit sint vel. Aliquid iusto, odio.</div>
<div>Asperiores, atque, aut commodi debitis distinctio dolores eligendi esse iure iusto laudantium molestias, nihil nostrum odit placeat porro quibusdam quis reiciendis reprehenderit sunt ullam. Beatae corporis cupiditate fugiat harum magni?</div>
<div>Amet consequatur cum deserunt ea est et hic, labore modi placeat tempora. Eaque eius facilis iure! Accusantium, asperiores eum explicabo ipsa labore natus neque repudiandae. Eos facilis quasi quidem sint?</div>
</div>
</div>
Related
I'm trying to do some sort of timeline design using CSS grid, with elements interweaved on both sides. But at least rows 1, 2 and the last have juste blanked unused space.
The columns are declared, but the rows aren't. So I tried using grid-auto-rows: min-content, but it didn't change anything. Actually, whatever the value I put doesn't change anything. I tried putting hardcoded px value (which is not a option) for testing, and I can easily keep the integrity of the design without the dead space.
Tested on Firefox and Brave
* {
margin: 0;
font-family: "Ubuntu", sans-serif;
box-sizing: border-box;
}
:root {
font-size: 1px;
}
body {
font-size: 16rem;
}
/*******/
.wrapper {
--border-width: 0.5em;
--gap: 8rem;
display: grid;
grid-template-columns: 1fr auto 1fr;
gap: var(--gap);
padding: 0.5em;
align-items: start;
}
h2 {
grid-column: 1/2;
grid-row: 1/2;
font-size: 2em;
}
section {
position: relative;
text-align: justify;
border-top: var(--border-width) solid var(--accent);
border-bottom: var(--border-width) solid transparent;
padding: 0 0.5em;
max-width: 60ch;
}
section::after {
content: "";
display: block;
height: var(--border-width);
width: calc(var(--border-width) + var(--gap));
background-color: var(--accent);
position: absolute;
top: calc(-1 * var(--border-width));
z-index: -1;
}
section:nth-of-type(odd) {
grid-column: 3/4;
border-right: var(--border-width) solid var(--accent);
border-top-right-radius: 1em;
}
section:nth-of-type(odd)::after {
left: 0;
translate: -100%;
}
section:nth-of-type(even) {
grid-column: 1/2;
justify-self: end;
border-left: var(--border-width) solid var(--accent);
border-top-left-radius: 1em;
}
section:nth-of-type(even)::after {
right: 0;
translate: 100%;
}
.date {
grid-column: 2/3;
display: flex;
flex-flow: column nowrap;
align-items: center;
font-size: 12rem;
padding: 0.6em 0.3em;
line-height: 0.7em;
background-color: var(--accent);
color: white;
}
.date:nth-of-type(odd) {
border-radius: 1em 0 1em 0;
}
.date:nth-of-type(even) {
border-radius: 0 1em 0 1em;
}
.date>* {
flex-basis: 100%;
}
.green {
--accent: hsl(171, 67%, 28%);
grid-row: 1/3;
}
.orange {
--accent: hsl(22, 99%, 50%);
grid-row: 2/4;
}
.orange.date {
grid-row: 2/3;
}
.yellow {
--accent: hsl(46, 100%, 47%);
grid-row: 3/5;
}
.yellow.date {
grid-row: 3/4;
}
.pink {
--accent: hsl(343, 78%, 62%);
grid-row: 4/6;
}
.pink.date {
grid-row: 4/5;
}
.blue {
--accent: hsl(192deg 80% 48%);
grid-row: 5/7;
}
.blue.date {
grid-row: 5/6;
}
<div class="wrapper">
<h2>Lorem Ipsum</h2>
<section class="green">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus quidem qui, aliquid asperiores commodi officiis inventore laboriosam dignissimos dolor officia id itaque tempora provident exercitationem accusamus expedita ullam dolorum fuga. Officiis temporibus
porro nesciunt libero, eum aliquid doloremque minima nisi sint minus id mollitia ea quisquam consequuntur laudantium autem. Aperiam.
</section>
<p class="green date"><span>03/2022</span>-<span>04/2023</span></p>
<section class="orange">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eum, quisquam atque. Dolores beatae, nisi, laborum perspiciatis architecto non dolorem quae, doloribus aliquam quaerat rem? Esse hic illum sint mollitia quibusdam repellendus totam dolorum voluptatem
ipsa, nobis sapiente. Quasi quo porro aperiam cumque nobis debitis praesentium dolorem omnis repellat saepe. Incidunt laudantium at similique nobis perferendis et illo dolor aliquid nisi voluptatum eaque ab accusamus maxime possimus, ut ratione soluta
nam, natus quibusdam illum! Qui modi cum libero odit blanditiis distinctio eveniet illo facilis alias, aut neque perspiciatis et ipsam, hic natus? Explicabo consequuntur voluptatibus a ipsam voluptatem, deleniti at doloribus!
</section>
<p class="orange date"><span>12/2020</span>-<span>02/2022</span></p>
<section class="yellow">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eius, aliquam blanditiis! Magnam dolorem nostrum molestias modi, ratione id quaerat adipisci dolore impedit quas voluptate recusandae nisi deleniti sed, doloremque ullam ducimus. Voluptatem aut
praesentium magni iusto blanditiis? Doloremque, maxime necessitatibus eaque obcaecati voluptate cumque veritatis exercitationem, dolor ex beatae blanditiis.
</section>
<p class="yellow date"><span>11/2018</span>-<span>10/2020</span></p>
<section class="pink">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Perferendis blanditiis repellat nulla iste illo quos, culpa sint nihil doloribus quae molestiae eaque perspiciatis reiciendis exercitationem eum minima molestias voluptatum consequatur quisquam
asperiores obcaecati? Quas animi quis itaque molestias praesentium maiores minima. Consequuntur hic explicabo eos expedita quidem, dolorum maiores perferendis, illum quod, placeat magni! Exercitationem architecto iusto deserunt magni possimus.
</section>
<p class="pink date"><span>01/2018</span>-<span>11/2018</span></p>
<section class="blue">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Consequatur minima illum, accusamus recusandae eveniet blanditiis repellendus quaerat ullam inventore eaque? Doloremque delectus quibusdam rem hic! Modi ducimus iusto perspiciatis incidunt quidem
cum, optio, soluta id voluptatum placeat nobis quasi maxime dolorem magni pariatur cumque illum odio dolor. Dolor libero sint ea iste, autem rerum cupiditate enim aliquam? Cumque voluptatum at dolore. Veritatis, assumenda autem. Culpa facilis dolorum
molestias voluptatum, natus, fugit fuga amet veritatis, dicta similique suscipit temporibus porro tempora?
</section>
<p class="blue date"><span>07/2014</span>-<span>07/2017</span></p>
</div
Hello
If you look at the screenshot of your code view with dev tools (we see the grid), it's normal you have blank space!
Try to take out the title out of the grid. Or that could also be solved with nested grid
try like below,
:root {
font-size: 1px;
}
body {
font-size: 16rem;
}
/*******/
.wrapper {
--border-width: 0.5em;
--gap: 8rem;
display: grid;
grid-template-columns: 1fr auto 1fr;
gap: var(--gap);
padding: 0.5em;
align-items: start;
}
h2 {
grid-column: 1/2;
grid-row: 1/2;
font-size: 2em;
}
section {
position: relative;
text-align: justify;
border-top: var(--border-width) solid var(--accent);
border-bottom: var(--border-width) solid transparent;
padding: 0 0.5em;
max-width: 60ch;
}
section::after {
content: "";
display: block;
height: var(--border-width);
width: calc(var(--border-width) + var(--gap));
background-color: var(--accent);
position: absolute;
top: calc(-1 * var(--border-width));
z-index: -1;
}
section:nth-of-type(odd) {
grid-column: 3/4;
border-right: var(--border-width) solid var(--accent);
border-top-right-radius: 1em;
}
section:nth-of-type(odd)::after {
left: 0;
translate: -100%;
}
section:nth-of-type(even) {
grid-column: 1/2;
justify-self: end;
border-left: var(--border-width) solid var(--accent);
border-top-left-radius: 1em;
}
section:nth-of-type(even)::after {
right: 0;
translate: 100%;
}
.date {
grid-column: 2/3;
display: flex;
flex-flow: column nowrap;
align-items: center;
font-size: 12rem;
padding: 0.6em 0.3em;
line-height: 0.7em;
background-color: var(--accent);
color: white;
margin:0px;
}
.date:nth-of-type(odd) {
border-radius: 1em 0 1em 0;
}
.date:nth-of-type(even) {
border-radius: 0 1em 0 1em;
}
.date>* {
flex-basis: 100%;
}
.green {
--accent: hsl(171, 67%, 28%);
grid-row: 1/3;
}
.orange {
--accent: hsl(22, 99%, 50%);
grid-row: 2/4;
}
.orange.date {
grid-row: 2/3;
}
.yellow {
--accent: hsl(46, 100%, 47%);
grid-row: 3/5;
}
.yellow.date {
grid-row: 3/4;
}
.pink {
--accent: hsl(343, 78%, 62%);
grid-row: 4/6;
}
.pink.date {
grid-row: 4/5;
}
.blue {
--accent: hsl(192deg 80% 48%);
grid-row: 5/7;
}
.blue.date {
grid-row: 5/6;
}
<div class="wrapper">
<h2>date wise</h2>
<section class="green">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Minus quidem qui, aliquid asperiores commodi officiis inventore laboriosam dignissimos dolor officia id itaque tempora provident exercitationem accusamus expedita ullam dolorum fuga. Officiis temporibus
porro nesciunt libero, eum aliquid doloremque minima nisi sint minus id mollitia ea quisquam consequuntur laudantium autem. Aperiam.
</section>
<p class="green date"><span>03/2022</span>-<span>04/2023</span></p>
<section class="orange">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eum, quisquam atque. Dolores beatae, nisi, laborum perspiciatis architecto non dolorem quae, doloribus aliquam quaerat rem? Esse hic illum sint mollitia quibusdam repellendus totam dolorum voluptatem
ipsa, nobis sapiente. Quasi quo porro aperiam cumque nobis debitis praesentium dolorem omnis repellat saepe. Incidunt laudantium at similique nobis perferendis et illo dolor aliquid nisi voluptatum eaque ab accusamus maxime possimus, ut ratione soluta
nam, natus quibusdam illum! Qui modi cum libero odit blanditiis distinctio eveniet illo facilis alias, aut neque perspiciatis et ipsam, hic natus? Explicabo consequuntur voluptatibus a ipsam voluptatem, deleniti at doloribus!
</section>
<p class="orange date"><span>12/2020</span>-<span>02/2022</span></p>
<section class="yellow">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eius, aliquam blanditiis! Magnam dolorem nostrum molestias modi, ratione id quaerat adipisci dolore impedit quas voluptate recusandae nisi deleniti sed, doloremque ullam ducimus. Voluptatem aut
praesentium magni iusto blanditiis? Doloremque, maxime necessitatibus eaque obcaecati voluptate cumque veritatis exercitationem, dolor ex beatae blanditiis.
</section>
<p class="yellow date"><span>11/2018</span>-<span>10/2020</span></p>
<section class="pink">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Perferendis blanditiis repellat nulla iste illo quos, culpa sint nihil doloribus quae molestiae eaque perspiciatis reiciendis exercitationem eum minima molestias voluptatum consequatur quisquam
asperiores obcaecati? Quas animi quis itaque molestias praesentium maiores minima. Consequuntur hic explicabo eos expedita quidem, dolorum maiores perferendis, illum quod, placeat magni! Exercitationem architecto iusto deserunt magni possimus.
</section>
<p class="pink date"><span>01/2018</span>-<span>11/2018</span></p>
<section class="blue">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Consequatur minima illum, accusamus recusandae eveniet blanditiis repellendus quaerat ullam inventore eaque? Doloremque delectus quibusdam rem hic! Modi ducimus iusto perspiciatis incidunt quidem
cum, optio, soluta id voluptatum placeat nobis quasi maxime dolorem magni pariatur cumque illum odio dolor. Dolor libero sint ea iste, autem rerum cupiditate enim aliquam? Cumque voluptatum at dolore. Veritatis, assumenda autem. Culpa facilis dolorum
molestias voluptatum, natus, fugit fuga amet veritatis, dicta similique suscipit temporibus porro tempora?
</section>
<p class="blue date"><span>07/2014</span>-<span>07/2017</span></p>
</div>
please comment if any query
How can I make the green background to cover all the .section class?
I have this structure:
section // one section will have one container
->container // one container can have multiple elements
->elements // the background class set here should override the section
relation everywhere and I would like to define a "background" element that covers the whole section.
Note:
I am getting the data like(section/container/elements) this and it is not going to change.
There is possible to have multiple sections each with it's own background.
.section {
width: 100%;
background-color: yellow;
}
.container {
height: 300px;
padding: 30px;
margin: 100px;
background-color: red;
}
.background {
width: 100%;
height: 100%;
}
.red {
background: red;
}
.green {
background: green;
}
<div class="section">
<div class="container">
<div class="background green" />
<div>
some content
</div>
</div>
</div>
Link to jsfiddle
you Should style give tag section
background-color: green;
You could set the position of .section to relative, and set the position of .green to absolute.
Now the position, width & height will use .section as reference.
Then you could set these properties freely.
here is the code:
.section{
position: relative;
}
.green {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
and then you may have to adjust their z-index
.green {
z-index: -1;
}
.section {
z-index: 0;
}
absolute: The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to its closest positioned ancestor, if any; otherwise, it is placed relative to the initial containing block. Its final position is determined by the values of top, right, bottom, and left. MDN
One approach, with explanatory comments in the code, is as below:
/* simple reset to ensure no browser-default padding/margin exists,
and forces all element-size calculations to include border-width
and padding inside the defined width: */
*,::before, ::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.section {
/* all values here are purely aesthetic, adjust to your own
requirements: */
border: 2px solid #000;
height: 80vmin;
width: 80vmin;
margin-block: 1em;
margin-inline: auto;
}
.container {
/* here we use grid layout on the .container element(s): */
display: grid;
/* we specify one grid-column and one grid-row, each of which
take 100% of the available space: */
grid-template-columns: 1fr;
grid-template-rows: 1fr;
/* sizing the grid to be 100% of the parent's width/height: */
width: 100%;
height: 100%;
}
/* all <div> elements that are children of the .container: */
.container > div {
/* are placed in the first grid-column and first grid-row: */
grid-column: 1;
grid-row: 1;
/* specifying a max-height and max-width of 100% to address
potential overflow; obviusly adjust to your requirements: */
max-height: 100%;
max-width: 100%;
}
/* styling the element that will hold the contents (so far as your
demo shows): */
.container > div:last-child {
/* hiding overflow on x-axis: */
overflow-x: hidden;
/* allowing scrolling on the y-axis to see
overflowing content: */
overflow-y: scroll;
/* defining some white-space between the element edges and contents
within: */
padding: 0.5em;
}
.blue {
background-color: hsl(180 65% 81% / 0.5);
}
.green {
background-color: hsl(120 93% 79% / 0.5);
}
.yellow {
background-color: hsl(60 100% 50% / 0.5);
}
<div class="section">
<div class="container">
<div class="background blue" />
<div>
Some very short content
</div>
</div>
</div>
<div class="section">
<div class="container">
<div class="background green" />
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Placeat corporis hic sunt nam perferendis assumenda asperiores nobis quae consequatur aut ut quidem id, optio consequuntur ipsa labore consectetur nihil incidunt.</p>
<p>Minima quibusdam hic culpa, ea porro vitae asperiores! Vitae odit, delectus nesciunt asperiores maxime. Voluptatum velit repellendus in. Impedit laudantium asperiores nostrum neque laboriosam eius libero fugiat itaque inventore nulla?</p>
<p>Laboriosam quod, facilis ratione quas consequatur quis quisquam vel eius velit eveniet, excepturi voluptatem ea dicta quam eaque praesentium sequi illum, optio animi! Libero minima iste, maiores temporibus reiciendis repellendus.</p>
<p>Blanditiis reiciendis eveniet perspiciatis excepturi, nulla eum, rem atque. Perspiciatis quae sed autem officiis error, earum officia, nulla quod, velit deleniti pariatur. Obcaecati inventore, in adipisci eveniet, expedita a quaerat.</p>
<p>Quaerat labore vitae iure expedita eaque nostrum distinctio et, atque velit quisquam corporis animi, aut voluptatem itaque odio ipsum. Iusto asperiores eius at quos totam voluptatum, ratione voluptate cum quas.</p>
<p>Sed minima aliquid voluptatum minus dolores, iusto, ducimus doloribus! Aliquam quae, atque! Iusto nobis alias magnam minima, impedit iste illo iure illum doloremque debitis! Ipsa assumenda soluta fugiat ut esse.</p>
<p>Sint voluptatum suscipit laborum, aliquid eveniet odit blanditiis corporis voluptatibus, rem consectetur quam quidem saepe, doloremque, sit nihil rerum voluptas deleniti a expedita quo quod beatae modi. Incidunt, aliquid. Ea.</p>
<p>Minus, dicta sed maiores amet necessitatibus eaque magnam asperiores, aperiam, qui earum numquam sequi eius provident officiis. Illum fuga earum architecto nesciunt ut beatae mollitia quod sunt nulla. Ipsum, modi?</p>
<p>Eligendi, officiis maxime cupiditate consectetur voluptates est ullam odio, porro sint cum suscipit rerum nisi quibusdam non enim repudiandae facere fugiat repellendus possimus, sequi excepturi culpa iusto. Velit, sunt deleniti!</p>
<p>Ipsa deserunt dolorum molestias reiciendis. Ad dolorum eius suscipit, sapiente laboriosam dolor sequi, accusamus id consequuntur modi aliquid, architecto fuga illum non voluptatum quod. Molestiae deserunt maxime suscipit tenetur, tempora.</p>
<p>Id non fugit in vitae. Fuga voluptates sapiente, impedit atque, quasi, consequatur autem sunt dignissimos unde nam quam! Officiis quos, distinctio facilis illum explicabo incidunt quia pariatur minima, enim at.</p>
<p>Commodi consectetur repellendus ad assumenda eius voluptatibus tempora dolor et possimus quaerat aperiam nesciunt qui optio, sapiente ab libero iste, recusandae, suscipit obcaecati accusamus perferendis minus molestias. Nisi, inventore, explicabo?</p>
</div>
</div>
</div>
<div class="section">
<div class="container">
<div class="background yellow" />
<div>
Some very short content
</div>
</div>
</div>
JS Fiddle demo.
References:
box-sizing.
CSS Pseudo-elements.
display.
grid-column.
grid-row.
grid-template-columns.
grid-template-rows.
hsl().
:last-child.
margin.
margin-inline.
margin-block.
max-height.
max-width.
overflow.
overflow-x.
overflow-y.
padding.
vmin.
width.
I have the following situation using flex: Codepen
<html>
<head>
<style>
* {
box-sizing: border-box;
}
.first-card {
border: 1px black solid;
width: 480px;
padding: 24px;
display: flex;
flex-direction: column;
}
.content {
flex-wrap: wrap;
grid-gap: 32px;
display:flex;
}
.left-content {
border: 1px green solid;
flex-grow: 1;
flex-basis: 200px;
}
.right-content {
flex-grow: 1;
border: 1px deeppink solid;
flex-basis: 200px;
}
</style>
</head>
<body>
<div class="first-card">
<h1>Title</h1>
<div class="content">
<div class="left-content">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Numquam ab assumenda ex sint esse atque consectetur in velit voluptate quibusdam beatae quam vero a natus, id recusandae laudantium sed, incidunt rem? Dolores eveniet beatae asperiores repellendus veniam hic accusantium qui, totam non fuga earum nobis architecto. Et rem alias tempore.
</div>
<div class="right-content">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolor, consequuntur omnis doloremque nostrum aliquam minima facere, modi possimus quo atque velit eum pariatur assumenda, quisquam laborum officiis at! Ut libero accusamus delectus dignissimos vitae reiciendis quasi tempora labore culpa doloremque ullam, vero, enim repellat qui nihil quaerat? Reiciendis, aspernatur quo?
</div>
</div>
<div class="buttons">
<button> My button</button>
</div>
</div>
</body>
</html>
I want that when the div is smaller than 490px (it wraps) that a margin exists between "My button" and the last item in "content", which would be "right content" from the example.
Is there a way to do so?
Use this with content margin-bottom:clamp(0px,(450px - 100%)*9999,20px). It's a conditional margin based on the value 450px. If the container is bigger than 450px you have 0px else you have 20px
* {
box-sizing: border-box;
}
.first-card {
border: 1px black solid;
width: 480px;
padding: 24px;
display: flex;
flex-direction: column;
overflow:auto;
resize:horizontal;
}
.content {
flex-wrap: wrap;
grid-gap: 32px;
display: flex;
margin-bottom:clamp(0px,(450px - 100%)*9999,20px)
}
.left-content {
border: 1px green solid;
flex-grow: 1;
flex-basis: 200px;
}
.right-content {
flex-grow: 1;
border: 1px deeppink solid;
flex-basis: 200px;
}
<html>
<body>
<div class="first-card">
<h1>Title</h1>
<div class="content">
<div class="left-content">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Numquam ab assumenda ex sint esse atque consectetur in velit voluptate quibusdam beatae quam vero a natus, id recusandae laudantium sed, incidunt rem? Dolores eveniet beatae asperiores repellendus
veniam hic accusantium qui, totam non fuga earum nobis architecto. Et rem alias tempore.
</div>
<div class="right-content">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolor, consequuntur omnis doloremque nostrum aliquam minima facere, modi possimus quo atque velit eum pariatur assumenda, quisquam laborum officiis at! Ut libero accusamus delectus dignissimos vitae
reiciendis quasi tempora labore culpa doloremque ullam, vero, enim repellat qui nihil quaerat? Reiciendis, aspernatur quo?
</div>
</div>
<div class="buttons">
<button> My button</button>
</div>
</div>
</body>
</html>
Unfortunately with just CSS, no. If your div width relies on something like screen width, you could use media queries.
With javascript, You could poll the element for its width and conditionally add or remove the styling needed for the buttons margin-top.
Total beginner here, learning HTML and CSS (not yet grid and flexbox). I am trying to make a section with two horizontal blocks consisting of an image/icon to the left and text on the right. Very simple, yet I somehow can't get the icon to float to the left of the text. It is stuck on the line below. What am I doing wrong? or what should I add?
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.container {
margin: auto;
max-width: 1100px;
overflow: auto;
padding: 50px;
}
#strengths {
height: 600px;
margin: auto;
Display: block;
background-image: url('./img/mountain-lake.jpg');
background-size: cover;
}
#strengths .primary {
background-color: #ccc7f2;
padding: 20px;
margin: 20px;
border-radius: 10px;
opacity: 0.75;
line-height: 1.4em;
}
#strengths .primary i {
float: left;
margin-right: 20px;
display: inline;
}
<section id="strengths" class="py-3">
<div class="container">
<h2 class="main-heading">My Strengths</h2>
<div class="primary">
<h3 class="sub-heading">Strategic Thinking</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Delectus perspiciatis culpa optio nam, ut maxime voluptatibus magnam amet id cupiditate reiciendis quis mollitia vitae nobis eum corporis! Dicta dolorum delectus repellat dolorem hic quis
qui illum minima ullam voluptas, reiciendis sed quos asperiores eaque amet velit magnam maxime officiis facere.</p>
<i class="fas fa-chess-pawn fa-3x"></i>
</div>
<br><br>
<div class="primary">
<h3 class="sub-heading">Analysis</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Delectus perspiciatis culpa optio nam, ut maxime voluptatibus magnam amet id cupiditate reiciendis quis mollitia vitae nobis eum corporis! Dicta dolorum delectus repellat dolorem hic quis
qui illum minima ullam voluptas, reiciendis sed quos asperiores eaque amet velit magnam maxime officiis facere.</p>
<i class="far fa-play-circle fa-3x"></i>
</div>
</div>
</section>
Bad idea to use float. I suggest you look into flexbox asap.
But if you really want to use it, you should change the HTML a little bit.
Think of it this way. You want the icon to be a 'column' and the title together with the text to be another column.
The best way to achieve that is to wrap the text and title inside a HTML element ( most probably a div )
Then you should specify the width used inside their parent .primary. So for example the icon has 5% and the title and text 95% - 20px which is the margin of the icon.
Also add float:left to the primary div. Because adding float:left to an element ( the icon and text ) gets that element out of the normal flow of the document. ( similar to position:fixed/absolute ) and so, for them to remain ' a part ' of the primary div , you need to add float:left on that div also.
See below
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.container {
margin: auto;
max-width: 1100px;
overflow: auto;
padding: 50px;
}
#strengths {
height: 600px;
margin: auto;
Display: block;
background-image: url('./img/mountain-lake.jpg');
background-size: cover;
}
#strengths .primary {
background-color: #ccc7f2;
padding: 20px;
margin: 20px;
border-radius: 10px;
opacity: 0.75;
line-height: 1.4em;
float:left;
width:100%;
}
#strengths .primary i {
float: left;
margin-right: 20px;
display: inline;
width:5%;
}
#strengths .primary .wrapper {
float:right;
width: calc(95% - 20px);
}
<section id="strengths" class="py-3">
<div class="container">
<h2 class="main-heading">My Strengths</h2>
<div class="primary">
<div class="wrapper">
<h3 class="sub-heading">Strategic Thinking</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Delectus perspiciatis culpa optio nam, ut maxime voluptatibus magnam amet id cupiditate reiciendis quis mollitia vitae nobis eum corporis! Dicta dolorum delectus repellat dolorem hic quis qui illum minima ullam voluptas, reiciendis sed quos asperiores eaque amet velit magnam maxime officiis facere.</p>
</div>
<i class="fas fa-chess-pawn fa-3x">icon</i>
</div>
<br><br>
<div class="primary">
<div class="wrapper">
<h3 class="sub-heading">Analysis</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Delectus perspiciatis culpa optio nam, ut maxime voluptatibus magnam amet id cupiditate reiciendis quis mollitia vitae nobis eum corporis! Dicta dolorum delectus repellat dolorem hic quis qui illum minima ullam voluptas, reiciendis sed quos asperiores eaque amet velit magnam maxime officiis facere.</p>
</div>
<i class="far fa-play-circle fa-3x">icon</i>
</div>
</div>
</section>
I have a container with lots of content and hence I have set overflow: auto; to make it scrolling.
I also need a button in the bottom-right corner of this container at a fixed position (similar to a FAB button in Material Design).
The button is nicely placed in the bottom-right of the container. Unfortunately when I scroll the content, the button moves with it. My understanding of absolute positioning is that it will position the element relative to its parent (with position: relative;). So why is this button moving? What's the right way to fix it to the parent?
The expected behavior is that the button stays in the bottom-right corner regardless of the scroll position. For an example, see this page and search for the phrase "Animation of toolbar off-screen during scrolling".
Note that I am looking to make this work with absolute positioning, not fixed positioning. The button should be in bottom-right corner of its container, not matter how deeply the container is nested in the browser window.
* {
box-sizing: border-box;
}
.container {
position: relative;
overflow: auto;
height: 256px;
width: 256px;
margin-right: 16px;
border: solid 1px red;
padding: 4px;
}
.fab {
position: absolute;
right: 20px;
bottom: 20px;
color: white;
background-color: blue;
}
<div class="container">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate quidem.
<Button class="fab">
Ok
</Button>
</div>
Please see my JSFiddle here
There are many ways of achieving this, but I would prefer wrapping the .container with a .container-parent of the same height and width, and position: relative. This gives you a non-scrolling element to position the .fab button against.
<div class="container-parent">
<div class="container">
Lorem ipsum dolor sit amet...
</div>
<Button class="fab">
Ok
</Button>
</div>
With CSS:
.container-parent {
position: relative;
height: 256px;
width: 256px;
}
.fab {
position: absolute;
bottom: 20px;
right: 20px;
}
Here's the fiddle: http://jsfiddle.net/t702Lknz/4/
Alternatively, you could use your original markup, with .fab having a fixed position. The CSS would be like:
.fab {
position: fixed;
top: 225px;
left: 190px;
}
Here's the fiddle for that: http://jsfiddle.net/t702Lknz/3/
You can do this by adding a wrapper div for the text. The wrapper div will its width and height set to 100% and overflow set to auto, so it will have the scroll box and your FAB can be positioned within the outer div like so:
* {
box-sizing: border-box;
}
.fab {
position: absolute;
right: 20px;
bottom: 20px;
}
.container {
position: relative;
border: solid 1px red;
height: 256px;
width: 256px;
float: left;
margin-right: 16px;
}
.text-wrapper {
overflow: auto;
height: 100%;
width: 100%
}
<div class="container">
<div class="text-wrapper">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus
maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio
cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate
quidem.
</div>
<Button class="fab">
Ok
</Button>
</div>
I finally figured out how to do this. Since only the text should scroll, it should be inside its own scrolling container. This makes the button latch on to its parent without being affected by the scrolling. Here's the final code.
* { box-sizing: border-box; }
.container {
/* keep the add button in a fixed location relative to this container */
position: relative;
display: flex;
flexDirection: column;
height: 200px;
width: 200px;
margin-right: 16px;
border: solid 1px red;
padding: 4px;
}
.text-container {
/* ensure that only the text scrolls, not the button */
flex: 1;
overflow: auto;
}
.fab {
position: absolute;
right: 20px;
bottom: 20px;
color: white;
background-color: blue;
}
<div class="container">
<div class="text-container">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur mollitia maxime facere quae cumque perferendis cum atque quia repellendus rerum eaque quod quibusdam incidunt blanditiis possimus temporibus reiciendis deserunt sequi eveniet necessitatibus maiores quas assumenda voluptate qui odio laboriosam totam repudiandae? Doloremque dignissimos voluptatibus eveniet rem quasi minus ex cumque esse culpa cupiditate cum architecto! Facilis deleniti unde suscipit minima obcaecati vero ea soluta odio cupiditate placeat vitae nesciunt quis alias dolorum nemo sint facere. Deleniti itaque incidunt eligendi qui nemo corporis ducimus beatae consequatur est iusto dolorum consequuntur vero debitis saepe voluptatem impedit sint ea numquam quia voluptate quidem.
</div>
<Button class="fab">
Ok
</Button>
</div>