How to aligning Pill items and generally improve my code? - html

I'm building my own Pill component using HTML:
.panel1 {
width: 100%;
}
.panel2 {
width: 100px;
}
.pill {
display: inline-block;
}
.pill-content {
display: flex;
flex-direction: row;
font-size: 12px;
vertical-align: middle;
padding: 0px;
background-color: white;
border: solid;
border-width: 1px;
border-radius: 4px;
color: black;
border-color: blue;
background-color: white;
}
.pill-text {
flex: 1;
}
.pill-icon {
flex: 1;
max-width: 18px;
padding: 0px 5px 0px 5px;
}
<div class="panel1">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Why is that text breaking as it fits in width?
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>
<div class="panel2">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Test
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Very big text that does not fit in width
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div>
</div>
My problems:
I need to position text always on the left side of the Pill.
I need to keep the close icon (I´m using an X here but that will be a font awesome icon) always vertically centered.
I can´t see why my text is breaking on panel1. I expected the text to break only when the panel size is smaller that text + icon.
Can I make that HTML/CSS simpler?
JSFiddle here

I've made some modifications to your code to give you the desired result:
/* recommended */
.panel1 *,
.panel2 * {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.panel1 {
/*width: 100%; by default*/
}
.panel2 {
width: 100px;
}
.pill {
display: inline-flex; /* modified, since you're using flexbox */
}
.pill-content {
display: flex;
/*flex-direction: row; by default*/
font-size: 12px;
/*vertical-align: middle; has no effect here*/
align-items: center; /* now its vertically centered */
/*padding: 0; already covered*/
border: 1px solid blue; /* shorthand */
border-radius: 4px;
}
.pill-text {
/*flex: 1; not necessary, the culprit for breaking*/
}
.pill-icon {
/*flex: 1; not necessary since you're using max-width*/
max-width: 18px;
padding: 0 5px;
text-align: center; /* for horizontal alignment */
}
<div class="panel1">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Why is that text breaking as it fits in width?
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>
<div class="panel2">
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Test
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
<div class="pill">
<div class="pill-content">
<div class="pill-text">
Very big text that does not fit in width
</div>
<div class="pill-icon">
X
</div>
</div>
</div>
</div>

Here are some simplification tips (as I cannot comment):
For all values that are 0, you can drop the units (px, etc.)
You can shorten your code by using the border shorthand: border: 1px solid blue. Luckily there's no need to specify each attribute individually
background-color: white; is specified twice for .pill-content, unless I'm missing something.

Related

Heading position alignment [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 7 months ago.
div.c {
width: 25px;
height: 10px;
background-color: black;
margin: 12px 0;
border-radius: 5px;
display: block;
}
<body>
<div class="header" ,style="display: inline">
<div class="menu">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
<h1>GeekForGeek</h1>
Here I want GeekForGeek to be in the center in the position which is marked yellow here
But it is getting displayed as shown in fig.
For example using a three columns layout (via display flex) on your header.
I made a demo to show the concept:
div.c {
/*width: 60px;*/
width: 60px;
height: 10px;
background-color: black;
margin: 12px 0;
border-radius: 5px;
display: block;
}
.header {
display: flex;
flex-direction: row;
width: 100%;
}
.header > div {
/*border just for educational purpose.. remove it*/
border: dashed 3px gray;
flex: 33.33%;
}
.header h1{
text-align: center;
}
/*-----------------------*/
.contents{
border: dashed 3px gray;
margin-top: 1rem;
font-size: 1.5rem;
padding: 1rem;
}
.contents ol li{
margin-bottom: 1rem;
}
<div class="header">
<div class="menu">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
<div>
<h1>GeekForGeek</h1>
</div>
<div>
</div>
</div>
<div class="contents">
<ol>
<li>I took the freedom to change the width of your hamburger menu selector, because it's common practice to have it more square shaped.. yours was very narrow in size;</li>
<li>The 3 columns layout grants you that the block in the middle will be perfectly centered related to the full width of the viewport;</li>
<li>The side effect is having to include the third column despite being empty;</li>
<li>The borders on header's blocks should be removed in the corresponding css rule and were added just to better show off the layout;</li>
</ol>
</div>
use flex and margin auto for h1
div.c {
width: 25px;
height: 10px;
background-color: black;
margin: 12px 0;
border-radius: 5px;
display: block;
}
.header {
display: flex;
}
h1 {
margin-left: auto;
margin-right: auto;
}
<body>
<div class="header" ,style="display: inline">
<div class="menu">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
<h1>GeekForGeek</h1>
</div>

4 responsive divs are not showing properly on tab and mobile

I am trying to create 4 responsive divs for homepage; tried to decreae padding and margin of div not working.divs should be responsive on mobile devices they shpuld move below each one ; two divs appear on tabs; 4 divs on desktop and one div on mobile
i have tried following HTML:
<div class="flex-container">
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
<div class="flex-item">
<div class="flex-item-inner">
<div class="homepage-div homepage-div-shadow">
<img class="homepage-div-icon"/>
<h2>Heading</h2>
<h6>Sub-Heading</h6>
<p>This is responsive paragraph. Text would not move out of div as below .</p>
<button class="bttn">Practice Now</button>
</div>
</div>
</div>
</div>
here is css divs are not showing properly on tab and mobile devices and i want to decreae the padding and margin:
.homepage-div-shadow
{
padding: 20px;
box-shadow: 2px 2px 8px 1px #cccccc;
border: solid 1px #cccccc;
border-radius: 2px;
}
.homepage-div-icon{
background-image: url("image.png");
width: 100px;
height: 100px;
}
.homepage-div a
{
text-decoration: none;
color: white;
font-size: 16px;
}
.homepage-div h2{
font-size: 28px;
opacity: 0.90;
font-weight: 600;
margin: 5%;
margin-bottom: 6px;
}
.homepage-div h6{
font-size: 17px;
padding: 0px;
margin: 0px;
margin-bottom: 20px;
opacity: 0.6;
}
#media(min-width: 769px) {
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-container .flex-item {
flex: 1 0 50%;
}
}
#media (min-width: 1024px) {
.flex-container .flex-item {
flex: 1 0 25%;
}
}
.flex-item-inner {
padding: 25px;
margin: 20px;
background-color: white;
}
Here is the demo link: testfellow
Hope the solution below helps. I have just tried to mimic the scenario where there are a certain number of div s. And as per your question and requirement, have:
1. Followed mobile-first approach.
2. For mobile devices, each div should come in separate lines.
3. For tabs (at your breakpoint: 769px and above), two divs should come in a line.
4. For desktops (at your breakpoint: 1024px and above), four divs should come in a line.
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
background-color: #2980b9;
}
.flex {
max-width: 80%;
}
.flex-item {
background-color: #ffffff;
padding: 5em;
border: 1px solid black;
margin: 2% auto;
width: 25%;
}
#media(min-width: 769px) {
.flex {
display: flex;
flex-wrap: wrap;
}
.flex-item {
width: 50%;
}
}
#media(min-width: 1024px) {
.flex {
display: flex;
flex-wrap: wrap;
}
.flex-item {
width: 25%;
}
}
<div class="flex">
<div class="flex-item"></div>
<div class="flex-item "></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item "></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>

Full width divs with max width content

I'm having issues having full width divs that have different % widths and also keeping the content fixed within it and lining it up with another full width div. The JS Fiddle should be better at explaining this.
<header> <!-- 100% width -->
<div></div> <!-- 100% width; max-width: 1000px; margin: auto -->
</header>
The above works fine if it was a single column.
I want to have two divs below it, one taking up 33% and the other taking up 67% and keeping the content within these lined up similar to how the above is working. The max-width div is the visible content container. So if you were viewing the site on a large screen everything would be edge to edge, but the content within would be framed in the middle.
Sample fiddle, where the divs with 2 and 3 should take up the same amount of space as the div above it. http://jsfiddle.net/qtLe7o8f/1/
header {
background: blue;
padding: 15px 0;
}
header div {
max-width: 500px;
background: red;
margin: auto;
}
section.one {
float: left;
width: 33%;
background: green;
padding: 15px 0;
}
section.one div {
background: red;
float: right;
}
section.two {
float: left;
width: 67%;
background: orange;
padding: 15px 0;
}
section.two div {
background: red;
float: left;
}
<header>
<div>
1
</div>
</header>
<section class="one">
<div>
2
</div>
</section>
<section class="two">
<div>
3
</div>
</section>
I think this is what you're going for? It's setup with some nested flexbox definitions so that the outer containers stretch edge to edge, but the child elements stick to a max-width and share the available space.
header {
background: blue;
padding: 15px 0;
display: flex;
justify-content: center;
}
header div {
flex: 0 0 500px;
background: red;
}
.container {
display: flex;
justify-content: center;
background-color: #ace;
}
.content {
display: flex;
flex-flow: row wrap;
width: 500px;
}
section.one {
flex: 0 0 33%;
background: green;
padding: 15px 0;
}
section.two {
flex: 0 0 67%;
background: orange;
padding: 15px 0;
}
section.one div, section.two div {
background: red;
}
<header>
<div>
1
</div>
</header>
<div class="container">
<div class="content">
<section class="one">
<div>
2
</div>
</section>
<section class="two">
<div>
3
</div>
</section>
</div>
</div>
If you're looking to extend the orange and green background colors to the edges of the desktop screen this should do it for your. This will also reorder your columns for mobile devices thanks to the use of a Bootstrap Grid
header {
background: blue;
padding: 15px 0;
}
.custom-container {
background: linear-gradient(to right, green 0%,green 50%,orange 50%,orange 50%,orange 100%);
}
.section-1-container {
background: red;
}
.row .section-2-container {
background: green;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 0px;
padding-right: 0px;
}
section.one div {
background: red;
}
.row .section-3-container {
background: orange;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 0px;
padding-right: 0px;
}
section.two div {
background: red;
}
header .row>div,
section>div {
text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<header>
<div class="container">
<div class="row">
<div class="col-sm-12 section-1-container">
1
</div>
</div>
</div>
</header>
<div class="custom-container">
<div class="container">
<div class="row">
<div class="col-sm-4 section-2-container">
<section class="one">
<div>
2
</div>
</section>
</div>
<div class="col-sm-8 section-3-container">
<section class="two">
<div>
3
</div>
</section>
</div>
</div>

How to make div (inside another div) horizontally and vertically align

I have 3 divs which are horizontally aligned (aqua color). Inside each div, there are two divs (red and black one).
What I am trying to do is, align the black divs horizontally regardless of the red div. The css for the black div is
.black-div {
width: 100%;
height: 45px;
max-width: 235px;
display: inline-block;
color: #33244a;
font-size: 16px;
text-transform: uppercase;
font-weight: normal;
text-align: center;
line-height: 43px;
border: 2px dashed #d5d1d8;
border-radius: 6px;
box-sizing: border-box;
}
Output will something like this
I am not good at all in css. I have tried using position: fixed / absolute but no luck.
Try it.
Use div and min-height.
section{
display: inline-block;
border: 1px solid red;
width: 100px;
}
.textarea-wrap{
overflow: hidden;
min-height: 200px;
}
.textarea-wrap > textarea{
width: 100%;
resize: none;
}
.red{
background-color: red;
}
<div>
<section>
<div class="textarea-wrap">
<textarea rows="3">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
<section>
<div class="textarea-wrap">
<textarea rows="10">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
<section>
<div class="textarea-wrap">
<textarea rows="6">12312312</textarea>
</div>
<div class="red">
red
</div>
</section>
</div>
You should use table to make it more manageable, or use absolute positioning on the black div so you can position them measure from the bottom of the blue div.
There may be a solution without the spacer. Im looking for it :)
found solution without spacer justify-content: space-between;
.wrapper {
display: flex;
flex-direction: row; /* flex in a row inside (make columns .col) */
}
.col {
display: flex;
flex-direction: column; /* flex in a column inside */
justify-content: space-between; /* since the elements must not grow, fill the space between them */
flex: 1 1 100px; /* grow and shrink of col allowed to fill row evenly starting at 100px*/
margin: 5px;
border: 3px solid black;
background-color: aqua;
}
.red {
flex: 0 1 auto; /* no vertical (col) growing (so it does not expand vertically) */
border: 3px solid black;
border-radius: 10px;
background-color: red;
margin: 5px;
padding: 10px;
}
.black {
background-color: black;
color: white;
margin: 5px;
padding: 10px;
display: block;
flex: 0 1 auto; /* no growing allowed */
}
.resize {
overflow: hidden;
resize: vertical;
}
<div class='wrapper'>
<div class='col'>
<div class='red'>Some wide wide wide wide wide wide Text</div>
<div class='black'>Footer</div>
</div>
<div class='col'>
<div class='red'>Some<br/>much<br/>longer<br/>Text</div>
<div class='black'>Footer</div>
</div>
<div class='col'>
<div class='red resize'>Some Text<br><b><u>Resize me!</u></b></div>
<div class='black'>Footer</div>
</div>
</div>
Edit removed spacer div
Edit2 added css commenting for easier understanding

Background of div does not scroll with the contents

I have the following snippet of html that forms an X-Y scrollable listbox
* {
font-family: "consolas";
}
.listbox {
border: 1px solid black;
padding: 4px;
width: 150px;
height: 200px;
display: flex;
flex-direction: column;
}
.caption {
font-weight: bold;
background-color: #aaf;
padding: 10px;
}
.content {
flex-grow: 1;
overflow: scroll;
}
.item {
background-color: #ddd;
padding: 2px;
padding-left: 6px;
margin-top: 4px;
white-space: nowrap;
}
<div class="listbox">
<div class="caption">Caption</div>
<div class="content">
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three (this has a longer bit)</div>
<div class="item">Four</div>
<div class="item">Five</div>
<div class="item">Six</div>
<div class="item">Seven</div>
<div class="item">Eight (so does this)</div>
<div class="item">Nine</div>
<div class="item">Ten</div>
</div>
</div>
It's working fine, with one problem, as the user scrolls from left to right, the background of the div seems to get left behind. It's as though the actual div only stretches the width of its parent, and the scrolling/overflow thing is "faked" somehow.
Why is this the case?
How do I address the problem? The behaviour I want is for all the items to appear to be the same width as the largest one.
Try adding a container <div class="items"> around the items set it to display:inline-block.
.items {
display: inline-block;
}
* {
font-family: "consolas";
}
.listbox {
border: 1px solid black;
padding: 4px;
width: 150px;
height: 200px;
display: flex;
flex-direction: column;
}
.caption {
font-weight: bold;
background-color: #aaf;
padding: 10px;
}
.content {
flex-grow: 1;
overflow: scroll;
}
.items {
display: inline-block;
}
.item {
background-color: #ddd;
padding: 2px;
padding-left: 6px;
margin-top: 4px;
white-space: nowrap;
}
<div class="listbox">
<div class="caption">Caption</div>
<div class="content">
<div class="items">
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three (this has a longer bit)</div>
<div class="item">Four</div>
<div class="item">Five</div>
<div class="item">Six</div>
<div class="item">Seven</div>
<div class="item">Eight (so does this)</div>
<div class="item">Nine</div>
<div class="item">Ten</div>
</div>
</div>
</div>
Explanation: by default a block level element takes 100% width of the container no more than that, however an inline block will expand to content length if available e.g. in a scrollable container.
Also apply .items {min-width: 100%;} in case you want the background to grow full width even with less text in every row.