I was unable to find any solution to my problem. I don't want any space between child elements so i have added float:left to child elements. But it is causing problem when elements breaks into 2 lines.
HTML :
<span class="section-title">
<span class="yellow">This</span>
<span class="red">is</span>
<span class="brown">test</span>
<span class="">content</span>
</span>
CSS :
.section-title {text-align:center;}
.section-title span {float: left; display:inline-block;}
To remove the unwanted space, don't float: left;, but set font-size: 0; on the parent element, re-setting it on the children to the font-size you want.
Also, you used a span for the container which by default has display: inline;. Here, you want an element that has display: block;. With display: inline; the text-centering cannot work.
So either set display: block; on the span or use a div instead of a span. div has display: block; by default. I'd recommend the latter, but I'm not sure whether you have control over the HTML, so I did not change it.
.section-title {
text-align: center;
font-size: 0;
display: block;
}
.section-title span {
font-size: 36px;
display: inline-block;
text-transform: uppercase;
}
.yellow {
color: yellow;
}
.red {
color: red;
}
.brown {
color: brown;
}
<span class="section-title">
<span class="yellow">This</span>
<span class="red">is</span>
<span class="brown">test</span>
<span>content</span>
</span>
Try following code :
.section-title {
text-align: center;
display: block;
}
.section-title span {
font-size: 36px;
display: inline-block;
text-transform: uppercase;
}
Related
I am trying to display array of text in rectangle boxes like this.
but I got it like text displayed as entire row
below is the code snippet
<span *ngFor="let mfal of mfals" class="review-span mfal">
{{mfal}}
</span>
.mfal {
background-color: #7A7A7A;
color: white;
}
appreciate any help
Here is what you can do:
<span *ngFor="let mfal of mfals" class="review-span mfal">
{{mfal}}</span>
.mfal {
display: inline-block;
background-color: #7A7A7A;
color: white;
padding: 5px;
margin: 0 5px;
}
Make sure your element has a display of inline-block or inline, add corresponding padding and margin.
display attributes of elements might be set to block somewhere in your code.
.mfal {
display: block;
background-color: #7A7A7A;
color: white;
}
<span class="review-span mfal">WANAB</span>
<span class="review-span mfal">WANAB</span>
Try display: inline; or display: inline-block;.
.mfal {
display: inline;
background-color: #7A7A7A;
color: white;
}
<span class="review-span mfal">WANAB</span>
<span class="review-span mfal">WANAB</span>
This should be simple, but I am misunderstanding CSS behavior.
The searchbar-title-group should occupy a full line and contain a left-justified title and 2 right-justified buttons. The buttons group is right-justified, but it appears on the next line. Why? And how should I fix this?
#searchbar-add-item {
font-size: 24px;
}
#searchbar-title {
font-size: x-large;
visibility: visible;
white-space: nowrap;
}
#searchbar-title-buttons-group {
float: right;
}
#searchbar-title-group {
display: block;
}
#searchbar-toggle-button {
font-size: 24px;
}
<div class="searchbar-title-group">
<span class="" id="searchbar-title">Search Entries.</span>
<span id="searchbar-title-buttons-group">
search
add_box
</span>
</div>
Edit: The searchbar-title-group is enclosed by a div with class "container." Here is the style as shown in the inspector:
.container {
width: 100%;
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
Container is wrapped in body. Here is body's style per inspector:
body {
margin: 0;
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #212529;
text-align: left;
background-color: #fff;
}
The inspector also shows body's style is block (from user agent stylesheet).
The inspector also shows that .container inherits a style of block from the user agent stylesheet for <div>. I don't know how that got assigned like that.
I would use flex
#searchbar-add-item {
font-size: 24px;
}
.searchbar-title-group {
width:100%;
display:flex; /* flex */
flex-direction: row; /* layout children in a row */
flex-wrap:nowrap; /* don't let children wrap */
justify-content:space-between; /* add space in between children so whole line is taken */
}
#searchbar-title {
font-size: x-large;
white-space: nowrap;
}
#searchbar-toggle-button {
font-size: 24px;
}
<div class="searchbar-title-group">
<span class="" id="searchbar-title">Search Entries.</span>
<span id="searchbar-title-buttons-group">
search
add_box
</span>
</div>
Here are a couple of good sites for learning flex and what it can do:
Flexbox codepen playground
Css Tricks - Complete guide to flexbox
Here's an HTML snippet:
<div class="source">
<h1 class="source-text">dar</h1>
<span class="sound">
<i class="fa fa-volume-up pronounce"></i>
</span>
</div>
And it renders as:
As you can see, the icon is bottom-aligned with the H1 tag. I need it center-aligned, instead. I have tried the following different ideas with my CSS:
Adding negative padding to the span:
.sound {
padding-left: 1.5em;
padding-top: -0.5em;
}
.pronounce {
font-size: 2em !important;
}
Adding negative padding to the icon:
.sound {
padding-left: 1.5em;
}
.pronounce {
font-size: 2em !important;
padding-top: -0.5em;
}
Adding negative margin to the span:
.sound {
padding-left: 1.5em;
margin-top: -0.5em;
}
.pronounce {
font-size: 2em !important;
}
Adding negative margin to the icon:
.sound {
padding-left: 1.5em;
}
.pronounce {
font-size: 2em !important;
margin-top: -0.5em;
}
I even tried vertical-align: middle on the span but the alignment is still unchanged. Any workaround?
Try this:
.source > .sound {
display: flex;
align-items: center;
}
or
.source { display: flex; }
.sound { align-self: center; }
or
.source { display: flex; }
.sound { display: flex; align-self: center; }
.pronounce { align-self: center; }
(without seeing a working demo, it's hard to tell which option, if any, is best)
you may do it like this:
h1, span{
display: inline-block;
vertical-align: middle;
}
You can try using the css line-height property. Link to explanation
What you do is you take the height of the "wrapper" <div> of the <h1> and the <i> and you set the line height to equal to that height.
Sample HTML
<div class="wrapper">
<h1>Some text</h1><i></i>
</div>
Sample CSS
.wrapper {
height:200px;
line-height:200px;
}
Hope this helps!
Today I was having an awful lot of trouble getting some small text vertically centered within elements that were circle which were created using border-radius.
Some of the elements looked fine, but one in particular (a lowercase e was too close to the bottom); I had 2px of padding and it seemed to look fine; however once viewed on a mobile device it was slightly lower.
Here is some code that is as close of a replica as I could come up with to show the issue; you will notice this text has a similar issue with the lowercase e being too close to the bottom.
HTML:
<div class="option">
<span class="icon">t</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">f</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">e</span>
<span class="text">moo#moo.com</span>
</div>
CSS:
.option {
margin-bottom: 10px;
font-family: 'Source Sans Pro', sans-serif;
font-size: 14px;
}
.option .icon {
display: inline-block;
text-align: center;
width: 24px;
height: 24px;
line-height: 24px;
color: #fff;
border-radius: 50%;
background-color: blue;
}
.option .text {
padding-left: 10px;
}
JSFiddle: http://jsfiddle.net/7bygsgn1/7/
Whilst I haven't tried them with the particular code on jsfiddle, when I was having the issue today I tried a whole range of centering techniques including:
Using line-height
Using absolute positioning
Using vertical-align: middle; in conjunction with display: table-cell;
Negative Margins
Using the method explained here.
Either it had no affect on the centering or caused the shape of the circle to change.
Is there any way you can reliably vertically center in situations such as this?
You may use an inline-block pseudo-element with an height of 24px / 100% , and vertical-align it to middle.
.option {
margin-bottom: 10px;
font-family: 'Source Sans Pro', sans-serif;
font-size: 14px;
}
.option .icon {
display: inline-block;
text-align: center;
width: 24px;
height: 24px;
color: #fff;
border-radius: 50%;
background-color: blue;
}
/* here the pseudo-element method */
.option .icon:before {
content: '';
display: inline-block;
padding-top: 100%;/* cause here we have a square and width for percentage vertical (padding/margin) is the reference , height:100%; or height:24px; will do as well */
vertical-align: middle;
}
/* end update */
.option .text {
padding-left: 10px;
}
<div class="option">
<span class="icon">t</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">f</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">e</span>
<span class="text">moo#moo.com</span>
</div>
or display:flex; , the most simple:
.option {
margin-bottom: 10px;
font-family: 'Source Sans Pro', sans-serif;
font-size: 14px;
}
.option .icon {
/* next-three lines to center content both axis */
display: inline-flex;
justify-content:center;
align-items:center;
/*text-align: center;*/
width: 24px;
height: 24px;
color: #fff;
border-radius: 50%;
background-color: blue;
}
.option .text {
padding-left: 10px;
}
<div class="option">
<span class="icon">t</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">f</span>
<span class="text">123456789</span>
</div>
<div class="option">
<span class="icon">e</span>
<span class="text">moo#moo.com</span>
</div>
Vertically/Horizontally center anything inside a parent element without knowing the heights/widths of either:
/* This parent can be any width and height */
.parent {
text-align: center;
/* May want to do this if there is risk the container may be narrower than the element inside */
white-space: nowrap;
}
/* The ghost, nudged to maintain perfect centering */
.parent:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can also be of any width and height */
.centered {
display: inline-block;
vertical-align: middle;
}
Essentially this creates a ghost element inside the parent that allows the child to be positioned relative to it. The height: 100% allows the vertical-align: middle to do its job properly.
Hope this helps!
Edit: Credit to https://css-tricks.com/centering-in-the-unknown/
I can't seem to understand why certain span properties (in particular, the width and text-align of "controller-row-number" and "controller-row-name"):
#controller {
width: 250px;
float: left;
font-size: 14px;
line-height: 1.5;
text-align: left;
}
.controller-row {
background-color: blue;
}
.controller-row-number {
background-color: yellow;
width: 60px;
text-align: right;
padding: 0 15px 0 0;
}
.controller-row-name {
background-color: orange;
width: 150px;
text-align: left;
padding: 0 0 0 0;
}
Are being ignored in the following code:
<div id="controller">
<div class="controller-row">
<span class="controller-row-number">1</span>
<span class="controller-row-name">First Name</span>
</div>
<div class="controller-row">
<span class="controller-row-number">2</span>
<span class="controller-row-name">Second Name</span>
</div>
</div>
I have a JSFiddle located here:
http://jsfiddle.net/WZFJD
Can anyone point me to the correct edits to make, so that the styles are adhered?
Thanks!
display: inline-block; to the rescue !
Fiddle
.controller-row-number {
background-color: yellow;
width: 60px;
display: inline-block;
text-align: right;
padding: 0 15px 0 0;
}
.controller-row-name {
background-color: orange;
width: 150px;
display: inline-block;
text-align: left;
padding: 0 0 0 0;
}
span elements are by default inline, so you have to make them block, or inline-block if you want your width rule to be applied, otherwise they just take up enough width to fit. The width and height of display: inline; elems cannot be set as you tried to do. Tho you can fake the height using line-height.
Spans are displayed as inline by default, if you want to specify a width you'll have to set display: inline-block.
width cannot be applied to an inline element like a <span>. You will have to also style your spans of class controller-row-number and controller-row-name to be display: inline-block