【问题标题】:Center bootstrap button in xsxs 中的中心引导按钮
【发布时间】:2017-06-20 22:31:26
【问题描述】:

我在这上面花了好几个小时,当屏幕宽度进入 xs 模式时,我无法让这个按钮居中,它开始根据左侧定位自己。我试过查看引导程序,但找不到任何关于为什么会发生这种情况的明确指示。

这是我的代码,主要是第三个主 div 块,里面有 btn-posit 类和媒体查询 461-767px。谢谢

https://codepen.io/fullmetal7777/pen/RgKagp

<div class="container">
    <div class="row">
        <div class="col-lg-4 col-md-6 col-sm-8 col-xs-10 text-center btn-posit">
            <button class="btn btn-primary twit-btn"><i class="fa fa-twitter-square fa-2x" aria-hidden="true"><span id="twit-Text">Tweet it</span></i></button>
            <button class="btn btn-default quote-btn"><div id="whole-text">Get Quote</div></button>
        </div>
    </div>

【问题讨论】:

  • “这是我的代码” - 代码在哪里?
  • 不敢相信我忘记链接已编辑的代码大声笑。如果你缩短了宽度,你最终会看到按钮弹出到屏幕的左侧并相对于那一侧定位自己,而不是像那样正确居中 > 767px
  • 这就是你想要的吗? codepen.io/mcoker/pen/VWbrLj
  • 该链接不显示任何按钮。这也是我遇到的另一个问题。我可以只做左右边距自动,但它不会显示在我想要的位置,因为它会隐藏在背景后面,除非它具有绝对/相对定位,否则我不能做 z-index,如果我这样做我不能自动做边距。这就是恶性循环。我真的很接近,它只是在 xs 设置下停止正常工作。
  • 啊。如果视口很小,按钮会显示在底部。这个怎么样? codepen.io/mcoker/pen/VWbrLj

标签: html css twitter-bootstrap


【解决方案1】:

这会让你接近你想要的。

问题:

  1. 带有按钮的列有一个 col-xs-10 类,如果您希望按钮居中,则应该是 col-xs-12
  2. twitter 按钮上仍然有 xs 大小的边距,这会将其推向左侧,从而使其无法居中。

https://codepen.io/anon/pen/xrdPVb

【讨论】:

    【解决方案2】:

    如果您只希望菜单居中,则不需要使用col-* 类来包装它们。删除这些并应用.text-center(您已经拥有),然后删除您用于偏移边距的所有媒体查询并尝试将该列居中。但后来我认为您在使用 transform: translateX(-1); 时遇到了一些显示问题,所以我只是颠倒了图像并删除了 transform 行。浏览器要做的工作越少越好,所以如果你能改变图像,我就这么做。

    $(document).ready(function(){
    
    var quotes = {
    	Mario: ["So long-a-Bowser!", "Nighty nighty. Ah spaghetti. Ah, ravioli. Ahh, mama mia.", "You know what they say: all toasters toast toast."],
    	Luigi: ["Let's-a go...", "Oh yeah! Who's number one now?! Luigi!", "Now, THIS is where the real action is"],
    	Bowser: ["Im HUUUUGE! Even scarier up close, huh?", "GAH HA HA HA! There's no WAY you're ready for a round against ME. Keep practicing, pip-squeak!"],
    	Peach: ['Oh, did I win?', "Go Peachy, Go Peachy, Go Peachy! Yay! Woohoo!"],
    	Yoshi: ['Yoshi!'],
    	Fox: ["This is Fox, returning to base!", "Better luck next time, Falco!"],
    	Falcon: ["Falcon Kick!", 'Blue Falcon!', 'Show me your moves!'],
    	Marth: ["Konkaiwa bokuno kachidane! (It's my victory this time!)", "Kyoumo ikinobiru kotoga dekita. (Even today I was able to survive.)",  
    			 "Bokuwa makeru wake niwa ikenainda! (There's no way I can lose!)"],
    	Pit: ["Three Sacred Treasures!", "It's game over for you!", "That all you got?"],
    	Link: ["....."],
    	Ike: ["Great... AETHER!", "I fight for my friends.", "You'll get no sympathy from me."],
    	Lucario: ["Behold, the power of Aura!", "The Aura is with me!", "Max Aura!"],
    	'Wii Fit Trainer': ["High energy, move that body!", "Salute the sun!", "Work hard to tone that tummy."],
    	Sheik: ['I\'ve been waiting for you, Hero of Time...', 'The flow of time is always cruel.'],
    	Ganandorf: ['Hero? ...I\'ve outlived more "heroes" than you can possibly imagine.', 'Behold! The power of the Demon King!',
    				'I am Ganondorf, the Demon King. Don\'t take that title lightly.'],
    	Mac: ["Let 'em have it, Mac!", "Way to go, Mac! You're the champ, baby!", "Nice moves, Mac. I can barely keep my eyes on you, son."],
    	Palutena: ["You shall be purified.", "No one can hide from the light.", "Celestial Fireworks!"],
    	Robin: ["The key to victory lies within.", "I'm always three steps ahead.", "It seems our fates are joined."],
    	Shulk: ["Now it's Shulk time!", "I can change the future!", "The future is ours to decide!"],
    	Sonic: ["That was almost too easy!", "Let's do that again some time!"],
    	Falco: ["Personally, I prefer the air!", "You aren't worth the trouble."],
    	Wario: ["Wa-Wa-Wa!", "Time for victory parade."],
    	'Toon Link': ["....."],
    	Lucina: ["The future is not written!", "Time to change fate!"]
    }
    
    var nameArr = ['Mario', 'Luigi', 'Bowser', 'Peach', 'Yoshi', 'Lucina', 'bowser jr', 'Wario', 'game and watch', 'donkeyk', 'diddyk', 'Link', 'Zelda', 
    			   'Sheik', 'Ganandorf', 'Toon Link', 'samus', 'zero suite samus', 'Pit', 'Palutena', 'Marth', 'Ike', 'Robin', 'kirby', 'king deedeedee', 'meta knight',
    			   'Mac', 'placeholder', 'Fox', 'Falco', 'pikachu', 'charizard', 'Lucario', 'jigglypuff', 'Greninja', 'duck hunt', 'Rob', 'ness', 'Falcon', 'villager', 
    			   'olimar', 'Wii Fit Trainer', 'dr mario', 'dark pit', 'Lucina', 'Shulk', 'pacman', 'Megaman', 'Sonic'];
    
    
    
    
    var imgurID = '0RQtP';
    var key = "cfb3f86a62ab07c";
    
    function randomFxn(){
    	$.ajax({
    	type: 'GET',
    	url: 'https://api.imgur.com/3/album/' + imgurID + '/images',
    	headers: {'Authorization': 'Client-ID '+ key},
    	}).done(function(data) {
    		showZones(data)
    	});
     }
    
    
    var lastNum = '70';
    
    function showZones(jsonData){
    	var name = '';
    	
    	
    	do{
    	var nameNum = Math.floor(Math.random()*(Object.keys(quotes).length));
    	// var nameNum = 70;
    	console.log(nameNum);
    	
    	} while (nameNum == lastNum)
    	console.log(lastNum);
    	lastNum = nameNum;
    	console.log(lastNum);
    
    	name = Object.keys(quotes)[nameNum];
    	
    	var quoteArrLength = quotes[name].length;
    	var randQuoteNum = Math.floor(Math.random()*quoteArrLength)
    	var randQuote = '\"' + quotes[name][randQuoteNum] + '\"';
    	var indexMult = (nameArr.indexOf(name)*8);
    	var addToIndex = Math.floor(Math.random()*8);
    	var url = jsonData.data[(indexMult)+addToIndex].link;
    	var imgCss = 'url(' + url + ')';
    	var isReverse = Math.floor(Math.random()*2); 
    
    	$('h3').text('-' + name);
    	$('h2').text('Hero? ...I\'ve outlived more "heroes" than you can possibly imagine.');
    
    	$('.bg').fadeOut(500, function() {
    		if(isReverse==1){
    			$(this).css({'background-image': imgCss, 'transform': 'scaleX(1)'}).fadeIn(700);
    		}
    		else{
    			$(this).css({'background-image': imgCss, 'transform': 'scaleX(-1)'}).fadeIn(700);
    		}						
    	});
    }
    
    var num = 0;
    $('.quote-btn').click(function(){
    
    	if (num == 0){
    		num++;
    
    		randomFxn();
    
    		setTimeout(function(){
    			num = 0;
    		}, 1200);
    	}
    });
    });
    body, html{
    	height: 100%;
    }
    
    .bg,
    .quote-btn {
    	background-repeat: no-repeat;
    	background-size: cover;
    }
    
    .bg{
    	background-image: url("http://i.imgur.com/sxwO4y3.jpg");
    	height: 100%;
    	background-size: 70% 100%;
    }
    
    .jumbotron{
    	margin-left: 16.66666667%;
    	margin-top: -78vh; 
    	background-color: rgba(100, 100, 100, 0.4);
    	border-style: solid;
    	border-color: rgba(0, 0, 0, 0);
    }
    
    h2, h3{
    	color: white;
    	opacity: 1;
    	font-weight: 900;
    
    }
    
    h2{
    	margin-top: -3vh;
    	margin-left: -1.7vw;
    }
    
    h3{
    	float: right;
    }
    
    .quote-btn {
      	border-color: #5e1111 #5e1111 hsl(0, 69%, 17%);
      	background-image: url("http://www.wtfgamersonly.com/wp-content/uploads/2015/06/Super-Smash-Bros.-img.2.jpg");
    	background-position: 50% 60%;
    	height: 45px;
    	width: 140px;	
    }
    
    .twit-btn{
    	height: 45px;
    	width: 140px;
    	margin-right: 35px;
    }
    
    #whole-text{
    	font-weight: 800;
    	color: #BC1818;
    	font-size: 22px;
    }
    
    .btn.quote-btn:focus,
    .btn.quote-btn:active:focus,
    .btn.quote-btn.active:focus,
    .btn.quote-btn.focus,
    .btn.quote-btn:active.focus,
    .btn.quote-btn.active.focus,
    .btn.twit-btn:focus,
    .btn.twit-btn:active:focus,
    .btn.twit-btn.active:focus,
    .btn.twit-btn.focus,
    .btn.twit-btn:active.focus,
    .btn.twit-btn.active.focus{
    	outline-color: none;
    	outline-style: none;
    	border-color: #5e1111 #5e1111 hsl(0, 69%, 17%);
    }
    
    .btn.quote-btn:hover{
    	border-style: none;
    }
    
    .btn.quote-btn:active{
    	background-image: url("http://www.wtfgamersonly.com/wp-content/uploads/2015/06/Super-Smash-Bros.-img.2.jpg");
    	opacity: .8;
    }
    
    
    
    
    @media only screen and (max-height: 568px){
    	.jumbotron{
    		margin-top:-89vh;
    	}
    }
    
    @media only screen and (min-height: 569px){
    	.jumbotron{
    		margin-top:-80vh;
    	}
    
    	.btn-posit {
    		margin-top: -42vh;
    	}
    }
    
    @media only screen and (min-width: 0px) and (max-width: 1199px){
    	.bg{
    		background-size: cover;
    		background-position: center;
    	}
    }
    
    /*medium*/
    
    @media only screen and (min-width: 992px) and (max-width: 1199px) { 
    	.jumbotron{
    		margin-left: 8.33333333%;
    	}
    
    }
    
    /*small*/
    @media only screen and (min-width: 768px) and (max-width: 991px){
    	.jumbotron{
    		margin-left: 4.15%;
    	}
    
    }
    
    /*xs*/
    @media only screen and (min-width: 461px) and (max-width: 767px){
    	.jumbotron{
    		margin-left: 8.33333333%;
    	}
    
    	.btn-posit{
    		width: auto;
    	}
    }
    
    @media only screen and (min-width: 0px) and (max-width: 460px) {
    	h2{
    		font-size: 22px;
    	}
    
    	h3{
    		font-size: 18px;
    	}
    
    	.btn-posit{
    		/*padding-left: 78px; */
    		margin-left: 1%;
    	}
    
    	.jumbotron{
    		margin-left: 8%;
    	}
    
    	.twit-btn{
    		margin-right: 55px;
    		width: auto;
    	}
    
    	#twit-text{
    		display: none;
    	}
    }
    
    /*@media only screen and (min-width: 0px) and (max-width: 422px) {
    	h2{
    		font-size: 22px;
    	}
    
    	h3{
    		font-size: 18px;
    	}
    
    	.twit-btn{
    		width: auto;
    		margin-right: 10px;
    	}
    
    	.btn-posit{
    		padding-left: 15%;
    	}
    
    	#twit-text{
    		display: none;
    	}
    }*/
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://use.fontawesome.com/5ad62430a6.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <html>
    		<head>
    		<title>AwesomeDesigns Random Quote Generator</title>
    		<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	</head> 
    <body>
    	<div class="bg"></div>
    	
    	<div class="container">	
    		<div class="row">	
    			<div class="col-lg-8 col-md-10 col-sm-11 col-xs-10 jumbotron">	
    				<h2>"Andrews Random Quote Generator. "</h2>
    				<h3>-Andrew Hickman</h3>
    			</div>		
    		</div>
    		
    	</div>
    	<div class="container">
    		<div class="row">
    			<div class="text-center btn-posit">
    				<button class="btn btn-primary twit-btn"><i class="fa fa-twitter-square fa-2x" aria-hidden="true"><span id="twit-Text">Tweet it</span></i></button>
    				<button class="btn btn-default quote-btn"><div id="whole-text">Get Quote</div></button>
    			</div>
    		</div>
    	</div>
    </html>

    【讨论】:

    • 由于某种原因,当我点击按钮时,按钮最终会淡出并隐藏起来。就像他们承担了 .bg css 更改的属性然后躲在它后面?这是唯一真正改变东西的 js 属性。
    • $('.bg').fadeOut(500, function() { if(isReverse==1){ $(this).css({'background-image': imgCss, 'transform ': 'scaleX(1)'}).fadeIn(700); } else{ $(this).css({'background-image': imgCss, 'transform': 'scaleX(-1)'}).fadeIn (700);}});我无法正确格式化,不知道为什么,我缩进了 4 个空格。
    • @Fullmetal7777 如果你想不通,你应该发布一个新问题。
    • @Fullmetal7777 np。这与原始问题无关,也可能比原始问题更复杂。如果问题中的问题得到解决,请务必选择您认为最佳解决方案的答案。
    【解决方案3】:

    添加文本中心类似乎是解决办法!

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 2021-06-17
      • 2013-03-29
      • 1970-01-01
      • 2013-01-01
      • 2021-04-03
      • 2019-08-14
      • 1970-01-01
      相关资源
      最近更新 更多