I'm practicing with HTML/CSS using Bootstrap v5.0 and there are some problems with the strange reactions between floats and divs. Particularly, I want to achieve something as below:
And I succeeded by applying the following piece of code:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="container">
<div class="center-div new-page">
<div class="row g-3 d-flex d-md-block">
<div class="col-lg-6 col-md-12 float-end">
<div class="third-slogan">
<h2 class="d-none d-md-block">Perfect for Operations HR and Finance</h2>
<h2 class="d-block d-md-none">OpenType features and Variable fonts</h2>
<p class="sub-slogan">Most calendars are designed for teams. Slate is designed for freelancers who want a simple way to plan<br>their schedule.</p>
</div>
</div>
<div class="col-lg-6 col-md-12 float-start">
<div class="screen3"><img src="https://via.placeholder.com/300x100" alt="Screen 3"></div>
</div>
<div class="col-lg-6 col-md-12 center-div float-end">
<div class="buttons-page-3">
<button id="button-button" class="btn btn-rounded btn-couple-2" style="color: #FFFFFF; background-color: #03D6F3; margin-top: 0;">
Button
</button>
</div>
</div>
</div>
</div>
</div>
My custom CSS:
.new-page {
margin-top: 10%;
}
.center-div {
text-align: center;
}
.third-slogan {
margin-top: 18%;
padding-right: 10%;
padding-left: 10%;
}
.third-slogan h2, p {
text-align: left;
}
.sub-slogan {
font-size: 16px;
font-weight: 700;
letter-spacing: 0.2px;
color: #5C5C5C;
margin-top: 10%;
}
.screen3 img {
width: 85%;
}
.buttons-page-3 {
text-align: left;
padding-left: 10%;
}
.btn-rounded {
border-radius: 39px;
font-size: 16px;
padding-top: 18px;
padding-bottom: 18px;
padding-left: 46px;
padding-right: 46px;
}
.btn-couple-2 {
margin-top: 5%;
box-shadow: 0px 4px 31px rgba(0, 0, 0, 0.15);
margin-right: 3%
}
But the problem is, after I apply the 2 float-end for the text and the button, and 1 float-start for the image, the divs which contains them does not display properly:
And it cause me a lot of troubles to continue to work with the divs after that. Could anyone please explain why this happens and how to fix it? Thank you very much.
P/s: The divs return to normal if I remove the float of the image or the button, but then it would not display as I desire, the button is pushed below the image.
The div around the button, which is the third div in the .row element is redundant and messes this up. This layout should have 2 columns (col-*) and the button should be inside of the second column. Title, intro text and button should be block elements without any floats, so they will stack on top of each other like your design mockup.
I have removed redundant html markup and cleaned up the CSS in order to let Bootstrap do most of the job for you: https://jsfiddle.net/3johtdxk/3/
EDIT: OP wants responsivity for mobile with text and heading above the image, and button below. Added second button in markup so we can hide/display them depending on the viewport width.
.new-page {
margin-top: 10%;
}
.sub-slogan {
font-size: 16px;
font-weight: 700;
letter-spacing: 0.2px;
color: #5C5C5C;
margin-top: 1.4rem;
}
.full-width {
display: block;
width: 100%;
}
.btn-rounded {
border-radius: 39px;
font-size: 16px;
padding-top: 18px;
padding-bottom: 18px;
padding-left: 46px;
padding-right: 46px;
}
.btn-couple-2 {
margin-top: 5%;
box-shadow: 0px 4px 31px rgba(0, 0, 0, 0.15);
margin-right: 3%
}
#media (max-width: 768px) {
.stack-order-mobile {
flex-direction: column-reverse;
}
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="container">
<div class="new-page">
<div class="row g-3 stack-order-mobile">
<div class="col-lg-6 col-md-12">
<img class="full-width" src="https://via.placeholder.com/1000x600" alt="Screen 3">
<button id="mobile-button" class="btn btn-rounded btn-couple-2 d-block d-md-none d-lg-none d-xl-none mt-2" style="color: #FFFFFF; background-color: #03D6F3;">
Button
</button>
</div>
<div class="col-lg-6 col-md-12">
<h2>Perfect for Operations HR and Finance</h2>
<p class="sub-slogan">Most calendars are designed for teams. Slate is designed for freelancers who want a simple way to plan<br>their schedule.</p>
<button id="desktop-button" class="btn btn-rounded btn-couple-2 d-none d-md-block d-lg-block d-xl-block" style="color: #FFFFFF; background-color: #03D6F3; margin-top: 0;">
Button
</button>
</div>
</div>
</div>
</div>
Note that when I removed floats I had to switch the order of the columns so that your image still stays on the left side. To get your desired stacking order, I added an extra button with hide/show classes from bootstrap at 768px and a media query for viewports <768px to move your text to the top on smaller screens.
The media query could probably be done with a Bootstrap utility, but I don't know it well enough. You have to reduce your whole browser window to less than 768px to see the stacking result as neither stackoverflow nor jsfiddle editors aren't great with responsiveness.
Added a larger image with 100% width so it fills up its left column completely. You may need to introduce some right padding/margin or reduce the image with percentage.
You had added a flex class in there that was redundant. Bootstrap columns ARE flex containers from the outset, so I removed it.
Remember: Always use as little CSS as possible! This is true also with Bootstrap. Don't load it up with a lot of stuff until you know what is going on. Try little by little and keep your markup lean. No need for extra divs around elements like img in most cases.
The issue of floats is another one, you don't need any floats here. Floats for responsivity is bad now that we have flex which is a cleaner solution. I removed them all. You may need them if/when you try to float the Invision, Marvel etc. divs in the element context in the left column.
But it looks like you're planning to use an image here, so no floats needed then. Try to stick with bootstrap columns only (less code, less mess).
Related
I'm making a card/grid layout using wells in bootstrap. My problem is that the button needs to always be positioned on the bottom of the well at all times with the well having a fixed height. The button is at the bottom but is also overlapping the text.
body {
background-color: #5C67B6;
}
html,
body {
height: 100%;
padding-top: 70px;
}
.btn-purple {
color: #fff;
background-color: #5C67B6;
border-color: #5C67B6;
position: absolute;
bottom: 30px;
left: 50%;
margin-left: -140px;
}
.btn-purple:hover,
.btn-purple:focus,
.btn-purple:active,
.btn-purple.active,
.open>.dropdown-toggle.btn-purple {
color: #fff;
background-color: #4b5496;
border-color: #4b5496;
}
.customClass {
width: 700px;
max-width: 100%;
margin: 0px auto;
}
.well {
min-height: 280px;
height: auto;
overflow-wrap: break-word;
word-wrap: break-word;
hyphens: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container content-sm customClass">
<div class="row">
<div class="col-sm-6 col-xs-12">
<div class="well">
<img class="center-block" src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon#2.png" style="border-radius: 50%;" height="80" width="80">
<h3 style="text-align: center;">Test123</h3> <p>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</p>
<i class="fa fa-sign-in" aria-hidden="true"></i> Join server!
</div>
</div>
</div>
</div>
Make sure to click show full page. The button is overlapping some of the text. What would I need to do to make it so where the text positions itself so where it avoids contact with the button? Changing the height fixes it somewhat, but it needs to stay at this height.
If you wanted each of your well class height to be fixed, You need to move each of your btn-purple class outside of your well class as well. Also, To retain the look of your current layout, Place some of your css property from your well class to your col-sm-12 class (parent container)
Here is a sample jsfiddle to guide you: https://jsfiddle.net/u7ecv316/1/
Note: I've place a col-item class in col-sm-12 then place the btn-purple outside of well class. I've also override the css properties of well class too.
Hope this will guide you well
GitHub use margin:40px and padding:24px. To use only margin or padding is not good? ex. margin:64px or padding:64px. I want to know the reason why GitHub use both of them.
<style text="css">
.mb-6 {
margin-bottom: 40px;
}
.pb-4 {
padding-bottom: 24px;
}
</style>
<p class="alt-lead text-center text-gray mb-6 pb-4 col-md-10 mx-auto">
Open source software is free for you to use and explore. Get involved to perfect your craft and be part of something big.
</p>
<div class="clearfix gut-lg">
<div class="float-left col-md-4">
<div class="clearfix mb-4 text-md-center">
<div class="float-left mr-4 float-md-none mr-md-0 mb-md-3"><img src="https://assets-cdn.github.com/images/modules/site/iconsnsource-ico-future.svg?sn" alt="" aria-hidden></div>
<div class="overflow-hidden">
<h3 class="alt-h4 mb-2">Shape the future of software</h3>
<p class="alt-text-small text-gray">Your contributions help make technology better for everyone, developers and non-develo alike.</p>
</div>
</div>
</div>
<div class="float-left col-md-4">
<div class="clearfix mb-4 text-md-center">
<div class="float-left mr-4 float-md-none mr-md-0 mb-md-3"><img src="https://assets-cdn.github.com/images/modules/site/iconsnsource-ico-best.svg?sn" alt="" aria-hidden></div>
<div class="overflow-hidden">
<h3 class="alt-h4 mb-2">Work with the best in the field</h3>
<p class="alt-text-small text-gray">Amazing developers use GitHub. Contribute code to projects that change how software&nbs built.</p>
</div>
</div>
</div>
<div class="float-left col-md-4">
<div class="clearfix mb-4 text-md-center">
<div class="float-left mr-4 float-md-none mr-md-0 mb-md-3"><img src="https://assets-cdn.github.com/images/modules/site/iconsnsource-ico-grow.svg?sn" alt="" aria-hidden></div>
<div class="overflow-hidden">
<h3 class="alt-h4 mb-2">Grow your skills and help others</h3>
<p class="alt-text-small text-gray">Whatever your skill level, working on open source software is a great way to learn newp;things.</p>
</div>
</div>
</div>
</div>
I guess you already know different between margin and padding. but wondering why they using both combined instead of one thing.
If you check their code. you will see they come from different class.
.mb-6 {
margin-bottom: 40px;
}
.pb-4 {
padding-bottom: 24px;
}
and If you dig a bit deeper you will see they have these classes in their framework.
.mb-1{ margin-bottom: 4px }
.mb-2{ margin-bottom: 8px }
.mb-3{ margin-bottom: 16px }
.mb-4{ margin-bottom: 24px }
.mb-5{ margin-bottom: 32px }
.mb-6{ margin-bottom: 40px }
and same things for padding pb-1 to pb-6
Now, If they want 64px space they have options to define a new class or re-use those class.
And they choose to reuse .pb-4 + .mb-6 to get 64px instead of define a new class just for one time using and without messing around with their framwork.
The reason why people would use margin and padding together would usually be due to the use of a background color, or background image.
If the background is left blank/transparent, it does not matter if you use a padding or a margin. However once you set the background color, the padding will increase the size of the element which includes the background color, while the margin will separate it from other elements creating white space in between.
Hope this helps you understand!
What I am understanding is that you went through GitHub's styles and noticed that they used both margin and padding in their CSS. Your question appears to be "Is using one/both preferred or does one method have an advantage?"
The answer to which is no, there isn't an advantage to using either, but you need to understand what margin and padding are
Margin
Margin is space between that element and elements around it. so saying margin:5px on something will put a five pixel wide margin around the entirety of the element, ensuring other elements do not "touch" it.
Example:
Notice that there is a very visible gab between the first element and the second element. And there is even a gap between the left side of the container and the first element.
.row > * {
float: left;
min-width: 25%;
max-width: 30%;
margin: 5px;
background-color: blue;
color: white;
}
<div class="row">
<div>Hi</div>
<div>Hello</div>
</div>
Padding
Padding, on the other hand, is how much space there should be between the edges of an element and the element's own contents. padding:5px says that there is a a sort of boundary inside the element five pixels wide on each side. To extend our first example:
Notice that there is a very small gap between the contents of each element's wall (where the background begins or ends) and the text content.
.row > * {
float: left;
min-width: 25%;
max-width: 30%;
margin: 5px;
padding: 5px;
/*Try removing/changing this value to see what effect it has.*/
background-color: blue;
color: white;
}
<div class="row">
<div>Hi, this text is longer so that we can see the border around the element and how much space there is between the walls of the element and the text.</div>
<div>Hello</div>
</div>
Tl;Dr
Margin is used to create a gap or some space between elements. Padding is used to create space between an elements contents and it's "walls."
So you seem to know
Padding is space inside the border, whereas Margin is space outside
the border.
Do you also know that that means, if you have margin set to elements following by the same elements it will just take the biggest possible value. So if margin-bottom is bigger than margin-top of the following element it will take margin-bottom.
So example gap will be margin-bottom from first element 20px.
* {margin:0; padding:0;}
div {
width: 100px; height: 100px;
background-color: orange;
border: solid 1px black;
}
div.one {
margin-bottom: 20px;
}
div.two {
margin-top: 5px;
}
<div class="one"></div>
<div class="two"></div>
Kinda same example gap is again 20px but this time it is the margin top from the second element.
* {margin:0; padding:0;}
div {
width: 100px; height: 100px;
background-color: orange;
border: solid 1px black;
}
div.one {
margin-bottom: 5px;
}
div.two {
margin-top: 20px;
}
<div class="one"></div>
<div class="two"></div>
And here what happens if you use padding. If you use your browser debugger you will see that now the gap should be 27px (25px from both elements padding + 2x1px border)
* {margin:0; padding:0;}
div {
width: 100px; height: 100px;
background-color: orange;
border: solid 1px black;
}
div.one {
padding-bottom: 5px;
}
div.two {
padding-top: 20px;
}
<div class="one"></div>
<div class="two"></div>
So to answer the why. If you know this you can have reasons to use one over the other.
What is happening
What I want
As you can see, the Amy grey box and rectangle grey box are not positioning themselves on the same line. This is strange considering my codes uses the Bootstrap col sys to put both grey boxes on the same row.
My cols add up to 12 so I'm not sure what is happening. I do notice, however, if I make the cols add up to 11, then both grey boxes are put on the same line. But this is not a fix I want as I would like my cols to add up to 12 and have both boxes appear on the same line.
If anyone could help me solve this it would be greatly appreciated!
My code
HTML
<div class="row student-row">
<div class="col-xs-2 student-box">
<h1>Amy</h1>
</div>
<div class="col-xs-10 worksheet-box">
...
</div>
</div>
CSS
.student-row{
margin: 20px 10px 10px 10px;
}
.student-box{
margin-right: 10px;
}
.worksheet-cell{
margin-top: 10px;
margin-left: 10px;
width: 20%;
}
.worksheet-group{
margin: 10px 15px 20px 15px;
}
JSFiddle
You've overridden the margins that Bootstrap set and set your own. Now the box needs more space so it doesn't fit.
Remove those margins and the two boxes fit side by side.
.student-row{
/* margin: 20px 10px 10px 10px;*/
}
.student-box{
background-color: #F5F5F5;
font-weight: 700;
text-transform: uppercase;
/* margin-right: 10px; */
}
Your style is breaking since you are adding the following to your .row element in Bootstrap. If you play with the margin of the row it will break.
.student-box{
margin-right: 10px;
}
I recommend to use the following pattern:
<div class="row student-row">
<div class="col-xs-2 student-box">
<h1>Amy</h1>
</div>
<div class="col-xs-10 worksheet-box">
<!-- Add new elements row wise here -->
<div class="row">
</div>
</div>
</div>
I have an image in which I need to put a button over, the problem is that I don't know how to place the button and automatically re-size and position it when making the browser smaller, right now I have the button in place, but when I re-size the browser to get smaller the button moves, I tried using percentages in the css buy doesn't work, what can I do?
<div id="discover" class="container-fluid">
<div class="row-fluid">
<div class="col-lg-12 col-sm-12 col-xs-12 col-md-12 withimg">
<img id="discoveryour" src="img/x.png" class="img-responsive">
</div>
</div>
<div class="row-fluid">
<div id="bttnimg" class="col-lg-12 col-sm-12 col-xs-12 col-md-12">
<form id="start" method="post" action="x.php">
<button class="btn-primary">text</button>
</form>
</div>
</div>
</div>
Css:
.withimg {
width: 100%;
overflow:hidden;
padding: 0px;
margin: 0px;
}
#discover{
position: relative;
}
#bttnimg{
float: left;
position: absolute;
left: 62%;
top: 25%;
max-width: 750px;
}
Ah, the good old "how to overlay stuff on top of a responsive image -- responsively" question.
A little tricky, but not too bad. The tricky bit is how to make the stuff's vertical position responsive when the image size changes.
Fear not, here's one simple way to do this:
HTML:
<div class="img-wrapper">
<img class="img-responsive"
src="http://lorempixel.com/output/people-q-c-1200-400-4.jpg">
<div class="img-overlay">
<button class="btn btn-md btn-success">Button</button>
</div>
</div>
CSS:
.img-wrapper {
position: relative;
}
.img-responsive {
width: 100%;
height: auto;
}
.img-overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
text-align: center;
}
.img-overlay:before {
content: ' ';
display: block;
/* adjust 'height' to position overlay content vertically */
height: 50%;
}
The img-overlay:before pseudo-class handles the vertical positioning job by pushing the img-overlay div down from the top of the image. In this example, the top of the button will always be 50% down the image (change the height: 50% attribute if you want the button higher or lower).
jsfiddle
To make the button size responsive to window width, you can create a new class for your button. Let's call it btn-responsive (this replaces btn-md in the example above). Then use #media queries to adjust the btn-responsive attributes for different window widths. Something like this:
.btn-responsive {
/* matches 'btn-md' */
padding: 10px 16px;
font-size: 18px;
line-height: 1.3333333;
border-radius: 6px;
}
#media (max-width:760px) {
/* matches 'btn-xs' */
.btn-responsive {
padding: 1px 5px;
font-size: 12px;
line-height: 1.5;
border-radius: 3px;
}
}
and so forth for other screen widths.
In case you're wondering how to do this with bootstrap 5 (like me), there are new classes that do the trick. For instance, I did this to put a button floating right top over the image (I also use font-awesome but you can use any text/icon you like for the button):
<div class="card">
<img class="card-img-top img-thumbnail" src="someimage.png" alt="alt text">
<div class="card-img-overlay">
<a href="#" class="btn btn-outline-warning btn-sm float-end"
data-bs-toggle="popover" data-bs-content="Edit image" data-bs-trigger="hover focus">
<i class="far fa-edit"></i>
</a>
</div>
<div class="card-body">
<h5 class="card-title">Some title</h5>
<p class="card-text">Some text</p>
</div>
</div>
Check out the official bootstrap documentation for more info.
I have to ask if there's a possibility to replace form tag into another DIV? Then you can just use position: absolute for button. I created fiddle to show how https://jsfiddle.net/1x1pjwk7/
I have started to make a website for a local restaurant and I am new to using Bootstrap. Currently i am working on getting the header of the page to work. I made a header with a fixed position and I am using the buttons built into bootstrap, albeit with many css style changes, as tabs for other pages on the website. There are 4 tabs. In between the 2nd and 3rd tab is the logo for the restaurant, a png file. To start out I have designed the page to work with only large screen displays. So on my 2560 x 1440 res screen, everything looks exactly as it should, as shown in this picture:
Though, immediately as I start to decrease the size of my screen, the padding between each button and the logo image starts to decrease. Eventually, at around an inner screen size of 1638 x 1277, ONLY the closest button to the logo on its right side starts to overlap with the logo, and this is all happening while it's still considered a "large" screen. Here is a picture of this:
To fix this I tried setting the width of the logo to 100% rather than 286px and it fixed the problem with the buttons overlapping with the logo, but it did not produce the result I wanted. I want the logo to always remain the same size. I also want the horizontal space in between each button and the logo to always be the same, not in relation to the original length in between, just the same in relation to each other. Like the space between button x and button y is the same amount between button y and the logo.
Here is the head:
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<style>
.header{
background-color: red;
}
.col-lg-2, .col-lg-4, .col-lg-1{
background-color: transparent;
text-align: center;
}
.header{
height: 140px;
}
.header h3{
color: white;
font-size: 200%;
font-family: 'HeaderFont';
margin-top: 10px;
}
#logo{
margin-top: 30px;
}
.button{
background-color: black;
background-repeat: no-repeat;
border: none;
cursor:pointer;
overflow: hidden;
outline: none;
margin-top: 45px;
padding-right: 18%;
padding-left: 18%;
padding-top: 5px;
padding-bottom: 5px;
}
</style>
Here is the body:
<div class="container-fluid">
<div class="row">
<div class="header">
<div class="col-lg-3"></div>
<div class="col-lg-1">
<button type="tab" class="button"><h3>Home</h3></button>
</div>
<div class="col-lg-1">
<button type="tab" class="button"><h3>About</h3></button>
</div>
<div class="col-lg-2">
<img src="http://www.sizzledcore.com/wp-content/uploads/2009/11/google-new-logo.png" id="logo" style="width:286px; height:106px; vertical-align:middle"></a>
</div>
<div class="col-lg-1">
<button type="tab" class="button"><h3>Menu</h3></button>
</div>
<div class="col-lg-1">
<button type="tab" class="button"><h3>Order</h3></button>
</div>
<div class="col-lg-3"></div>
</div>
</div>
</div>
Okay so I figured it out. What I wanted was that the column would always be a certain width, what would happen is the image of the picture would become greater than the width of the column. To fix this I set .header .col-lg-2 to have a width: 286px;
In other words, this is the CSS needed:
.header .col-lg-2{
width: 286px;
}