Use CSS grid to align an ALT SYMBOL and regular text? - html

Aanyone know the best way to align an "alt symbol" with "regular text" - can't seem to get these two to both be centered (or anything) without some hacky maneuvers.
I'm currently using CSS grid but I'm open to other solutions if they're easy and sustainable to implement.
display.jsx
<div onClick={props.toggle} className='data__item__header'>
<span className='data__item__header__bullet'>●</span>
<p className='data__item__header__title'>Clashes {props.index}</p>
</div>
display.scss
.data__item__header {
display: grid;
grid-template-columns: 20px 1fr;
line-height: 20px;
align-content: center;
grid-gap: 10px;
}
.data__item__header__bullet {
font-size: 36px;
color: $tomato;
border: 1px solid blue;
display:grid;
text-align: center;
}
.data__item__header__title {
border: 1px solid green;
}
Currently is:
Should be:

Add align-self: center and justify-self: center to the .data__item__header__bullet element, and also adjust the line height - see demo below:
.data__item__header {
display: grid;
grid-template-columns: 20px 1fr;
line-height: 36px; /*changed*/
/* align-content: center; */ /* not needed */
grid-gap: 10px;
}
.data__item__header__bullet {
font-size: 36px;
color: $tomato;
border: 1px solid blue;
display:grid;
text-align: center;
align-self: center; /* added */
justify-self: center; /* added */
}
.data__item__header__title {
border: 1px solid green;
}
<div class='data__item__header'>
<span class='data__item__header__bullet'>●</span>
<p class='data__item__header__title'>Clashes {props.index}</p>
</div>
Instead of using ● character which may give you alignment issues, you can use border-radius to give you a better bullet style - see demo below:
.data__item__header {
display: grid;
grid-template-columns: 20px 1fr;
grid-gap: 10px;
}
.data__item__header__bullet {
background: blue;
border-radius: 100%; /* added */
height: 10px; /* added */
width: 10px; /* added */
display:grid;
text-align: center;
align-self: center;
justify-self: center;
}
.data__item__header__title {
border: 1px solid green;
}
<div class='data__item__header'>
<span class='data__item__header__bullet'></span>
<p class='data__item__header__title'>Clashes {props.index}</p>
</div>

Related

Why does the rule "grid-template-columns: repeat( auto-fit, minmax())" not work?

My parent flex element has several child elements (flex and grid mixed). I can't use auto-fit and minmax() properties in the grid-template-columns rule in the child grid element.
grid-template-columns: repeat(auto-fit, minmax(min(9em, 100%), 1fr));
Expected behavior:
The menu items should line up.
And the width of each menu item should be the same regardless of the length of the character text.
The width of the menu items should dynamically change depending on the number of menu items.
The menu items should fill all the free space on the line and only when there is not enough free space on the line, they should be moved to a new line to keep the grid flat.
Actual behavior:
The menu items do not fill the free space in the line and are moved to a new line, despite the fact that their width allows them to line up.
As a result, instead of items lined up in a line with dynamic width, we get menu items lined up in a column with minimal width.
My work project is written in React and has many nested components, so citing code here is difficult. Instead, I wrote a small header in html + css, similar to what I have in my working project.
* {
box-sizing: border-box;
}
button {
height: 40px;
color: #fff;
border: 1.5px solid #fff;
background-color: transparent;
}
.header {
width: 100%;
height: fit-content;
background-color: #1b1b1b;
}
/* parent flex element */
.wrapper {
width: 1080px;
margin: 0 auto;
/* Maybe it's because the parent element is flex and the child is grid, but that would be strange... */
display: flex;
justify-content: space-between; /* This rule does not affect "auto-fit" */
align-items: center;
}
.logo {
width: 40px;
font-size: 1.5em;
}
/* children grid element (auto-fit not working) */
.menu {
display: grid;
grid-template-columns: repeat(3, minmax(min(9em, 100%), 1fr)); /* WORKING */
grid-template-columns: repeat(auto-fit, 1fr ); /* WORKING */
grid-template-columns: repeat(auto-fit, minmax(9em, 1fr)); /* auto-fit + minmax = NOT WORKING !!! */
grid-template-columns: repeat(auto-fit, minmax(min(9em, 100%), 1fr)); /* auto-fit + minmax = NOT WORKING !!! */
}
.menu-item {
padding: 0px 1.5ch;
font-size: 1.2em;
letter-spacing: .035em;
transition: all ease-in-out 150ms;
}
.menu-item:hover {
background-color: #30ba8f;
}
.change-theme-buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.change-theme-buttons > button {
width: 40px;
height: 40px;
transition: all ease-in-out 150ms;
}
.change-theme-buttons > button:hover {
background-color: lightgray;
color: #000;
font-size: 1.2em;
}
.change-theme-buttons :nth-child(1) {
border-bottom-left-radius: 5px;
border-top-left-radius: 5px;
border-right: 0px;
}
.change-theme-buttons :nth-child(2) {
border-radius: 0px;
}
.change-theme-buttons :nth-child(3) {
border-bottom-right-radius: 5px;
border-top-right-radius: 5px;
border-left: 0px;
}
.search {
position: relative;
}
.search-input {
height: 32px;
padding: 4px 15px;
background-color: transparent;
border: 1.5px solid #fff;
border-radius: 15px;
color: #fff;
font-size: 16px;
}
.search-input::placeholder {
color: red;
padding: 5px;
border: 1.5px solid white;
border-radius: 15px;
font-size: 16px;
}
.search-svg {
display: none;
}
<header class="header">
<wrapper class="wrapper"> <!-- parent flex element -->
<button class="logo">L</button>
<menu class="menu"> <!-- children grid element (auto-fit not working)-->
<button class="menu-item">About</button>
<button class="menu-item">Technology</button>
<button class="menu-item">Blog</button>
</menu>
<div class="change-theme-buttons">
<button>D</button>
<button>L</button>
<button>S</button>
</div>
<div class="search">
<input class="search-input" type="search" />
<svg class="search-svg"></svg>
</div>
</wrapper>
</header>
Also a link to codepen.io.
The problem seems to be with elements in the wrapper and menu classes. I added some comments in the CSS that may be helpful. Ideally, any solution would keep the original idea of using auto-fit and minmax() (if it's possible).
Try this
grid-template-columns: repeat(auto-fit, minmax(9em, 1fr));
You need to explain to us why you want to use auto-fit and minmax(). Your header looks pretty good with just grid-template-columns: repeat(3, 1fr);.
* {
box-sizing: border-box;
}
button {
height: 40px;
color: #fff;
border: 1.5px solid #fff;
background-color: transparent;
}
.header {
width: 100%;
height: fit-content;
background-color: #1b1b1b;
}
.wrapper {
width: 1080px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
width: 40px;
font-size: 1.5em;
}
.menu {
display: grid;
grid-template-columns: repeat(3, 1fr);
/*
grid-template-columns: repeat(3, minmax(min(9em, 100%), 1fr));
grid-template-columns: repeat(auto-fit, 1fr );
grid-template-columns: repeat(auto-fit, minmax(9em, 1fr));
grid-template-columns: repeat(auto-fit, minmax(min(9em, 100%), 1fr));
*/
}
.menu-item {
padding: 0px 1.5ch;
font-size: 1.2em;
letter-spacing: .035em;
transition: all ease-in-out 150ms;
}
.menu-item:hover {
background-color: #30ba8f;
}
.change-theme-buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.change-theme-buttons > button {
width: 40px;
height: 40px;
transition: all ease-in-out 150ms;
}
.change-theme-buttons > button:hover {
background-color: lightgray;
color: #000;
font-size: 1.2em;
}
.change-theme-buttons :nth-child(1) {
border-bottom-left-radius: 5px;
border-top-left-radius: 5px;
border-right: 0px;
}
.change-theme-buttons :nth-child(2) {
border-radius: 0px;
}
.change-theme-buttons :nth-child(3) {
border-bottom-right-radius: 5px;
border-top-right-radius: 5px;
border-left: 0px;
}
.search {
position: relative;
}
.search-input {
height: 32px;
padding: 4px 15px;
background-color: transparent;
border: 1.5px solid #fff;
border-radius: 15px;
color: #fff;
font-size: 16px;
}
.search-input::placeholder {
color: red;
padding: 5px;
border: 1.5px solid white;
border-radius: 15px;
font-size: 16px;
}
.search-svg {
display: none;
}
<header class="header">
<wrapper class="wrapper"> <!-- parent flex element -->
<button class="logo">L</button>
<menu class="menu"> <!-- children grid element (auto-fit not working)-->
<button class="menu-item">About</button>
<button class="menu-item">Technology</button>
<button class="menu-item">Blog</button>
</menu>
<div class="change-theme-buttons">
<button>D</button>
<button>L</button>
<button>S</button>
</div>
<div class="search">
<input class="search-input" type="search" />
<svg class="search-svg"></svg>
</div>
</wrapper>
</header>

Make element dividing lines equal height with container

I'm trying to do a simple navbar, but the size of the border matches the size of the text and not its container's height. I'm trying to 'close' each text inside its own 'rectangle'. How do I achieve that?
.flex-r {
display: flex;
justify-content: space-around;
justify-items: center;
flex-wrap: nowrap;
gap: 1em;
align-items: center;
border: 1px solid black;
min-height: 40px;
}
.nav-item {
border-right: 1px solid black;
width: auto;
}
<div class='flex-r'><a class='nav-item'>Release</a><a class='nav-item'>Statistics</a><a class='nav-item'>Frequent releases</a></div>
You have the container set to min-height: 40px.
Add the same code to the items.
But here's a better overall solution that may work for you:
.flex-r {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1px; /* sets the dividing line width */
background-color: black; /* sets the dividing line color */
border: 1px solid black; /* sets the border around the container */
min-height: 40px;
}
.nav-item {
background-color: white; /* restores background color */
/* center the content */
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
<div class='flex-r'>
<a class='nav-item'>Release</a>
<a class='nav-item'>Statistics</a>
<a class='nav-item'>Frequent releases</a>
</div>

place a text between two arrows

I want to place a text inline and between two arrows. at first it seems easy but I can't place everything in their right place:
html, body {
height: 100%;
}
body {
background: #fafafc;
margin: 0;
}
.flex-container {
height: 100%;
padding: 0;
margin: 0;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
justify-content: center;
}
.grid-container {
width: 50%;
display: grid;
grid-template-columns: 40px auto 40px;
}
.referenceText {
font-family: Arial;
display: inline-block;
font-size: 3vw;
color: #4287f5;
/*outline: 0.1vw dashed red;*/
}
.link {
font-size: 200px;
color: #a7a4bf;
text-decoration: none;
width: 100px;
height: 100px;
outline: 0.1vw dashed orange;
}
.link:hover {
color: #636175;
}
<div class="flex-container">
<div class="grid-container">
<a class="link" href="">‹</a>
<div class="referenceText">This is the reference</div>
<a class="link" href="">›</a>
</div>
</div>
Please note that we want the arrows to be at the edges of the screen and the text at the center...
Make these changes in .grid-container and .link
.grid-container {
width: 80%;
display: grid;
grid-template-columns: 100px auto 100px;
align-items: center;
justify-items: center;
}
.link {
font-size: 200px;
color: #a7a4bf;
text-decoration: none;
width: 100px;
/* height: 100px; remove height*/
outline: 0.1vw dashed orange;
}
check codepen
Add display: flex;, align-items: center; and justify-content: space-between; to .grid-container and change the width to 100%.

How to set internal border around a CSS Grid items?

So far I have tried like this. I want to set a border around the grid elements inside the grid. Is there any way to do it?
.grid{
background: cornflowerblue;
padding: 20px;
margin: 10px;
height: 500px;
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 60px 60px 60px;
grid-gap: 30px 10px;
justify-content: center;
align-content: center;
}
.item{
font-size: 20px;
font-family: 'Montserrat', sans-serif;
line-height: 32px;
letter-spacing: 5px;
font-weight: bold;
border: 1px solid white;
text-align: center;
display: grid;
align-content: center;
justify-content: center;
}
<div class="grid">
<div class="item">A1</div>
<div class="item">A2</div>
<div class="item">A3</div>
<div class="item">A4</div>
<div class="item">A5</div>
<div class="item">A6</div>
<div class="item">A7</div>
<div class="item">A8</div>
<div class="item">A9</div>
</div>
But I want a border around the items like shown in the image.
Give higher outline, if you want a very big offset
.subgrid {
outline-offset: 50px;
}
.grid {
background: cornflowerblue;
padding: 20px;
margin: 10px;
height: 500px;
display: flex;
justify-content: center;
align-items: center;
}
.subgrid {
display: inline-grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 60px 60px 60px;
grid-gap: 30px 10px;
outline: 1px solid red;
outline-offset: 50px;
}
.item {
font-size: 20px;
font-family: 'Montserrat', sans-serif;
line-height: 32px;
letter-spacing: 5px;
font-weight: bold;
border: 1px solid white;
text-align: center;
display: grid;
align-content: center;
justify-content: center;
}
<div class="grid">
<div class="subgrid">
<div class="item">A1</div>
<div class="item">A2</div>
<div class="item">A3</div>
<div class="item">A4</div>
<div class="item">A5</div>
<div class="item">A6</div>
<div class="item">A7</div>
<div class="item">A8</div>
<div class="item">A9</div>
</div>
</div>
Create a new div around and
.newDiv {
border: 5px solid red;
}

CSS Grid content to stay in the grid area and not to flow out

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.