Flex item being bumped down exactly half height of previous item - css

I'll include an image for context, then the code:
The box at far right with "ABC" in it and a left/bottom border is being bumped down exactly half the height of the gradient image next to it. I know a lot of the heights/etc don't make sense, but I've removed them all and the problem persists. Any guidance here?
The HTML:
<div className="thing">
<img src="https://unsplash.it/75/90/?blur" alt="hey there" className="thingImage" />
<div className="content">
<span className="thingName">Jibber Jabb Super Long Title of a Movie or Thing Here</span><br/>
<span className="thingRanks">
Rank1: 1<br/>
Rank2: 2<br/>
Rank3: 2
</span>
</div>
<div className="thingMeta">
<img src="https://unsplash.it/30/30/?blur" alt="name" className="thingIcon" />
<span className="thingAbbrev">ABC</span>
</div>
</div>
(the "className" is from React, just read it as "class" if you're not familiar with React)
The CSS:
.thing{
border: 1px solid #ccc;
width: 564px;
min-height: 68px;
padding: 10px;
font-family: "Helvetica", arial, sans-serif;
font-size: 14px;
line-height: 18px;
display: flex;
}
.thingImage{
border-radius: 5px;
margin-right: 10px;
}
.thingName{
font-weight: bold;
margin-right: 0.3em;
}
.thingMeta{
margin-left: auto;
align-self: flex-start;
height: 35px;
}
.thingAbbrev{
border-bottom: medium solid #000000;
border-left: medium solid #000;
align-self: flex-start;
padding-left: 5px;
padding-bottom: 5px;
margin-left: 10px;
width: 30px;
}
.thingIcon{
height: 30px;
}
The main focus of this project for me is learning React, using Flexbox is just a bonus for me here. Thanks in advance, any help or guidance is greatly appreciated!

Since the thingMeta is not a flex container (doesn't have display: flex;(1)), the thingAbbrev is not a flex item, hence the align-self: flex-start won't apply .
As the img and span in the thingMeta are normal inline elements, they align along the baseline, so i.e. adding vertical-align: top will align them at the top.
.thing{
border: 1px solid #ccc;
width: 564px;
min-height: 68px;
padding: 10px;
font-family: "Helvetica", arial, sans-serif;
font-size: 14px;
line-height: 18px;
display: flex;
}
.thingImage{
border-radius: 5px;
margin-right: 10px;
}
.thingName{
font-weight: bold;
margin-right: 0.3em;
}
.thingMeta{
margin-left: auto;
align-self: flex-start;
height: 35px;
}
.thingAbbrev{
border-bottom: medium solid #000000;
border-left: medium solid #000;
/* align-self: flex-start; removed */
vertical-align: top; /* added */
padding-left: 5px;
padding-bottom: 5px;
margin-left: 10px;
width: 30px;
}
.thingIcon{
height: 30px;
}
<div class="thing">
<img src="https://unsplash.it/75/90/?blur" alt="hey there" class="thingImage" />
<div class="content">
<span class="thingName">Jibber Jabb Super Long Title of a Movie or Thing Here</span><br/>
<span class="thingRanks">
Rank1: 1<br/>
Rank2: 2<br/>
Rank3: 2
</span>
</div>
<div class="thingMeta">
<img src="https://unsplash.it/30/30/?blur" alt="name" class="thingIcon" />
<span class="thingAbbrev">ABC</span>
</div>
</div>
Alternatively you can of course also simply add display: flex to the thingMeta. The downside with that is that your img then becomes a flex item and based on what you want to do with it, there is some cross browser issues, and many of them can be avoided by wrapping the img (which I didn't in below sample)
.thing{
border: 1px solid #ccc;
width: 564px;
min-height: 68px;
padding: 10px;
font-family: "Helvetica", arial, sans-serif;
font-size: 14px;
line-height: 18px;
display: flex;
}
.thingImage{
border-radius: 5px;
margin-right: 10px;
}
.thingName{
font-weight: bold;
margin-right: 0.3em;
}
.thingMeta{
margin-left: auto;
align-self: flex-start;
height: 35px;
display: flex; /* added */
}
.thingAbbrev{
border-bottom: medium solid #000000;
border-left: medium solid #000;
align-self: flex-start;
padding-left: 5px;
padding-bottom: 5px;
margin-left: 10px;
width: 30px;
}
.thingIcon{
height: 30px;
}
<div class="thing">
<img src="https://unsplash.it/75/90/?blur" alt="hey there" class="thingImage" />
<div class="content">
<span class="thingName">Jibber Jabb Super Long Title of a Movie or Thing Here</span><br/>
<span class="thingRanks">
Rank1: 1<br/>
Rank2: 2<br/>
Rank3: 2
</span>
</div>
<div class="thingMeta">
<img src="https://unsplash.it/30/30/?blur" alt="name" class="thingIcon" />
<span class="thingAbbrev">ABC</span>
</div>
</div>
(1) When display: flex is set on an element, it is only its children that becomes flex items

You need to set the below CSS and html
CSS:
.vcenter{
display: flex;
align-item: center;
justify-content: center;
}
Also I wrapped the <span class="thingAbbrev">ABC</span> in another span to get rid of an height issue due to margins.
HTML:
<div class="thingMeta vcenter">
<img src="https://unsplash.it/30/30/?blur" alt="name" class="thingIcon" />
<span class="vcenter"><span class="thingAbbrev">ABC</span></span>
</div>
JSFiddle: here

Related

How to stop the influence of CSS grid to just what's visible. Experimenting on vertical buttons

I'm experimenting on vertical buttons using CSS display grid & inline-grid and have them working. It's when I try to add regular buttons that things go wrong.
The image displays the issue.
]1
The grid's influence extends out past the visible items it contains and hence the "control" buttons are ending up below the vertical buttons instead of up top next to the first vertical button. I've tried surrounding the grid divs with other divs to try to contain their influence, but no luck.
How do I get the "control" buttons currently at the bottom back up toward the top?
<div id="control">
<div id="title">
Buttons In Grid
</div>
<div class="sideButtonsOuterContainer">
<div class="sideButtonsInnerContainer">
<button id="buttonR0" class="sideButtons">A</button>
<button id="buttonR1" class="sideButtons">B</button>
<button id="buttonR2" class="sideButtons">C</button>
</div>
<div class="sideButtonsInnerContainer">
<button id="buttonR3" class="sideButtons">D</button>
<button id="buttonR4" class="sideButtons">E</button>
<button id="buttonR5" class="sideButtons">F</button>
</div>
<div class="sideButtonsInnerContainer">
<button id="buttonR6" class="sideButtons">G</button>
<button id="buttonR7" class="sideButtons">H</button>
<button id="buttonR8" class="sideButtons">I</button>
</div>
</div>
<div id="sideControls">
<button class="topButtons">Control</button>
<button class="topButtons">Control</button>
<button class="topButtons">Control</button>
<button class="topButtons">Control</button>
<button class="topButtons">Control</button>
</div>
</div>
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
#control{
width: 20%;
}
#title{
text-align: center;
font-size: 43px;
color: rgb(255, 0, 0);
}
.sideButtonsOuterContainer{
display: grid;
width: 52px;
grid-template-rows: auto auto auto;
gap: 10px;
background-color: #000000;
padding-left: 0px;
padding-right: 0px;
padding-top: 10px;
padding-bottom: 10px;
}
.sideButtonsInnerContainer {
display: inline-grid;
width: 45px;
grid-template-rows: auto auto auto;
gap: 4px;
padding: 3px;
}
.sideButtons{
background-color: #4CAF50; /* Green */
color: white;
width: 45px;
height: 127px;
text-align: center;
font-size: 38px;
}
.topButtons{
background-color: #4CAF50; /* Green */
border: none;
color: white;
text-align: center;
text-decoration: none;
font-size: 38px;
}

Moving a tag to the top of a todo bar

The spent text with the teal background is meant to be a tag, and I want the tag to appear above the todo bar...kind of like this:
Like a small rectangle on top of a big one. So the tag would be on the top left corner of the todo bar. How would I achieve this? I've tried doing margin to the tag, but that did not work out at all.
CSS for the tag (style.css)
.tag {
color: white;
font-size: 15px;
font-weight: 400;
letter-spacing: 2px;
text-transform: uppercase;
background: #36d1dc;
padding: 3px;
border-radius: 5px;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
}
React JS code for the tag part (Todo.js)
<li className={`todo-item${todo.completed ? "completed" : ""}`}>
{isSpent && <p className="tag">Spent</p>}
{isReceived && <p className="tag">Received</p>} ${text}
</li>
In case anyone needs the whole of the todo.css file: https://pastecode.io/s/s5XZ9e3DRW
If you need anymore information, or if my question was poorly phrased, please tell me. Any help is very much appreciated. Thank you!
I think if yow will separate the tag and the navbar to two different div tags and put them on main div something like:
<div id="wrapper">
<div id="top-left">top left div</div>
<div id="down">down side div</div>
</div>
and the css will be something like (using grid on the main div):
#wrapper {
display: grid;
}
#top-left {
background: green;
width: 250px;
float:left;
margin-right: 20px;
}
#down {
background: blue;
float:left;
width: 500px;
}
the result is:
I would go with something like this, where input:focus could be a class set on on .container, for example, if the input has any values.
I couldn't understand why you used li and p in your original code, because you need to override so much stuff to make it look nice.
Using "rem" over a fixed pixel value is also preferred if you want to create a responsive site, where you just override the font-size in the body to make everything scale.
.container {
position: relative;
display: flex;
}
body,
input {
padding: 1rem;
}
.container.selected > .todo-item,
input:focus ~ .todo-item {
transform: translateY(-1rem);
}
.todo-item {
position: absolute;
left: 1rem;
transform: translateY(1rem);
transition: transform 400ms;
}
.tag {
color: white;
font-size: 15px;
font-weight: 400;
letter-spacing: 2px;
text-transform: uppercase;
background: #36d1dc;
padding: 3px;
border-radius: 5px;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
}
<div class="container">
<input type="number">
<div class="todo-item"><span class="tag">Spent</span></div>
<div style="padding-top: 1rem"><-- select this input</div>
</div>
<div class="selected container" style="padding-top: 2rem">
<input type="number">
<div class="todo-item"><span class="tag">Spent</span></div>
</div>
body {
background-color: #48AEE0;
}
.container {
height: 200px;
width: 500px;
display: flex;
flex-direction: column;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.tag {
color: white;
font-size: 15px;
font-weight: 400;
letter-spacing: 2px;
text-transform: uppercase;
background: #36d1dc;
padding: 3px;
width: 80px;
text-align: center;
border-radius: 5px;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
}
.other {
margin: 0;
display: inline-flex;
flex-direction: column;
}
input {
height: 30px;
width: 200px;
border: white;
margin: 0;
}
<div class="container">
<div class="tag">spent</div>
<div class="others">
<input type="text">
</div>
</div>

How to display element on the right between elements using css

I have element 1, element 2, element 3 on may page, how can i place element3 on right top below element1 in right top?
It should looks like on the screen below:
I read a lot of examples but it didn't work for me,
i have tried to use
position: absolute;
float: right;
with position: absolutel it places this element on the top of the page but i don't want it,
the maximum i achievedit's right bottom position
Also i tried to use:
margin-top: -150px;
it didn't help neither, while minimizing windows it becomes a mess:
Please help me to solve this issue ?
Edited:
1999,19,1600+,8 is .hero-fact-title,
Founded in Helsinki, Offices, Digital Natives, Design Studios is .hero-fact-description, .fact-summary is that text i want to move also it element3.
.fact-wrapper {
margin-top: 10px;
display: grid;
grid-template-columns: 300px 1fr;
grid-gap: 3px;
//display: inline-block;
&__hero-fact-title {
width: 90px;
height: 42px;
font-family: MaisonNeue;
font-size: 32px;
font-weight: bold;
font-stretch: normal;
font-style: normal;
letter-spacing: -1px;
color: #333333;
}
&__hero-fact-description {
width: 193px;
height: 22px;
font-family: MaisonNeue;
font-size: 16px;
font-weight: normal;
font-stretch: normal;
font-style: normal;
letter-spacing: normal;
color: #333333;
}
}
.fact-summary {
width: 596px;
height: 96px;
font-family: MaisonNeue;
font-size: 24px;
font-weight: normal;
font-stretch: normal;
font-style: normal;
line-height: 1.33;
letter-spacing: normal;
color: #333333;
float: right;
}
to display this mess on page i use the code below:
<div className="fact-wrapper">
{facts.map(obj => {
return (
<div>
<div className="fact-wrapper__hero-fact-title">{obj.title}</div>
<div className="fact-wrapper__hero-fact-descriptio">
{obj.description}
</div>
</div>
)
})}
</div>
<h2 className="fact-summary">{factSummary}</h2>
Use flexbox
.container {
display: flex;
flex-direction: column;
justify-content: center;
width: 80%;
margin: 0 auto;
}
.bottom {
display: flex;
align-items: flex-start;
}
.box {
display: flex;
flex: 1;
margin: 5px;
}
.box-one {
border: 2px solid tomato;
padding: 2rem 5px;
}
.box-two {
border: 2px solid teal;
padding: 2rem 5px;
}
.box-three {
border: 2px solid goldenrod;
padding: 1rem 5px;
}
<div class="container">
<div class="top">
<div class="box box-one">box one</div>
</div>
<div class="bottom">
<div class="box box-two">box two</div>
<div class="box box-three">box three</div>
</div>
</div>
I'd propose you to use flex.
Basically you have here two lines.
The first line is a simple div. The second line is a combination of two divs side by side.
There are multiple options to place them side by side you, but it's usually better for future changes to use flex.
To make it work you need to
1. Add "display: flex" to the parent div.
2. Add "flex-grow: 1" style to children. That's it.
Flex-grow defines the ability for a flex item to grow if necessary.
<div>
<div class='block'>Element 1</div>
<div class='parent'>
<div class='block child'>Elemet 2</div>
<div class='block child'>Elemet 3</div>
</div>
</div>
.parent {
display: flex;
}
.child {
flex-grow: 1;
}
.block {
padding: 10px;
border: 1px solid #ccc
}
Example: https://codepen.io/tony-freed/full/vYOrYVE
You can learn more about it here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
You could use Flexbox, giving flex-basis: 100% to the first element, e.g.
main {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
justify-content: space-between;
}
div {
box-sizing: border-box;
border: 5px solid currentColor;
flex-basis: calc(50% - 5px);
margin-bottom: 10px;
}
.e1 { color: red; flex-basis: 100%; }
.e2 { color: blue; }
.e3 { color: green; }
<main>
<div class="e1">element 1</div>
<div class="e2">element 2 <br /><br />Lot of text</div>
<div class="e3">element 3</div>
</main>

Positioning elements inside DIV

I have the following HTML:
<div class="Section__item">
<div class="Section__item__title">Title</div>
<div>
<img
class="Section__item__image"
width="120px"
src="/static/images/test.jpeg"
>
<i class="Section__item__icon icon-right-nav-workflow"/>
</div>
<div class="Section__item__text">This is a descritption</div>
</div>
And this is my style using scss:
.Section {
&__item{
border: #EEF3F7 solid 1px;
padding: 10px;
height: 150px;
margin-bottom: 15px;
box-shadow: 3px 3px #EEF3F7;
&:hover {
background-color: #E3F4FE;
cursor: pointer;
}
&__title {
text-align: left;
color: black;
font-size: 16px;
font-weight: 900;
}
&__image {
padding-top: 5px;
float: left;
}
&__icon {
float: right;
font-size: 40px;
}
&__text {
float: left;
}
}
}
The result is the following:
And what I need to get is the following:
I need the text to be under the image and where you see a "red" line in the right the text can't go further, if text is bigger then wrap text.
Also if you see right icon has to be positioned exactly on the same top level as the image.
Any clue?
There's loads of ways to do this (flexbox, grid, tables, absolute positioning). The oldschool way would be a clearfix but really you should avoid floats altogether. The simplest solution to what you have so far is to remove ALL of the float's; make the div that holds the image and the icon position:relative; and set the icon to position:absolute; top:0; right:0;.
.Section__item {
border: #EEF3F7 solid 1px;
padding: 10px;
min-height: 150px; /* changed to min-height so that it expands if there's loads of text */
margin-bottom: 15px;
box-shadow: 3px 3px #EEF3F7;
width:400px;
}
.Section__item:hover {
background-color: #E3F4FE;
cursor: pointer;
}
.Section__item__title {
color: black;
font-size: 16px;
font-weight: 900;
}
.Section__item__imagewrap {
position: relative;
}
.Section__item__image {
margin-top: 5px;
}
.Section__item__icon {
font-size: 40px;
line-height: 40px;
position: absolute;
top: 0;
right: 0;
}
.Section__item__text {}
<div class="Section__item">
<div class="Section__item__title">Title</div>
<div class="Section__item__imagewrap">
<img class="Section__item__image" width="120px" src="https://placeimg.com/320/240/any">
<i class="Section__item__icon icon-right-nav-workflow">i</i>
</div>
<div class="Section__item__text">This is a description. If the text is long it will wrap and the section__item's height will increase to fit the content.</div>
</div>
Uh... don't use float? Or rather, only use float on the one thing you want to break out of normal flow, which is the icon.
PS: <i> is not an autoclosing tag, so writing <i /> is incorrect even if browsers will likely ignore your mistake. Also, putting padding on an image doesn't seem right, I switched to margin-top in this code.
.Section__item {
display: inline-block; /* so it doesn't take full width of the snippet */
border: #EEF3F7 solid 1px;
padding: 10px;
height: 150px;
margin-bottom: 15px;
box-shadow: 3px 3px #EEF3F7;
}
.Section__item:hover {
background-color: #E3F4FE;
cursor: pointer;
}
.Section__item__title {
text-align: left;
color: black;
font-size: 16px;
font-weight: 900;
}
.Section__item__image {
margin-top: 5px;
vertical-align: top;
}
.Section__item__icon {
font-size: 40px;
float: right;
}
<div class="Section__item">
<div class="Section__item__title">Title</div>
<div>
<img class="Section__item__image" width="120" height="120">
<i class="Section__item__icon icon-right-nav-workflow">Icon</i>
</div>
<div class="Section__item__text">This is a descritption</div>
</div>

How to stop text in div pushing other divs down the page?

I want to align the text in my div ('what' and 'where') but now that I have the text a the bottom it has pushed the divs (search-field1 and searchfield2) further down the page!
How can I have it so the text doesnt affect this? Is it something to do with the cellpadding/spacing?
Sorry im not the best at CSS I'm teaching myself it.
HTML:
<center>
<div class="what"><p>what?</div>
<div class="where">where?</div>
<div>
</center>
<center>
<div class="search-field1">
</div>
<div class="search-field2">
</div>
</center>
CSS:
.what{
display: inline;
font: 16px HelveticaNeue-Light;
color: #A9E2F3;
font-weight: bold;
height: 35px;
width: 320px;
background: #151515;
display: inline-block;
vertical-align: center;
}
.where{
display: inline;
font: 16px HelveticaNeue-Light;
color: #A9E2F3;
font-weight: bold;
height: 35px;
width: 320px;
background: #ffffff;
display: inline-block;
verticle-align: center;
}
.search-field1{
height: 35px;
width: 320px;
background: #ffffff;
border:1px solid;
border-color: #BDBDBD;
display: inline-block;
verticle-align: center;
}
.search-field2{
height: 35px;
width: 320px;
background: #ffffff;
border:1px solid;
border-color: #BDBDBD;
display: inline-block;
horizontal-align: center;
}
Thanks!
James
James is this what you are looking for? http://jsfiddle.net/jkeyes/WFPK2/
Set the line-height for your containers (in this case .what and .where to be the same as the height:
line-height: 35px;
sounds like you need float:left on your center tags
Html:
<div class="center">
<div class="what">what?</div>
<div class="where">where?</div>
</div>
<div class="center">
<div class="search-field1"></div>
<div class="search-field2"></div>
</div>
CSS:
If you use float:left on the enclosing div's it will allow the two center div's to float side by side.
.center {
float:left;
}
Another option would be to set your line height to zero on the hidden div like so:
line-height: 0px;
Then add padding to the surrounding divs. Like so:
padding-top: 10px;
Hope that helps someone.

Resources