I have a CSS grid, and my problem is, that one of the container widths expand over 100%.
I understand it expands because of the content is larger, but I would like to stay 100%.
Why I need this: Because one field (witch course the problem) I would like to shrink with CSS calc. Problem is that the parent width percentage browser calculates before calc function in the child. This way, even though the field is small at the end when the content size of a parent is calculated is big.
.parent {
grid-area: info_panel;
z-index: 1000;
background-color: $color-card-background;
margin: 0.4rem;
margin-bottom: 1rem;
padding: 0.9rem;
box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2), 0 9px 26px 0 rgba(0, 0, 0, 0.19);
border-radius: 1rem;
}
child {
display: inline-flex;
justify-content: space-between;
width: 100%;
margin-bottom: 1rem;
}
sub-child { // this is the problem
display: inline-block;
max-width: calc(100% - 20rem);
}
EDIT
.index-page{
display: grid;
justify-content: stretch;
justify-items: stretch;
box-sizing: border-box;
overflow: hidden;
grid-template-columns: 12rem auto;
grid-template-rows: calc(100vh - 50px - 17rem) 17rem;
grid-template-areas: "left_panel right_panel "
"left_panel info_panel ";
}
.panel__left {
background-color: blue;
overflow: scroll;
grid-area: left_panel;
}
.panel__right {
background-color: red;
grid-area: right_panel;
grid-column: right_panel-start / right_panel-end;
grid-row: right_panel-start / info_panel-end;
}
.panel__info {
grid-area: info_panel;
z-index: 1000;
background-color: white;
margin: 0.4rem;
margin-bottom: 1rem;
padding: 0.9rem;
box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2), 0 9px 26px 0 rgba(0, 0, 0, 0.19);
border-radius: 1rem;
}
.panel__info__top {
display: inline-flex;
justify-content: space-between;
width: 100%;
margin-bottom: 1rem;
}
.panel__info__top__titles {
display: inline-block;
max-width: calc(100% - 17rem);
}
.panel__info__top__buttons {
display: inline-block;
padding-right: 1.4rem;
}
.row_info_text {
padding-left: 1.4rem;
padding-right: 0.7rem;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
line-height: 120%
}
.icon_button {
width: 3rem;
height: 3rem;
margin: 1rem;
vertical-align: top;
background-color: transparent;
border: none;
}
.icon_button span {
margin: auto;
text-align:center;
font-size: 2.2rem;
color: $color-theme-main;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons">
<div class="index-page">
<div class="panel__left"></div>
<div class="panel__right"></div>
<div class="panel__info">
<div class="panel__info__top">
<div class="panel__info__top__titles">
<div class="row_info_text "><b>Small text</b></div>
<div class="row_info_text">Long text that ruin my layout, because is too long. Long text that ruin my layout, because is too long.Long text that ruin my layout, because is too long.Long text that ruin my layout, because is too long. It is tooooo long</div>
<div class="row_info_text">small text</div>
</div>
<div class="panel__info__top__buttons">
<button class="icon_button"><span class="material-icons">assignment_turned_in_outlined</span></button>
<button class="icon_button"><span class="material-icons">perm_phone_msg</span></button>
<button class="icon_button"><span class="material-icons">accessibility</span></button>
</div>
</div>
</div>
</div>
grid-template-columns: repeat(2, 12rem, 1fr);
that line should clear up your problem.
Related
I'm having a serious CSS challenge on a small project I'm building:
I have a <main> element which is set as flexbox of row. inside are 3 children:
:root {
--clr-light-text: #f2f2f2;
--clr-dark-text: #48484a;
--shadow-default: 0px 2px 8px rgba(0, 0, 0, 0.2);
--vid-container-h: 0px;
color: var(--clr-light-text);
font-family: 'Poppins', sans-serif;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
list-style: none;
text-decoration: none;
}
h1,
h2,
h3,
h4 {
font-weight: 400;
}
body {
background-color: var(--clr-dark-text);
text-align: center;
overflow-x: hidden;
scroll-behavior: smooth;
}
.glass {
background-image: linear-gradient( 135deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0));
backdrop-filter: blur(5px);
border: 1px solid rgba(255, 255, 255, 0.15);
box-shadow: var(--shadow-default);
border-radius: 0.5rem;
}
main {
display: flex;
justify-content: center;
padding: 0 1rem;
gap: 2rem;
}
.main-container {
background-color: hsl(0, 0%, 75%);
padding-bottom: 10rem;
position: relative;
}
.weather-box {
width: clamp(13.5rem, 18%, 20rem);
display: flex;
flex-direction: column;
justify-content: space-evenly;
padding: 0 1em 1em 1em;
}
#weather-icon {
margin: -15% -10%;
}
.weather-details {
display: flex;
flex-direction: column;
justify-content: space-between;
}
#forecast,
#temp,
#humidity {
display: flex;
flex-direction: column;
align-items: center;
}
#temp {
margin-bottom: 1em;
font-weight: 200;
}
#temp-num {
font-size: 3em;
}
#humid {
font-size: 1.5em;
}
#humid-value {
font-size: 1.5em;
font-weight: 200;
}
#humid-value img {
max-width: 1em;
}
#forecast-desc {
font-size: 2.5em;
font-weight: 400;
}
.playlist-box {
width: clamp(13.5rem, 18%, 20rem);
display: flex;
align-items: center;
flex-direction: column;
padding: 1rem 0.5em;
flex-shrink: 0;
}
.playlist-box h3::after {
content: '';
width: 70%;
height: 1px;
background-color: rgba(255, 255, 255, 0.3);
border-radius: 100vmax;
display: block;
margin: 1em auto;
}
#playlist {
display: grid;
width: 100%;
padding: 1em;
overflow-y: scroll;
scroll-behavior: smooth;
}
#playlist li {
margin-bottom: 1rem;
width: 100%;
padding: 1rem;
border: 1px solid rgba(255, 255, 255, 0);
}
#playlist li:hover,
#playlist li:focus {
background-image: linear-gradient( 135deg, rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0));
border: 1px solid rgba(255, 255, 255, 0.15);
box-shadow: var(--shadow-default);
border-radius: 0.5rem;
}
.song-title {
margin-bottom: 0.5em;
}
.vid-thumb {
width: 100%;
/* object-position: center;
object-fit: cover; */
}
.video-container {
align-self: flex-start;
padding: 1em;
}
#song-frame {
position: relative;
overflow: hidden;
width: 100%;
padding-top: 56.25%;
}
#song-frame iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="main-container">
<main>
<aside class="weather-box glass">
<div id="forecast">
<img id="weather-icon" src="http://openweathermap.org/img/wn/02d#4x.png" alt="Weather Icon" />
<span id="forecast-desc">(--)</span>
</div>
<div class="weather-details">
<div id="temp">
<span id="temp-num">0°c</span>
<span id="feels-like">feels like 0°c</span>
</div>
<div id="humidity">
<span id="humid">Humidity</span>
<div id="humid-value">
<span id="humid-num">0%</span>
</div>
</div>
</div>
</aside>
<div class="video-container glass">
<div id="song-frame">
<iframe src="https://www.youtube.com/embed/oG08ukJPtR8" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" width="1024" height="576" frameborder="0"></iframe>
</div>
</div>
<aside class="playlist-box glass">
<h3>Browse Playlist:</h3>
<ul id="playlist">
<li>
<h4>Song Title</h4>
<img class="vid-thumb" src="https://i.ytimg.com/vi/ru0K8uYEZWw/hqdefault.jpg" alt="" />
</li>
<li>
<h4>Song Title</h4>
<img class="vid-thumb" src="https://i.ytimg.com/vi/pkCyfBibIbI/hqdefault.jpg" alt="" />
</li>
</ul>
</aside>
</main>
</div>
I applied to both <aside> elements to have a responsive width with the following width: clamp(13.5rem, 18%, 20rem). The div in the center uses the rest of the available width of the viewport. Generally, the layout kinda works.
The weather-container's clamp works just fine. The playlist-container though is able to shrink to less than 13.5rem (216px) and in some screen sizes I'm left with uneven containers in the sides.
The interesting part is that problem is somehow connected to the <img> thumbnails. The set width for them is 100%. If I cancel it out, the container behaves correctly but the thumbnails are cut off.
setting a fixed width value doesn't work.
setting a min-width to the element doesn't work.
setting flex-shrink: 0; to the element doesn't work.
messing with flex-basis also didn't work for me.
changing the <img> to a <div> with background-image instead produces the same result.
Would love to get over this problem, thank you.
Edit: I've produced a CodePen which recreates the problem: https://codepen.io/xandertem/pen/XWZLaVR
Try going to responsive mode and see the right section shrinking around <560px.
Adding flex-shrink: 0 to the aside (.playlist-box) solves the problem.
You should probably add the same rule to the other aside (.weather-box), even though you don't need it, just in case the type of content in that container ever changes. As you can see, images and videos make a difference.
For reasons I don't understand clamp in the width setting of flex just does not seem to work.
Also to automatically have the two asides with the height of the middle item you could use grid.
To get round what seems to be the clamp problem this snippet separates out the ranges by using min and max width media queries [when 18% is 13.5rem the overall width is 75rem and so on).
* {
box-sizing: border-box;
}
main {
display: grid;
grid-template-columns: 18% 1fr 18%;
width: 100vw;
}
#media screen and (max-width: 75rem) {
main {
grid-template-columns: 13.5rem 1fr 13.5rem;
}
}
#media screen and (min-width: 111.111rem) {
main {
grid-template-columns: 20rem 1fr 20rem;
}
}
main {
padding: 0 1rem;
gap: 2rem;
}
.weather-container {
display: flex;
flex-direction: column;
justify-content: space-evenly;
padding: 0 1em 1em 1em;
}
.video-container {
padding: 1em;
}
.playlist-container {
display: flex;
align-items: center;
flex-direction: column;
padding: 1rem 0.5em;
}
.vid-thumb {
width: 100%;
/* object-position: center;
object-fit: cover; */
}
<main>
<div class="weather-container">
...divs...
</div>
<div class="video-container">
<div>
<iframe>...</iframe>
</div>
</div>
<div class="playlist-container">
<h3>Browse Playlist:</h3>
<ul>
<li>
<h4>Song Title</h4>
<img class="vid-thumb" src="https://i.ytimg.com/vi/example.jpg" alt="">
</li>
<li>etc...</li>
</ul>
</div>
</main>
I can't align a div container at the bottom of an image.
What I've tried is editing the attribute position of the image and set it's value to "relative".
Explaining this is a little bit difficult, but I got html and css code snippets with a little preview:
codepen.io/Proudyy/pen/PoOjYpK
(it's not 84 lines long, so not too much in my opinion)
This is how it should look like:
https://imgur.com/a/LShx2cM
set position: absolute for .skin-item-footer and position: relative on it's parent div. Check below code
.skin-item-splashart {
width: 100%;
border-radius: 12px;
position: relative;
}
.skin-item-footer-left-container {
grid-column: 1;
display: grid;
grid-template-rows: repeat(2, auto);
align-self: start;
height: 100%;
}
.skin-item-footer-right-container {
grid-column: 2;
display: grid;
align-items: center;
justify-content: end;
align-self: end;
height: 100%;
}
.skin-item-default-name {
font-size: 24px;
font-weight: bold;
margin-left: 3vh;
}
.skin-item-name {
font-size: 18px;
color: gainsboro;
margin: 0;
margin-left: 3vh;
grid-row: 1;
}
.skin-item-cost {
display: grid;
grid-row: 2;
grid-template-columns: repeat(2, auto);
grid-template-rows: 1fr;
}
.skin-item-cost-value {
color: gainsboro;
grid-column: 1;
margin: 0;
}
.skin-item-cost-icon {
grid-column: 2;
margin: 0;
}
.skin-item-save-icon {
cursor: pointer;
margin-right: 2vh;
}
.skin-item {
background-color: rgb(0, 40, 75);
margin: 15px;
border-radius: 12px;
height: auto;
width: 50%;
position: relative;
}
img{
max-width: 100%;
}
.skin-item-footer {
background-color: rgba(0, 0, 0, 0.4);
border-radius: 0 0 12px 12px;
position: absolute;
bottom: 0;
height: auto;
width: 100%;
}
<div class="skin-item">
<img class="skin-item-splashart" src="https://cdn.communitydragon.org/latest/champion/1/splash-art/skin/1"/>
<div class="skin-item-footer">
<div class="skin-item-footer-left-container">
<p class="skin-item-name">SkinName</p>
<div class="skin-item-cost">
<img class="skin-item-cost-icon" src="https://raw.communitydragon.org/latest/plugins/rcp-fe-lol-static-assets/global/default/images/icon-rp-24.png"/>
<p class="skin-item-cost-value">Spaceholder</p>
</div>
</div>
<div class="skin-item-footer-right-container">
<img class="skin-item-save-icon" src="https://img.icons8.com/color/32/000000/save--v1.png"/>
</div>
</div>
</div>
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 1 year ago.
I have flex container and first element needs to align left and second center. I try different options with justify-content and align items => center so that the second element of this paging box is in the middle but there is no help.
This is css and html code
paging-section {
display: flex;
height: 60px;
}
/* .paging-section div {
flex: 1;
} */
.pagination-info {
align-items: flex-start;
margin-top: 15px;
background-color: red;
}
.pagination-box {
width: auto;
height: 38px;
margin: 20px 308px 369px 215px;
padding: 0 16px 0 16px;
border-radius: 4px;
box-shadow: 0px 1px 2px 0 rgba(0, 0, 0, 0.08);
border: solid 1px #d8dce6;
background-color: #ffffff;
display: flex;
background-color: yellow;
// justify-content: center;
// align-items: center;
}
<div class="paging-section">
<div class="pagination-info">
<p>
{{ size }}
{{ 'ADMIN.PAGINATION.RESULTS' | translate }}
</p>
</div>
<div class="pagination-box">
....
</div>
</div>
How to achieve that this yellow element is in the middle of the container and the first one remains on the left side ? Thanks
ive set fixed width to .pagination-info and also set margin: 0px auto; to .pagination-box at the end ive translate the .pagination-box to the left on half width of .pagination-info there is transform: translateX(-50px);
.paging-section {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
height: 60px;
}
/* .paging-section div {
flex: 1;
} */
.pagination-info {
align-items: flex-start;
background-color: red;
max-width: 100px;
width: 100px;
min-width: 100px;
word-break: break-all;
}
.pagination-box {
margin: 0px auto;
transform: translateX(-50px);
width: auto;
height: 38px;
padding: 0 16px 0 16px;
border-radius: 4px;
box-shadow: 0px 1px 2px 0 rgba(0, 0, 0, 0.08);
border: solid 1px #d8dce6;
background-color: #ffffff;
display: flex;
background-color: yellow;
// justify-content: center;
// align-items: center;
}
<div class="paging-section">
<div class="pagination-info">
<p>
{{ size }}
{{ 'ADMIN.PAGINATION.RESULTS' | translate }}
</p>
</div>
<div class="pagination-box">
....
</div>
</div>
Is there a way for the grid areas to automatically adjust to the size of the content in them.
Right now I have used:
grid-auto-rows: min-content;
And
grid-auto-columns: min-content;
and thought that should fix the problem.
The image below shows how the content is not contained in the grid area.
.container {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
grid-auto-rows: min-content;
grid-auto-columns: min-content;
}
:root {
--yellow: #ffc600;
--black: #272727;
}
html {
box-sizing: border-box;
font-size: 10px;
color: var(--black);
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.item {
display: grid;
justify-content: center;
align-items: center;
border: 5px solid rgba(0, 0, 0, 0.03);
border-radius: 3px;
font-size: 35px;
background-color: var(--yellow);
}
.item p {
margin: 0 0 5px 0;
}
<div class="container">
<div class="item item1">1kajhdkha</div>
<div class="item item2">2aksdakj</div>
<div class="item item3">as,jhdahfjhs,h3</div>
<div class="item item4">4akshdasd</div>
</div>
Please add Css
.item {
display: grid;
justify-content: center;
align-items: center;
border: 5px solid rgba(0, 0, 0, 0.03);
border-radius: 3px;
font-size: 35px;
background-color: var(--yellow);
word-break: break-all;
}
The grid properties should be given to the parent so that child can stay within the boundaries of the parent. Hence you have to put the content inside a span or div like this:
<div class="item item1"><span>1kajhdkh sdsd sdsa</span></div>
And add this to css for ".item" :
word-break: break-all;
Add overflow:none and word-break:xxx as you want in .item class.
I want to fit a picture into a website that still works when you scale it, but my picture is gigantic and I can't seem to make it fit. I tried to put a restriction on the wrapper div (here called "innpakning") on the picture itself, and the surrounding div. Nothing seems to work. And even if I could get it to work, that would defeat the purpose as I want it to scale to the website window. How do I make it fit the window?
I tried using
max-width: 100%;
max-height: 100%;
and it fixed the image, but not the spacing of the rest of the website. I end up with an enormous gap between .overskrift and .innhold.
<body>
<div class="innpakning">
<div class="overskrift">
<p id="text"> Hvaler </p>
</div>
<div class="innhold">
<div class="meny">
<div class="alt"> Info </div>
<div class="alt">Bilde </div>
<div class="alt"> Lyd </div>
<div class="alt"> Video </div>
</div>
<div class="faktisk">
<img src="css/media/hval.jpg" alt="">
</div>
</div>
</div>
</body>
innpakning{
margin: 20px;
padding-left: 10px;
padding-right: 10px;
display: grid;
grid-template-rows: 1fr 1fr;
}
.overskrift{
background-color: none;
color: black;
font-weight: bold;
}
.innhold {
background-color: #29648A;
}
.meny {
background-color: none;
display: grid;
margin: auto;
grid-template-columns: 1fr 1fr 1fr 1fr;
}
a {
text-decoration: none;
margin-left: 25px;
color: white;
}
.alt {
color: #FAFAFA;
text-decoration: none;
background-color: #25274D;
color: white;
padding: 10px;
margin: 2px;
}
.alt:hover {
background-color: #464866;
}
img {
margin-left: auto;
border: solid black;
max-width: 100%;
max-height: 100%;
}
.faktisk {
color: #DEDEDE;
padding: 15px;
margin: auto;
display: grid;
grid-template-columns: 1fr
}
#import url('https://fonts.googleapis.com/css?family=Asset');
#text {
font-family: Asset, cursive;
text-align: center;
font-size: 35px;
color: rgba(245, 246, 255, 0.96);
background-color: rgba(168, 168, 168, 0.11);
text-shadow: rgba(0, 0, 0, 0.99) 2px 2px 2px;
}
I suggest that you use the CSS unit type vw to set the max-width of the image, like so:
img {
margin-left: auto;
border: solid black;
max-height: 100%;
max-width: 100vw;
height: auto;
}
Read more at MDN: https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Values_and_units#Numbers_lengths_and_percentages