Span leaks outside of its parent elements - css

Span tags leak outside of its direct parent and all other wrapping parents too. Take a look at this fiddle.
I've tried display, overflow and other properties but I can't figure this out. It just keeps leaking out.
How to fix that?
.tag {
padding: 2px 5px 3px 5px;
margin-right: 5px;
border: 1px solid red;
white-space: nowrap; /* prevent splitting spans */
}
.tag-wrapper {
border: 1px solid blue;
max-width: 300px;
}
.parent {
border: 1px solid green;
}
<div class="parent">
<div class="tag-wrapper">
<span class="tag">tag 1</span>
<span class="tag">tag 2</span>
<span class="tag">tag 3</span>
<span class="tag">tag 4</span>
<span class="tag">tag 5</span>
<span class="tag">tag 6</span>
<span class="tag">tag 7</span>
</div>
</div>
I tried to find an answer but it seems that I don't know how to phrase this question. Im familiar with collapsing margins and it seems to be a different problem.

You're probably looking for display: inline-block;.
<span> elements by nature are an inline element, so their effective height is actually based on their content, rather than their bounding box. By making it behave as a block element, you should achieve your desired effect.
Here is an updated version of your fiddle, and here is the updated tag CSS:
.tag {
padding: 2px 5px 3px 5px;
margin-right: 5px;
border: 1px solid red;
white-space: nowrap; /* prevent splitting spans */
display: inline-block;
}

You should use display: inline-block in .tag. Checkout the updated Fiddle
Just like:
.tag {
display: inline-block;
}
Hope this helps!

Related

Element and container same border radius but doesn't align well [duplicate]

This question already has answers here:
CSS wrong appearance of border-radius on a nested div
(6 answers)
Closed 2 years ago.
I'm trying to align a rounded link inside a rounded container at the bottom of it.
But even when the border-radius is the same on both elements, there's a small difference between them and is noticeable with high contrast colors.
Here's a jsfiddle of the issue. https://jsfiddle.net/rumeau/q349vse7/3/
.container {
border: 2px solid red;
border-radius: 16px;
background:white;
overflow:hidden;
}
.link {
display:block;
text-align:center;
border-radius: 16px;
background: red;
color: white;
padding-top: 8px;
padding-bottom: 8px;
}
<div class="container">
<p>
Content
</p>
<a class="link" href="#">Read more</a>
</div>
Is there any standard solution to this, so I dont have to use a workaround?..
Greetings.
I can suggest such a solution:
.container {
border: 2px solid red;
border-bottom: none; /* <----- add this it */
border-radius: 16px;
background:white;
overflow:hidden;
}
.link {
display:block;
text-align:center;
border-radius: 16px;
background: red;
color: white;
padding-top: 8px;
padding-bottom: 8px;
}
<div class="container">
<p>
Content
</p>
<a class="link" href="#">Read more</a>
</div>

Center images while maintaining responsive design

I'd like to push the three icons below towards the center of the page while still retaining a responsive layout.
Is display: grid; or display: row; more suitable?
And depending on your answer, what are the cleanest properties to apply?
<html>
<div id="contact">
<h1>Let's connect.</h1>
<div id="image-holder">
<div id="github-div">
<a href="https://github.com/klin-nj-97" target="_blank" id="profile-link">
<img src="https://image.flaticon.com/icons/svg/25/25231.svg" alt="github" class="contact-img">
</a>
</div>
<div id="linkedin-div">
<a href="https://www.linkedin.com/in/kevin-lin-33085a133/" target="_blank">
<img src="http://simpleicon.com/wp-content/uploads/linkedin.png" alt="linkedin" class="contact-img">
</a>
</div>
<div id="email-div">
<a href="mailto:kevin_lin#brown.edu">
<img src="https://image.freepik.com/free-icon/email-envelope-outline-shape-with-rounded-corners_318-49938.jpg" alt="email" class="contact-img">
</a>
</div>
</div>
</div>
</html>
<style>
#contact h1 {
text-align: center;
padding-top: 75px;
padding-bottom: 55px;
}
#image-holder {
display: flex;
flex-flow: row;
margin-left:
}
#contact a{
color: white;
}
.contact-img {
height: 35px;
width: 35px;
padding: 30px;
border-radius: 15px;
border: 2px solid black;
}
</style>
You should be use this simple trick.
Please give align-items: center; justify-content: center; into #image-holder
For more details Go to display:flex
Hope this help.
Let me know further clarifications.
#contact h1 {
text-align: center;
padding-top: 75px;
padding-bottom: 55px;
}
#image-holder {
display: flex;
flex-flow: row;
align-items: center;
justify-content: center;
}
#contact a{
color: white;
}
.contact-img {
height: 35px;
width: 35px;
padding: 30px;
border-radius: 15px;
border: 2px solid black;
}
<html>
<div id="contact">
<h1>Let's connect.</h1>
<div id="image-holder">
<div id="github-div">
<a href="https://github.com/klin-nj-97" target="_blank" id="profile-link">
<img src="https://image.flaticon.com/icons/svg/25/25231.svg" alt="github" class="contact-img">
</a>
</div>
<div id="linkedin-div">
<a href="https://www.linkedin.com/in/kevin-lin-33085a133/" target="_blank">
<img src="http://simpleicon.com/wp-content/uploads/linkedin.png" alt="linkedin" class="contact-img">
</a>
</div>
<div id="email-div">
<a href="mailto:kevin_lin#brown.edu">
<img src="https://image.freepik.com/free-icon/email-envelope-outline-shape-with-rounded-corners_318-49938.jpg" alt="email" class="contact-img">
</a>
</div>
</div>
</div>
</html>
Add the property justify-content: center; to the #image-holder id.
The buttons will be centered.
Code below
JSFiddle
The cleanest way to do this is to:
Use class-based selectors instead of ID selectors
Use flexbox to centre the layout (and text) horizontally, and centre the layout vertically
I've changed your HTML to use class-based selectors instead of IDs, e.g. class="contact" instead of id="contact":
<div class="contact">
<h1 class="contact__title">Let's connect.</h1>
<div class="contact__images">
<a class="contact__link" href="https://github.com/klin-nj-97" target="_blank">
<img src="https://image.flaticon.com/icons/svg/25/25231.svg" alt="github" class="contact__icon">
</a>
<a class="contact__link" href="https://www.linkedin.com/in/kevin-lin-33085a133/" target="_blank">
<img src="http://simpleicon.com/wp-content/uploads/linkedin.png" alt="linkedin" class="contact__icon">
</a>
<a class="contact__link" href="mailto:kevin_lin#brown.edu">
<img src="https://image.freepik.com/free-icon/email-envelope-outline-shape-with-rounded-corners_318-49938.jpg" alt="email" class="contact__icon">
</a>
</div>
</div>
For the cleanest CSS, it's ideal that all your selectors have the same level of specificity, and the best way to do that is use only class-based selectors. This will let you override styles more easily. You can read more about CSS specificity here.
The following CSS uses flexbox to position your content accordingly, assuming you are trying to centre everything vertically within the page:
body {
margin: 0; /* browser adds margins by default */
}
.contact {
display: flex;
flex-direction: column;
justify-content: center; /* centers your content horizontally */
align-items: center; /* centers your content vertically */
height: 100vh;
}
.contact__title {
margin: 0 0 55px; /* if you have a header you'd like to account for, the first value can be the header height */
}
.contact__images {
/* you don't even need anything here but the wrapping div of this classname is required to keep your icons aligned */
}
.contact__link {
text-decoration: none; /* proper way to hide the text link underline */
}
.contact__icon {
height: 35px;
width: 35px;
padding: 30px;
border-radius: 15px;
border: 2px solid black;
}
The CSS class naming convention I used is called BEM. I recommend reading more about it if you are interested in writing clean CSS. You can do so here or here.
I have a working example here on CodePen. You can change the height of the page to see it's centred vertically.

How do I evenly space these links within my navbar div?

I would like to evenly space the 3 links ('About', 'Hours', 'Contact') within the containing 'banLinks' div. I do not want to use a list of any kind.
I would like each link to be evenly spaced, taking up 1/3 of their container. I am very new to HTML and CSS and I'm not sure how to do this.
I think one way of doing it may be by dividing the width of the div container in pixels by 3, account for the font size, then set the margins somehow around this figure. But to me this seems a bit unseemly, I'n not sure if this is the done thing.
<body>
<div id="wrapper">
<div class="bruceBanner">
<a href="#">
<img border="0" alt="XYZ Banner" src="http://bit.ly/1QSpdbq" width="553" height="172">
</a>
</div>
<nav>
<div class="banLinks">
<a id="about" href="#">About</a>
<a id="hours" href="#">Hours</a>
<a id="contact" href="#">Contact</a>
</div> </nav>
</div><!-- .wrapper-->
</body>
CSS:
#wrapper {
}
.bruceBanner img {
border: 2px solid black;
height: 172px;
width: 553px;
display: block;
margin: 0 auto;
}
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
}
#about, #hours, #contact {
font-size: 20px;
border: 2px solid blue;
}
Here is a jsfiddle. https://jsfiddle.net/yuy84gmq/6/
you can do this using flexbox. Do as followed:
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
display: flex;
justify-content: space-around; //or space-between whatever you like best
}
JSfiddle: https://jsfiddle.net/yuy84gmq/10/
flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Add this to style:
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
padding: 0;
}
.banLinks a{
width: calc(33% - 4px);
display: inline-block;
margin: 0;
}
Use a display table
.banLinks {
display:table;
table-layout:fixed;
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
}
.banLinks a {
display:table-cell;
}
Here is the fiddle: https://jsfiddle.net/yuy84gmq/8/
A couple of options here...both of which work regardless of the number of list items...assuming there is enough width.
Display:Table-cell
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
display: table;
}
.banLinks a {
display: table-cell;
border: 1px solid grey
}
<div class="banLinks">
<a id="about" href="#">About</a>
<a id="hours" href="#">Hours</a>
<a id="contact" href="#">Contact</a>
</div>
Flexbox
.banLinks {
border: 2px solid black;
width: 553px;
text-align: center;
margin: 0 auto;
display: flex;
}
.banLinks a {
flex: 1;
border: 1px solid grey
}
<div class="banLinks">
<a id="about" href="#">About</a>
<a id="hours" href="#">Hours</a>
<a id="contact" href="#">Contact</a>
</div>
Instead of usual a links, put them into a list, and set the list to be inline. Then you can apply margin to the list items to space them out.
HTML
<nav>
<ul class="banLinks">
<li><a id="about" href="#">About</a></li>
<li><a id="hours" href="#">Hours</a></li>
<li><a id="contact" href="#">Contact</a></li>
</ul>
</nav>
CSS
.banLinks li { display:inline-block;margin:0 10px;} /* Adjust left/right margin as appropriate */

making entire div clickable with a link

Let me just say before I ask this question ..I did try searching for a long time through existing topics but I can't find a working solution to my problem.
I am using Bootstrap and have a horizontal list of items with icons and a text link underneath. What I want is basically the entire div clickable as the link, not just the actual text.
Here is the HTML:
<ul class="glyphs character-mapping">
<li>
<div data-icon="a" class="icon"></div>
Link 1
</li>
<li>
<div data-icon="b" class="icon"></div>
Link 2
</li>
</ul>
And the CSS:
.glyphs.character-mapping {
margin: 0px;
padding: 0px;
color: #000000;
}
.glyphs.character-mapping li {
margin: 0px 10px 30px;
display: inline-block;
width: 165px;
text-align: center;
}
.glyphs.character-mapping .icon {
margin: 10px 0px 10px 45px;
padding: 0px;
position: relative;
width: 70px;
height: 70px;
color: #FF0000;
overflow: hidden;
border-radius: 3px;
font-size: 68px;
}
I tried several solutions that are related on here, including giving no padding or margin to the 'li' and instead making the 'a' 100% wide and high, but that didn't work either.
Any ideas what I'm doing wrong here?
Thank you so much in advance for any help.
Can't you just do this:
<ul class="glyphs character-mapping">
<li>
<a href="">
<div data-icon="a" class="icon"></div>
Link 1
</a>
</li>
<li>
<a href="">
<div data-icon="b" class="icon"></div>
Link 2
</a>
</li>
</ul>
Where the entire div and text is clickable?

Floating image to right in HTML lists causes alignment issues

For simplicity, I'm using inline styles so you can see what's going on. I've used a red border for list elements and a black border for the images? Why aren't the images aligned properly? This has been driving me nuts.
See screenshot:
See code:
<ul>
<li style="border: 1px solid red">Result <span style="display: block; float: right; border: 1px solid #000"><img src="../../resources/images/delete.png" /></span></li>
<li style="border: 1px solid red">Result <span style="display: block; float: right; border: 1px solid #000"><img src="../../resources/images/delete.png" /></span></li>
<li style="border: 1px solid red">Result <span style="display: block; float: right; border: 1px solid #000"><img src="../../resources/images/delete.png" /></span></li>
</ul>
JS Fiddle
http://jsfiddle.net/jMzub/
You need to clear the floats
<ul>
<li style="border: 1px solid red">Result
<span style="display: block; float: right; border: 1px solid #000">
<img src="../../resources/images/delete.png" />
</span>
<div style="clear: both;"></div>
</li>
<!--Same for other li-->
</ul>
But this is a dirty way, I would've used background-image and background-position for <li> element instead(Unless and until you'll be using image as a link or for some click purpose)
You need to include height to li
li{height: 30px;}

Resources