Wrapping text within container without messing up styling in the parent container - html

I have a problem with text wrapping within my container. Working code snippet below.
In the sample above, everything works fine until the stepper-hor container has enough space to present the content:
I'd like the step-text container (box with blue border) to always stay to in line with step-additional-label container (box with green border). Step-text container (box with blue border) should also wrap the text inside when container's width shrinks.
Currently, when I set stepper-hor width to 350px, step-text container (box with blue border) goes below the box with green border:
What I wish to achieve is something like this:
I've tried using different variations of
display: inline-block;
overflow-wrap: break-word;
in lines 64-65 but that didn't work as expected and often messed up the horizontal alignment between the step-circle-active and step-text.
Any help is very much appreciated.
Here is a more editing-friendly sandbox to play around:
https://codesandbox.io/s/confident-breeze-qm4bf?file=/styles.css
EDIT: #Temani and #Daniel below suggested display: flex; which helped nicely.
Here is the codesandbox fork with implemented changes:
https://codesandbox.io/s/suspicious-wescoff-wgyny?file=/styles.css
Thank you lads.
body {
font-family: "Arial Light";
background-color: #1e1e1e;
padding-top: 60px;
}
.stepper-hor {
background-color: #252525;
display: flex;
flex-direction: column;
/* width: 350px; */
/* HERE YOU CAN CHANGE THE WIDTH OF THE CONTAINER */
}
.step-container {
border: 1px solid rgba(255, 255, 0, 0.459);
min-height: 63px;
margin: 0;
}
.step-circle-default {
display: inline-block;
height: 21px;
width: 21px;
background-color: #666666;
color: #333333;
font-size: 0.65rem;
font-weight: bold;
border-radius: 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
text-align: center;
line-height: 21px;
margin-right: 9px;
}
.step-circle-active {
display: inline-block;
height: 21px;
width: 21px;
background-color: #d85603;
color: #ffffff;
font-size: 0.65rem;
font-weight: bold;
border-radius: 50%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
text-align: center;
line-height: 21px;
margin-right: 9px;
}
.step-additional-label {
border: 1px solid rgba(0, 128, 0, 0.575);
display: inline-block;
min-width: 26px;
font-size: 0.59rem;
color: #666666;
padding-right: 21px;
}
.step-text {
border: 1px solid rgba(0, 0, 255, 0.404);
display: inline-block;
overflow-wrap: break-word;
font-size: 0.82rem;
color: #ffffff;
position: relative;
min-height: 54px;
top: 4px;
}
.step-line {
border: 1px solid #444444;
}
<div class="stepper-hor">
<p class="step-container" }>
<span class="step-circle-default">1</span>
<span class="step-additional-label">100%</span>
<span class="step-text">Take a shower</span>
</p>
<p class="step-container" }>
<span class="step-circle-default">2</span>
<span class="step-additional-label">10%</span>
<span class="step-text">Read a book</span>
</p>
<p class="step-container" }>
<span class="step-circle-active">13</span>
<span class="step-additional-label">79%</span>
<span class="step-text">
Do some activity with long description that will require more space
</span>
</p>
</div>

You should change your step-container structure and use display: flex in order to achieve the result you want.
Here is an example:
HTML
<div class="step-container">
<div class="stats">
<span class="step-circle-active">13</span>
<span class="step-additional-label">79%</span>
</div>
<p class="step-text">
Do some activity with long description that will require more space
</p>
</div>
CSS
.step-container {
display: flex;
}
.step-container .stats {
display: flex;
}
There is no point in creating the step-container as a p element with multiple spans in it.

Adding CSS property display: flex; to the container .step-container will do the thing.

Related

how to get the same line through different <p> Tags

am trying to use this css property "text-decoration: line-through;" on two different Tags and without breaking the line
.pricereview {
display: flex;
justify-content: space-between;
align-items: flex-start;
width: 100%;
}
.price {
display: flex;
align-items: end;
text-decoration: line-through;
}
.prixunit {
color: lightgrey;
font-size: 20px;
line-height: 20px;
}
.tnd {
color: lightgrey;
font-size: 13px;
line-height: 26px;
padding-left: 5px;
}
<div class="pricereview">
<div class="price">
<p class="prixunit">560</p>
<p class="tnd">TND</p>
</div>
</div>
This is the result that I want:
Screenshot here
I'd suggest using <span> tags and wrapping them in another element. Use a pseudo-element on the wrapper to make your "linebreak" span multiple elements of different font sizes and line heights without breaking.
.price {
position: relative;
display: inline-block;
}
.price::after {
content: '';
position: absolute;
top: 45%;
left: 0;
width: 100%;
border-bottom: solid 1px #000;
}
.prixunit {
font-size: 20px;
line-height: 20px;
}
.tnd {
font-size: 13px;
line-height: 26px;
padding-left: 5px;
}
<p class="price">
<span class="prixunit">560</span>
<span class="tnd">TND</span>
</p>
You cant do that with a p tag, cause its a block-level element
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/p
And it seems that in your case you want to make a line that is not cutting exactly in the middle of the word. If its your case, text-decoration: line-through dont will help you.

Is it possible to make a text inside horizontal line like i'm doing?

I don't get it what i doing wrong here. I want the small text to look a like the big one without fixed values (run the snipped to see)
Have some way to do this that don't involves text being surrounded by two pseudo-elements?
.ruler {
border-bottom: 1px solid black;
text-align: center;
height: 0px;
}
.text1 {
background-color: white;
padding: 0px 2px;
transform: translateY(-50%);
display: inline-block;
font-weight: 500;
font-size: 12px;
}
.text2 {
background-color: white;
padding: 0px 2px;
transform: translateY(-50%);
display: inline-block;
font-weight: 500;
font-size: 16px;
}
<div class="ruler">
<span class="text1">MyText</span>
</div>
<br />
<br />
<div class="ruler">
<span class="text2">MyText</span>
</div>
I am curious to what you mean by I want the small text to look a like the big one without fixed values (run the snipped to see) I have an example of making them the same. Can you please provide some clarification below if this is not what you are looking for? Possibly a screenshot example of what you are looking for in your original question may be the most helpful.
.ruler {
border-bottom: 1px solid black;
text-align: center;
height: 0px;
}
.text {
background-color: white;
padding: 0px 2px;
transform: translateY(-50%);
display: inline-block;
font-weight: 500;
font-size: 16px;
}
<div class="ruler">
<span class="text">MyText</span>
</div>
<br />
<div class="ruler">
<span class="text">MyText</span>
</div>
you can use
::after or ::before
.title {
width: 100%;
text-align: center;
position: relative;
}
.title::before {
content: '';
z-index: -1;
width: 100%;
height: 2px;
background: #000;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
span.text2 {
font-size: 18px;
background: #fff;
}
<div class="title">
<span class="text2">MyText</span>
</div>
I used :
:before
it's relative to the div that has the span, I center it with top, left, and transform properties will make it however the size of the span, it will be always in the center
Note: that it's a line passing through the whole div, so I gave the span background so it will cover it, and give you the possibility to control the padding from the text and line
hope it's useful
Sorry for all the confusion, I eventually solved the problem by going the other way around.
I would delete this question and add this answer to a related question, but I can do neither.
Whatever... here's the answer:
.ruler {
border-bottom: 1px solid black;
text-align: center;
}
.text1 {
background-color: white;
padding: 0px 2px;
transform: translateY(50%);
display: inline-block;
font-weight: 500;
font-size: 12px;
padding-top: 2px;
}
.text2 {
background-color: white;
padding: 0px 2px;
transform: translateY(50%);
display: inline-block;
font-weight: 500;
font-size: 16px;
padding-top: 2px;
}
<div class="ruler">
<span class="text1">MyText</span>
</div>
<br />
<br />
<div class="ruler">
<span class="text2">MyText</span>
</div>
The padding-top: 2px acts the same with all text sizes, because it's there to compensate the border size.

Padding on div changes height and width of parent container

I was wondering if there is an alternative method to achieving the results shown in the attached picture. I used absolute positioning to get .resume-icon-container to sit flush with .resume-container.
Every time I tried to add padding or height/width to .resume-icon-container it would undesirably resize .resume-container. I experimented with overflow: auto and the z-index but the only way I could achieve the results I want is with absolute positioning and adding margin-left to position it then padding and font-size to make it flush with .resume-container. I was browsing similar questions as well and someone said to add box-sizing: border-box but I already declared that setting in my CSS reset under the * selector.
I would prefer to stay away from absolute positioning for responsive purposes, so I was wondering if there is another way to achieve what I want.
This is the desired result:
.resume-container {
display: flex;
flex-direction: row;
background: rgba(144, 144, 144, 0.3);
margin-top: 20px;
border-radius: 20px;
padding: 20px;
width: 350px;
margin-left: auto;
margin-right: auto;
align-items: center;
justify-content: space-between;
}
.resume-container h1 {
color: #fff;
font-size: 25px;
}
.resume-icon-container {
background: rgba(196, 196, 196, 0.3);
padding: 20px;
position: absolute;
margin-left: 268px;
border-radius: 20px;
font-size: 10px;
}
.resume-icon-container i {
color: #fff;
}
<div class="resume-container">
<h1>Download Resume</h1>
<div class="resume-icon-container">
<i class="fa-solid fa-file fa-3x"></i>
</div>
</div>
Remove the absolute positioning and padding and use margin-left on your h1.
.resume-container {
display: flex;
flex-direction: row;
background: rgba(144, 144, 144, 0.3);
margin-top: 20px;
border-radius: 20px;
width: 350px;
margin-left: auto;
margin-right: auto;
align-items: center;
justify-content: space-between;
}
.resume-container h1 {
color: #fff;
font-size: 25px;
margin-left: 1em;
}
.resume-icon-container {
background: rgba(196, 196, 196, 0.3);
padding: 20px;
border-radius: 20px;
}
.resume-icon-container i {
color: #fff;
width: 100%;
}
<script src="https://kit.fontawesome.com/6140596fcb.js" crossorigin="anonymous"></script>
<div class="resume-container">
<h1>Download Resume</h1>
<div class="resume-icon-container">
<i class="fa-solid fa-file fa-3x"></i>
</div>
</div>
The way you are tuning those margins and paddings, you will always be looking at something different depending on the size of the screen. You need to use relative positioning so that the items appear on the screen the same regardless of the number of pixels. It can also get confusing to mix margin with padding. Margin will push the element from its nearest element while padding will push elements inside that element away from the left,top,etc.
I like to start by creating a container for each element so that we can design each new div element like its own page.
Consider the following code:
<div id="View">
<div id="OptionBlock">
<div id="Options1">
<div id="AddDocument" class="options">Add New Document<div id="DocumentIcon"></div></div>
<div id="AddTemplate" class="options">Add New Template<div id="TemplateIcon"></div></div>
</div>
<div id="Options2">
<div id="ChangeSignature" class="options">Change Your Signature<div id="SignatureIcon"></div></div>
<div id="Settings" class="options">Settings and Subscription<div id="SettingsIcon"></div></div>
</div>
</div>
</div>
#View {
width: 100%;
height: 60%;
}
#OptionBlock {
width: 100%;
}
#Options1 {
width: 100%;
float: left;
}
#Options2 {
width: 100%;
float: left;
}
#AddDocument {
float: left;
padding: 5px;
width: 25%;
height: 38%;
margin-left: 24%;
margin-right: 2%;
margin-top: 2%;
text-align: center;
border: 2px solid black;
border-radius: 25px;
background-color: #ffffff;
font-size: x-large;
font-weight: bold;
}
#AddTemplate {
float: left;
padding: 5px;
width: 25%;
height: 38%;
margin-top: 2%;
margin-right: 24%;
text-align: center;
border: 2px solid black;
border-radius: 25px;
background-color: #ffffff;
font-size: x-large;
font-weight: bold;
}
Notice how I treat the outer boxes as large containers, defining all the total width and height we need, then leaving it to the css for particular elements showing content to position themselves within that container on the screen. The width and left and right margins of elements #AddDocument and #AddTemplate add up to 100% width so that the entire box it is placed in is accounted for.
Preview CSS Placements (this renders dead center at the top of the webpage)
It's just a matter of playing with the css.
For this kind of "trial and error" problem you should use CodePen or similar. It'll make your life much easier.
.resume-container {
display: flex;
flex-direction: row;
background: rgba(144, 144, 144, 0.3);
margin-top: 20px;
border-radius: 20px;
/* padding: 20px;*/
width: 350px;
margin-left: auto;
margin-right: auto;
align-items: center;
justify-content: space-between;
}
.resume-container h1 {
color: #fff;
font-size: 25px;
padding: 20px;
margin: 0;
}
.resume-icon-container {
background: rgba(196, 196, 196, 0.3);
padding: 20px;
float: right;
/*margin-left: 268px;*/
border-radius: 20px;
/*font-size: 10px;*/
height: 100%;
}
.resume-icon-container i {
color: #fff;
}
.bi {
font-size: 2rem;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.8.1/font/bootstrap-icons.css" rel="stylesheet" />
<div class="resume-container">
<h1>Download Resume</h1>
<div class="resume-icon-container">
<i class="fa-solid fa-file fa-3x"></i>
<i class="bi bi-file-earmark-text"></i>
</div>
</div>

Firefox and Safari show inline-block elements aligned differently inside other block

I have inline block element (red circle) inside other block with some paddings. In Firefox this looks correctly and circle located at the middle of block occupied by equal paddings. However in Safari this looks incorrectly - circle a little bit moved to top and not centered vertically. I tried different vertical-align and line-height settings but this does not help, in Safari this still looks to different not like in Firefox.
HTML:
<div class="post-categories">
<a href="#">
<span class="cat-dot"></span>Uncategorized
</a>
</div>
CSS:
body {
line-height: 1.7;
font-family: Arial;
}
.post-categories {
position: relative;
font-size: 14px;
margin-bottom: 10px;
font-weight: bold;
text-transform: lowercase;
display: flex;
flex-wrap: wrap;
}
.post-categories a {
font-weight: normal;
text-decoration: none;
padding: 1px 15px 1px 10px;
background-color: #ffffff;
border: 1px solid #000;
margin-bottom: 5px;
margin-right: 10px;
border-radius: 5px;
}
.post-categories .cat-dot {
display: inline-block;
width: 13px;
height: 13px;
position: relative;
top: 1px;
line-height: 14px;
border-radius: 100%;
margin-right: 10px;
background-color: red;
}
Codepen where you can check this: https://codepen.io/dedalx/pen/xxbbMOo
Question - How I can make circle and text vertically centered in parent block in the same way in all modern browsers?

HTML button only uses width and height of text in the button

I have a small problem. I am trying to change the width and height of a button but for some reason, it will not let me. The button automatically stays the same width and height as the contained text.
CSS
.flexcontainer {
display: flex;
align-items: center;
overflow: hidden;
}
img[width="500"] {
border: 3px solid #5F5F5F;
border-radius:3px;
float: left;
}
#leftRetail {
display: block;
height:354px;
width: 1308px;
float:right;
text-align: center;
vertical-align: middle;
line-height: 354px;
}
.button {
width: 250px;
height: 200px;
background: #ed2626;
border-radius: 2px;
color: white;
font-weight: bold;
text-decoration:none;
}
HTML
<div class="flexcontainer">
<div>
<img src="anyImage.jpg" width="500" height="350"/>
</div>
<div id="leftRetail">
Retail Menu
</div>
</div>
You need to change your .button to use display: block or inline-block:
.button {
display: block;
width: 250px;
height: 200px;
background: #ed2626;
border-radius: 2px;
color: white;
font-weight: bold;
text-decoration:none;
}
CHANGED ANSWER after copying the original code into a snippet:
I just realized that the whole thing is inside a flex container, which makes all child elements flex items automatically. (BTW: The float parameters have no effect in this case)
So, one method to add width and height to your .button is to give it some padding, as shown below:
.flexcontainer {
display: flex;
align-items: center;
overflow: hidden;
}
img[width="500"] {
border: 3px solid #5F5F5F;
border-radius: 3px;
}
#leftRetail {
height: 354px;
width: 1308px;
text-align: center;
vertical-align: middle;
line-height: 354px;
}
.button {
background: #ed2626;
border-radius: 2px;
color: white;
font-weight: bold;
text-decoration: none;
padding: 8px 12px;
}
<div class="flexcontainer">
<div>
<img src="anyImage.jpg" width="500" height="350" />
</div>
<div id="leftRetail">
Retail Menu
</div>
</div>
You cannot modify the width and height of inline elements, manually.
Add display: block; (or inline-block) to your .button block, and you can observe that the height and width changes are you define it.
Only block elements may have their width and height set specifically.
Your button should now look like:
.button {
width: 250px;
height: 200px;
background: #ed2626;
border-radius: 2px;
color: white;
font-weight: bold;
text-decoration:none;
display: block;
}
Just make it block-level element by adding display:bock to its style. Then you can apply whatever style you want!